include only authors from our organization in list
[koha-bibliografija] / html.pl
1 #!/usr/bin/perl
2
3 # LC_COLLATE=hr_HR.utf8 KOHA_CONF=/etc/koha/sites/ffzg/koha-conf.xml ./html.pl
4
5 use warnings;
6 use strict;
7
8 use DBI;
9 use Data::Dump qw(dump);
10 use autodie;
11 use locale;
12 use Text::Unaccent;
13
14 use lib '/srv/koha_ffzg';
15 use C4::Context;
16 use XML::LibXML;
17 use XML::LibXSLT;
18 use XML::Simple;
19
20 my $dbh = C4::Context->dbh;
21
22 my $xslfilename = 'compact.xsl';
23
24 my $authors;
25 my $marcxml;
26
27 my $sth_select_authors  = $dbh->prepare(q{
28 select
29         biblionumber,
30         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
31         ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
32         ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
33         itemtype,
34         marcxml
35 from biblioitems
36 where
37         agerestriction > 0
38         and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
39 order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
40 });
41
42 $sth_select_authors->execute();
43 while( my $row = $sth_select_authors->fetchrow_hashref ) {
44 #       warn dump($row),$/;
45         if ( $row->{first_author} || $row->{itemtype} !~ m/^KNJ/ ) {
46                 my $all_authors = join(' ', $row->{first_author}, $row->{other_authors});
47                 foreach my $authid ( split(/\s+/, $all_authors) ) {
48                         push @{ $authors->{$authid}->{ $row->{category} } }, $row->{biblionumber};
49                         $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
50                 }
51         } else {
52                 my $xml = XMLin( $row->{marcxml}, ForceArray => [ 'subfield' ] );
53                 foreach my $f700 ( map { $_->{subfield} } grep { $_->{tag} eq 700 } @{ $xml->{datafield} } ) {
54                         my $authid = 0;
55                         my $is_edt = 0;
56                         foreach my $sf ( @$f700 ) {
57                                 if ( $sf->{code} eq '4' && $sf->{content} =~ m/^edt/ ) {
58                                         $is_edt++;
59                                 } elsif ( $sf->{code} eq '9' ) {
60                                         $authid = $sf->{content};
61                                 }
62                         }
63                         if ( $authid && $is_edt ) {
64 #                               warn "# ++ ", $row->{biblionumber}, " $authid f700 ", dump( $f700 );
65                                 push @{ $authors->{$authid}->{ $row->{category} } }, $row->{biblionumber};
66                                 $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
67                         } else {
68 #                               warn "# -- ", $row->{biblionumber}, " f700 ", dump( $f700 );
69                         }
70                 }
71         }
72 }
73
74 my $auth_header;
75 my $auth_department;
76 my @authors;
77
78 my $all_authids = join(',', grep { length($_) > 0 } keys %$authors);
79 my $sth_auth = $dbh->prepare(q{
80 select
81         authid,
82         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
83         ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department
84 from auth_header
85 where
86         authid in (} . $all_authids . q{)
87 });
88
89 $sth_auth->execute();
90 while( my $row = $sth_auth->fetchrow_hashref ) {
91         $auth_header->{ $row->{authid} } = $row->{full_name};
92         $row->{department} =~ s/, Filozofski fakultet u Zagrebu\s*// || next;
93         $row->{department} =~ s/^.+\.\s*//;
94         push @{ $auth_department->{ $row->{department} } }, $row->{authid};
95         warn dump( $row );
96         push @authors, $row;
97
98 }
99
100 my $category_label;
101 my $sth_categories = $dbh->prepare(q{
102 select authorised_value, lib from authorised_values where category = 'BIBCAT'
103 });
104 $sth_categories->execute();
105 while( my $row = $sth_categories->fetchrow_hashref ) {
106         $category_label->{ $row->{authorised_value} } = $row->{lib};
107
108 }
109 #warn dump( $category_label );
110
111 sub html_title {
112         return qq|<html>
113 <head>
114 <meta charset="UTF-8">
115 <title>|, join(" ", @_), qq|</title>
116 <link href="style.css" type="text/css" rel="stylesheet" />
117 </head>
118 <body>
119 |;
120 }
121
122 sub html_end {
123         return qq|</body>\n</html\n|;
124 }
125
126 my $biblio_html;
127
128 sub biblioitem_html {
129         my $biblionumber = shift;
130
131         return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber};
132
133         my $xmlrecord = $marcxml->{$biblionumber} || die "missing $biblionumber marcxml";
134
135         my $parser = XML::LibXML->new();
136         $parser->recover_silently(0); # don't die when you find &, >, etc
137     my $source = $parser->parse_string($xmlrecord);
138         my $style_doc = $parser->parse_file($xslfilename);
139
140         my $xslt = XML::LibXSLT->new();
141         my $parsed = $xslt->parse_stylesheet($style_doc);
142         my $transformed = $parsed->transform($source);
143         return $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
144 }
145
146
147 mkdir 'html' unless -d 'html';
148
149 open(my $index, '>:encoding(utf-8)', 'html/index.new');
150 print $index html_title('Bibliografija Filozogskog fakulteta');
151
152 my $first_letter = '';
153
154 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
155
156         my $first = substr( $row->{full_name}, 0, 1 );
157         if ( $first ne $first_letter ) {
158                 print $index qq{</ul>\n} if $first_letter;
159                 $first_letter = $first;
160                 print $index qq{<h1>$first</h1>\n<ul>\n};
161         }
162         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
163
164         my $path = "html/$row->{authid}";
165         open(my $fh, '>:encoding(utf-8)', "$path.new");
166         print $fh html_title($row->{full_name}, "bibliografija");
167         foreach my $category ( sort keys %{ $authors->{ $row->{authid} } } ) {
168                 my $label = $category_label->{$category} || 'Bez kategorije';
169                 print $fh qq|<h1>$label</h1>\n<ul>\n|;
170                 foreach my $biblionumber ( @{ $authors->{ $row->{authid} }->{$category} } ) {
171                         print $fh qq|<li>|,
172                                 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
173                                 biblioitem_html($biblionumber),
174                                 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
175                                 qq|</li>\n|;
176                 }
177                 print $fh qq|</ul>\n|;
178         }
179         print $fh html_end;
180         close($fh);
181         rename "$path.new", "$path.html";
182
183 }
184
185 print $index html_end;
186 close($index);
187 rename 'html/index.new', 'html/index.html';
188
189 #print dump( $authors );
190
191 print dump( $auth_header );
192
193 print dump( $auth_department );
194
195
196 my $department_category_author;
197 foreach my $department ( sort keys %$auth_department ) {
198         foreach my $authid ( sort @{ $auth_department->{$department} } ) {
199                 foreach my $category ( sort keys %{ $authors->{$authid} } ) {
200                         push @{ $department_category_author->{$department}->{$category} }, $authid;
201                 }
202         }
203 }
204
205 print dump( $department_category_author );
206
207 mkdir 'html/departments' unless -d 'html/departments';
208
209 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
210 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
211 foreach my $department ( sort keys %$department_category_author ) {
212         my $dep = $department || 'Nema odsjeka';
213         my $dep_file = unac_string('utf-8',$dep);
214         print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
215         open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
216         print $fh html_title($department . ' bibliografija');
217         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
218                 my $label = $category_label->{$category} || 'Bez kategorije';
219                 print $fh qq|<h1>$label</h1>\n<ul>\n|;
220
221                 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
222                         foreach my $biblionumber ( @{ $authors->{$authid}->{$category} } ) {
223                                 print $fh qq|<li>|,
224                                         qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
225                                         biblioitem_html($biblionumber),
226                                         qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
227                                         qq|</li>\n|;
228                         }
229                 }
230
231                 print $fh qq|</ul>|;
232         }
233         print $fh html_end;
234         close($fh);
235         rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
236 }
237 print $dep_fh qq|</ul>\n|, html_end;
238 close($dep_fh);
239 rename 'html/departments/index.new', 'html/departments/index.html';
240