80d3cc15045c5e1938b007629e7787e5c86e5ab0
[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
19 my $dbh = C4::Context->dbh;
20
21 sub debug {
22         my ($title, $data) = @_;
23         print "# $title ",dump($data), $/;
24 }
25
26 my $xslfilename = 'compact.xsl';
27
28 my $auth_header;
29 my $auth_department;
30 my @authors;
31
32 my $skip;
33
34 my $sth_auth = $dbh->prepare(q{
35 select
36         authid,
37         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
38         ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department
39 from auth_header
40 });
41
42 $sth_auth->execute();
43 while( my $row = $sth_auth->fetchrow_hashref ) {
44         $auth_header->{ $row->{authid} } = $row->{full_name};
45         $row->{department} =~ s/, Filozofski fakultet u Zagrebu\s*// || next;
46         $row->{department} =~ s/^.+\.\s*//;
47         push @{ $auth_department->{ $row->{department} } }, $row->{authid};
48 #       warn dump( $row );
49         push @authors, $row;
50
51 }
52
53 debug 'auth_department' => $auth_department;
54
55
56 my $authors;
57 my $author_count;
58 my $marcxml;
59
60 my $sth_select_authors  = $dbh->prepare(q{
61 select
62         biblionumber,
63         itemtype,
64         marcxml
65 from biblioitems
66 where
67         agerestriction > 0
68 });
69
70 =for sql
71 --      ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
72 --      ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
73 --      ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
74
75 --      and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
76 -- order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
77 =cut
78
79 my $biblio_year;
80 my $type_stats;
81
82 my $parser = XML::LibXML->new();
83 $parser->recover_silently(0); # don't die when you find &, >, etc
84 my $style_doc = $parser->parse_file($xslfilename);
85 my $xslt = XML::LibXSLT->new();
86 my $parsed = $xslt->parse_stylesheet($style_doc);
87
88 my $biblio_html;
89
90 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
91
92 sub biblioitem_html {
93         my $biblionumber = shift;
94
95         return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber};
96
97         my $xmlrecord = $marcxml->{$biblionumber} || die "missing $biblionumber marcxml";
98
99         print $xml_fh $xmlrecord if $ENV{XML};
100
101         my $source = eval { $parser->parse_string($xmlrecord) };
102         if ( $@ ) {
103                 warn "SKIP $biblionumber corrupt XML";
104                 push @{ $skip->{XML_corrupt} }, $biblionumber;
105                 return;
106         }
107
108         my $transformed = $parsed->transform($source);
109         $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
110
111         return ( $biblio_html->{$biblionumber}, $source ) if wantarray;
112         return $biblio_html->{$biblionumber};
113 }
114
115 $sth_select_authors->execute();
116 while( my $row = $sth_select_authors->fetchrow_hashref ) {
117 #       warn dump($row),$/;
118
119         my $biblio;
120
121         $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
122
123         my ( undef, $doc ) = biblioitem_html( $row->{biblionumber} );
124         if ( ! $doc ) {
125                 warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
126                 next;
127         }
128
129         my $root = $doc->documentElement;
130 =for leader
131         my @leaders = $root->getElementsByLocalName('leader');
132         if (@leaders) {
133                 my $leader = $leaders[0]->textContent;
134                 warn "leader $leader\n";
135         }
136 =cut
137
138         my $extract = {
139                 '008' => undef,
140                 '100' => '9',
141                 '700' => '(9|4)',
142                 '942' => 't'
143         };
144
145         my $data;
146
147         foreach my $elt ($root->getChildrenByLocalName('*')) {
148                 my $tag = $elt->getAttribute('tag');
149                 next if ! $tag;
150                 next unless exists $extract->{ $tag };
151
152         if ($elt->localname eq 'controlfield') {
153                         if ( $tag eq '008' ) {
154                                  $biblio_year->{ $row->{biblionumber} } = $elt->textContent;
155                         }
156                         next;
157         } elsif ($elt->localname eq 'datafield') {
158                         my $sf_data;
159             foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
160                 my $sf = $sfelt->getAttribute('code');
161                                 next unless $sf =~ m/$extract->{$tag}/;
162                                 if ( exists $sf_data->{$sf} ) {
163                                         $sf_data->{$sf} .= " " . $sfelt->textContent();
164                                 } else {
165                                         $sf_data->{$sf} = $sfelt->textContent();
166                                 }
167                         }
168                         push @{ $data->{$tag} }, $sf_data if $sf_data;
169         }
170     }
171
172 #       warn "# ", $row->{biblionumber}, " data ",dump($data);
173
174         my $category = $data->{942}->[0]->{'t'};
175         if ( ! $category ) {
176                 warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
177                 push @{ $skip->{no_category} }, $row->{biblionumber};
178                 next;
179         }
180
181
182         if ( exists $data->{100} ) {
183                         my @first_author = map { $_->{'9'} } @{ $data->{100} };
184                         foreach my $authid ( @first_author ) {
185                                 push @{ $authors->{$authid}->{ $category } }, $row->{biblionumber};
186                                 $author_count->{aut}->{$authid}++;
187                         }
188         }
189
190         if ( exists $data->{700} ) {
191                         foreach my $auth ( @{ $data->{700} } ) {
192                                 my $authid = $auth->{9} || next;
193                                 my $type   = $auth->{4} || next; #die "no 4 in ",dump($data);
194
195                                 $type_stats->{$type}++;
196
197                                 push @{ $authors->{$authid}->{ $category } }, $row->{biblionumber};
198                                 if ( $type =~ m/aut/ ) {
199                                         $author_count->{aut}->{ $authid }++;
200                                 } elsif ( $type =~ m/(edt|tr)/ ) {
201                                         $author_count->{$1}->{ $authid }++;
202                                 } else {
203                                         warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
204                                         push @{ $skip->{ 'no_700$4' } }, $row->{biblionumber};
205                                 }
206                         }
207         }
208
209 }
210
211 debug 'authors' => $authors;
212 debug 'author_count' => $author_count;
213 debug 'type_stats' => $type_stats;
214
215 my $category_label;
216 my $sth_categories = $dbh->prepare(q{
217 select authorised_value, lib from authorised_values where category = 'BIBCAT'
218 });
219 $sth_categories->execute();
220 while( my $row = $sth_categories->fetchrow_hashref ) {
221         $category_label->{ $row->{authorised_value} } = $row->{lib};
222
223 }
224 debug 'category_label' => $category_label;
225
226 sub html_title {
227         return qq|<html>
228 <head>
229 <meta charset="UTF-8">
230 <title>|, join(" ", @_), qq|</title>
231 <link href="style.css" type="text/css" rel="stylesheet" />
232 </head>
233 <body>
234 |;
235 }
236
237 sub html_end {
238         return qq|</body>\n</html\n|;
239 }
240
241 mkdir 'html' unless -d 'html';
242
243 open(my $index, '>:encoding(utf-8)', 'html/index.new');
244 print $index html_title('Bibliografija Filozofskog fakulteta');
245
246 my $first_letter = '';
247
248 debug 'authors' => \@authors;
249
250 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
251
252         my $first = substr( $row->{full_name}, 0, 1 );
253         if ( $first ne $first_letter ) {
254                 print $index qq{</ul>\n} if $first_letter;
255                 $first_letter = $first;
256                 print $index qq{<h1>$first</h1>\n<ul>\n};
257         }
258         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
259
260         my $path = "html/$row->{authid}";
261         open(my $fh, '>:encoding(utf-8)', "$path.new");
262         print $fh html_title($row->{full_name}, "bibliografija");
263         print $fh qq|<h1>$row->{full_name} - bibliografija za razdoblje 2008-2013</h1>|;
264         foreach my $category ( sort keys %{ $authors->{ $row->{authid} } } ) {
265                 my $label = $category_label->{$category} || 'Bez kategorije';
266                 print $fh qq|<h2>$label</h2>\n<ul>\n|;
267                 foreach my $biblionumber ( @{ $authors->{ $row->{authid} }->{$category} } ) {
268                         print $fh qq|<li>|,
269                                 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
270                                 biblioitem_html($biblionumber),
271                                 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
272                                 qq|</li>\n|;
273                 }
274                 print $fh qq|</ul>\n|;
275         }
276         print $fh html_end;
277         close($fh);
278         rename "$path.new", "$path.html";
279
280 }
281
282 print $index html_end;
283 close($index);
284 rename 'html/index.new', 'html/index.html';
285
286 debug 'auth_header' => $auth_header;
287
288
289 my $department_category_author;
290 foreach my $department ( sort keys %$auth_department ) {
291         foreach my $authid ( sort @{ $auth_department->{$department} } ) {
292                 foreach my $category ( sort keys %{ $authors->{$authid} } ) {
293                         push @{ $department_category_author->{$department}->{$category} }, $authid;
294                 }
295         }
296 }
297
298 debug 'department_category_author' => $department_category_author;
299
300 mkdir 'html/departments' unless -d 'html/departments';
301
302 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
303 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
304 foreach my $department ( sort keys %$department_category_author ) {
305         my $dep = $department || 'Nema odsjeka';
306         my $dep_file = unac_string('utf-8',$dep);
307         print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
308         open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
309         print $fh html_title($department . ' bibliografija');
310         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
311                 my $label = $category_label->{$category} || 'Bez kategorije';
312                 print $fh qq|<h1>$label</h1>\n<ul>\n|;
313
314                 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
315                         foreach my $biblionumber ( @{ $authors->{$authid}->{$category} } ) {
316                                 print $fh qq|<li>|,
317                                         qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
318                                         biblioitem_html($biblionumber),
319                                         qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
320                                         qq|</li>\n|;
321                         }
322                 }
323
324                 print $fh qq|</ul>|;
325         }
326         print $fh html_end;
327         close($fh);
328         rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
329 }
330 print $dep_fh qq|</ul>\n|, html_end;
331 close($dep_fh);
332 rename 'html/departments/index.new', 'html/departments/index.html';
333
334 debug 'skip' => $skip;
335