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/(profesor|docent|znanstveni)/i,
32 'lektori i predavači' => qr/(lektor|predavač)/i,
33 'asistenti i novaci' => qr/(asistent|novak)/i,
36 my $department_groups = {
37 'AAA_humanističke' => qr/(anglistiku|arheologiju|antropologiju|filozofiju|fonetiku|germanistiku|hungarologiju|indologiju|slavenske|filologiju|komparativnu|kroatistiku|lingvistiku|povijest|romanistiku|talijanistiku)/i,
38 'AAB_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 } elsif ( $data->{year} < 2008 ) {
243 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
245 } elsif ( $data->{year} > 2013 ) {
246 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
250 # warn "# ", $row->{biblionumber}, " data ",dump($data);
252 my $category = $data->{942}->[0]->{'t'};
254 # warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
255 push @{ $skip->{no_category} }, $row->{biblionumber};
261 if ( exists $data->{100} ) {
265 if ( ! exists $_->{9} ) {
266 $biblio_author_external->{ $row->{biblionumber} }++;
268 } elsif ( exists $auth_header->{ $_->{9} } ) {
275 foreach my $authid ( @first_author ) {
276 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
284 if ( exists $data->{700} ) {
287 if ( ! exists $_->{9} ) {
288 $biblio_author_external->{ $row->{biblionumber} }++;
290 } elsif ( exists $auth_header->{ $_->{9} } ) {
297 foreach my $auth ( @other_authors ) {
298 my $authid = $auth->{9} || next;
299 my $type = $auth->{4} || next; #die "no 4 in ",dump($data);
301 $type_stats->{$type}++;
303 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
304 push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
305 push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
306 } elsif ( $type =~ m/aut/ ) {
308 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
310 $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
312 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
315 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
318 # warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
319 $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
325 $biblio_data->{ $row->{biblionumber} } = $data;
329 debug 'authors' => $authors;
330 debug 'type_stats' => $type_stats;
331 debug 'skip' => $skip;
332 debug 'biblio_year' => $biblio_year;
333 debug 'biblio_data' => $biblio_data;
334 debug 'biblio_author_external' => $biblio_author_external;
337 my $sth_categories = $dbh->prepare(q{
338 select authorised_value, lib from authorised_values where category = 'BIBCAT'
340 $sth_categories->execute();
341 while( my $row = $sth_categories->fetchrow_hashref ) {
342 $category_label->{ $row->{authorised_value} } = $row->{lib};
345 debug 'category_label' => $category_label;
350 <meta charset="UTF-8">
351 <title>|, join(" ", @_), qq|</title>
352 <link href="style.css" type="text/css" rel="stylesheet" />
359 return qq|</body>\n</html>\n|;
362 mkdir 'html' unless -d 'html';
364 open(my $index, '>:encoding(utf-8)', 'html/index.new');
365 print $index html_title('Bibliografija Filozofskog fakulteta');
367 my $first_letter = '';
369 debug 'authors' => \@authors;
372 my ($biblionumber) = @_;
374 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
375 biblioitem_html($biblionumber),
376 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
381 my ( $fh, $authid, $type, $label ) = @_;
383 return unless exists $authors->{$authid}->{$type};
385 print $fh qq|<h2>$label</h2>\n|;
387 foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
388 my $label = $category_label->{$category} || 'Bez kategorije';
389 print $fh qq|<h3>$label</h3>\n<ul>\n|;
390 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
391 print $fh li_biblio( $biblionumber );
393 print $fh qq|</ul>\n|;
397 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
399 my $first = substr( $row->{full_name}, 0, 1 );
400 if ( $first ne $first_letter ) {
401 print $index qq{</ul>\n} if $first_letter;
402 $first_letter = $first;
403 print $index qq{<h1>$first</h1>\n<ul>\n};
405 print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
407 my $path = "html/$row->{authid}";
408 open(my $fh, '>:encoding(utf-8)', "$path.new");
409 print $fh html_title($row->{full_name}, "bibliografija");
410 print $fh qq|<h1>$row->{full_name} - bibliografija za razdoblje 2008-2013</h1>|;
412 author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
413 author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
417 rename "$path.new", "$path.html";
421 print $index html_end;
423 rename 'html/index.new', 'html/index.html';
425 debug 'auth_header' => $auth_header;
428 my $department_category_author;
429 foreach my $department ( sort keys %$auth_department ) {
430 foreach my $authid ( sort @{ $auth_department->{$department} } ) {
431 my @categories = keys %{ $authors->{$authid}->{aut} };
432 push @categories, keys %{ $authors->{$authid}->{sec} };
433 foreach my $category ( sort @categories ) {
434 push @{ $department_category_author->{$department}->{$category} }, $authid;
435 push @{ $department_category_author->{'AAZ_ukupno'}->{$category} }, $authid if $department_in_sum->{$department};
436 if ( my $group = $department_in_group->{ $department } ) {
437 push @{ $department_category_author->{$group}->{$category} }, $authid;
439 $skip->{'department_not_in_group'}->{ $department }++;
445 debug 'department_category_author' => $department_category_author;
447 mkdir 'html/departments' unless -d 'html/departments';
449 sub unique_biblionumber {
452 $u->{$_}++ foreach @v;
453 return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
456 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
457 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
458 foreach my $department ( sort keys %$department_category_author ) {
459 my $dep = $department || 'Nema odsjeka';
460 my $dep_file = unac_string('utf-8',$dep);
461 print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
462 open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
464 print $fh html_title($department . ' bibliografija');
465 print $fh qq|<h1>$department bibliografija</h1>\n|;
467 print $fh qq|<h2>Primarno autorstvo</h2>\n|;
469 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
471 my @authids = @{ $department_category_author->{$department}->{$category} };
472 next unless @authids;
474 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
476 $unique->{$_}++ foreach @biblionumber;
478 next unless @biblionumber;
480 my $label = $category_label->{$category} || 'Bez kategorije';
481 print $fh qq|<h3>$label</h3>\n<ul>\n|;
483 print $fh li_biblio( $_ ) foreach @biblionumber;
489 print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
491 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
493 my @authids = @{ $department_category_author->{$department}->{$category} };
494 next unless @authids;
496 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
498 next unless @biblionumber;
500 my $label = $category_label->{$category} || 'Bez kategorije';
501 print $fh qq|<h3>$label</h3>\n<ul>\n|;
503 print $fh li_biblio( $_ ) foreach @biblionumber;
511 rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
513 print $dep_fh qq|</ul>\n|, html_end;
515 rename 'html/departments/index.new', 'html/departments/index.html';
517 my $azvo_stat_biblio;
519 foreach my $department ( sort keys %$department_category_author ) {
520 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
521 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
522 my $group = $auth_group->{$authid};
524 push @{ $skip->{no_auth_group} }, $authid;
527 foreach my $type ( keys %{ $authors->{$authid} } ) {
528 next unless exists $authors->{$authid}->{$type}->{$category};
529 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} }, @{ $authors->{$authid}->{$type}->{$category} };
530 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} }, @{ $authors->{$authid}->{$type}->{$category} };
533 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
534 foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
535 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
536 $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
542 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
549 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
552 if ( /^([^\t]+)\t+(.+)/ ) {
554 push @report_labels, $label;
555 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
556 push @report_lines, [ $label, @$type ];
557 } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
558 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
560 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
561 push @report_labels, $label . $sub_label;
567 debug 'report_lines', \@report_lines;
568 debug 'sub_labels', $sub_labels;
569 debug 'report_labels', \@report_labels;
571 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
573 debug 'departments' => \@departments;
576 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
578 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
583 my $label = shift @_;
584 my $department = shift @_;
585 my $group = shift @_;
586 my @biblionumbers = @_;
588 $unique->{$_}++ foreach @biblionumbers;
589 my @bibs = keys %$unique;
590 $table->{ffzg}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar @bibs;
591 $table->{external}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar grep { $biblio_author_external->{$_} } @bibs;
594 foreach my $group ( '', keys %$azvo_group_title ) {
596 foreach my $department ( @departments ) {
597 foreach my $line ( @report_lines ) {
598 my $label = $line->[0];
600 foreach ( 1 .. $#$line ) {
601 my ( $category, $type ) = @{ $line->[ $_ ] };
602 my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type}->{$group};
603 push @biblionumbers, @$b if $b;
605 if ( $sub_labels->{$label} ) {
607 foreach my $biblionumber ( @biblionumbers ) {
608 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
609 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
610 my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
612 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
615 if ( $field < 100 ) {
616 if ( $data->{$field} =~ m/$regex/ ) {
617 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
621 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
622 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
628 foreach my $sub_label ( keys %$sub_stats ) {
629 my $full_label = $label . $sub_label;
630 table_count $full_label, $department, $group, @{ $sub_stats->{$sub_label} };
633 table_count $label, $department, $group, @biblionumbers;
640 debug 'table', $table;
642 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
643 open(my $fh2, '>:encoding(utf-8)', 'html/azvo2.new');
650 print $fh html_title('AZVO tablica - FFZG');
651 print $fh2 html_title('AZVO tablica - kolaboracija sa FFZG');
653 foreach my $group ( keys %{ $table->{ffzg} } ) {
655 print_fh "<h1>$group</h1>" if $group;
657 print_fh "<table border=1>\n";
658 print_fh "<tr><th></th>";
659 print_fh "<th>$_</th>" foreach @departments;
662 foreach my $row ( 0 .. $#{ $table->{ffzg}->{$group} } ) {
663 print_fh "<tr><th>", $report_labels[$row], "</th>\n";
664 foreach ( 0 .. $#departments ) {
666 print $fh $table->{ffzg}->{$group}->[ $row ]->[ $_ ] || '';
667 print $fh2 $table->{external}->{$group}->[ $row ]->[ $_ ] || '';
673 print_fh "</table>\n";
680 rename 'html/azvo.new', 'html/azvo.html';
681 rename 'html/azvo2.new', 'html/azvo2.html';