109578bf9a3fe608292a65b3f23ca2304139fd96
[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 use JSON;
16 use POSIX qw(strftime);
17 use Storable;
18
19 use lib '/srv/koha_ffzg';
20 use C4::Context;
21 use XML::LibXML;
22 use XML::LibXSLT;
23
24 my $pid_file = '/dev/shm/bibliografija.pid';
25 {
26         if ( -e $pid_file ) {
27                 open(my $fh, '<', $pid_file);
28                 my $pid = <$fh>;
29                 no autodie; # it will die on kill
30                 kill 0, $pid || die "$0 allready running as pid $pid";
31         }
32         open(my $fh, '>', $pid_file);
33         print $fh $$;
34         close($fh);
35 }
36
37
38 my $dbh = C4::Context->dbh;
39
40 sub debug {
41         my ($title, $data) = @_;
42         print "# $title ",dump($data), $/ if $ENV{DEBUG};
43 }
44
45 my $xslfilename = 'compact.xsl';
46
47 my $azvo_group_title = {
48 'znanstveno nastavni' => qr/(profes|docent|znanstveni savjetnik|znanstveni suradnik)/i,
49 'lektori i predavači' => qr/(lektor|predavač)/i,
50 'asistenti i novaci' => qr/(asistent|novak)/i,
51 };
52
53 my $department_groups = {
54 'AAB_humanističke'             => qr/(anglistiku|arheologiju|antropologiju|filozofiju|fonetiku|germanistiku|hungarologiju|indologiju|slavenske|filologiju|komparativnu|kroatistiku|lingvistiku|povijest|romanistiku|talijanistiku)/i,
55 'AAC_društvene'                        => qr/(informacijske|pedagogiju|psihologiju|sociologiju)/i,
56 };
57
58 my $auth_header;
59 my $auth_department;
60 my $auth_group;
61 my @authors;
62 my $department_in_sum;
63 my $department_in_group;
64
65 my $skip;
66
67 my $sth_auth = $dbh->prepare(q{
68 select
69         authid,
70         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
71         ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department,
72         ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="i"]') as academic_title
73 from auth_header
74 });
75
76 $sth_auth->execute();
77 while( my $row = $sth_auth->fetchrow_hashref ) {
78         if ( $row->{department} !~ m/Filozofski fakultet u Zagrebu/ ) {
79                 push @{ $skip->{nije_ffzg} }, $row;
80                 next;
81         }
82         $auth_header->{ $row->{authid} } = $row->{full_name};
83         $row->{department} =~ s/, Filozofski fakultet u Zagrebu.*$//;
84         $row->{department} =~ s/^.+\.\s*//;
85         $row->{department} =~ s/\s+$//s;
86         my $group;
87         foreach my $title ( keys %$azvo_group_title ) {
88                 if ( $row->{academic_title} =~ $azvo_group_title->{$title} ) {
89                         $group = $title;
90                         last;
91                 }
92         }
93         if ( $group ) {
94                 $row->{academic_group} = $group;
95                 $auth_group->{ $row->{authid} } = $group;
96                 $skip->{group_stat}->{$group}++;
97         } else {
98                 push @{ $skip->{no_academic_group} }, $row;
99         }
100
101 #       warn "# ", dump( $row );
102         push @{ $auth_department->{ $row->{department} } }, $row->{authid};
103         push @authors, $row;
104         $department_in_sum->{ $row->{department} }++;
105         foreach my $name ( keys %$department_groups ) {
106                 my $regex = $department_groups->{$name};
107                 if ( $row->{department} =~ $regex ) {
108                         $department_in_group->{ $row->{department} } = $name;
109                         last;
110                 }
111         }
112 }
113
114 debug 'department_in_group' => $department_in_group;
115
116 foreach my $department ( keys %$department_in_sum ) {
117 #       $department_in_sum->{$department} = 0 unless $department =~ m/(centar|croaticum|katedra|odsjek)/i;
118 }
119
120 debug 'auth_department' => $auth_department;
121 store $auth_department, '/dev/shm/auth_department.storable';
122 debug 'auth_group' => $auth_group;
123 debug 'department_in_sum' => $department_in_sum;
124
125
126 my $authors;
127 my $marcxml;
128
129 my $sth_select_authors  = $dbh->prepare(q{
130 select
131         biblionumber,
132         itemtype,
133         marcxml
134 from biblioitems
135 where
136         agerestriction > 0
137 });
138
139 =for sql
140 --      ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
141 --      ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
142 --      ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
143
144 --      and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
145 -- order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
146 =cut
147
148 my $biblio_year;
149 my $biblio_full_name;
150 my $type_stats;
151
152 my $parser = XML::LibXML->new();
153 $parser->recover_silently(0); # don't die when you find &, >, etc
154 my $style_doc = $parser->parse_file($xslfilename);
155 my $xslt = XML::LibXSLT->new();
156 my $parsed = $xslt->parse_stylesheet($style_doc);
157
158 my $biblio_html;
159 my $biblio_parsed;
160 my $biblio_data;
161 my $biblio_author_external;
162
163 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
164
165 sub biblioitem_html {
166         my ($biblionumber, $parse_only) = @_;
167
168         return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber} && ! $parse_only;
169
170         my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
171
172         print $xml_fh $xmlrecord if $ENV{XML};
173
174         my $source = eval { $parser->parse_string($xmlrecord) };
175         if ( $@ ) {
176 #               warn "SKIP $biblionumber corrupt XML";
177                 push @{ $skip->{XML_corrupt} }, $biblionumber;
178                 return;
179         }
180
181         if ( $parse_only ) {
182                 $biblio_parsed->{$biblionumber} = $source;
183                 return $source;
184         }
185
186         my $transformed = $parsed->transform($source);
187         $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
188
189         delete $biblio_parsed->{$biblionumber};
190
191         return $biblio_html->{$biblionumber};
192 }
193
194 $sth_select_authors->execute();
195 while( my $row = $sth_select_authors->fetchrow_hashref ) {
196 #       warn dump($row),$/;
197
198         my $biblio;
199
200         $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
201
202         my $doc = biblioitem_html( $row->{biblionumber}, 1 );
203         if ( ! $doc ) {
204 #               warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
205                 next;
206         }
207
208         my $root = $doc->documentElement;
209 =for leader
210         my @leaders = $root->getElementsByLocalName('leader');
211         if (@leaders) {
212                 my $leader = $leaders[0]->textContent;
213                 warn "leader $leader\n";
214         }
215 =cut
216
217         my $extract = {
218                 '008' => undef,
219                 '100' => '(9|a)',
220                 '245' => 'a',
221                 '680' => 'i',
222                 '700' => '(9|4|a)',
223                 '942' => '(t|r|v)'
224         };
225
226         my $data;
227
228         foreach my $elt ($root->getChildrenByLocalName('*')) {
229                 my $tag = $elt->getAttribute('tag');
230                 next if ! $tag;
231                 next unless exists $extract->{ $tag };
232
233         if ($elt->localname eq 'controlfield') {
234                         if ( $tag eq '008' ) {
235                                 my $content = $elt->textContent;
236                                 my $year = substr($content, 7, 4 );
237                                 if ( $year !~ m/^\d+$/ ) {
238                                         $year = 0;
239                                         push @{ $skip->{invalid_year} }, $row->{biblionumber};
240                                 }
241                                 $biblio_year->{ $row->{biblionumber} } = $data->{year} = $year;
242                                 $data->{'008'} = $content;
243                         }
244                         next;
245         } elsif ($elt->localname eq 'datafield') {
246                         my $sf_data;
247             foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
248                 my $sf = $sfelt->getAttribute('code');
249                                 next unless $sf =~ m/$extract->{$tag}/;
250                                 if ( exists $sf_data->{$sf} ) {
251                                         $sf_data->{$sf} .= " " . $sfelt->textContent();
252                                 } else {
253                                         $sf_data->{$sf} = $sfelt->textContent();
254                                 }
255                         }
256                         push @{ $data->{$tag} }, $sf_data if $sf_data;
257                 }
258         }
259
260         if ( ! defined $data->{year} ) {
261                 warn "MISSING year in ", $row->{biblionumber};
262 =for remove-year-limit
263         } elsif ( $data->{year} < 2008 ) {
264                 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
265                 next;
266         } elsif ( $data->{year} > 2013 ) {
267                 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
268                 next;
269 =cut
270         }
271
272 #       warn "# ", $row->{biblionumber}, " data ",dump($data);
273
274         my $category = $data->{942}->[0]->{'t'};
275         if ( ! $category ) {
276 #               warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
277                 push @{ $skip->{no_category} }, $row->{biblionumber};
278                 next;
279         }
280
281         my $have_100 = 1;
282
283         if ( exists $data->{100} ) {
284                         my @first_author =
285                                 map { $_->{'9'} }
286                                 grep {
287                                         if ( ! exists $_->{9} ) {
288                                                 $biblio_author_external->{ $row->{biblionumber} }++;
289                                                 0;
290                                         } elsif ( exists $auth_header->{ $_->{9} } ) {
291                                                 1; # from FFZXG
292                                         } else {
293                                                 0;
294                                         }
295                                 }
296                                 @{ $data->{100} };
297                         foreach my $authid ( @first_author ) {
298                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
299                         }
300                         $biblio_full_name->{ $row->{biblionumber} } = $data->{100}->[0]->{a};
301         } else {
302                 $have_100 = 0;
303         }
304
305         $biblio_full_name->{ $row->{biblionumber} } ||= $data->{245}->[0]->{a};
306
307         my $have_edt;
308
309         if ( exists $data->{700} ) {
310                         my @other_authors =
311                                 grep {
312                                         if ( ! exists $_->{9} ) {
313                                                 $biblio_author_external->{ $row->{biblionumber} }++;
314                                                 0;
315                                         } elsif ( exists $auth_header->{ $_->{9} } ) {
316                                                 1; # from FFZXG
317                                         } else {
318                                                 0;
319                                         }
320                                 }
321                                 @{ $data->{700} };
322                         foreach my $auth ( @other_authors ) {
323                                 my $authid = $auth->{9} || next;
324                                 my $type   = $auth->{4} || next; #die "no 4 in ",dump($data);
325
326                                 $type_stats->{$type}++;
327
328                                 my @types = split(/[\s\/]+/, $type);
329
330                                 foreach my $type ( @types ) {
331                                         my $type = substr($type,0,3);
332                                         $type_stats->{_count_each_type}->{$type}++;
333
334                                         if ( $type =~ m/(edt|trl|com|ctb)/ ) {
335                                                 push @{ $authors->{$authid}->{__sec}->{ $category } }, $row->{biblionumber};
336                                                 push @{ $authors->{$authid}->{$type}->{ $category } }, $row->{biblionumber};
337                                                 $type =~ s/(com|ctb)/_ostalo/;
338                                                 push @{ $authors->{$authid}->{$type}->{ $category } }, $row->{biblionumber};
339
340                                         } elsif ( $type =~ m/aut/ ) {
341                                                 if ( ! $have_100 ) {
342                                                         $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
343                                                         if ( $have_edt ) {
344                                                                 $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
345                                                         } else {
346                                                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
347                                                         }
348                                                 } else {
349                                                         push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
350                                                 }
351                                         } else {
352 #                                               warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
353                                                 $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
354                                         }
355                                 }
356                         }
357                         delete $data->{700};
358         }
359
360         $biblio_data->{ $row->{biblionumber} } = $data;
361
362 }
363
364 debug 'authors' => $authors;
365 store $authors, '/dev/shm/authors.storable';
366 debug 'type_stats' => $type_stats;
367 debug 'skip' => $skip;
368 debug 'biblio_year' => $biblio_year;
369 debug 'biblio_full_name' => $biblio_full_name;
370 debug 'biblio_data' => $biblio_data;
371 debug 'biblio_author_external' => $biblio_author_external;
372
373 my $category_label;
374 my $sth_categories = $dbh->prepare(q{
375 select authorised_value, lib from authorised_values where category = 'BIBCAT'
376 });
377 $sth_categories->execute();
378 while( my $row = $sth_categories->fetchrow_hashref ) {
379         $category_label->{ $row->{authorised_value} } = $row->{lib};
380
381 }
382 debug 'category_label' => $category_label;
383
384 sub html_title {
385         return qq|<html>
386 <head>
387 <meta charset="UTF-8">
388 <title>|, join(" ", @_), qq|</title>
389 <link href="style.css" type="text/css" rel="stylesheet" />
390 <script src="//code.jquery.com/jquery-1.11.2.js"></script>
391 <script src="filters.js"></script>
392 </head>
393 <body>
394 |;
395 }
396
397 sub html_end {
398         return
399                 qq|<small style="color:gray">Zadnji puta osvježeno: |,
400                 strftime("%Y-%m-%d %H:%M:%S\n", localtime()),
401                 qq|</body>\n</html>\n|;
402 }
403
404 mkdir 'html' unless -d 'html';
405
406 open(my $index, '>:encoding(utf-8)', 'html/index.new');
407 print $index html_title('Bibliografija Filozofskog fakulteta');
408
409 my $first_letter = '';
410
411 debug 'authors' => \@authors;
412
413 sub li_biblio {
414         my ($biblionumber) = @_;
415         return qq|<li class="y|, $biblio_year->{$biblionumber}, qq|">|,
416                 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
417                 biblioitem_html($biblionumber),
418                 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
419                 qq|</li>\n|;
420 }
421
422 sub unique {
423         my $unique;
424         $unique->{$_}++ foreach @_;
425         return keys %$unique;
426 }
427
428 sub unique_biblionumber {
429         my @v = unique @_;
430         return sort {
431                 $biblio_year->{$b} <=> $biblio_year->{$a} ||
432                 $biblio_full_name->{$a} cmp $biblio_full_name->{$b} ||
433                 $a <=> $b
434         } @v;
435 }
436
437 sub author_html {
438         my ( $fh, $authid, $type, $label ) = @_;
439
440         return unless exists $authors->{$authid}->{$type};
441
442         print $fh qq|<a name="$type"><h2>$label</h2></a>\n|;
443
444         foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
445                 my $label = $category_label->{$category} || 'Bez kategorije';
446                 print $fh qq|<a name="$type-$category"><h3>$label</h3></a>\n<ol>\n|;
447                 foreach my $biblionumber ( unique_biblionumber @{ $authors->{$authid}->{$type}->{$category} } ) {
448                         print $fh li_biblio( $biblionumber );
449                 }
450                 print $fh qq|</ol>\n|;
451         }
452 }
453
454 my @toc_type_label = (
455 'aut' => 'Primarno autorstvo',
456 'edt' => 'Uredništva',
457 'trl' => 'Prijevodi',
458 '_ostalo' => 'Ostalo',
459 );
460
461
462 sub count_author_years {
463         my $years = shift;
464         my ($authid) = @_;
465         foreach my $type ( keys %{ $authors->{$authid} } ) {
466 #               next if $type =~ m/^_/; # FIXME
467                 foreach my $category ( keys %{ $authors->{$authid}->{$type} } ) {
468                         foreach my $biblionumber ( unique_biblionumber @{ $authors->{$authid}->{$type}->{$category} } ) {
469                                 $years->{ $biblio_year->{ $biblionumber } }->{ $type . '-' . $category }->{ $biblionumber }++;
470                         }
471                 }
472         }
473         return $years;
474 }
475
476 sub html_year_selection {
477         my $fh = shift;
478         my @authids = unique @_;
479
480         debug 'html_year_selection authids=', [ @authids ];
481
482         print $fh qq|<span id="years">Godine:\n|;
483         my $type_cat_count = {};
484         my $years;
485
486         foreach my $authid ( @authids ) {
487                 $years = count_author_years( $years, $authid );
488         }
489
490         debug 'years' => $years;
491
492         foreach my $year ( sort { $b <=> $a } keys %$years ) {
493                 print $fh qq|<label><input name="year_selection" value="$year" type=checkbox onClick="toggle_year($year, this)" checked="checked">$year</label>&nbsp;\n|;
494                 foreach my $type_cat ( keys %{ $years->{$year} } ) {
495                         my $count = scalar keys %{ $years->{$year}->{$type_cat} };
496                         $years->{$year}->{$type_cat} = $count; # remove biblionumbers and use count
497                         $type_cat_count->{ $type_cat } += $count;
498                         my ($type,$cat) = split(/-/, $type_cat);
499                         $type_cat_count->{_toc}->{$type}->{$cat}++;
500                         $type_cat_count->{_toc_count}->{$type} += $count;
501                 }
502         }
503
504         print $fh qq|
505 <input type=button value="all" onClick="all_years(1)">
506 <input type=button value="none" onClick="all_years(0)">
507         |;
508
509         print $fh qq|</span>|;
510
511         print $fh q|
512 <script>
513
514 var years = |, encode_json($years), q|;
515
516 var type_cat_count = |, encode_json($type_cat_count), q|;
517
518 </script>
519
520         |;
521
522         debug 'type_cat_count' => $type_cat_count;
523
524         # TOC
525         print $fh qq|<ul id="toc">\n|;
526         my $i = 0;
527         while ( $i < $#toc_type_label ) {
528                 my $type  = $toc_type_label[$i++] || die "type";
529                 my $label = $toc_type_label[$i++] || die "label";
530                 next unless exists $type_cat_count->{_toc}->{$type};
531                 print $fh qq| <li class="toc" id="toc-$type"><a href="#$type">$label</a> <tt id="toc-count-$type">$type_cat_count->{_toc_count}->{$type}</tt></li>\n <ul>\n|;
532                 foreach my $category ( sort keys %{ $type_cat_count->{_toc}->{$type} } ) {
533                         my $label = $category_label->{$category} || 'Bez kategorije';
534                         my $count = $type_cat_count->{ $type . '-' . $category };
535                         my $cat_html = $category;
536                         $cat_html =~ s/\./-/g;
537                         print $fh qq|  <li class="toc" id="toc-$category"><a href="#$type-$category">$label</a> <tt id="toc-count-$type-$cat_html">$count</tt></li>\n|;
538                 }
539                 print $fh qq| </ul>\n|;
540         }
541         print $fh qq|</ul>\n|;
542
543 }
544
545
546 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
547
548         my $first = substr( $row->{full_name}, 0, 1 );
549         if ( $first ne $first_letter ) {
550                 print $index qq{</ul>\n} if $first_letter;
551                 $first_letter = $first;
552                 print $index qq{<h1>$first</h1>\n<ul>\n};
553         }
554         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
555
556         my $path = "html/$row->{authid}";
557         open(my $fh, '>:encoding(utf-8)', "$path.new");
558         print $fh html_title($row->{full_name}, "bibliografija");
559         print $fh qq|<h1>$row->{full_name} - bibliografija</h1>\n|;
560
561         html_year_selection $fh => $row->{authid};
562
563         my $i = 0;
564         while ( $i < $#toc_type_label ) {
565                 my $type  = $toc_type_label[$i++] || die "type";
566                 my $label = $toc_type_label[$i++] || die "label";
567                 author_html( $fh, $row->{authid}, $type => $label );
568         }
569
570         print $fh html_end;
571         close($fh);
572         rename "$path.new", "$path.html";
573
574 }
575
576 print $index html_end;
577 close($index);
578 rename 'html/index.new', 'html/index.html';
579
580 debug 'auth_header' => $auth_header;
581
582
583 my $department_category_author;
584 foreach my $department ( sort keys %$auth_department ) {
585         foreach my $authid ( sort @{ $auth_department->{$department} } ) {
586                 my   @categories = keys %{ $authors->{$authid}->{aut} };
587                 push @categories,  keys %{ $authors->{$authid}->{__sec} };
588                 foreach my $category ( sort @categories ) {
589                         push @{ $department_category_author->{$department}->{$category} }, $authid;
590                         push @{ $department_category_author->{'AAA_ukupno'}->{$category} }, $authid if $department_in_sum->{$department};
591                         if ( my $group = $department_in_group->{ $department } ) {
592                                 push @{ $department_category_author->{$group}->{$category} }, $authid;
593                         } else {
594                                 $skip->{'department_not_in_group'}->{ $department }++;
595                         }
596                 }
597         }
598 }
599
600 debug 'department_category_author' => $department_category_author;
601
602
603 sub department_html {
604         my ( $fh, $department, $type, $label, $csv_fh ) = @_;
605
606         print $fh qq|<a name="$type"><h2>$label</h2></a>\n|;
607
608         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
609
610                 my @authids = @{ $department_category_author->{$department}->{$category} };
611                 next unless @authids;
612
613                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{$type}->{$category} } } grep { exists $authors->{$_}->{$type}->{$category} } @authids;
614
615                 next unless @biblionumber;
616
617                 my $cat_label = $category_label->{$category} || 'Bez kategorije';
618                 print $fh qq|<a name="$type-$category"><h3>$cat_label</h3></a>\n<ol>\n|;
619
620                 foreach my $bib_num ( @biblionumber ) {
621                         my @li = li_biblio( $bib_num );
622                         my $li_html = join('', @li);
623                         $li_html =~ s{<a name="(col-\d+)"/a>}{<!-- $1 -->}gs;
624                         print $fh $li_html;
625
626                         next unless $csv_fh;
627
628                         my $year = $li[1];
629                         my @html;
630                         foreach ( split(/<a name="col-/, $li[4]) ) {
631                                 if ( s{^(\d+)"></a>}{} ) {
632                                         my $nr = $1;
633                                         s{\s+}{ }gs;
634                                         $html[$nr] = $_;
635                                 } else {
636                                         warn "SKIPPED: Can't find col in [$_] from $li[4]" unless m/^<[^>]+>$/;
637                                 }
638                         }
639                         my $html = join("\t", @html);
640
641                         $html =~ s{</?[^>]*>}{}gs;
642                         $html =~ s{\s+$}{}gs;
643                         print $csv_fh "$bib_num\t$year\t$type\t$label\t$category\t$cat_label\t$html\n";
644                 }
645
646                 print $fh qq|</ol>|;
647         }
648
649 }
650
651
652 mkdir 'html/departments' unless -d 'html/departments';
653
654 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
655 print $dep_fh html_title('Odsjeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
656 foreach my $department ( sort keys %$department_category_author ) {
657         my $dep = $department || 'Nema odsjeka';
658         my $dep_file = unac_string('utf-8',$dep);
659         print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
660         open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
661
662         print $fh html_title($department . ' bibliografija');
663         print $fh qq|<h1>$department bibliografija</h1>\n|;
664
665         my @authids;
666         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
667                 push @authids, @{ $department_category_author->{$department}->{$category} };
668         }
669         html_year_selection $fh => @authids;
670
671         my $csv_fh;
672         if ( $department eq 'AAA_ukupno' ) {
673                 open($csv_fh, '>:encoding(utf-8)', "html/departments/$department.csv");
674         }
675
676         my $i = 0;
677         while ( $i < $#toc_type_label ) {
678                 my $type  = $toc_type_label[$i++] || die "type";
679                 my $label = $toc_type_label[$i++] || die "label";
680                 department_html( $fh, $department, $type, $label, $csv_fh );
681         }
682
683         close($csv_fh) if $csv_fh;
684
685         print $fh html_end;
686         close($fh);
687         rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
688
689 }
690 print $dep_fh qq|</ul>\n|, html_end;
691 close($dep_fh);
692 rename 'html/departments/index.new', 'html/departments/index.html';
693
694 my $azvo_stat_biblio;
695
696 foreach my $department ( sort keys %$department_category_author ) {
697         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
698                 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
699                         my $group = $auth_group->{$authid};
700                         if ( ! $group ) {
701                                 push @{ $skip->{no_auth_group} }, $authid;
702                                 next;
703                         }
704                         foreach my $type ( keys %{ $authors->{$authid} } ) {
705                                 next unless exists $authors->{$authid}->{$type}->{$category};
706                                 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} },  @{ $authors->{$authid}->{$type}->{$category} };
707                                 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} },  @{ $authors->{$authid}->{$type}->{$category} };
708                         }
709                 }
710                 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
711                         foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
712                                 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
713                                 $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
714                         }
715                 }
716         }
717 }
718
719 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
720
721 my @report_lines;
722 my @report_labels;
723
724 my $label;
725 my $sub_labels;
726 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
727 while( <$report> ) {
728         chomp;
729         if ( /^([^\t]+)\t+(.+)/ ) {
730                 $label = $1;
731                 push @report_labels, $label;
732                 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
733                 push @report_lines, [ $label, @$type ];
734         } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
735                 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
736                 my $sub_label = $1;
737                 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
738                 push @report_labels, $label . $sub_label;
739         } else {
740                 die "ERROR: [$_]\n";
741         }
742 }
743
744 debug 'report_lines', \@report_lines;
745 debug 'sub_labels', $sub_labels;
746 debug 'report_labels', \@report_labels;
747
748 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
749
750 debug 'departments' => \@departments;
751
752 my $department2col;
753 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
754 my $label2row;
755 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
756
757 my $table;
758
759 sub table_count {
760         my $label = shift @_;
761         my $department = shift @_;
762         my $group = shift @_;
763         my @biblionumbers = unique @_;
764         $table->{ffzg}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar @biblionumbers;
765         $table->{external}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar grep { $biblio_author_external->{$_} } @biblionumbers;
766 }
767
768 foreach my $group ( '', keys %$azvo_group_title ) {
769
770 foreach my $department ( @departments ) {
771         foreach my $line ( @report_lines ) {
772                 my $label = $line->[0];
773                 my @biblionumbers;
774                 foreach ( 1 .. $#$line ) {
775                         my ( $category, $type ) = @{ $line->[ $_ ] };
776                         my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type}->{$group};
777                         push @biblionumbers, @$b if $b;
778                 }
779                 if ( $sub_labels->{$label} ) {
780                         my $sub_stats;
781                         foreach my $biblionumber ( @biblionumbers ) {
782                                 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
783                                 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
784                                         my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
785                                         if ( ! $regex ) {
786                                                 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
787                                                 last;
788                                         }
789                                         if ( $field < 100 ) {
790                                                 if ( $data->{$field} =~ m/$regex/ ) {
791                                                         push @{ $sub_stats->{ $sub_label } }, $biblionumber;
792                                                         last;
793                                                 }
794                                         } else {
795                                                 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
796                                                         push @{ $sub_stats->{ $sub_label } }, $biblionumber;
797                                                         last;
798                                                 }
799                                         }
800                                 }
801                         }
802                         foreach my $sub_label ( keys %$sub_stats ) {
803                                 my $full_label = $label . $sub_label;
804                                 table_count $full_label, $department, $group, @{ $sub_stats->{$sub_label} };
805                         }
806                 } else {
807                         table_count $label, $department, $group, @biblionumbers;
808                 }
809         }
810 }
811
812 } # group
813
814 debug 'table', $table;
815
816 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
817 open(my $fh2, '>:encoding(utf-8)', 'html/azvo2.new');
818
819 sub print_fh {
820         print $fh @_;
821         print $fh2 @_;
822 }
823
824 print $fh html_title('AZVO tablica - FFZG');
825 print $fh2 html_title('AZVO tablica - kolaboracija sa FFZG');
826
827 foreach my $group ( keys %{ $table->{ffzg} } ) {
828
829                 print_fh "<h1>$group</h1>" if $group;
830
831                 print_fh "<table border=1>\n";
832                 print_fh "<tr><th></th>";
833                 print_fh "<th>$_</th>" foreach @departments;
834                 print_fh "</tr>\n";
835
836                 foreach my $row ( 0 .. $#{ $table->{ffzg}->{$group} } ) {
837                         print_fh "<tr><th>", $report_labels[$row], "</th>\n";
838                         foreach ( 0 .. $#departments ) {
839                                 print_fh "<td>";
840                                 print $fh $table->{ffzg}->{$group}->[ $row ]->[ $_ ] || '';
841                                 print $fh2 $table->{external}->{$group}->[ $row ]->[ $_ ] || '';
842                                 print_fh "</td>\n"
843                         }
844                         print_fh "</tr>\n";
845                 }
846
847                 print_fh "</table>\n";
848
849 } # group
850
851 print_fh html_end;
852 close($fh);
853 close($fh2);
854 rename 'html/azvo.new', 'html/azvo.html';
855 rename 'html/azvo2.new', 'html/azvo2.html';
856
857 unlink $pid_file;
858