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>|,
389 my ( $fh, $authid, $type, $label ) = @_;
391 return unless exists $authors->{$authid}->{$type};
393 print $fh qq|<h2>$label</h2>\n|;
395 foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
396 my $label = $category_label->{$category} || 'Bez kategorije';
397 print $fh qq|<a name="$type-$category"><h3>$label</h3></a>\n<ul>\n|;
398 foreach my $biblionumber ( sort {
399 $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b
400 } @{ $authors->{$authid}->{$type}->{$category} } ) {
401 print $fh li_biblio( $biblionumber );
403 print $fh qq|</ul>\n|;
407 sub count_author_years {
410 foreach my $type ( keys %{ $authors->{$authid} } ) {
411 foreach my $category ( keys %{ $authors->{$authid}->{$type} } ) {
412 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
413 $years->{ $biblio_year->{ $biblionumber } }->{ $type . '-' . $category }++;
420 sub html_year_selection {
424 print $fh qq|<span id="years">Godine:\n|;
425 my $type_cat_count = {};
428 foreach my $authid ( @authids ) {
429 $years = count_author_years( $years, $authid );
432 foreach my $year ( sort { $b <=> $a } keys %$years ) {
433 print $fh qq|<label><input name="year_selection" value="$year" type=checkbox onClick="toggle_year($year, this)" checked="checked">$year</label> \n|;
434 foreach my $type_cat ( keys %{ $years->{$year} } ) {
435 $type_cat_count->{ $type_cat } += $years->{$year}->{$type_cat};
440 <input type=button value="all" onClick="all_years(1)">
441 <input type=button value="none" onClick="all_years(0)">
444 print $fh qq|</span>|;
449 var years = |, encode_json($years), q|;
451 var type_cat_count = |, encode_json($type_cat_count), q|;
453 function year_show(year) {
455 console.debug('show', year);
456 for(var type_cat in years[year]) {
457 if ( ( type_cat_count[ type_cat ] += years[year][type_cat] ) == years[year][type_cat]) {
458 $('a[name="'+type_cat+'"]').show();
459 console.debug(type_cat, 'show');
464 function year_hide(year) {
466 console.debug('hide', year);
467 for(var type_cat in years[year]) {
468 if ( ( type_cat_count[ type_cat ] -= years[year][type_cat] ) == 0 ) {
469 $('a[name="'+type_cat+'"]').hide();
470 console.debug(type_cat, 'hide');
475 function toggle_year(year, el) {
483 function all_years( turn_on ) {
484 $('input[name=year_selection]').each( function(i,el) {
486 if ( ! el.checked ) {
488 year_show( el.value );
493 year_hide( el.value );
499 $(document).ready( function() {
500 console.info('ready');
502 $('input[name=year_selection]').each( function(i, el) {
504 console.debug( 'on load', year, el.checked );
505 if (! el.checked) year_hide(year);
513 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
515 my $first = substr( $row->{full_name}, 0, 1 );
516 if ( $first ne $first_letter ) {
517 print $index qq{</ul>\n} if $first_letter;
518 $first_letter = $first;
519 print $index qq{<h1>$first</h1>\n<ul>\n};
521 print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
523 my $path = "html/$row->{authid}";
524 open(my $fh, '>:encoding(utf-8)', "$path.new");
525 print $fh html_title($row->{full_name}, "bibliografija");
526 print $fh qq|<h1>$row->{full_name} - bibliografija</h1>\n|;
528 html_year_selection $fh => $row->{authid};
530 author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
531 author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
535 rename "$path.new", "$path.html";
539 print $index html_end;
541 rename 'html/index.new', 'html/index.html';
543 debug 'auth_header' => $auth_header;
546 my $department_category_author;
547 foreach my $department ( sort keys %$auth_department ) {
548 foreach my $authid ( sort @{ $auth_department->{$department} } ) {
549 my @categories = keys %{ $authors->{$authid}->{aut} };
550 push @categories, keys %{ $authors->{$authid}->{sec} };
551 foreach my $category ( sort @categories ) {
552 push @{ $department_category_author->{$department}->{$category} }, $authid;
553 push @{ $department_category_author->{'AAA_ukupno'}->{$category} }, $authid if $department_in_sum->{$department};
554 if ( my $group = $department_in_group->{ $department } ) {
555 push @{ $department_category_author->{$group}->{$category} }, $authid;
557 $skip->{'department_not_in_group'}->{ $department }++;
563 debug 'department_category_author' => $department_category_author;
565 mkdir 'html/departments' unless -d 'html/departments';
567 sub unique_biblionumber {
570 $u->{$_}++ foreach @v;
571 return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
574 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
575 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
576 foreach my $department ( sort keys %$department_category_author ) {
577 my $dep = $department || 'Nema odsjeka';
578 my $dep_file = unac_string('utf-8',$dep);
579 print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
580 open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
582 print $fh html_title($department . ' bibliografija');
583 print $fh qq|<h1>$department bibliografija</h1>\n|;
585 print $fh qq|<h2>Primarno autorstvo</h2>\n|;
587 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
589 my @authids = @{ $department_category_author->{$department}->{$category} };
590 next unless @authids;
592 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
594 $unique->{$_}++ foreach @biblionumber;
596 next unless @biblionumber;
598 my $label = $category_label->{$category} || 'Bez kategorije';
599 print $fh qq|<h3>$label</h3>\n<ul>\n|;
601 print $fh li_biblio( $_ ) foreach @biblionumber;
607 print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
609 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
611 my @authids = @{ $department_category_author->{$department}->{$category} };
612 next unless @authids;
614 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
616 next unless @biblionumber;
618 my $label = $category_label->{$category} || 'Bez kategorije';
619 print $fh qq|<h3>$label</h3>\n<ul>\n|;
621 print $fh li_biblio( $_ ) foreach @biblionumber;
629 rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
631 print $dep_fh qq|</ul>\n|, html_end;
633 rename 'html/departments/index.new', 'html/departments/index.html';
635 my $azvo_stat_biblio;
637 foreach my $department ( sort keys %$department_category_author ) {
638 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
639 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
640 my $group = $auth_group->{$authid};
642 push @{ $skip->{no_auth_group} }, $authid;
645 foreach my $type ( keys %{ $authors->{$authid} } ) {
646 next unless exists $authors->{$authid}->{$type}->{$category};
647 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} }, @{ $authors->{$authid}->{$type}->{$category} };
648 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} }, @{ $authors->{$authid}->{$type}->{$category} };
651 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
652 foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
653 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
654 $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
660 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
667 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
670 if ( /^([^\t]+)\t+(.+)/ ) {
672 push @report_labels, $label;
673 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
674 push @report_lines, [ $label, @$type ];
675 } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
676 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
678 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
679 push @report_labels, $label . $sub_label;
685 debug 'report_lines', \@report_lines;
686 debug 'sub_labels', $sub_labels;
687 debug 'report_labels', \@report_labels;
689 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
691 debug 'departments' => \@departments;
694 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
696 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
701 my $label = shift @_;
702 my $department = shift @_;
703 my $group = shift @_;
704 my @biblionumbers = @_;
706 $unique->{$_}++ foreach @biblionumbers;
707 my @bibs = keys %$unique;
708 $table->{ffzg}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar @bibs;
709 $table->{external}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar grep { $biblio_author_external->{$_} } @bibs;
712 foreach my $group ( '', keys %$azvo_group_title ) {
714 foreach my $department ( @departments ) {
715 foreach my $line ( @report_lines ) {
716 my $label = $line->[0];
718 foreach ( 1 .. $#$line ) {
719 my ( $category, $type ) = @{ $line->[ $_ ] };
720 my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type}->{$group};
721 push @biblionumbers, @$b if $b;
723 if ( $sub_labels->{$label} ) {
725 foreach my $biblionumber ( @biblionumbers ) {
726 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
727 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
728 my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
730 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
733 if ( $field < 100 ) {
734 if ( $data->{$field} =~ m/$regex/ ) {
735 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
739 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
740 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
746 foreach my $sub_label ( keys %$sub_stats ) {
747 my $full_label = $label . $sub_label;
748 table_count $full_label, $department, $group, @{ $sub_stats->{$sub_label} };
751 table_count $label, $department, $group, @biblionumbers;
758 debug 'table', $table;
760 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
761 open(my $fh2, '>:encoding(utf-8)', 'html/azvo2.new');
768 print $fh html_title('AZVO tablica - FFZG');
769 print $fh2 html_title('AZVO tablica - kolaboracija sa FFZG');
771 foreach my $group ( keys %{ $table->{ffzg} } ) {
773 print_fh "<h1>$group</h1>" if $group;
775 print_fh "<table border=1>\n";
776 print_fh "<tr><th></th>";
777 print_fh "<th>$_</th>" foreach @departments;
780 foreach my $row ( 0 .. $#{ $table->{ffzg}->{$group} } ) {
781 print_fh "<tr><th>", $report_labels[$row], "</th>\n";
782 foreach ( 0 .. $#departments ) {
784 print $fh $table->{ffzg}->{$group}->[ $row ]->[ $_ ] || '';
785 print $fh2 $table->{external}->{$group}->[ $row ]->[ $_ ] || '';
791 print_fh "</table>\n";
798 rename 'html/azvo.new', 'html/azvo.html';
799 rename 'html/azvo2.new', 'html/azvo2.html';