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 lib '/srv/koha_ffzg';
21 my $dbh = C4::Context->dbh;
24 my ($title, $data) = @_;
25 print "# $title ",dump($data), $/ if $ENV{DEBUG};
28 my $xslfilename = 'compact.xsl';
30 my $azvo_group_title = {
31 'znanstveno nastavni' => qr/(profes|docent|znanstveni savjetnik|znanstveni suradnik)/i,
32 'lektori i predavači' => qr/(lektor|predavač)/i,
33 'asistenti i novaci' => qr/(asistent|novak)/i,
36 my $department_groups = {
37 'AAB_humanističke' => qr/(anglistiku|arheologiju|antropologiju|filozofiju|fonetiku|germanistiku|hungarologiju|indologiju|slavenske|filologiju|komparativnu|kroatistiku|lingvistiku|povijest|romanistiku|talijanistiku)/i,
38 'AAC_društvene' => qr/(informacijske|pedagogiju|psihologiju|sociologiju)/i,
45 my $department_in_sum;
46 my $department_in_group;
50 my $sth_auth = $dbh->prepare(q{
53 ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
54 ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department,
55 ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="i"]') as academic_title
60 while( my $row = $sth_auth->fetchrow_hashref ) {
61 if ( $row->{department} !~ m/Filozofski fakultet u Zagrebu/ ) {
62 push @{ $skip->{nije_ffzg} }, $row;
65 $auth_header->{ $row->{authid} } = $row->{full_name};
66 $row->{department} =~ s/, Filozofski fakultet u Zagrebu.*$//;
67 $row->{department} =~ s/^.+\.\s*//;
68 $row->{department} =~ s/\s+$//s;
70 foreach my $title ( keys %$azvo_group_title ) {
71 if ( $row->{academic_title} =~ $azvo_group_title->{$title} ) {
77 $row->{academic_group} = $group;
78 $auth_group->{ $row->{authid} } = $group;
79 $skip->{group_stat}->{$group}++;
81 push @{ $skip->{no_academic_group} }, $row;
84 # warn "# ", dump( $row );
85 push @{ $auth_department->{ $row->{department} } }, $row->{authid};
87 $department_in_sum->{ $row->{department} }++;
88 foreach my $name ( keys %$department_groups ) {
89 my $regex = $department_groups->{$name};
90 if ( $row->{department} =~ $regex ) {
91 $department_in_group->{ $row->{department} } = $name;
97 debug 'department_in_group' => $department_in_group;
99 foreach my $department ( keys %$department_in_sum ) {
100 $department_in_sum->{$department} = 0 unless $department =~ m/(centar|croaticum|katedra|odsjek)/i;
103 debug 'auth_department' => $auth_department;
104 debug 'auth_group' => $auth_group;
105 debug 'department_in_sum' => $department_in_sum;
111 my $sth_select_authors = $dbh->prepare(q{
122 -- ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
123 -- ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
124 -- ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
126 -- and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
127 -- order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
133 my $parser = XML::LibXML->new();
134 $parser->recover_silently(0); # don't die when you find &, >, etc
135 my $style_doc = $parser->parse_file($xslfilename);
136 my $xslt = XML::LibXSLT->new();
137 my $parsed = $xslt->parse_stylesheet($style_doc);
142 my $biblio_author_external;
144 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
146 sub biblioitem_html {
147 my ($biblionumber, $parse_only) = @_;
149 return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber} && ! $parse_only;
151 my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
153 print $xml_fh $xmlrecord if $ENV{XML};
155 my $source = eval { $parser->parse_string($xmlrecord) };
157 # warn "SKIP $biblionumber corrupt XML";
158 push @{ $skip->{XML_corrupt} }, $biblionumber;
163 $biblio_parsed->{$biblionumber} = $source;
167 my $transformed = $parsed->transform($source);
168 $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
170 delete $biblio_parsed->{$biblionumber};
172 return $biblio_html->{$biblionumber};
175 $sth_select_authors->execute();
176 while( my $row = $sth_select_authors->fetchrow_hashref ) {
177 # warn dump($row),$/;
181 $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
183 my $doc = biblioitem_html( $row->{biblionumber}, 1 );
185 # warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
189 my $root = $doc->documentElement;
191 my @leaders = $root->getElementsByLocalName('leader');
193 my $leader = $leaders[0]->textContent;
194 warn "leader $leader\n";
208 foreach my $elt ($root->getChildrenByLocalName('*')) {
209 my $tag = $elt->getAttribute('tag');
211 next unless exists $extract->{ $tag };
213 if ($elt->localname eq 'controlfield') {
214 if ( $tag eq '008' ) {
215 my $content = $elt->textContent;
216 my $year = substr($content, 7, 4 );
217 if ( $year !~ m/^\d+$/ ) {
219 push @{ $skip->{invalid_year} }, $row->{biblionumber};
221 $biblio_year->{ $row->{biblionumber} } = $data->{year} = $year;
222 $data->{'008'} = $content;
225 } elsif ($elt->localname eq 'datafield') {
227 foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
228 my $sf = $sfelt->getAttribute('code');
229 next unless $sf =~ m/$extract->{$tag}/;
230 if ( exists $sf_data->{$sf} ) {
231 $sf_data->{$sf} .= " " . $sfelt->textContent();
233 $sf_data->{$sf} = $sfelt->textContent();
236 push @{ $data->{$tag} }, $sf_data if $sf_data;
240 if ( ! defined $data->{year} ) {
241 warn "MISSING year in ", $row->{biblionumber};
242 =for remove-year-limit
243 } elsif ( $data->{year} < 2008 ) {
244 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
246 } elsif ( $data->{year} > 2013 ) {
247 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
252 # warn "# ", $row->{biblionumber}, " data ",dump($data);
254 my $category = $data->{942}->[0]->{'t'};
256 # warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
257 push @{ $skip->{no_category} }, $row->{biblionumber};
263 if ( exists $data->{100} ) {
267 if ( ! exists $_->{9} ) {
268 $biblio_author_external->{ $row->{biblionumber} }++;
270 } elsif ( exists $auth_header->{ $_->{9} } ) {
277 foreach my $authid ( @first_author ) {
278 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
286 if ( exists $data->{700} ) {
289 if ( ! exists $_->{9} ) {
290 $biblio_author_external->{ $row->{biblionumber} }++;
292 } elsif ( exists $auth_header->{ $_->{9} } ) {
299 foreach my $auth ( @other_authors ) {
300 my $authid = $auth->{9} || next;
301 my $type = $auth->{4} || next; #die "no 4 in ",dump($data);
303 $type_stats->{$type}++;
305 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
306 push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
307 push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
308 } elsif ( $type =~ m/aut/ ) {
310 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
312 $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
314 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
317 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
320 # warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
321 $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
327 $biblio_data->{ $row->{biblionumber} } = $data;
331 debug 'authors' => $authors;
332 debug 'type_stats' => $type_stats;
333 debug 'skip' => $skip;
334 debug 'biblio_year' => $biblio_year;
335 debug 'biblio_data' => $biblio_data;
336 debug 'biblio_author_external' => $biblio_author_external;
339 my $sth_categories = $dbh->prepare(q{
340 select authorised_value, lib from authorised_values where category = 'BIBCAT'
342 $sth_categories->execute();
343 while( my $row = $sth_categories->fetchrow_hashref ) {
344 $category_label->{ $row->{authorised_value} } = $row->{lib};
347 debug 'category_label' => $category_label;
352 <meta charset="UTF-8">
353 <title>|, join(" ", @_), qq|</title>
354 <link href="style.css" type="text/css" rel="stylesheet" />
361 return qq|</body>\n</html>\n|;
364 mkdir 'html' unless -d 'html';
366 open(my $index, '>:encoding(utf-8)', 'html/index.new');
367 print $index html_title('Bibliografija Filozofskog fakulteta');
369 my $first_letter = '';
371 debug 'authors' => \@authors;
374 my ($biblionumber) = @_;
376 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
377 biblioitem_html($biblionumber),
378 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
383 my ( $fh, $authid, $type, $label ) = @_;
385 return unless exists $authors->{$authid}->{$type};
387 print $fh qq|<h2>$label</h2>\n|;
389 foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
390 my $label = $category_label->{$category} || 'Bez kategorije';
391 print $fh qq|<h3>$label</h3>\n<ul>\n|;
392 foreach my $biblionumber ( sort {
393 $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b
394 } @{ $authors->{$authid}->{$type}->{$category} } ) {
395 print $fh li_biblio( $biblionumber );
397 print $fh qq|</ul>\n|;
401 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
403 my $first = substr( $row->{full_name}, 0, 1 );
404 if ( $first ne $first_letter ) {
405 print $index qq{</ul>\n} if $first_letter;
406 $first_letter = $first;
407 print $index qq{<h1>$first</h1>\n<ul>\n};
409 print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
411 my $path = "html/$row->{authid}";
412 open(my $fh, '>:encoding(utf-8)', "$path.new");
413 print $fh html_title($row->{full_name}, "bibliografija");
414 print $fh qq|<h1>$row->{full_name} - bibliografija</h1>|;
416 author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
417 author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
421 rename "$path.new", "$path.html";
425 print $index html_end;
427 rename 'html/index.new', 'html/index.html';
429 debug 'auth_header' => $auth_header;
432 my $department_category_author;
433 foreach my $department ( sort keys %$auth_department ) {
434 foreach my $authid ( sort @{ $auth_department->{$department} } ) {
435 my @categories = keys %{ $authors->{$authid}->{aut} };
436 push @categories, keys %{ $authors->{$authid}->{sec} };
437 foreach my $category ( sort @categories ) {
438 push @{ $department_category_author->{$department}->{$category} }, $authid;
439 push @{ $department_category_author->{'AAA_ukupno'}->{$category} }, $authid if $department_in_sum->{$department};
440 if ( my $group = $department_in_group->{ $department } ) {
441 push @{ $department_category_author->{$group}->{$category} }, $authid;
443 $skip->{'department_not_in_group'}->{ $department }++;
449 debug 'department_category_author' => $department_category_author;
451 mkdir 'html/departments' unless -d 'html/departments';
453 sub unique_biblionumber {
456 $u->{$_}++ foreach @v;
457 return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
460 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
461 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
462 foreach my $department ( sort keys %$department_category_author ) {
463 my $dep = $department || 'Nema odsjeka';
464 my $dep_file = unac_string('utf-8',$dep);
465 print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
466 open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
468 print $fh html_title($department . ' bibliografija');
469 print $fh qq|<h1>$department bibliografija</h1>\n|;
471 print $fh qq|<h2>Primarno autorstvo</h2>\n|;
473 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
475 my @authids = @{ $department_category_author->{$department}->{$category} };
476 next unless @authids;
478 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
480 $unique->{$_}++ foreach @biblionumber;
482 next unless @biblionumber;
484 my $label = $category_label->{$category} || 'Bez kategorije';
485 print $fh qq|<h3>$label</h3>\n<ul>\n|;
487 print $fh li_biblio( $_ ) foreach @biblionumber;
493 print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
495 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
497 my @authids = @{ $department_category_author->{$department}->{$category} };
498 next unless @authids;
500 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
502 next unless @biblionumber;
504 my $label = $category_label->{$category} || 'Bez kategorije';
505 print $fh qq|<h3>$label</h3>\n<ul>\n|;
507 print $fh li_biblio( $_ ) foreach @biblionumber;
515 rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
517 print $dep_fh qq|</ul>\n|, html_end;
519 rename 'html/departments/index.new', 'html/departments/index.html';
521 my $azvo_stat_biblio;
523 foreach my $department ( sort keys %$department_category_author ) {
524 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
525 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
526 my $group = $auth_group->{$authid};
528 push @{ $skip->{no_auth_group} }, $authid;
531 foreach my $type ( keys %{ $authors->{$authid} } ) {
532 next unless exists $authors->{$authid}->{$type}->{$category};
533 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} }, @{ $authors->{$authid}->{$type}->{$category} };
534 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} }, @{ $authors->{$authid}->{$type}->{$category} };
537 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
538 foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
539 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
540 $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
546 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
553 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
556 if ( /^([^\t]+)\t+(.+)/ ) {
558 push @report_labels, $label;
559 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
560 push @report_lines, [ $label, @$type ];
561 } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
562 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
564 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
565 push @report_labels, $label . $sub_label;
571 debug 'report_lines', \@report_lines;
572 debug 'sub_labels', $sub_labels;
573 debug 'report_labels', \@report_labels;
575 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
577 debug 'departments' => \@departments;
580 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
582 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
587 my $label = shift @_;
588 my $department = shift @_;
589 my $group = shift @_;
590 my @biblionumbers = @_;
592 $unique->{$_}++ foreach @biblionumbers;
593 my @bibs = keys %$unique;
594 $table->{ffzg}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar @bibs;
595 $table->{external}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar grep { $biblio_author_external->{$_} } @bibs;
598 foreach my $group ( '', keys %$azvo_group_title ) {
600 foreach my $department ( @departments ) {
601 foreach my $line ( @report_lines ) {
602 my $label = $line->[0];
604 foreach ( 1 .. $#$line ) {
605 my ( $category, $type ) = @{ $line->[ $_ ] };
606 my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type}->{$group};
607 push @biblionumbers, @$b if $b;
609 if ( $sub_labels->{$label} ) {
611 foreach my $biblionumber ( @biblionumbers ) {
612 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
613 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
614 my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
616 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
619 if ( $field < 100 ) {
620 if ( $data->{$field} =~ m/$regex/ ) {
621 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
625 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
626 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
632 foreach my $sub_label ( keys %$sub_stats ) {
633 my $full_label = $label . $sub_label;
634 table_count $full_label, $department, $group, @{ $sub_stats->{$sub_label} };
637 table_count $label, $department, $group, @biblionumbers;
644 debug 'table', $table;
646 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
647 open(my $fh2, '>:encoding(utf-8)', 'html/azvo2.new');
654 print $fh html_title('AZVO tablica - FFZG');
655 print $fh2 html_title('AZVO tablica - kolaboracija sa FFZG');
657 foreach my $group ( keys %{ $table->{ffzg} } ) {
659 print_fh "<h1>$group</h1>" if $group;
661 print_fh "<table border=1>\n";
662 print_fh "<tr><th></th>";
663 print_fh "<th>$_</th>" foreach @departments;
666 foreach my $row ( 0 .. $#{ $table->{ffzg}->{$group} } ) {
667 print_fh "<tr><th>", $report_labels[$row], "</th>\n";
668 foreach ( 0 .. $#departments ) {
670 print $fh $table->{ffzg}->{$group}->[ $row ]->[ $_ ] || '';
671 print $fh2 $table->{external}->{$group}->[ $row ]->[ $_ ] || '';
677 print_fh "</table>\n";
684 rename 'html/azvo.new', 'html/azvo.html';
685 rename 'html/azvo2.new', 'html/azvo2.html';