c6a2750c171945599ff18e1820dda8fa0ef47db8
[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 use utf8;
15
16 use lib '/srv/koha_ffzg';
17 use C4::Context;
18 use XML::LibXML;
19 use XML::LibXSLT;
20
21 my $dbh = C4::Context->dbh;
22
23 sub debug {
24         my ($title, $data) = @_;
25         print "# $title ",dump($data), $/ if $ENV{DEBUG};
26 }
27
28 my $xslfilename = 'compact.xsl';
29
30 my $azvo_group_title = {
31 'znanstveno nastavni' => qr/(profesor|docent|znanstveni)/i,
32 'lektori i predavači' => qr/(lektor|predavač)/i,
33 'asistenti i novaci' => qr/(asistent|novak)/i,
34 };
35
36 my $auth_header;
37 my $auth_department;
38 my $auth_group;
39 my @authors;
40 my $department_in_sum;
41
42 my $skip;
43
44 my $sth_auth = $dbh->prepare(q{
45 select
46         authid,
47         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
48         ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department,
49         ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="i"]') as academic_title
50 from auth_header
51 });
52
53 $sth_auth->execute();
54 while( my $row = $sth_auth->fetchrow_hashref ) {
55         if ( $row->{department} !~ m/Filozofski fakultet u Zagrebu/ ) {
56                 push @{ $skip->{nije_ffzg} }, $row;
57                 next;
58         }
59         $auth_header->{ $row->{authid} } = $row->{full_name};
60         $row->{department} =~ s/, Filozofski fakultet u Zagrebu.*$//;
61         $row->{department} =~ s/^.+\.\s*//;
62         $row->{department} =~ s/\s+$//s;
63         my $group;
64         foreach my $title ( keys %$azvo_group_title ) {
65                 if ( $row->{academic_title} =~ $azvo_group_title->{$title} ) {
66                         $group = $title;
67                         last;
68                 }
69         }
70         if ( $group ) {
71                 $row->{academic_group} = $group;
72                 $auth_group->{ $row->{authid} } = $group;
73                 $skip->{group_stat}->{$group}++;
74         } else {
75                 push @{ $skip->{no_academic_group} }, $row;
76         }
77
78         warn dump( $row );
79         push @{ $auth_department->{ $row->{department} } }, $row->{authid};
80         push @authors, $row;
81         $department_in_sum->{ $row->{department} }++;
82 }
83
84 foreach my $department ( keys %$department_in_sum ) {
85         $department_in_sum->{$department} = 0 unless $department =~ m/(centar|croaticum|katedra|odsjek)/i;
86 }
87
88 debug 'auth_department' => $auth_department;
89 debug 'auth_group' => $auth_group;
90 debug 'department_in_sum' => $department_in_sum;
91
92
93 my $authors;
94 my $marcxml;
95
96 my $sth_select_authors  = $dbh->prepare(q{
97 select
98         biblionumber,
99         itemtype,
100         marcxml
101 from biblioitems
102 where
103         agerestriction > 0
104 });
105
106 =for sql
107 --      ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
108 --      ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
109 --      ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
110
111 --      and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
112 -- order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
113 =cut
114
115 my $biblio_year;
116 my $type_stats;
117
118 my $parser = XML::LibXML->new();
119 $parser->recover_silently(0); # don't die when you find &, >, etc
120 my $style_doc = $parser->parse_file($xslfilename);
121 my $xslt = XML::LibXSLT->new();
122 my $parsed = $xslt->parse_stylesheet($style_doc);
123
124 my $biblio_html;
125 my $biblio_parsed;
126 my $biblio_data;
127
128 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
129
130 sub biblioitem_html {
131         my ($biblionumber, $parse_only) = @_;
132
133         return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber} && ! $parse_only;
134
135         my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
136
137         print $xml_fh $xmlrecord if $ENV{XML};
138
139         my $source = eval { $parser->parse_string($xmlrecord) };
140         if ( $@ ) {
141 #               warn "SKIP $biblionumber corrupt XML";
142                 push @{ $skip->{XML_corrupt} }, $biblionumber;
143                 return;
144         }
145
146         if ( $parse_only ) {
147                 $biblio_parsed->{$biblionumber} = $source;
148                 return $source;
149         }
150
151         my $transformed = $parsed->transform($source);
152         $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
153
154         delete $biblio_parsed->{$biblionumber};
155
156         return $biblio_html->{$biblionumber};
157 }
158
159 $sth_select_authors->execute();
160 while( my $row = $sth_select_authors->fetchrow_hashref ) {
161 #       warn dump($row),$/;
162
163         my $biblio;
164
165         $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
166
167         my $doc = biblioitem_html( $row->{biblionumber}, 1 );
168         if ( ! $doc ) {
169 #               warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
170                 next;
171         }
172
173         my $root = $doc->documentElement;
174 =for leader
175         my @leaders = $root->getElementsByLocalName('leader');
176         if (@leaders) {
177                 my $leader = $leaders[0]->textContent;
178                 warn "leader $leader\n";
179         }
180 =cut
181
182         my $extract = {
183                 '008' => undef,
184                 '100' => '9',
185                 '680' => 'i',
186                 '700' => '(9|4)',
187                 '942' => '(t|r|v)'
188         };
189
190         my $data;
191
192         foreach my $elt ($root->getChildrenByLocalName('*')) {
193                 my $tag = $elt->getAttribute('tag');
194                 next if ! $tag;
195                 next unless exists $extract->{ $tag };
196
197         if ($elt->localname eq 'controlfield') {
198                         if ( $tag eq '008' ) {
199                                 my $content = $elt->textContent;
200                                 my $year = substr($content, 7, 4 );
201                                 if ( $year !~ m/^\d+$/ ) {
202                                         $year = 0;
203                                         push @{ $skip->{invalid_year} }, $row->{biblionumber};
204                                 }
205                                 $biblio_year->{ $row->{biblionumber} } = $data->{year} = $year;
206                                 $data->{'008'} = $content;
207                         }
208                         next;
209         } elsif ($elt->localname eq 'datafield') {
210                         my $sf_data;
211             foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
212                 my $sf = $sfelt->getAttribute('code');
213                                 next unless $sf =~ m/$extract->{$tag}/;
214                                 if ( exists $sf_data->{$sf} ) {
215                                         $sf_data->{$sf} .= " " . $sfelt->textContent();
216                                 } else {
217                                         $sf_data->{$sf} = $sfelt->textContent();
218                                 }
219                         }
220                         push @{ $data->{$tag} }, $sf_data if $sf_data;
221         }
222     }
223
224         if ( $data->{year} < 2008 ) {
225                 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
226                 next;
227         } elsif ( $data->{year} > 2013 ) {
228                 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
229                 next;
230         }
231
232 #       warn "# ", $row->{biblionumber}, " data ",dump($data);
233
234         my $category = $data->{942}->[0]->{'t'};
235         if ( ! $category ) {
236 #               warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
237                 push @{ $skip->{no_category} }, $row->{biblionumber};
238                 next;
239         }
240
241         my $have_100 = 1;
242
243         if ( exists $data->{100} ) {
244                         my @first_author = map { $_->{'9'} } @{ $data->{100} };
245                         foreach my $authid ( @first_author ) {
246                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
247                         }
248         } else {
249                 $have_100 = 0;
250         }
251
252         my $have_edt;
253
254         if ( exists $data->{700} ) {
255                         foreach my $auth ( @{ $data->{700} } ) {
256                                 my $authid = $auth->{9} || next;
257                                 my $type   = $auth->{4} || next; #die "no 4 in ",dump($data);
258
259                                 $type_stats->{$type}++;
260
261                                 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
262                                         push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
263                                         push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
264                                 } elsif ( $type =~ m/aut/ ) {
265                                         if ( ! $have_100 ) {
266                                                 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
267                                                 if ( $have_edt ) {
268                                                         $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
269                                                 } else {
270                                                         push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
271                                                 }
272                                         } else {
273                                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
274                                         }
275                                 } else {
276 #                                       warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
277                                         $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
278                                 }
279                         }
280                         delete $data->{700};
281         }
282
283         $biblio_data->{ $row->{biblionumber} } = $data;
284
285 }
286
287 debug 'authors' => $authors;
288 debug 'type_stats' => $type_stats;
289 debug 'skip' => $skip;
290 debug 'biblio_year' => $biblio_year;
291 debug 'biblio_data' => $biblio_data;
292
293 my $category_label;
294 my $sth_categories = $dbh->prepare(q{
295 select authorised_value, lib from authorised_values where category = 'BIBCAT'
296 });
297 $sth_categories->execute();
298 while( my $row = $sth_categories->fetchrow_hashref ) {
299         $category_label->{ $row->{authorised_value} } = $row->{lib};
300
301 }
302 debug 'category_label' => $category_label;
303
304 sub html_title {
305         return qq|<html>
306 <head>
307 <meta charset="UTF-8">
308 <title>|, join(" ", @_), qq|</title>
309 <link href="style.css" type="text/css" rel="stylesheet" />
310 </head>
311 <body>
312 |;
313 }
314
315 sub html_end {
316         return qq|</body>\n</html\n|;
317 }
318
319 mkdir 'html' unless -d 'html';
320
321 open(my $index, '>:encoding(utf-8)', 'html/index.new');
322 print $index html_title('Bibliografija Filozofskog fakulteta');
323
324 my $first_letter = '';
325
326 debug 'authors' => \@authors;
327
328 sub li_biblio {
329         my ($biblionumber) = @_;
330         return qq|<li>|,
331                 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
332                 biblioitem_html($biblionumber),
333                 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
334                 qq|</li>\n|;
335 }
336
337 sub author_html {
338         my ( $fh, $authid, $type, $label ) = @_;
339
340         return unless exists $authors->{$authid}->{$type};
341
342         print $fh qq|<h2>$label</h2>\n|;
343
344         foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
345                 my $label = $category_label->{$category} || 'Bez kategorije';
346                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
347                 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
348                         print $fh li_biblio( $biblionumber );
349                 }
350                 print $fh qq|</ul>\n|;
351         }
352 }
353
354 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
355
356         my $first = substr( $row->{full_name}, 0, 1 );
357         if ( $first ne $first_letter ) {
358                 print $index qq{</ul>\n} if $first_letter;
359                 $first_letter = $first;
360                 print $index qq{<h1>$first</h1>\n<ul>\n};
361         }
362         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
363
364         my $path = "html/$row->{authid}";
365         open(my $fh, '>:encoding(utf-8)', "$path.new");
366         print $fh html_title($row->{full_name}, "bibliografija");
367         print $fh qq|<h1>$row->{full_name} - bibliografija za razdoblje 2008-2013</h1>|;
368
369         author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
370         author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
371
372         print $fh html_end;
373         close($fh);
374         rename "$path.new", "$path.html";
375
376 }
377
378 print $index html_end;
379 close($index);
380 rename 'html/index.new', 'html/index.html';
381
382 debug 'auth_header' => $auth_header;
383
384
385 my $department_category_author;
386 foreach my $department ( sort keys %$auth_department ) {
387         foreach my $authid ( sort @{ $auth_department->{$department} } ) {
388                 my   @categories = keys %{ $authors->{$authid}->{aut} };
389                 push @categories,  keys %{ $authors->{$authid}->{sec} };
390                 foreach my $category ( sort @categories ) {
391                         push @{ $department_category_author->{$department}->{$category} }, $authid;
392                         push @{ $department_category_author->{''}->{$category} }, $authid if $department_in_sum->{$department};
393                 }
394         }
395 }
396
397 debug 'department_category_author' => $department_category_author;
398
399 mkdir 'html/departments' unless -d 'html/departments';
400
401 sub unique_biblionumber {
402         my @v = @_;
403         my $u;
404         $u->{$_}++ foreach @v;
405         return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
406 }
407
408 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
409 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
410 foreach my $department ( sort keys %$department_category_author ) {
411         my $dep = $department || 'Nema odsjeka';
412         my $dep_file = unac_string('utf-8',$dep);
413         print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
414         open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
415
416         print $fh html_title($department . ' bibliografija');
417         print $fh qq|<h1>$department bibliografija</h1>\n|;
418
419         print $fh qq|<h2>Primarno autorstvo</h2>\n|;
420
421         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
422
423                 my @authids = @{ $department_category_author->{$department}->{$category} };
424                 next unless @authids;
425
426                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
427                 my $unique;
428                 $unique->{$_}++ foreach @biblionumber;
429
430                 next unless @biblionumber;
431
432                 my $label = $category_label->{$category} || 'Bez kategorije';
433                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
434
435                 print $fh li_biblio( $_ ) foreach @biblionumber;
436
437                 print $fh qq|</ul>|;
438         }
439
440
441         print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
442
443         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
444
445                 my @authids = @{ $department_category_author->{$department}->{$category} };
446                 next unless @authids;
447
448                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
449
450                 next unless @biblionumber;
451
452                 my $label = $category_label->{$category} || 'Bez kategorije';
453                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
454
455                 print $fh li_biblio( $_ ) foreach @biblionumber;
456
457                 print $fh qq|</ul>|;
458         }
459
460
461         print $fh html_end;
462         close($fh);
463         rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
464 }
465 print $dep_fh qq|</ul>\n|, html_end;
466 close($dep_fh);
467 rename 'html/departments/index.new', 'html/departments/index.html';
468
469 my $azvo_stat_biblio;
470
471 foreach my $department ( sort keys %$department_category_author ) {
472         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
473                 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
474                         foreach my $type ( keys %{ $authors->{$authid} } ) {
475                                 next unless exists $authors->{$authid}->{$type}->{$category};
476                                 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } },  @{ $authors->{$authid}->{$type}->{$category} };
477                         }
478                 }
479                 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
480                                 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } };
481 #                               $azvo_stat_biblio->{ $department }->{ $category }->{ $type } = $#biblios + 1;
482                                 $azvo_stat_biblio->{ $department }->{ $category }->{ $type } = [ @biblios ];
483                 }
484         }
485 }
486
487 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
488
489 my @report_lines;
490 my @report_labels;
491
492 my $label;
493 my $sub_labels;
494 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
495 while( <$report> ) {
496         chomp;
497         if ( /^([^\t]+)\t+(.+)/ ) {
498                 $label = $1;
499                 push @report_labels, $label;
500                 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
501                 push @report_lines, [ $label, @$type ];
502         } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
503                 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
504                 my $sub_label = $1;
505                 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
506                 push @report_labels, $label . $sub_label;
507         } else {
508                 die "ERROR: [$_]\n";
509         }
510 }
511
512 debug 'report_lines', \@report_lines;
513 debug 'sub_labels', $sub_labels;
514 debug 'report_labels', \@report_labels;
515
516 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
517
518 debug 'departments' => \@departments;
519
520 my $department2col;
521 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
522 my $label2row;
523 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
524
525 my $table;
526
527 sub table_count {
528         my $label = shift @_;
529         my $department = shift @_;
530         my @biblionumbers = @_;
531         my $unique;
532         $unique->{$_}++ foreach @biblionumbers;
533         $table->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar keys %$unique;
534 }
535
536 foreach my $department ( @departments ) {
537         foreach my $line ( @report_lines ) {
538                 my $label = $line->[0];
539                 my @biblionumbers;
540                 foreach ( 1 .. $#$line ) {
541                         my ( $category, $type ) = @{ $line->[ $_ ] };
542                         my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type};
543                         push @biblionumbers, @$b if $b;
544                 }
545                 if ( $sub_labels->{$label} ) {
546                         my $sub_stats;
547                         foreach my $biblionumber ( @biblionumbers ) {
548                                 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
549                                 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
550                                         my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
551                                         if ( ! $regex ) {
552                                                 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
553                                                 last;
554                                         }
555                                         if ( $field < 100 ) {
556                                                 if ( $data->{$field} =~ m/$regex/ ) {
557                                                         push @{ $sub_stats->{ $sub_label } }, $biblionumber;
558                                                         last;
559                                                 }
560                                         } else {
561                                                 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
562                                                         push @{ $sub_stats->{ $sub_label } }, $biblionumber;
563                                                         last;
564                                                 }
565                                         }
566                                 }
567                         }
568                         foreach my $sub_label ( keys %$sub_stats ) {
569                                 my $full_label = $label . $sub_label;
570                                 table_count $full_label, $department, @{ $sub_stats->{$sub_label} };
571                         }
572                 } else {
573                         table_count $label, $department, @biblionumbers;
574                 }
575         }
576 }
577
578 debug 'table', $table;
579
580 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
581
582 print $fh html_title('AZVO tablica');
583
584 print $fh "<table border=1>\n";
585 print $fh "<tr><th></th>";
586 print $fh "<th>$_</th>" foreach @departments;
587 print $fh "</tr>\n";
588
589 foreach my $row ( 0 .. $#$table ) {
590         print $fh "<tr><th>", $report_labels[$row], "</th>";
591         print $fh "<td>", $table->[ $row ]->[ $_ ] || '', "</td>" foreach 0 .. $#departments;
592         print $fh "</tr>";
593 }
594
595 print $fh "</table>\n", html_end;
596
597 close($fh);
598 rename 'html/azvo.new', 'html/azvo.html';
599