b8f041fddbd885c3e145b66113337fde1382b682
[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 use Carp qw(confess);
14
15 use lib '/srv/koha_ffzg';
16 use C4::Context;
17 use XML::LibXML;
18 use XML::LibXSLT;
19
20 my $dbh = C4::Context->dbh;
21
22 sub debug {
23         my ($title, $data) = @_;
24         print "# $title ",dump($data), $/ if $ENV{DEBUG};
25 }
26
27 my $xslfilename = 'compact.xsl';
28
29 my $auth_header;
30 my $auth_department;
31 my @authors;
32
33 my $skip;
34
35 my $sth_auth = $dbh->prepare(q{
36 select
37         authid,
38         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
39         ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department
40 from auth_header
41 });
42
43 $sth_auth->execute();
44 while( my $row = $sth_auth->fetchrow_hashref ) {
45         if ( $row->{department} !~ m/Filozofski fakultet u Zagrebu/ ) {
46                 push @{ $skip->{nije_ffzg} }, $row;
47                 next;
48         }
49         $auth_header->{ $row->{authid} } = $row->{full_name};
50         $row->{department} =~ s/, Filozofski fakultet u Zagrebu.*$//;
51         $row->{department} =~ s/^.+\.\s*//;
52 #       warn dump( $row );
53         push @{ $auth_department->{ $row->{department} } }, $row->{authid};
54         push @authors, $row;
55
56 }
57
58 debug 'auth_department' => $auth_department;
59
60
61 my $authors;
62 my $marcxml;
63
64 my $sth_select_authors  = $dbh->prepare(q{
65 select
66         biblionumber,
67         itemtype,
68         marcxml
69 from biblioitems
70 where
71         agerestriction > 0
72 });
73
74 =for sql
75 --      ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
76 --      ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
77 --      ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
78
79 --      and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
80 -- order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
81 =cut
82
83 my $biblio_year;
84 my $type_stats;
85
86 my $parser = XML::LibXML->new();
87 $parser->recover_silently(0); # don't die when you find &, >, etc
88 my $style_doc = $parser->parse_file($xslfilename);
89 my $xslt = XML::LibXSLT->new();
90 my $parsed = $xslt->parse_stylesheet($style_doc);
91
92 my $biblio_html;
93 my $biblio_parsed;
94
95 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
96
97 sub biblioitem_html {
98         my ($biblionumber, $parse_only) = @_;
99
100         return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber} && ! $parse_only;
101
102         my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
103
104         print $xml_fh $xmlrecord if $ENV{XML};
105
106         my $source = eval { $parser->parse_string($xmlrecord) };
107         if ( $@ ) {
108 #               warn "SKIP $biblionumber corrupt XML";
109                 push @{ $skip->{XML_corrupt} }, $biblionumber;
110                 return;
111         }
112
113         if ( $parse_only ) {
114                 $biblio_parsed->{$biblionumber} = $source;
115                 return $source;
116         }
117
118         my $transformed = $parsed->transform($source);
119         $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
120
121         delete $biblio_parsed->{$biblionumber};
122
123         return $biblio_html->{$biblionumber};
124 }
125
126 $sth_select_authors->execute();
127 while( my $row = $sth_select_authors->fetchrow_hashref ) {
128 #       warn dump($row),$/;
129
130         my $biblio;
131
132         $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
133
134         my $doc = biblioitem_html( $row->{biblionumber}, 1 );
135         if ( ! $doc ) {
136 #               warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
137                 next;
138         }
139
140         my $root = $doc->documentElement;
141 =for leader
142         my @leaders = $root->getElementsByLocalName('leader');
143         if (@leaders) {
144                 my $leader = $leaders[0]->textContent;
145                 warn "leader $leader\n";
146         }
147 =cut
148
149         my $extract = {
150                 '008' => undef,
151                 '100' => '9',
152                 '700' => '(9|4)',
153                 '942' => 't'
154         };
155
156         my $data;
157
158         foreach my $elt ($root->getChildrenByLocalName('*')) {
159                 my $tag = $elt->getAttribute('tag');
160                 next if ! $tag;
161                 next unless exists $extract->{ $tag };
162
163         if ($elt->localname eq 'controlfield') {
164                         if ( $tag eq '008' ) {
165                                 my $year = substr($elt->textContent, 7, 4 );
166                                 if ( $year !~ m/^\d+$/ ) {
167                                         $year = 0;
168                                         push @{ $skip->{invalid_year} }, $row->{biblionumber};
169                                 }
170                                 $biblio_year->{ $row->{biblionumber} } = $data->{year} = $year;
171                         }
172                         next;
173         } elsif ($elt->localname eq 'datafield') {
174                         my $sf_data;
175             foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
176                 my $sf = $sfelt->getAttribute('code');
177                                 next unless $sf =~ m/$extract->{$tag}/;
178                                 if ( exists $sf_data->{$sf} ) {
179                                         $sf_data->{$sf} .= " " . $sfelt->textContent();
180                                 } else {
181                                         $sf_data->{$sf} = $sfelt->textContent();
182                                 }
183                         }
184                         push @{ $data->{$tag} }, $sf_data if $sf_data;
185         }
186     }
187
188         if ( $data->{year} < 2008 ) {
189                 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
190                 next;
191         } elsif ( $data->{year} > 2013 ) {
192                 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
193                 next;
194         }
195
196 #       warn "# ", $row->{biblionumber}, " data ",dump($data);
197
198         my $category = $data->{942}->[0]->{'t'};
199         if ( ! $category ) {
200 #               warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
201                 push @{ $skip->{no_category} }, $row->{biblionumber};
202                 next;
203         }
204
205
206         my $have_100 = 1;
207
208         if ( exists $data->{100} ) {
209                         my @first_author = map { $_->{'9'} } @{ $data->{100} };
210                         foreach my $authid ( @first_author ) {
211                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
212                         }
213         } else {
214                 $have_100 = 0;
215         }
216
217         my $have_edt;
218
219         if ( exists $data->{700} ) {
220                         foreach my $auth ( @{ $data->{700} } ) {
221                                 my $authid = $auth->{9} || next;
222                                 my $type   = $auth->{4} || next; #die "no 4 in ",dump($data);
223
224                                 $type_stats->{$type}++;
225
226                                 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
227                                         push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
228                                         push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
229                                 } elsif ( $type =~ m/aut/ ) {
230                                         if ( ! $have_100 ) {
231                                                 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
232                                                 if ( $have_edt ) {
233                                                         $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
234                                                 } else {
235                                                         push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
236                                                 }
237                                         } else {
238                                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
239                                         }
240                                 } else {
241 #                                       warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
242                                         $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
243                                 }
244                         }
245         }
246
247 }
248
249 debug 'authors' => $authors;
250 debug 'type_stats' => $type_stats;
251 debug 'skip' => $skip;
252 debug 'biblio_year' => $biblio_year;
253
254 my $category_label;
255 my $sth_categories = $dbh->prepare(q{
256 select authorised_value, lib from authorised_values where category = 'BIBCAT'
257 });
258 $sth_categories->execute();
259 while( my $row = $sth_categories->fetchrow_hashref ) {
260         $category_label->{ $row->{authorised_value} } = $row->{lib};
261
262 }
263 debug 'category_label' => $category_label;
264
265 sub html_title {
266         return qq|<html>
267 <head>
268 <meta charset="UTF-8">
269 <title>|, join(" ", @_), qq|</title>
270 <link href="style.css" type="text/css" rel="stylesheet" />
271 </head>
272 <body>
273 |;
274 }
275
276 sub html_end {
277         return qq|</body>\n</html\n|;
278 }
279
280 mkdir 'html' unless -d 'html';
281
282 open(my $index, '>:encoding(utf-8)', 'html/index.new');
283 print $index html_title('Bibliografija Filozofskog fakulteta');
284
285 my $first_letter = '';
286
287 debug 'authors' => \@authors;
288
289 sub li_biblio {
290         my ($biblionumber) = @_;
291         return qq|<li>|,
292                 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
293                 biblioitem_html($biblionumber),
294                 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
295                 qq|</li>\n|;
296 }
297
298 sub author_html {
299         my ( $fh, $authid, $type, $label ) = @_;
300
301         return unless exists $authors->{$authid}->{$type};
302
303         print $fh qq|<h2>$label</h2>\n|;
304
305         foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
306                 my $label = $category_label->{$category} || 'Bez kategorije';
307                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
308                 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
309                         print $fh li_biblio( $biblionumber );
310                 }
311                 print $fh qq|</ul>\n|;
312         }
313 }
314
315 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
316
317         my $first = substr( $row->{full_name}, 0, 1 );
318         if ( $first ne $first_letter ) {
319                 print $index qq{</ul>\n} if $first_letter;
320                 $first_letter = $first;
321                 print $index qq{<h1>$first</h1>\n<ul>\n};
322         }
323         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
324
325         my $path = "html/$row->{authid}";
326         open(my $fh, '>:encoding(utf-8)', "$path.new");
327         print $fh html_title($row->{full_name}, "bibliografija");
328         print $fh qq|<h1>$row->{full_name} - bibliografija za razdoblje 2008-2013</h1>|;
329
330         author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
331         author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
332
333         print $fh html_end;
334         close($fh);
335         rename "$path.new", "$path.html";
336
337 }
338
339 print $index html_end;
340 close($index);
341 rename 'html/index.new', 'html/index.html';
342
343 debug 'auth_header' => $auth_header;
344
345
346 my $department_category_author;
347 foreach my $department ( sort keys %$auth_department ) {
348         foreach my $authid ( sort @{ $auth_department->{$department} } ) {
349                 my   @categories = keys %{ $authors->{$authid}->{aut} };
350                 push @categories,  keys %{ $authors->{$authid}->{sec} };
351                 foreach my $category ( sort @categories ) {
352                         push @{ $department_category_author->{$department}->{$category} }, $authid;
353                 }
354         }
355 }
356
357 debug 'department_category_author' => $department_category_author;
358
359 mkdir 'html/departments' unless -d 'html/departments';
360
361 sub unique_biblionumber {
362         my @v = @_;
363         my $u;
364         $u->{$_}++ foreach @v;
365         return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
366 }
367
368 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
369 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
370 foreach my $department ( sort keys %$department_category_author ) {
371         my $dep = $department || 'Nema odsjeka';
372         my $dep_file = unac_string('utf-8',$dep);
373         print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
374         open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
375
376         print $fh html_title($department . ' bibliografija');
377         print $fh qq|<h1>$department bibliografija</h1>\n|;
378
379         print $fh qq|<h2>Primarno autorstvo</h2>\n|;
380
381         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
382
383                 my @authids = @{ $department_category_author->{$department}->{$category} };
384                 next unless @authids;
385
386                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
387                 my $unique;
388                 $unique->{$_}++ foreach @biblionumber;
389
390                 next unless @biblionumber;
391
392                 my $label = $category_label->{$category} || 'Bez kategorije';
393                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
394
395                 print $fh li_biblio( $_ ) foreach @biblionumber;
396
397                 print $fh qq|</ul>|;
398         }
399
400
401         print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
402
403         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
404
405                 my @authids = @{ $department_category_author->{$department}->{$category} };
406                 next unless @authids;
407
408                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
409
410                 next unless @biblionumber;
411
412                 my $label = $category_label->{$category} || 'Bez kategorije';
413                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
414
415                 print $fh li_biblio( $_ ) foreach @biblionumber;
416
417                 print $fh qq|</ul>|;
418         }
419
420
421         print $fh html_end;
422         close($fh);
423         rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
424 }
425 print $dep_fh qq|</ul>\n|, html_end;
426 close($dep_fh);
427 rename 'html/departments/index.new', 'html/departments/index.html';
428
429 my $azvo_stat;
430
431 foreach my $department ( sort keys %$department_category_author ) {
432         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
433                 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
434                         foreach my $type ( keys %{ $authors->{$authid} } ) {
435                                 next unless exists $authors->{$authid}->{$type}->{$category};
436                                 $azvo_stat->{ $department }->{ $category }->{ $type } += $#{ $authors->{$authid}->{$type}->{$category} } + 1;
437                         }
438                 }
439         }
440 }
441
442 debug 'azvo_stat' => $azvo_stat;
443
444 =for later
445 open(my $fh, '>', 'html/azvo.new');
446
447
448
449 close($fh);
450 rename 'html/azvo.new', 'html/azvo.html';
451 =cut
452