generate biblio_in_database from 942$r
[koha-bibliografija] / html.pl
1 #!/usr/bin/perl
2
3 # LC_COLLATE=hr_HR.utf8 KOHA_CONF=/etc/koha/sites/ffzg/koha-conf.xml ./html.pl
4
5 use warnings;
6 use strict;
7
8 use DBI;
9 use Data::Dump qw(dump);
10 use autodie;
11 use locale;
12 use Text::Unaccent;
13 use Carp qw(confess);
14
15 use lib '/srv/koha_ffzg';
16 use C4::Context;
17 use XML::LibXML;
18 use XML::LibXSLT;
19
20 my $dbh = C4::Context->dbh;
21
22 sub debug {
23         my ($title, $data) = @_;
24         print "# $title ",dump($data), $/ if $ENV{DEBUG};
25 }
26
27 my $xslfilename = 'compact.xsl';
28
29 my $auth_header;
30 my $auth_department;
31 my @authors;
32
33 my $skip;
34
35 my $sth_auth = $dbh->prepare(q{
36 select
37         authid,
38         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
39         ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department
40 from auth_header
41 });
42
43 $sth_auth->execute();
44 while( my $row = $sth_auth->fetchrow_hashref ) {
45         if ( $row->{department} !~ m/Filozofski fakultet u Zagrebu/ ) {
46                 push @{ $skip->{nije_ffzg} }, $row;
47                 next;
48         }
49         $auth_header->{ $row->{authid} } = $row->{full_name};
50         $row->{department} =~ s/, Filozofski fakultet u Zagrebu.*$//;
51         $row->{department} =~ s/^.+\.\s*//;
52 #       warn dump( $row );
53         push @{ $auth_department->{ $row->{department} } }, $row->{authid};
54         push @authors, $row;
55
56 }
57
58 debug 'auth_department' => $auth_department;
59
60
61 my $authors;
62 my $marcxml;
63
64 my $sth_select_authors  = $dbh->prepare(q{
65 select
66         biblionumber,
67         itemtype,
68         marcxml
69 from biblioitems
70 where
71         agerestriction > 0
72 });
73
74 =for sql
75 --      ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
76 --      ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
77 --      ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
78
79 --      and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
80 -- order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
81 =cut
82
83 my $biblio_year;
84 my $type_stats;
85
86 my $parser = XML::LibXML->new();
87 $parser->recover_silently(0); # don't die when you find &, >, etc
88 my $style_doc = $parser->parse_file($xslfilename);
89 my $xslt = XML::LibXSLT->new();
90 my $parsed = $xslt->parse_stylesheet($style_doc);
91
92 my $biblio_html;
93 my $biblio_parsed;
94 my $biblio_in_database;
95
96 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
97
98 sub biblioitem_html {
99         my ($biblionumber, $parse_only) = @_;
100
101         return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber} && ! $parse_only;
102
103         my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
104
105         print $xml_fh $xmlrecord if $ENV{XML};
106
107         my $source = eval { $parser->parse_string($xmlrecord) };
108         if ( $@ ) {
109 #               warn "SKIP $biblionumber corrupt XML";
110                 push @{ $skip->{XML_corrupt} }, $biblionumber;
111                 return;
112         }
113
114         if ( $parse_only ) {
115                 $biblio_parsed->{$biblionumber} = $source;
116                 return $source;
117         }
118
119         my $transformed = $parsed->transform($source);
120         $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
121
122         delete $biblio_parsed->{$biblionumber};
123
124         return $biblio_html->{$biblionumber};
125 }
126
127 $sth_select_authors->execute();
128 while( my $row = $sth_select_authors->fetchrow_hashref ) {
129 #       warn dump($row),$/;
130
131         my $biblio;
132
133         $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
134
135         my $doc = biblioitem_html( $row->{biblionumber}, 1 );
136         if ( ! $doc ) {
137 #               warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
138                 next;
139         }
140
141         my $root = $doc->documentElement;
142 =for leader
143         my @leaders = $root->getElementsByLocalName('leader');
144         if (@leaders) {
145                 my $leader = $leaders[0]->textContent;
146                 warn "leader $leader\n";
147         }
148 =cut
149
150         my $extract = {
151                 '008' => undef,
152                 '100' => '9',
153                 '700' => '(9|4)',
154                 '942' => '(t|r)'
155         };
156
157         my $data;
158
159         foreach my $elt ($root->getChildrenByLocalName('*')) {
160                 my $tag = $elt->getAttribute('tag');
161                 next if ! $tag;
162                 next unless exists $extract->{ $tag };
163
164         if ($elt->localname eq 'controlfield') {
165                         if ( $tag eq '008' ) {
166                                 my $year = substr($elt->textContent, 7, 4 );
167                                 if ( $year !~ m/^\d+$/ ) {
168                                         $year = 0;
169                                         push @{ $skip->{invalid_year} }, $row->{biblionumber};
170                                 }
171                                 $biblio_year->{ $row->{biblionumber} } = $data->{year} = $year;
172                         }
173                         next;
174         } elsif ($elt->localname eq 'datafield') {
175                         my $sf_data;
176             foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
177                 my $sf = $sfelt->getAttribute('code');
178                                 next unless $sf =~ m/$extract->{$tag}/;
179                                 if ( exists $sf_data->{$sf} ) {
180                                         $sf_data->{$sf} .= " " . $sfelt->textContent();
181                                 } else {
182                                         $sf_data->{$sf} = $sfelt->textContent();
183                                 }
184                         }
185                         push @{ $data->{$tag} }, $sf_data if $sf_data;
186         }
187     }
188
189         if ( $data->{year} < 2008 ) {
190                 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
191                 next;
192         } elsif ( $data->{year} > 2013 ) {
193                 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
194                 next;
195         }
196
197 #       warn "# ", $row->{biblionumber}, " data ",dump($data);
198
199         my $category = $data->{942}->[0]->{'t'};
200         if ( ! $category ) {
201 #               warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
202                 push @{ $skip->{no_category} }, $row->{biblionumber};
203                 next;
204         }
205
206         if ( my $in_database = $data->{942}->[0]->{'r'} ) {
207                 $biblio_in_database->{ $row->{biblionumber} } = $in_database;
208         }
209
210         my $have_100 = 1;
211
212         if ( exists $data->{100} ) {
213                         my @first_author = map { $_->{'9'} } @{ $data->{100} };
214                         foreach my $authid ( @first_author ) {
215                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
216                         }
217         } else {
218                 $have_100 = 0;
219         }
220
221         my $have_edt;
222
223         if ( exists $data->{700} ) {
224                         foreach my $auth ( @{ $data->{700} } ) {
225                                 my $authid = $auth->{9} || next;
226                                 my $type   = $auth->{4} || next; #die "no 4 in ",dump($data);
227
228                                 $type_stats->{$type}++;
229
230                                 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
231                                         push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
232                                         push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
233                                 } elsif ( $type =~ m/aut/ ) {
234                                         if ( ! $have_100 ) {
235                                                 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
236                                                 if ( $have_edt ) {
237                                                         $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
238                                                 } else {
239                                                         push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
240                                                 }
241                                         } else {
242                                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
243                                         }
244                                 } else {
245 #                                       warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
246                                         $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
247                                 }
248                         }
249         }
250
251 }
252
253 debug 'authors' => $authors;
254 debug 'type_stats' => $type_stats;
255 debug 'skip' => $skip;
256 debug 'biblio_year' => $biblio_year;
257 debug 'biblio_in_database' => $biblio_in_database;
258
259 my $category_label;
260 my $sth_categories = $dbh->prepare(q{
261 select authorised_value, lib from authorised_values where category = 'BIBCAT'
262 });
263 $sth_categories->execute();
264 while( my $row = $sth_categories->fetchrow_hashref ) {
265         $category_label->{ $row->{authorised_value} } = $row->{lib};
266
267 }
268 debug 'category_label' => $category_label;
269
270 sub html_title {
271         return qq|<html>
272 <head>
273 <meta charset="UTF-8">
274 <title>|, join(" ", @_), qq|</title>
275 <link href="style.css" type="text/css" rel="stylesheet" />
276 </head>
277 <body>
278 |;
279 }
280
281 sub html_end {
282         return qq|</body>\n</html\n|;
283 }
284
285 mkdir 'html' unless -d 'html';
286
287 open(my $index, '>:encoding(utf-8)', 'html/index.new');
288 print $index html_title('Bibliografija Filozofskog fakulteta');
289
290 my $first_letter = '';
291
292 debug 'authors' => \@authors;
293
294 sub li_biblio {
295         my ($biblionumber) = @_;
296         return qq|<li>|,
297                 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
298                 biblioitem_html($biblionumber),
299                 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
300                 qq|</li>\n|;
301 }
302
303 sub author_html {
304         my ( $fh, $authid, $type, $label ) = @_;
305
306         return unless exists $authors->{$authid}->{$type};
307
308         print $fh qq|<h2>$label</h2>\n|;
309
310         foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
311                 my $label = $category_label->{$category} || 'Bez kategorije';
312                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
313                 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
314                         print $fh li_biblio( $biblionumber );
315                 }
316                 print $fh qq|</ul>\n|;
317         }
318 }
319
320 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
321
322         my $first = substr( $row->{full_name}, 0, 1 );
323         if ( $first ne $first_letter ) {
324                 print $index qq{</ul>\n} if $first_letter;
325                 $first_letter = $first;
326                 print $index qq{<h1>$first</h1>\n<ul>\n};
327         }
328         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
329
330         my $path = "html/$row->{authid}";
331         open(my $fh, '>:encoding(utf-8)', "$path.new");
332         print $fh html_title($row->{full_name}, "bibliografija");
333         print $fh qq|<h1>$row->{full_name} - bibliografija za razdoblje 2008-2013</h1>|;
334
335         author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
336         author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
337
338         print $fh html_end;
339         close($fh);
340         rename "$path.new", "$path.html";
341
342 }
343
344 print $index html_end;
345 close($index);
346 rename 'html/index.new', 'html/index.html';
347
348 debug 'auth_header' => $auth_header;
349
350
351 my $department_category_author;
352 foreach my $department ( sort keys %$auth_department ) {
353         foreach my $authid ( sort @{ $auth_department->{$department} } ) {
354                 my   @categories = keys %{ $authors->{$authid}->{aut} };
355                 push @categories,  keys %{ $authors->{$authid}->{sec} };
356                 foreach my $category ( sort @categories ) {
357                         push @{ $department_category_author->{$department}->{$category} }, $authid;
358                 }
359         }
360 }
361
362 debug 'department_category_author' => $department_category_author;
363
364 mkdir 'html/departments' unless -d 'html/departments';
365
366 sub unique_biblionumber {
367         my @v = @_;
368         my $u;
369         $u->{$_}++ foreach @v;
370         return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
371 }
372
373 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
374 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
375 foreach my $department ( sort keys %$department_category_author ) {
376         my $dep = $department || 'Nema odsjeka';
377         my $dep_file = unac_string('utf-8',$dep);
378         print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
379         open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
380
381         print $fh html_title($department . ' bibliografija');
382         print $fh qq|<h1>$department bibliografija</h1>\n|;
383
384         print $fh qq|<h2>Primarno autorstvo</h2>\n|;
385
386         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
387
388                 my @authids = @{ $department_category_author->{$department}->{$category} };
389                 next unless @authids;
390
391                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
392                 my $unique;
393                 $unique->{$_}++ foreach @biblionumber;
394
395                 next unless @biblionumber;
396
397                 my $label = $category_label->{$category} || 'Bez kategorije';
398                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
399
400                 print $fh li_biblio( $_ ) foreach @biblionumber;
401
402                 print $fh qq|</ul>|;
403         }
404
405
406         print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
407
408         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
409
410                 my @authids = @{ $department_category_author->{$department}->{$category} };
411                 next unless @authids;
412
413                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
414
415                 next unless @biblionumber;
416
417                 my $label = $category_label->{$category} || 'Bez kategorije';
418                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
419
420                 print $fh li_biblio( $_ ) foreach @biblionumber;
421
422                 print $fh qq|</ul>|;
423         }
424
425
426         print $fh html_end;
427         close($fh);
428         rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
429 }
430 print $dep_fh qq|</ul>\n|, html_end;
431 close($dep_fh);
432 rename 'html/departments/index.new', 'html/departments/index.html';
433
434 my $azvo_stat_authors;
435 my $azvo_stat_biblio;
436
437 foreach my $department ( sort keys %$department_category_author ) {
438         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
439                 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
440                         foreach my $type ( keys %{ $authors->{$authid} } ) {
441                                 next unless exists $authors->{$authid}->{$type}->{$category};
442                                 $azvo_stat_authors->{ $department }->{ $category }->{ $type } += $#{ $authors->{$authid}->{$type}->{$category} } + 1;
443                                 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } },  @{ $authors->{$authid}->{$type}->{$category} };
444                         }
445                 }
446                 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
447                                 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } };
448                                 $azvo_stat_biblio->{ $department }->{ $category }->{ $type } = $#biblios + 1;
449                 }
450         }
451 }
452
453 debug 'azvo_stat_authors' => $azvo_stat_authors;
454 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
455
456 =for later
457 open(my $fh, '>', 'html/azvo.new');
458
459
460
461 close($fh);
462 rename 'html/azvo.new', 'html/azvo.html';
463 =cut
464