3 # LC_COLLATE=hr_HR.utf8 KOHA_CONF=/etc/koha/sites/ffzg/koha-conf.xml ./html.pl
9 use Data::Dump qw(dump);
17 use lib '/srv/koha_ffzg';
22 my $dbh = C4::Context->dbh;
25 my ($title, $data) = @_;
26 print "# $title ",dump($data), $/ if $ENV{DEBUG};
29 my $xslfilename = 'compact.xsl';
31 my $azvo_group_title = {
32 'znanstveno nastavni' => qr/(profes|docent|znanstveni savjetnik|znanstveni suradnik)/i,
33 'lektori i predavači' => qr/(lektor|predavač)/i,
34 'asistenti i novaci' => qr/(asistent|novak)/i,
37 my $department_groups = {
38 'AAB_humanističke' => qr/(anglistiku|arheologiju|antropologiju|filozofiju|fonetiku|germanistiku|hungarologiju|indologiju|slavenske|filologiju|komparativnu|kroatistiku|lingvistiku|povijest|romanistiku|talijanistiku)/i,
39 'AAC_društvene' => qr/(informacijske|pedagogiju|psihologiju|sociologiju)/i,
46 my $department_in_sum;
47 my $department_in_group;
51 my $sth_auth = $dbh->prepare(q{
54 ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
55 ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department,
56 ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="i"]') as academic_title
61 while( my $row = $sth_auth->fetchrow_hashref ) {
62 if ( $row->{department} !~ m/Filozofski fakultet u Zagrebu/ ) {
63 push @{ $skip->{nije_ffzg} }, $row;
66 $auth_header->{ $row->{authid} } = $row->{full_name};
67 $row->{department} =~ s/, Filozofski fakultet u Zagrebu.*$//;
68 $row->{department} =~ s/^.+\.\s*//;
69 $row->{department} =~ s/\s+$//s;
71 foreach my $title ( keys %$azvo_group_title ) {
72 if ( $row->{academic_title} =~ $azvo_group_title->{$title} ) {
78 $row->{academic_group} = $group;
79 $auth_group->{ $row->{authid} } = $group;
80 $skip->{group_stat}->{$group}++;
82 push @{ $skip->{no_academic_group} }, $row;
85 # warn "# ", dump( $row );
86 push @{ $auth_department->{ $row->{department} } }, $row->{authid};
88 $department_in_sum->{ $row->{department} }++;
89 foreach my $name ( keys %$department_groups ) {
90 my $regex = $department_groups->{$name};
91 if ( $row->{department} =~ $regex ) {
92 $department_in_group->{ $row->{department} } = $name;
98 debug 'department_in_group' => $department_in_group;
100 foreach my $department ( keys %$department_in_sum ) {
101 $department_in_sum->{$department} = 0 unless $department =~ m/(centar|croaticum|katedra|odsjek)/i;
104 debug 'auth_department' => $auth_department;
105 debug 'auth_group' => $auth_group;
106 debug 'department_in_sum' => $department_in_sum;
112 my $sth_select_authors = $dbh->prepare(q{
123 -- ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
124 -- ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
125 -- ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
127 -- and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
128 -- order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
134 my $parser = XML::LibXML->new();
135 $parser->recover_silently(0); # don't die when you find &, >, etc
136 my $style_doc = $parser->parse_file($xslfilename);
137 my $xslt = XML::LibXSLT->new();
138 my $parsed = $xslt->parse_stylesheet($style_doc);
143 my $biblio_author_external;
145 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
147 sub biblioitem_html {
148 my ($biblionumber, $parse_only) = @_;
150 return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber} && ! $parse_only;
152 my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
154 print $xml_fh $xmlrecord if $ENV{XML};
156 my $source = eval { $parser->parse_string($xmlrecord) };
158 # warn "SKIP $biblionumber corrupt XML";
159 push @{ $skip->{XML_corrupt} }, $biblionumber;
164 $biblio_parsed->{$biblionumber} = $source;
168 my $transformed = $parsed->transform($source);
169 $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
171 delete $biblio_parsed->{$biblionumber};
173 return $biblio_html->{$biblionumber};
176 $sth_select_authors->execute();
177 while( my $row = $sth_select_authors->fetchrow_hashref ) {
178 # warn dump($row),$/;
182 $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
184 my $doc = biblioitem_html( $row->{biblionumber}, 1 );
186 # warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
190 my $root = $doc->documentElement;
192 my @leaders = $root->getElementsByLocalName('leader');
194 my $leader = $leaders[0]->textContent;
195 warn "leader $leader\n";
209 foreach my $elt ($root->getChildrenByLocalName('*')) {
210 my $tag = $elt->getAttribute('tag');
212 next unless exists $extract->{ $tag };
214 if ($elt->localname eq 'controlfield') {
215 if ( $tag eq '008' ) {
216 my $content = $elt->textContent;
217 my $year = substr($content, 7, 4 );
218 if ( $year !~ m/^\d+$/ ) {
220 push @{ $skip->{invalid_year} }, $row->{biblionumber};
222 $biblio_year->{ $row->{biblionumber} } = $data->{year} = $year;
223 $data->{'008'} = $content;
226 } elsif ($elt->localname eq 'datafield') {
228 foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
229 my $sf = $sfelt->getAttribute('code');
230 next unless $sf =~ m/$extract->{$tag}/;
231 if ( exists $sf_data->{$sf} ) {
232 $sf_data->{$sf} .= " " . $sfelt->textContent();
234 $sf_data->{$sf} = $sfelt->textContent();
237 push @{ $data->{$tag} }, $sf_data if $sf_data;
241 if ( ! defined $data->{year} ) {
242 warn "MISSING year in ", $row->{biblionumber};
243 =for remove-year-limit
244 } elsif ( $data->{year} < 2008 ) {
245 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
247 } elsif ( $data->{year} > 2013 ) {
248 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
253 # warn "# ", $row->{biblionumber}, " data ",dump($data);
255 my $category = $data->{942}->[0]->{'t'};
257 # warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
258 push @{ $skip->{no_category} }, $row->{biblionumber};
264 if ( exists $data->{100} ) {
268 if ( ! exists $_->{9} ) {
269 $biblio_author_external->{ $row->{biblionumber} }++;
271 } elsif ( exists $auth_header->{ $_->{9} } ) {
278 foreach my $authid ( @first_author ) {
279 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
287 if ( exists $data->{700} ) {
290 if ( ! exists $_->{9} ) {
291 $biblio_author_external->{ $row->{biblionumber} }++;
293 } elsif ( exists $auth_header->{ $_->{9} } ) {
300 foreach my $auth ( @other_authors ) {
301 my $authid = $auth->{9} || next;
302 my $type = $auth->{4} || next; #die "no 4 in ",dump($data);
304 $type_stats->{$type}++;
306 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
307 push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
308 push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
309 } elsif ( $type =~ m/aut/ ) {
311 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
313 $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
315 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
318 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
321 # warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
322 $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
328 $biblio_data->{ $row->{biblionumber} } = $data;
332 debug 'authors' => $authors;
333 debug 'type_stats' => $type_stats;
334 debug 'skip' => $skip;
335 debug 'biblio_year' => $biblio_year;
336 debug 'biblio_data' => $biblio_data;
337 debug 'biblio_author_external' => $biblio_author_external;
340 my $sth_categories = $dbh->prepare(q{
341 select authorised_value, lib from authorised_values where category = 'BIBCAT'
343 $sth_categories->execute();
344 while( my $row = $sth_categories->fetchrow_hashref ) {
345 $category_label->{ $row->{authorised_value} } = $row->{lib};
348 debug 'category_label' => $category_label;
353 <meta charset="UTF-8">
354 <title>|, join(" ", @_), qq|</title>
355 <link href="style.css" type="text/css" rel="stylesheet" />
356 <script src="//code.jquery.com/jquery-1.11.2.js"></script>
363 return qq|</body>\n</html>\n|;
366 mkdir 'html' unless -d 'html';
368 open(my $index, '>:encoding(utf-8)', 'html/index.new');
369 print $index html_title('Bibliografija Filozofskog fakulteta');
371 my $first_letter = '';
373 debug 'authors' => \@authors;
376 my ($biblionumber) = @_;
377 return qq|<li class="y|, $biblio_year->{$biblionumber}, qq|">|,
378 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
379 biblioitem_html($biblionumber),
380 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
385 my ( $fh, $authid, $type, $label ) = @_;
387 return unless exists $authors->{$authid}->{$type};
389 print $fh qq|<h2>$label</h2>\n|;
391 foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
392 my $label = $category_label->{$category} || 'Bez kategorije';
393 print $fh qq|<a name="$type-$category"><h3>$label</h3></a>\n<ul>\n|;
394 foreach my $biblionumber ( sort {
395 $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b
396 } @{ $authors->{$authid}->{$type}->{$category} } ) {
397 print $fh li_biblio( $biblionumber );
399 print $fh qq|</ul>\n|;
403 sub count_author_years {
406 foreach my $type ( keys %{ $authors->{$authid} } ) {
407 foreach my $category ( keys %{ $authors->{$authid}->{$type} } ) {
408 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
409 $years->{ $biblio_year->{ $biblionumber } }->{ $type . '-' . $category }++;
416 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
418 my $first = substr( $row->{full_name}, 0, 1 );
419 if ( $first ne $first_letter ) {
420 print $index qq{</ul>\n} if $first_letter;
421 $first_letter = $first;
422 print $index qq{<h1>$first</h1>\n<ul>\n};
424 print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
426 my $path = "html/$row->{authid}";
427 open(my $fh, '>:encoding(utf-8)', "$path.new");
428 print $fh html_title($row->{full_name}, "bibliografija");
429 print $fh qq|<h1>$row->{full_name} - bibliografija</h1>|;
431 my $years = count_author_years( $row->{authid} );
432 print $fh qq|<span id="years">Godine:|;
433 my $type_cat_count = {};
434 foreach my $year ( sort { $b <=> $a } keys %$years ) {
435 print $fh qq|<label><input name="year_selection" value="$year" type=checkbox onClick="toggle_year($year, this)" checked>$year</label> \n|;
436 foreach my $type_cat ( keys %{ $years->{$year} } ) {
437 $type_cat_count->{ $type_cat } += $years->{$year}->{$type_cat};
440 print $fh qq|</span>|;
445 var years = |, encode_json($years), q|;
447 var type_cat_count = |, encode_json($type_cat_count), q|;
449 function year_show(year) {
451 console.debug('show', year);
452 for(var type_cat in years[year]) {
453 if ( ( type_cat_count[ type_cat ] += years[year][type_cat] ) == years[year][type_cat]) {
454 $('a[name="'+type_cat+'"]').show();
455 console.debug(type_cat, 'show');
460 function year_hide(year) {
462 console.debug('hide', year);
463 for(var type_cat in years[year]) {
464 if ( ( type_cat_count[ type_cat ] -= years[year][type_cat] ) == 0 ) {
465 $('a[name="'+type_cat+'"]').hide();
466 console.debug(type_cat, 'hide');
471 function toggle_year(year, el) {
479 $(document).ready( function() {
480 console.info('ready');
482 $('input[name=year_selection]').each( function(i, el) {
484 console.debug( 'on load', year, el.checked );
485 if (! el.checked) year_hide(year);
492 author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
493 author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
497 rename "$path.new", "$path.html";
501 print $index html_end;
503 rename 'html/index.new', 'html/index.html';
505 debug 'auth_header' => $auth_header;
508 my $department_category_author;
509 foreach my $department ( sort keys %$auth_department ) {
510 foreach my $authid ( sort @{ $auth_department->{$department} } ) {
511 my @categories = keys %{ $authors->{$authid}->{aut} };
512 push @categories, keys %{ $authors->{$authid}->{sec} };
513 foreach my $category ( sort @categories ) {
514 push @{ $department_category_author->{$department}->{$category} }, $authid;
515 push @{ $department_category_author->{'AAA_ukupno'}->{$category} }, $authid if $department_in_sum->{$department};
516 if ( my $group = $department_in_group->{ $department } ) {
517 push @{ $department_category_author->{$group}->{$category} }, $authid;
519 $skip->{'department_not_in_group'}->{ $department }++;
525 debug 'department_category_author' => $department_category_author;
527 mkdir 'html/departments' unless -d 'html/departments';
529 sub unique_biblionumber {
532 $u->{$_}++ foreach @v;
533 return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
536 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
537 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
538 foreach my $department ( sort keys %$department_category_author ) {
539 my $dep = $department || 'Nema odsjeka';
540 my $dep_file = unac_string('utf-8',$dep);
541 print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
542 open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
544 print $fh html_title($department . ' bibliografija');
545 print $fh qq|<h1>$department bibliografija</h1>\n|;
547 print $fh qq|<h2>Primarno autorstvo</h2>\n|;
549 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
551 my @authids = @{ $department_category_author->{$department}->{$category} };
552 next unless @authids;
554 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
556 $unique->{$_}++ foreach @biblionumber;
558 next unless @biblionumber;
560 my $label = $category_label->{$category} || 'Bez kategorije';
561 print $fh qq|<h3>$label</h3>\n<ul>\n|;
563 print $fh li_biblio( $_ ) foreach @biblionumber;
569 print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
571 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
573 my @authids = @{ $department_category_author->{$department}->{$category} };
574 next unless @authids;
576 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
578 next unless @biblionumber;
580 my $label = $category_label->{$category} || 'Bez kategorije';
581 print $fh qq|<h3>$label</h3>\n<ul>\n|;
583 print $fh li_biblio( $_ ) foreach @biblionumber;
591 rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
593 print $dep_fh qq|</ul>\n|, html_end;
595 rename 'html/departments/index.new', 'html/departments/index.html';
597 my $azvo_stat_biblio;
599 foreach my $department ( sort keys %$department_category_author ) {
600 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
601 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
602 my $group = $auth_group->{$authid};
604 push @{ $skip->{no_auth_group} }, $authid;
607 foreach my $type ( keys %{ $authors->{$authid} } ) {
608 next unless exists $authors->{$authid}->{$type}->{$category};
609 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} }, @{ $authors->{$authid}->{$type}->{$category} };
610 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} }, @{ $authors->{$authid}->{$type}->{$category} };
613 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
614 foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
615 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
616 $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
622 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
629 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
632 if ( /^([^\t]+)\t+(.+)/ ) {
634 push @report_labels, $label;
635 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
636 push @report_lines, [ $label, @$type ];
637 } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
638 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
640 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
641 push @report_labels, $label . $sub_label;
647 debug 'report_lines', \@report_lines;
648 debug 'sub_labels', $sub_labels;
649 debug 'report_labels', \@report_labels;
651 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
653 debug 'departments' => \@departments;
656 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
658 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
663 my $label = shift @_;
664 my $department = shift @_;
665 my $group = shift @_;
666 my @biblionumbers = @_;
668 $unique->{$_}++ foreach @biblionumbers;
669 my @bibs = keys %$unique;
670 $table->{ffzg}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar @bibs;
671 $table->{external}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar grep { $biblio_author_external->{$_} } @bibs;
674 foreach my $group ( '', keys %$azvo_group_title ) {
676 foreach my $department ( @departments ) {
677 foreach my $line ( @report_lines ) {
678 my $label = $line->[0];
680 foreach ( 1 .. $#$line ) {
681 my ( $category, $type ) = @{ $line->[ $_ ] };
682 my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type}->{$group};
683 push @biblionumbers, @$b if $b;
685 if ( $sub_labels->{$label} ) {
687 foreach my $biblionumber ( @biblionumbers ) {
688 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
689 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
690 my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
692 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
695 if ( $field < 100 ) {
696 if ( $data->{$field} =~ m/$regex/ ) {
697 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
701 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
702 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
708 foreach my $sub_label ( keys %$sub_stats ) {
709 my $full_label = $label . $sub_label;
710 table_count $full_label, $department, $group, @{ $sub_stats->{$sub_label} };
713 table_count $label, $department, $group, @biblionumbers;
720 debug 'table', $table;
722 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
723 open(my $fh2, '>:encoding(utf-8)', 'html/azvo2.new');
730 print $fh html_title('AZVO tablica - FFZG');
731 print $fh2 html_title('AZVO tablica - kolaboracija sa FFZG');
733 foreach my $group ( keys %{ $table->{ffzg} } ) {
735 print_fh "<h1>$group</h1>" if $group;
737 print_fh "<table border=1>\n";
738 print_fh "<tr><th></th>";
739 print_fh "<th>$_</th>" foreach @departments;
742 foreach my $row ( 0 .. $#{ $table->{ffzg}->{$group} } ) {
743 print_fh "<tr><th>", $report_labels[$row], "</th>\n";
744 foreach ( 0 .. $#departments ) {
746 print $fh $table->{ffzg}->{$group}->[ $row ]->[ $_ ] || '';
747 print $fh2 $table->{external}->{$group}->[ $row ]->[ $_ ] || '';
753 print_fh "</table>\n";
760 rename 'html/azvo.new', 'html/azvo.html';
761 rename 'html/azvo2.new', 'html/azvo2.html';