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 'ZZA_humanističke' => qr/(anglistiku|arheologiju|antropologiju|filozofiju|fonetiku|germanistiku|hungarologiju|indologiju|slavenske|filologiju|komparativnu|kroatistiku|lingvistiku|povijest|romanistiku|talijanistiku)/i,
38 'ZZB_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);
143 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
145 sub biblioitem_html {
146 my ($biblionumber, $parse_only) = @_;
148 return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber} && ! $parse_only;
150 my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
152 print $xml_fh $xmlrecord if $ENV{XML};
154 my $source = eval { $parser->parse_string($xmlrecord) };
156 # warn "SKIP $biblionumber corrupt XML";
157 push @{ $skip->{XML_corrupt} }, $biblionumber;
162 $biblio_parsed->{$biblionumber} = $source;
166 my $transformed = $parsed->transform($source);
167 $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
169 delete $biblio_parsed->{$biblionumber};
171 return $biblio_html->{$biblionumber};
174 $sth_select_authors->execute();
175 while( my $row = $sth_select_authors->fetchrow_hashref ) {
176 # warn dump($row),$/;
180 $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
182 my $doc = biblioitem_html( $row->{biblionumber}, 1 );
184 # warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
188 my $root = $doc->documentElement;
190 my @leaders = $root->getElementsByLocalName('leader');
192 my $leader = $leaders[0]->textContent;
193 warn "leader $leader\n";
207 foreach my $elt ($root->getChildrenByLocalName('*')) {
208 my $tag = $elt->getAttribute('tag');
210 next unless exists $extract->{ $tag };
212 if ($elt->localname eq 'controlfield') {
213 if ( $tag eq '008' ) {
214 my $content = $elt->textContent;
215 my $year = substr($content, 7, 4 );
216 if ( $year !~ m/^\d+$/ ) {
218 push @{ $skip->{invalid_year} }, $row->{biblionumber};
220 $biblio_year->{ $row->{biblionumber} } = $data->{year} = $year;
221 $data->{'008'} = $content;
224 } elsif ($elt->localname eq 'datafield') {
226 foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
227 my $sf = $sfelt->getAttribute('code');
228 next unless $sf =~ m/$extract->{$tag}/;
229 if ( exists $sf_data->{$sf} ) {
230 $sf_data->{$sf} .= " " . $sfelt->textContent();
232 $sf_data->{$sf} = $sfelt->textContent();
235 push @{ $data->{$tag} }, $sf_data if $sf_data;
239 if ( $data->{year} < 2008 ) {
240 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
242 } elsif ( $data->{year} > 2013 ) {
243 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
247 # warn "# ", $row->{biblionumber}, " data ",dump($data);
249 my $category = $data->{942}->[0]->{'t'};
251 # warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
252 push @{ $skip->{no_category} }, $row->{biblionumber};
258 if ( exists $data->{100} ) {
259 my @first_author = map { $_->{'9'} } @{ $data->{100} };
260 foreach my $authid ( @first_author ) {
261 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
269 if ( exists $data->{700} ) {
270 foreach my $auth ( @{ $data->{700} } ) {
271 my $authid = $auth->{9} || next;
272 my $type = $auth->{4} || next; #die "no 4 in ",dump($data);
274 $type_stats->{$type}++;
276 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
277 push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
278 push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
279 } elsif ( $type =~ m/aut/ ) {
281 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
283 $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
285 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
288 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
291 # warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
292 $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
298 $biblio_data->{ $row->{biblionumber} } = $data;
302 debug 'authors' => $authors;
303 debug 'type_stats' => $type_stats;
304 debug 'skip' => $skip;
305 debug 'biblio_year' => $biblio_year;
306 debug 'biblio_data' => $biblio_data;
309 my $sth_categories = $dbh->prepare(q{
310 select authorised_value, lib from authorised_values where category = 'BIBCAT'
312 $sth_categories->execute();
313 while( my $row = $sth_categories->fetchrow_hashref ) {
314 $category_label->{ $row->{authorised_value} } = $row->{lib};
317 debug 'category_label' => $category_label;
322 <meta charset="UTF-8">
323 <title>|, join(" ", @_), qq|</title>
324 <link href="style.css" type="text/css" rel="stylesheet" />
331 return qq|</body>\n</html>\n|;
334 mkdir 'html' unless -d 'html';
336 open(my $index, '>:encoding(utf-8)', 'html/index.new');
337 print $index html_title('Bibliografija Filozofskog fakulteta');
339 my $first_letter = '';
341 debug 'authors' => \@authors;
344 my ($biblionumber) = @_;
346 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
347 biblioitem_html($biblionumber),
348 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
353 my ( $fh, $authid, $type, $label ) = @_;
355 return unless exists $authors->{$authid}->{$type};
357 print $fh qq|<h2>$label</h2>\n|;
359 foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
360 my $label = $category_label->{$category} || 'Bez kategorije';
361 print $fh qq|<h3>$label</h3>\n<ul>\n|;
362 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
363 print $fh li_biblio( $biblionumber );
365 print $fh qq|</ul>\n|;
369 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
371 my $first = substr( $row->{full_name}, 0, 1 );
372 if ( $first ne $first_letter ) {
373 print $index qq{</ul>\n} if $first_letter;
374 $first_letter = $first;
375 print $index qq{<h1>$first</h1>\n<ul>\n};
377 print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
379 my $path = "html/$row->{authid}";
380 open(my $fh, '>:encoding(utf-8)', "$path.new");
381 print $fh html_title($row->{full_name}, "bibliografija");
382 print $fh qq|<h1>$row->{full_name} - bibliografija za razdoblje 2008-2013</h1>|;
384 author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
385 author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
389 rename "$path.new", "$path.html";
393 print $index html_end;
395 rename 'html/index.new', 'html/index.html';
397 debug 'auth_header' => $auth_header;
400 my $department_category_author;
401 foreach my $department ( sort keys %$auth_department ) {
402 foreach my $authid ( sort @{ $auth_department->{$department} } ) {
403 my @categories = keys %{ $authors->{$authid}->{aut} };
404 push @categories, keys %{ $authors->{$authid}->{sec} };
405 foreach my $category ( sort @categories ) {
406 push @{ $department_category_author->{$department}->{$category} }, $authid;
407 push @{ $department_category_author->{'ZZZ_ukupno'}->{$category} }, $authid if $department_in_sum->{$department};
408 if ( my $group = $department_in_group->{ $department } ) {
409 push @{ $department_category_author->{$group}->{$category} }, $authid;
411 $skip->{'department_not_in_group'}->{ $department }++;
417 debug 'department_category_author' => $department_category_author;
419 mkdir 'html/departments' unless -d 'html/departments';
421 sub unique_biblionumber {
424 $u->{$_}++ foreach @v;
425 return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
428 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
429 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
430 foreach my $department ( sort keys %$department_category_author ) {
431 my $dep = $department || 'Nema odsjeka';
432 my $dep_file = unac_string('utf-8',$dep);
433 print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
434 open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
436 print $fh html_title($department . ' bibliografija');
437 print $fh qq|<h1>$department bibliografija</h1>\n|;
439 print $fh qq|<h2>Primarno autorstvo</h2>\n|;
441 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
443 my @authids = @{ $department_category_author->{$department}->{$category} };
444 next unless @authids;
446 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
448 $unique->{$_}++ foreach @biblionumber;
450 next unless @biblionumber;
452 my $label = $category_label->{$category} || 'Bez kategorije';
453 print $fh qq|<h3>$label</h3>\n<ul>\n|;
455 print $fh li_biblio( $_ ) foreach @biblionumber;
461 print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
463 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
465 my @authids = @{ $department_category_author->{$department}->{$category} };
466 next unless @authids;
468 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
470 next unless @biblionumber;
472 my $label = $category_label->{$category} || 'Bez kategorije';
473 print $fh qq|<h3>$label</h3>\n<ul>\n|;
475 print $fh li_biblio( $_ ) foreach @biblionumber;
483 rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
485 print $dep_fh qq|</ul>\n|, html_end;
487 rename 'html/departments/index.new', 'html/departments/index.html';
489 my $azvo_stat_biblio;
491 foreach my $department ( sort keys %$department_category_author ) {
492 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
493 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
494 my $group = $auth_group->{$authid};
496 push @{ $skip->{no_auth_group} }, $authid;
499 foreach my $type ( keys %{ $authors->{$authid} } ) {
500 next unless exists $authors->{$authid}->{$type}->{$category};
501 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} }, @{ $authors->{$authid}->{$type}->{$category} };
502 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} }, @{ $authors->{$authid}->{$type}->{$category} };
505 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
506 foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
507 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
508 $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
514 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
521 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
524 if ( /^([^\t]+)\t+(.+)/ ) {
526 push @report_labels, $label;
527 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
528 push @report_lines, [ $label, @$type ];
529 } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
530 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
532 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
533 push @report_labels, $label . $sub_label;
539 debug 'report_lines', \@report_lines;
540 debug 'sub_labels', $sub_labels;
541 debug 'report_labels', \@report_labels;
543 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
545 debug 'departments' => \@departments;
548 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
550 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
555 my $label = shift @_;
556 my $department = shift @_;
557 my $group = shift @_;
558 my @biblionumbers = @_;
560 $unique->{$_}++ foreach @biblionumbers;
561 $table->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar keys %$unique;
564 foreach my $group ( '', keys %$azvo_group_title ) {
566 foreach my $department ( @departments ) {
567 foreach my $line ( @report_lines ) {
568 my $label = $line->[0];
570 foreach ( 1 .. $#$line ) {
571 my ( $category, $type ) = @{ $line->[ $_ ] };
572 my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type}->{$group};
573 push @biblionumbers, @$b if $b;
575 if ( $sub_labels->{$label} ) {
577 foreach my $biblionumber ( @biblionumbers ) {
578 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
579 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
580 my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
582 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
585 if ( $field < 100 ) {
586 if ( $data->{$field} =~ m/$regex/ ) {
587 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
591 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
592 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
598 foreach my $sub_label ( keys %$sub_stats ) {
599 my $full_label = $label . $sub_label;
600 table_count $full_label, $department, $group, @{ $sub_stats->{$sub_label} };
603 table_count $label, $department, $group, @biblionumbers;
610 debug 'table', $table;
612 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
614 print $fh html_title('AZVO tablica');
616 foreach my $group ( keys %$table ) {
618 print $fh "<h1>$group</h1>" if $group;
620 print $fh "<table border=1>\n";
621 print $fh "<tr><th></th>";
622 print $fh "<th>$_</th>" foreach @departments;
625 foreach my $row ( 0 .. $#{ $table->{$group} } ) {
626 print $fh "<tr><th>", $report_labels[$row], "</th>\n";
627 print $fh " <td>", $table->{$group}->[ $row ]->[ $_ ] || '', "</td>\n" foreach 0 .. $#departments;
631 print $fh "</table>\n";
637 rename 'html/azvo.new', 'html/azvo.html';