3 # LC_COLLATE=hr_HR.utf8 KOHA_CONF=/etc/koha/sites/ffzg/koha-conf.xml ./html.pl
9 use Data::Dump qw(dump);
16 use POSIX qw(strftime);
18 use lib '/srv/koha_ffzg';
23 my $dbh = C4::Context->dbh;
26 my ($title, $data) = @_;
27 print "# $title ",dump($data), $/ if $ENV{DEBUG};
30 my $xslfilename = 'compact.xsl';
32 my $azvo_group_title = {
33 'znanstveno nastavni' => qr/(profes|docent|znanstveni savjetnik|znanstveni suradnik)/i,
34 'lektori i predavači' => qr/(lektor|predavač)/i,
35 'asistenti i novaci' => qr/(asistent|novak)/i,
38 my $department_groups = {
39 'AAB_humanističke' => qr/(anglistiku|arheologiju|antropologiju|filozofiju|fonetiku|germanistiku|hungarologiju|indologiju|slavenske|filologiju|komparativnu|kroatistiku|lingvistiku|povijest|romanistiku|talijanistiku)/i,
40 'AAC_društvene' => qr/(informacijske|pedagogiju|psihologiju|sociologiju)/i,
47 my $department_in_sum;
48 my $department_in_group;
52 my $sth_auth = $dbh->prepare(q{
55 ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
56 ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department,
57 ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="i"]') as academic_title
62 while( my $row = $sth_auth->fetchrow_hashref ) {
63 if ( $row->{department} !~ m/Filozofski fakultet u Zagrebu/ ) {
64 push @{ $skip->{nije_ffzg} }, $row;
67 $auth_header->{ $row->{authid} } = $row->{full_name};
68 $row->{department} =~ s/, Filozofski fakultet u Zagrebu.*$//;
69 $row->{department} =~ s/^.+\.\s*//;
70 $row->{department} =~ s/\s+$//s;
72 foreach my $title ( keys %$azvo_group_title ) {
73 if ( $row->{academic_title} =~ $azvo_group_title->{$title} ) {
79 $row->{academic_group} = $group;
80 $auth_group->{ $row->{authid} } = $group;
81 $skip->{group_stat}->{$group}++;
83 push @{ $skip->{no_academic_group} }, $row;
86 # warn "# ", dump( $row );
87 push @{ $auth_department->{ $row->{department} } }, $row->{authid};
89 $department_in_sum->{ $row->{department} }++;
90 foreach my $name ( keys %$department_groups ) {
91 my $regex = $department_groups->{$name};
92 if ( $row->{department} =~ $regex ) {
93 $department_in_group->{ $row->{department} } = $name;
99 debug 'department_in_group' => $department_in_group;
101 foreach my $department ( keys %$department_in_sum ) {
102 $department_in_sum->{$department} = 0 unless $department =~ m/(centar|croaticum|katedra|odsjek)/i;
105 debug 'auth_department' => $auth_department;
106 debug 'auth_group' => $auth_group;
107 debug 'department_in_sum' => $department_in_sum;
113 my $sth_select_authors = $dbh->prepare(q{
124 -- ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
125 -- ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
126 -- ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
128 -- and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
129 -- order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
135 my $parser = XML::LibXML->new();
136 $parser->recover_silently(0); # don't die when you find &, >, etc
137 my $style_doc = $parser->parse_file($xslfilename);
138 my $xslt = XML::LibXSLT->new();
139 my $parsed = $xslt->parse_stylesheet($style_doc);
144 my $biblio_author_external;
146 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
148 sub biblioitem_html {
149 my ($biblionumber, $parse_only) = @_;
151 return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber} && ! $parse_only;
153 my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
155 print $xml_fh $xmlrecord if $ENV{XML};
157 my $source = eval { $parser->parse_string($xmlrecord) };
159 # warn "SKIP $biblionumber corrupt XML";
160 push @{ $skip->{XML_corrupt} }, $biblionumber;
165 $biblio_parsed->{$biblionumber} = $source;
169 my $transformed = $parsed->transform($source);
170 $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
172 delete $biblio_parsed->{$biblionumber};
174 return $biblio_html->{$biblionumber};
177 $sth_select_authors->execute();
178 while( my $row = $sth_select_authors->fetchrow_hashref ) {
179 # warn dump($row),$/;
183 $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
185 my $doc = biblioitem_html( $row->{biblionumber}, 1 );
187 # warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
191 my $root = $doc->documentElement;
193 my @leaders = $root->getElementsByLocalName('leader');
195 my $leader = $leaders[0]->textContent;
196 warn "leader $leader\n";
210 foreach my $elt ($root->getChildrenByLocalName('*')) {
211 my $tag = $elt->getAttribute('tag');
213 next unless exists $extract->{ $tag };
215 if ($elt->localname eq 'controlfield') {
216 if ( $tag eq '008' ) {
217 my $content = $elt->textContent;
218 my $year = substr($content, 7, 4 );
219 if ( $year !~ m/^\d+$/ ) {
221 push @{ $skip->{invalid_year} }, $row->{biblionumber};
223 $biblio_year->{ $row->{biblionumber} } = $data->{year} = $year;
224 $data->{'008'} = $content;
227 } elsif ($elt->localname eq 'datafield') {
229 foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
230 my $sf = $sfelt->getAttribute('code');
231 next unless $sf =~ m/$extract->{$tag}/;
232 if ( exists $sf_data->{$sf} ) {
233 $sf_data->{$sf} .= " " . $sfelt->textContent();
235 $sf_data->{$sf} = $sfelt->textContent();
238 push @{ $data->{$tag} }, $sf_data if $sf_data;
242 if ( ! defined $data->{year} ) {
243 warn "MISSING year in ", $row->{biblionumber};
244 =for remove-year-limit
245 } elsif ( $data->{year} < 2008 ) {
246 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
248 } elsif ( $data->{year} > 2013 ) {
249 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
254 # warn "# ", $row->{biblionumber}, " data ",dump($data);
256 my $category = $data->{942}->[0]->{'t'};
258 # warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
259 push @{ $skip->{no_category} }, $row->{biblionumber};
265 if ( exists $data->{100} ) {
269 if ( ! exists $_->{9} ) {
270 $biblio_author_external->{ $row->{biblionumber} }++;
272 } elsif ( exists $auth_header->{ $_->{9} } ) {
279 foreach my $authid ( @first_author ) {
280 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
288 if ( exists $data->{700} ) {
291 if ( ! exists $_->{9} ) {
292 $biblio_author_external->{ $row->{biblionumber} }++;
294 } elsif ( exists $auth_header->{ $_->{9} } ) {
301 foreach my $auth ( @other_authors ) {
302 my $authid = $auth->{9} || next;
303 my $type = $auth->{4} || next; #die "no 4 in ",dump($data);
305 $type_stats->{$type}++;
307 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
308 push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
309 push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
310 } elsif ( $type =~ m/aut/ ) {
312 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
314 $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
316 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
319 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
322 # warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
323 $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
329 $biblio_data->{ $row->{biblionumber} } = $data;
333 debug 'authors' => $authors;
334 debug 'type_stats' => $type_stats;
335 debug 'skip' => $skip;
336 debug 'biblio_year' => $biblio_year;
337 debug 'biblio_data' => $biblio_data;
338 debug 'biblio_author_external' => $biblio_author_external;
341 my $sth_categories = $dbh->prepare(q{
342 select authorised_value, lib from authorised_values where category = 'BIBCAT'
344 $sth_categories->execute();
345 while( my $row = $sth_categories->fetchrow_hashref ) {
346 $category_label->{ $row->{authorised_value} } = $row->{lib};
349 debug 'category_label' => $category_label;
354 <meta charset="UTF-8">
355 <title>|, join(" ", @_), qq|</title>
356 <link href="style.css" type="text/css" rel="stylesheet" />
357 <script src="//code.jquery.com/jquery-1.11.2.js"></script>
365 qq|<small style="color:gray">Zadnji puta osvježeno: |,
366 strftime("%Y-%m-%d %H:%M:%S\n", localtime()),
367 qq|</body>\n</html>\n|;
370 mkdir 'html' unless -d 'html';
372 open(my $index, '>:encoding(utf-8)', 'html/index.new');
373 print $index html_title('Bibliografija Filozofskog fakulteta');
375 my $first_letter = '';
377 debug 'authors' => \@authors;
380 my ($biblionumber) = @_;
381 return qq|<li class="y|, $biblio_year->{$biblionumber}, qq|">|,
382 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
383 biblioitem_html($biblionumber),
384 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
388 sub unique_biblionumber {
391 $u->{$_}++ foreach @v;
392 return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
396 my ( $fh, $authid, $type, $label ) = @_;
398 return unless exists $authors->{$authid}->{$type};
400 print $fh qq|<h2>$label</h2>\n|;
402 foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
403 my $label = $category_label->{$category} || 'Bez kategorije';
404 print $fh qq|<a name="$type-$category"><h3>$label</h3></a>\n<ul>\n|;
405 foreach my $biblionumber ( unique_biblionumber @{ $authors->{$authid}->{$type}->{$category} } ) {
406 print $fh li_biblio( $biblionumber );
408 print $fh qq|</ul>\n|;
413 sub count_author_years {
416 foreach my $type ( keys %{ $authors->{$authid} } ) {
417 foreach my $category ( keys %{ $authors->{$authid}->{$type} } ) {
418 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
419 $years->{ $biblio_year->{ $biblionumber } }->{ $type . '-' . $category }++;
426 sub html_year_selection {
430 print $fh qq|<span id="years">Godine:\n|;
431 my $type_cat_count = {};
434 foreach my $authid ( @authids ) {
435 $years = count_author_years( $years, $authid );
438 foreach my $year ( sort { $b <=> $a } keys %$years ) {
439 print $fh qq|<label><input name="year_selection" value="$year" type=checkbox onClick="toggle_year($year, this)" checked="checked">$year</label> \n|;
440 foreach my $type_cat ( keys %{ $years->{$year} } ) {
441 $type_cat_count->{ $type_cat } += $years->{$year}->{$type_cat};
446 <input type=button value="all" onClick="all_years(1)">
447 <input type=button value="none" onClick="all_years(0)">
450 print $fh qq|</span>|;
455 var years = |, encode_json($years), q|;
457 var type_cat_count = |, encode_json($type_cat_count), q|;
459 function year_show(year) {
461 console.debug('show', year);
462 for(var type_cat in years[year]) {
463 if ( ( type_cat_count[ type_cat ] += years[year][type_cat] ) == years[year][type_cat]) {
464 $('a[name="'+type_cat+'"]').show();
465 console.debug(type_cat, 'show');
470 function year_hide(year) {
472 console.debug('hide', year);
473 for(var type_cat in years[year]) {
474 if ( ( type_cat_count[ type_cat ] -= years[year][type_cat] ) == 0 ) {
475 $('a[name="'+type_cat+'"]').hide();
476 console.debug(type_cat, 'hide');
481 function toggle_year(year, el) {
489 function all_years( turn_on ) {
490 $('input[name=year_selection]').each( function(i,el) {
492 if ( ! el.checked ) {
494 year_show( el.value );
499 year_hide( el.value );
505 $(document).ready( function() {
506 console.info('ready');
508 $('input[name=year_selection]').each( function(i, el) {
510 console.debug( 'on load', year, el.checked );
511 if (! el.checked) year_hide(year);
519 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
521 my $first = substr( $row->{full_name}, 0, 1 );
522 if ( $first ne $first_letter ) {
523 print $index qq{</ul>\n} if $first_letter;
524 $first_letter = $first;
525 print $index qq{<h1>$first</h1>\n<ul>\n};
527 print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
529 my $path = "html/$row->{authid}";
530 open(my $fh, '>:encoding(utf-8)', "$path.new");
531 print $fh html_title($row->{full_name}, "bibliografija");
532 print $fh qq|<h1>$row->{full_name} - bibliografija</h1>\n|;
534 html_year_selection $fh => $row->{authid};
536 author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
537 author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
541 rename "$path.new", "$path.html";
545 print $index html_end;
547 rename 'html/index.new', 'html/index.html';
549 debug 'auth_header' => $auth_header;
552 my $department_category_author;
553 foreach my $department ( sort keys %$auth_department ) {
554 foreach my $authid ( sort @{ $auth_department->{$department} } ) {
555 my @categories = keys %{ $authors->{$authid}->{aut} };
556 push @categories, keys %{ $authors->{$authid}->{sec} };
557 foreach my $category ( sort @categories ) {
558 push @{ $department_category_author->{$department}->{$category} }, $authid;
559 push @{ $department_category_author->{'AAA_ukupno'}->{$category} }, $authid if $department_in_sum->{$department};
560 if ( my $group = $department_in_group->{ $department } ) {
561 push @{ $department_category_author->{$group}->{$category} }, $authid;
563 $skip->{'department_not_in_group'}->{ $department }++;
569 debug 'department_category_author' => $department_category_author;
572 sub department_html {
573 my ( $fh, $department, $type, $label ) = @_;
575 print $fh qq|<h2>$label</h2>\n|;
577 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
579 my @authids = @{ $department_category_author->{$department}->{$category} };
580 next unless @authids;
582 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{$type}->{$category} } } grep { exists $authors->{$_}->{$type}->{$category} } @authids;
584 next unless @biblionumber;
586 my $label = $category_label->{$category} || 'Bez kategorije';
587 print $fh qq|<a name="$type-$category"><h3>$label</h3></a>\n<ul>\n|;
589 print $fh li_biblio( $_ ) foreach @biblionumber;
597 mkdir 'html/departments' unless -d 'html/departments';
599 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
600 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
601 foreach my $department ( sort keys %$department_category_author ) {
602 my $dep = $department || 'Nema odsjeka';
603 my $dep_file = unac_string('utf-8',$dep);
604 print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
605 open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
607 print $fh html_title($department . ' bibliografija');
608 print $fh qq|<h1>$department bibliografija</h1>\n|;
611 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
612 push @authids, @{ $department_category_author->{$department}->{$category} };
614 html_year_selection $fh => @authids;
616 department_html( $fh, $department, 'aut', 'Primarno autorstvo' );
618 department_html( $fh, $department, 'sec', 'Sekundarno autorstvo' );
622 rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
624 print $dep_fh qq|</ul>\n|, html_end;
626 rename 'html/departments/index.new', 'html/departments/index.html';
628 my $azvo_stat_biblio;
630 foreach my $department ( sort keys %$department_category_author ) {
631 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
632 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
633 my $group = $auth_group->{$authid};
635 push @{ $skip->{no_auth_group} }, $authid;
638 foreach my $type ( keys %{ $authors->{$authid} } ) {
639 next unless exists $authors->{$authid}->{$type}->{$category};
640 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} }, @{ $authors->{$authid}->{$type}->{$category} };
641 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} }, @{ $authors->{$authid}->{$type}->{$category} };
644 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
645 foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
646 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
647 $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
653 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
660 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
663 if ( /^([^\t]+)\t+(.+)/ ) {
665 push @report_labels, $label;
666 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
667 push @report_lines, [ $label, @$type ];
668 } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
669 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
671 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
672 push @report_labels, $label . $sub_label;
678 debug 'report_lines', \@report_lines;
679 debug 'sub_labels', $sub_labels;
680 debug 'report_labels', \@report_labels;
682 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
684 debug 'departments' => \@departments;
687 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
689 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
694 my $label = shift @_;
695 my $department = shift @_;
696 my $group = shift @_;
697 my @biblionumbers = @_;
699 $unique->{$_}++ foreach @biblionumbers;
700 my @bibs = keys %$unique;
701 $table->{ffzg}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar @bibs;
702 $table->{external}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar grep { $biblio_author_external->{$_} } @bibs;
705 foreach my $group ( '', keys %$azvo_group_title ) {
707 foreach my $department ( @departments ) {
708 foreach my $line ( @report_lines ) {
709 my $label = $line->[0];
711 foreach ( 1 .. $#$line ) {
712 my ( $category, $type ) = @{ $line->[ $_ ] };
713 my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type}->{$group};
714 push @biblionumbers, @$b if $b;
716 if ( $sub_labels->{$label} ) {
718 foreach my $biblionumber ( @biblionumbers ) {
719 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
720 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
721 my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
723 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
726 if ( $field < 100 ) {
727 if ( $data->{$field} =~ m/$regex/ ) {
728 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
732 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
733 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
739 foreach my $sub_label ( keys %$sub_stats ) {
740 my $full_label = $label . $sub_label;
741 table_count $full_label, $department, $group, @{ $sub_stats->{$sub_label} };
744 table_count $label, $department, $group, @biblionumbers;
751 debug 'table', $table;
753 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
754 open(my $fh2, '>:encoding(utf-8)', 'html/azvo2.new');
761 print $fh html_title('AZVO tablica - FFZG');
762 print $fh2 html_title('AZVO tablica - kolaboracija sa FFZG');
764 foreach my $group ( keys %{ $table->{ffzg} } ) {
766 print_fh "<h1>$group</h1>" if $group;
768 print_fh "<table border=1>\n";
769 print_fh "<tr><th></th>";
770 print_fh "<th>$_</th>" foreach @departments;
773 foreach my $row ( 0 .. $#{ $table->{ffzg}->{$group} } ) {
774 print_fh "<tr><th>", $report_labels[$row], "</th>\n";
775 foreach ( 0 .. $#departments ) {
777 print $fh $table->{ffzg}->{$group}->[ $row ]->[ $_ ] || '';
778 print $fh2 $table->{external}->{$group}->[ $row ]->[ $_ ] || '';
784 print_fh "</table>\n";
791 rename 'html/azvo.new', 'html/azvo.html';
792 rename 'html/azvo2.new', 'html/azvo2.html';