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,
40 my $department_in_sum;
44 my $sth_auth = $dbh->prepare(q{
47 ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
48 ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department,
49 ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="i"]') as academic_title
54 while( my $row = $sth_auth->fetchrow_hashref ) {
55 if ( $row->{department} !~ m/Filozofski fakultet u Zagrebu/ ) {
56 push @{ $skip->{nije_ffzg} }, $row;
59 $auth_header->{ $row->{authid} } = $row->{full_name};
60 $row->{department} =~ s/, Filozofski fakultet u Zagrebu.*$//;
61 $row->{department} =~ s/^.+\.\s*//;
62 $row->{department} =~ s/\s+$//s;
64 foreach my $title ( keys %$azvo_group_title ) {
65 if ( $row->{academic_title} =~ $azvo_group_title->{$title} ) {
71 $row->{academic_group} = $group;
72 $auth_group->{ $row->{authid} } = $group;
73 $skip->{group_stat}->{$group}++;
75 push @{ $skip->{no_academic_group} }, $row;
78 # warn "# ", dump( $row );
79 push @{ $auth_department->{ $row->{department} } }, $row->{authid};
81 $department_in_sum->{ $row->{department} }++;
84 foreach my $department ( keys %$department_in_sum ) {
85 $department_in_sum->{$department} = 0 unless $department =~ m/(centar|croaticum|katedra|odsjek)/i;
88 debug 'auth_department' => $auth_department;
89 debug 'auth_group' => $auth_group;
90 debug 'department_in_sum' => $department_in_sum;
96 my $sth_select_authors = $dbh->prepare(q{
107 -- ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
108 -- ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
109 -- ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
111 -- and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
112 -- order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
118 my $parser = XML::LibXML->new();
119 $parser->recover_silently(0); # don't die when you find &, >, etc
120 my $style_doc = $parser->parse_file($xslfilename);
121 my $xslt = XML::LibXSLT->new();
122 my $parsed = $xslt->parse_stylesheet($style_doc);
128 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
130 sub biblioitem_html {
131 my ($biblionumber, $parse_only) = @_;
133 return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber} && ! $parse_only;
135 my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
137 print $xml_fh $xmlrecord if $ENV{XML};
139 my $source = eval { $parser->parse_string($xmlrecord) };
141 # warn "SKIP $biblionumber corrupt XML";
142 push @{ $skip->{XML_corrupt} }, $biblionumber;
147 $biblio_parsed->{$biblionumber} = $source;
151 my $transformed = $parsed->transform($source);
152 $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
154 delete $biblio_parsed->{$biblionumber};
156 return $biblio_html->{$biblionumber};
159 $sth_select_authors->execute();
160 while( my $row = $sth_select_authors->fetchrow_hashref ) {
161 # warn dump($row),$/;
165 $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
167 my $doc = biblioitem_html( $row->{biblionumber}, 1 );
169 # warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
173 my $root = $doc->documentElement;
175 my @leaders = $root->getElementsByLocalName('leader');
177 my $leader = $leaders[0]->textContent;
178 warn "leader $leader\n";
192 foreach my $elt ($root->getChildrenByLocalName('*')) {
193 my $tag = $elt->getAttribute('tag');
195 next unless exists $extract->{ $tag };
197 if ($elt->localname eq 'controlfield') {
198 if ( $tag eq '008' ) {
199 my $content = $elt->textContent;
200 my $year = substr($content, 7, 4 );
201 if ( $year !~ m/^\d+$/ ) {
203 push @{ $skip->{invalid_year} }, $row->{biblionumber};
205 $biblio_year->{ $row->{biblionumber} } = $data->{year} = $year;
206 $data->{'008'} = $content;
209 } elsif ($elt->localname eq 'datafield') {
211 foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
212 my $sf = $sfelt->getAttribute('code');
213 next unless $sf =~ m/$extract->{$tag}/;
214 if ( exists $sf_data->{$sf} ) {
215 $sf_data->{$sf} .= " " . $sfelt->textContent();
217 $sf_data->{$sf} = $sfelt->textContent();
220 push @{ $data->{$tag} }, $sf_data if $sf_data;
224 if ( ! defined $data->{year} ) {
225 warn "MISSING year in ", $row->{biblionumber};
226 } elsif ( $data->{year} < 2008 ) {
227 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
229 } elsif ( $data->{year} > 2013 ) {
230 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
234 # warn "# ", $row->{biblionumber}, " data ",dump($data);
236 my $category = $data->{942}->[0]->{'t'};
238 # warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
239 push @{ $skip->{no_category} }, $row->{biblionumber};
245 if ( exists $data->{100} ) {
246 my @first_author = map { $_->{'9'} } @{ $data->{100} };
247 foreach my $authid ( @first_author ) {
248 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
256 if ( exists $data->{700} ) {
257 foreach my $auth ( @{ $data->{700} } ) {
258 my $authid = $auth->{9} || next;
259 my $type = $auth->{4} || next; #die "no 4 in ",dump($data);
261 $type_stats->{$type}++;
263 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
264 push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
265 push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
266 } elsif ( $type =~ m/aut/ ) {
268 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
270 $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
272 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
275 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
278 # warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
279 $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
285 $biblio_data->{ $row->{biblionumber} } = $data;
289 debug 'authors' => $authors;
290 debug 'type_stats' => $type_stats;
291 debug 'skip' => $skip;
292 debug 'biblio_year' => $biblio_year;
293 debug 'biblio_data' => $biblio_data;
296 my $sth_categories = $dbh->prepare(q{
297 select authorised_value, lib from authorised_values where category = 'BIBCAT'
299 $sth_categories->execute();
300 while( my $row = $sth_categories->fetchrow_hashref ) {
301 $category_label->{ $row->{authorised_value} } = $row->{lib};
304 debug 'category_label' => $category_label;
309 <meta charset="UTF-8">
310 <title>|, join(" ", @_), qq|</title>
311 <link href="style.css" type="text/css" rel="stylesheet" />
318 return qq|</body>\n</html>\n|;
321 mkdir 'html' unless -d 'html';
323 open(my $index, '>:encoding(utf-8)', 'html/index.new');
324 print $index html_title('Bibliografija Filozofskog fakulteta');
326 my $first_letter = '';
328 debug 'authors' => \@authors;
331 my ($biblionumber) = @_;
333 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
334 biblioitem_html($biblionumber),
335 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
340 my ( $fh, $authid, $type, $label ) = @_;
342 return unless exists $authors->{$authid}->{$type};
344 print $fh qq|<h2>$label</h2>\n|;
346 foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
347 my $label = $category_label->{$category} || 'Bez kategorije';
348 print $fh qq|<h3>$label</h3>\n<ul>\n|;
349 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
350 print $fh li_biblio( $biblionumber );
352 print $fh qq|</ul>\n|;
356 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
358 my $first = substr( $row->{full_name}, 0, 1 );
359 if ( $first ne $first_letter ) {
360 print $index qq{</ul>\n} if $first_letter;
361 $first_letter = $first;
362 print $index qq{<h1>$first</h1>\n<ul>\n};
364 print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
366 my $path = "html/$row->{authid}";
367 open(my $fh, '>:encoding(utf-8)', "$path.new");
368 print $fh html_title($row->{full_name}, "bibliografija");
369 print $fh qq|<h1>$row->{full_name} - bibliografija za razdoblje 2008-2013</h1>|;
371 author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
372 author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
376 rename "$path.new", "$path.html";
380 print $index html_end;
382 rename 'html/index.new', 'html/index.html';
384 debug 'auth_header' => $auth_header;
387 my $department_category_author;
388 foreach my $department ( sort keys %$auth_department ) {
389 foreach my $authid ( sort @{ $auth_department->{$department} } ) {
390 my @categories = keys %{ $authors->{$authid}->{aut} };
391 push @categories, keys %{ $authors->{$authid}->{sec} };
392 foreach my $category ( sort @categories ) {
393 push @{ $department_category_author->{$department}->{$category} }, $authid;
394 push @{ $department_category_author->{''}->{$category} }, $authid if $department_in_sum->{$department};
399 debug 'department_category_author' => $department_category_author;
401 mkdir 'html/departments' unless -d 'html/departments';
403 sub unique_biblionumber {
406 $u->{$_}++ foreach @v;
407 return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
410 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
411 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
412 foreach my $department ( sort keys %$department_category_author ) {
413 my $dep = $department || 'Nema odsjeka';
414 my $dep_file = unac_string('utf-8',$dep);
415 print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
416 open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
418 print $fh html_title($department . ' bibliografija');
419 print $fh qq|<h1>$department bibliografija</h1>\n|;
421 print $fh qq|<h2>Primarno autorstvo</h2>\n|;
423 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
425 my @authids = @{ $department_category_author->{$department}->{$category} };
426 next unless @authids;
428 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
430 $unique->{$_}++ foreach @biblionumber;
432 next unless @biblionumber;
434 my $label = $category_label->{$category} || 'Bez kategorije';
435 print $fh qq|<h3>$label</h3>\n<ul>\n|;
437 print $fh li_biblio( $_ ) foreach @biblionumber;
443 print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
445 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
447 my @authids = @{ $department_category_author->{$department}->{$category} };
448 next unless @authids;
450 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
452 next unless @biblionumber;
454 my $label = $category_label->{$category} || 'Bez kategorije';
455 print $fh qq|<h3>$label</h3>\n<ul>\n|;
457 print $fh li_biblio( $_ ) foreach @biblionumber;
465 rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
467 print $dep_fh qq|</ul>\n|, html_end;
469 rename 'html/departments/index.new', 'html/departments/index.html';
471 my $azvo_stat_biblio;
473 foreach my $department ( sort keys %$department_category_author ) {
474 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
475 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
476 my $group = $auth_group->{$authid};
478 push @{ $skip->{no_auth_group} }, $authid;
481 foreach my $type ( keys %{ $authors->{$authid} } ) {
482 next unless exists $authors->{$authid}->{$type}->{$category};
483 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} }, @{ $authors->{$authid}->{$type}->{$category} };
484 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} }, @{ $authors->{$authid}->{$type}->{$category} };
487 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
488 foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
489 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
490 $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
496 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
503 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
506 if ( /^([^\t]+)\t+(.+)/ ) {
508 push @report_labels, $label;
509 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
510 push @report_lines, [ $label, @$type ];
511 } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
512 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
514 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
515 push @report_labels, $label . $sub_label;
521 debug 'report_lines', \@report_lines;
522 debug 'sub_labels', $sub_labels;
523 debug 'report_labels', \@report_labels;
525 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
527 debug 'departments' => \@departments;
530 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
532 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
537 my $label = shift @_;
538 my $department = shift @_;
539 my $group = shift @_;
540 my @biblionumbers = @_;
542 $unique->{$_}++ foreach @biblionumbers;
543 $table->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar keys %$unique;
546 foreach my $group ( '', keys %$azvo_group_title ) {
548 foreach my $department ( @departments ) {
549 foreach my $line ( @report_lines ) {
550 my $label = $line->[0];
552 foreach ( 1 .. $#$line ) {
553 my ( $category, $type ) = @{ $line->[ $_ ] };
554 my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type}->{$group};
555 push @biblionumbers, @$b if $b;
557 if ( $sub_labels->{$label} ) {
559 foreach my $biblionumber ( @biblionumbers ) {
560 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
561 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
562 my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
564 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
567 if ( $field < 100 ) {
568 if ( $data->{$field} =~ m/$regex/ ) {
569 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
573 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
574 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
580 foreach my $sub_label ( keys %$sub_stats ) {
581 my $full_label = $label . $sub_label;
582 table_count $full_label, $department, $group, @{ $sub_stats->{$sub_label} };
585 table_count $label, $department, $group, @biblionumbers;
592 debug 'table', $table;
594 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
596 print $fh html_title('AZVO tablica');
598 foreach my $group ( keys %$table ) {
600 print $fh "<h1>$group</h1>" if $group;
602 print $fh "<table border=1>\n";
603 print $fh "<tr><th></th>";
604 print $fh "<th>$_</th>" foreach @departments;
607 foreach my $row ( 0 .. $#{ $table->{$group} } ) {
608 print $fh "<tr><th>", $report_labels[$row], "</th>\n";
609 print $fh " <td>", $table->{$group}->[ $row ]->[ $_ ] || '', "</td>\n" foreach 0 .. $#departments;
613 print $fh "</table>\n";
619 rename 'html/azvo.new', 'html/azvo.html';