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