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;
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 ( $data->{year} < 2008 ) {
225 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
227 } elsif ( $data->{year} > 2013 ) {
228 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
232 # warn "# ", $row->{biblionumber}, " data ",dump($data);
234 my $category = $data->{942}->[0]->{'t'};
236 # warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
237 push @{ $skip->{no_category} }, $row->{biblionumber};
243 if ( exists $data->{100} ) {
244 my @first_author = map { $_->{'9'} } @{ $data->{100} };
245 foreach my $authid ( @first_author ) {
246 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
254 if ( exists $data->{700} ) {
255 foreach my $auth ( @{ $data->{700} } ) {
256 my $authid = $auth->{9} || next;
257 my $type = $auth->{4} || next; #die "no 4 in ",dump($data);
259 $type_stats->{$type}++;
261 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
262 push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
263 push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
264 } elsif ( $type =~ m/aut/ ) {
266 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
268 $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
270 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
273 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
276 # warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
277 $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
283 $biblio_data->{ $row->{biblionumber} } = $data;
287 debug 'authors' => $authors;
288 debug 'type_stats' => $type_stats;
289 debug 'skip' => $skip;
290 debug 'biblio_year' => $biblio_year;
291 debug 'biblio_data' => $biblio_data;
294 my $sth_categories = $dbh->prepare(q{
295 select authorised_value, lib from authorised_values where category = 'BIBCAT'
297 $sth_categories->execute();
298 while( my $row = $sth_categories->fetchrow_hashref ) {
299 $category_label->{ $row->{authorised_value} } = $row->{lib};
302 debug 'category_label' => $category_label;
307 <meta charset="UTF-8">
308 <title>|, join(" ", @_), qq|</title>
309 <link href="style.css" type="text/css" rel="stylesheet" />
316 return qq|</body>\n</html\n|;
319 mkdir 'html' unless -d 'html';
321 open(my $index, '>:encoding(utf-8)', 'html/index.new');
322 print $index html_title('Bibliografija Filozofskog fakulteta');
324 my $first_letter = '';
326 debug 'authors' => \@authors;
329 my ($biblionumber) = @_;
331 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
332 biblioitem_html($biblionumber),
333 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
338 my ( $fh, $authid, $type, $label ) = @_;
340 return unless exists $authors->{$authid}->{$type};
342 print $fh qq|<h2>$label</h2>\n|;
344 foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
345 my $label = $category_label->{$category} || 'Bez kategorije';
346 print $fh qq|<h3>$label</h3>\n<ul>\n|;
347 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
348 print $fh li_biblio( $biblionumber );
350 print $fh qq|</ul>\n|;
354 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
356 my $first = substr( $row->{full_name}, 0, 1 );
357 if ( $first ne $first_letter ) {
358 print $index qq{</ul>\n} if $first_letter;
359 $first_letter = $first;
360 print $index qq{<h1>$first</h1>\n<ul>\n};
362 print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
364 my $path = "html/$row->{authid}";
365 open(my $fh, '>:encoding(utf-8)', "$path.new");
366 print $fh html_title($row->{full_name}, "bibliografija");
367 print $fh qq|<h1>$row->{full_name} - bibliografija za razdoblje 2008-2013</h1>|;
369 author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
370 author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
374 rename "$path.new", "$path.html";
378 print $index html_end;
380 rename 'html/index.new', 'html/index.html';
382 debug 'auth_header' => $auth_header;
385 my $department_category_author;
386 foreach my $department ( sort keys %$auth_department ) {
387 foreach my $authid ( sort @{ $auth_department->{$department} } ) {
388 my @categories = keys %{ $authors->{$authid}->{aut} };
389 push @categories, keys %{ $authors->{$authid}->{sec} };
390 foreach my $category ( sort @categories ) {
391 push @{ $department_category_author->{$department}->{$category} }, $authid;
392 push @{ $department_category_author->{''}->{$category} }, $authid if $department_in_sum->{$department};
397 debug 'department_category_author' => $department_category_author;
399 mkdir 'html/departments' unless -d 'html/departments';
401 sub unique_biblionumber {
404 $u->{$_}++ foreach @v;
405 return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
408 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
409 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
410 foreach my $department ( sort keys %$department_category_author ) {
411 my $dep = $department || 'Nema odsjeka';
412 my $dep_file = unac_string('utf-8',$dep);
413 print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
414 open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
416 print $fh html_title($department . ' bibliografija');
417 print $fh qq|<h1>$department bibliografija</h1>\n|;
419 print $fh qq|<h2>Primarno autorstvo</h2>\n|;
421 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
423 my @authids = @{ $department_category_author->{$department}->{$category} };
424 next unless @authids;
426 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
428 $unique->{$_}++ foreach @biblionumber;
430 next unless @biblionumber;
432 my $label = $category_label->{$category} || 'Bez kategorije';
433 print $fh qq|<h3>$label</h3>\n<ul>\n|;
435 print $fh li_biblio( $_ ) foreach @biblionumber;
441 print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
443 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
445 my @authids = @{ $department_category_author->{$department}->{$category} };
446 next unless @authids;
448 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
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;
463 rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
465 print $dep_fh qq|</ul>\n|, html_end;
467 rename 'html/departments/index.new', 'html/departments/index.html';
469 my $azvo_stat_biblio;
471 foreach my $department ( sort keys %$department_category_author ) {
472 foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
473 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
474 foreach my $type ( keys %{ $authors->{$authid} } ) {
475 next unless exists $authors->{$authid}->{$type}->{$category};
476 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } }, @{ $authors->{$authid}->{$type}->{$category} };
479 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
480 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } };
481 # $azvo_stat_biblio->{ $department }->{ $category }->{ $type } = $#biblios + 1;
482 $azvo_stat_biblio->{ $department }->{ $category }->{ $type } = [ @biblios ];
487 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
494 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
497 if ( /^([^\t]+)\t+(.+)/ ) {
499 push @report_labels, $label;
500 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
501 push @report_lines, [ $label, @$type ];
502 } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
503 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
505 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
506 push @report_labels, $label . $sub_label;
512 debug 'report_lines', \@report_lines;
513 debug 'sub_labels', $sub_labels;
514 debug 'report_labels', \@report_labels;
516 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
518 debug 'departments' => \@departments;
521 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
523 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
528 my $label = shift @_;
529 my $department = shift @_;
530 my @biblionumbers = @_;
532 $unique->{$_}++ foreach @biblionumbers;
533 $table->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar keys %$unique;
536 foreach my $department ( @departments ) {
537 foreach my $line ( @report_lines ) {
538 my $label = $line->[0];
540 foreach ( 1 .. $#$line ) {
541 my ( $category, $type ) = @{ $line->[ $_ ] };
542 my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type};
543 push @biblionumbers, @$b if $b;
545 if ( $sub_labels->{$label} ) {
547 foreach my $biblionumber ( @biblionumbers ) {
548 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
549 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
550 my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
552 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
555 if ( $field < 100 ) {
556 if ( $data->{$field} =~ m/$regex/ ) {
557 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
561 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
562 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
568 foreach my $sub_label ( keys %$sub_stats ) {
569 my $full_label = $label . $sub_label;
570 table_count $full_label, $department, @{ $sub_stats->{$sub_label} };
573 table_count $label, $department, @biblionumbers;
578 debug 'table', $table;
580 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
582 print $fh html_title('AZVO tablica');
584 print $fh "<table border=1>\n";
585 print $fh "<tr><th></th>";
586 print $fh "<th>$_</th>" foreach @departments;
589 foreach my $row ( 0 .. $#$table ) {
590 print $fh "<tr><th>", $report_labels[$row], "</th>";
591 print $fh "<td>", $table->[ $row ]->[ $_ ] || '', "</td>" foreach 0 .. $#departments;
595 print $fh "</table>\n", html_end;
598 rename 'html/azvo.new', 'html/azvo.html';