ef041d24777fc9f24f8d5a71ec2a6a983dbe2cc6
[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), $/;
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
94 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
95
96 sub biblioitem_html {
97         my $biblionumber = shift;
98
99         return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber};
100
101         my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
102
103         print $xml_fh $xmlrecord if $ENV{XML};
104
105         my $source = eval { $parser->parse_string($xmlrecord) };
106         if ( $@ ) {
107 #               warn "SKIP $biblionumber corrupt XML";
108                 push @{ $skip->{XML_corrupt} }, $biblionumber;
109                 return;
110         }
111
112         my $transformed = $parsed->transform($source);
113         $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
114
115         return ( $biblio_html->{$biblionumber}, $source ) if wantarray;
116         return $biblio_html->{$biblionumber};
117 }
118
119 $sth_select_authors->execute();
120 while( my $row = $sth_select_authors->fetchrow_hashref ) {
121 #       warn dump($row),$/;
122
123         my $biblio;
124
125         $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
126
127         my ( undef, $doc ) = biblioitem_html( $row->{biblionumber} );
128         if ( ! $doc ) {
129 #               warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
130                 next;
131         }
132
133         my $root = $doc->documentElement;
134 =for leader
135         my @leaders = $root->getElementsByLocalName('leader');
136         if (@leaders) {
137                 my $leader = $leaders[0]->textContent;
138                 warn "leader $leader\n";
139         }
140 =cut
141
142         my $extract = {
143                 '008' => undef,
144                 '100' => '9',
145                 '700' => '(9|4)',
146                 '942' => 't'
147         };
148
149         my $data;
150
151         foreach my $elt ($root->getChildrenByLocalName('*')) {
152                 my $tag = $elt->getAttribute('tag');
153                 next if ! $tag;
154                 next unless exists $extract->{ $tag };
155
156         if ($elt->localname eq 'controlfield') {
157                         if ( $tag eq '008' ) {
158                                  $biblio_year->{ $row->{biblionumber} } = $data->{year} = substr($elt->textContent, 7, 4 );
159                         }
160                         next;
161         } elsif ($elt->localname eq 'datafield') {
162                         my $sf_data;
163             foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
164                 my $sf = $sfelt->getAttribute('code');
165                                 next unless $sf =~ m/$extract->{$tag}/;
166                                 if ( exists $sf_data->{$sf} ) {
167                                         $sf_data->{$sf} .= " " . $sfelt->textContent();
168                                 } else {
169                                         $sf_data->{$sf} = $sfelt->textContent();
170                                 }
171                         }
172                         push @{ $data->{$tag} }, $sf_data if $sf_data;
173         }
174     }
175
176         if ( $data->{year} < 2008 ) {
177                 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
178                 next;
179         } elsif ( $data->{year} > 2013 ) {
180                 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
181                 next;
182         }
183
184 #       warn "# ", $row->{biblionumber}, " data ",dump($data);
185
186         my $category = $data->{942}->[0]->{'t'};
187         if ( ! $category ) {
188 #               warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
189                 push @{ $skip->{no_category} }, $row->{biblionumber};
190                 next;
191         }
192
193
194         my $have_100 = 1;
195
196         if ( exists $data->{100} ) {
197                         my @first_author = map { $_->{'9'} } @{ $data->{100} };
198                         foreach my $authid ( @first_author ) {
199                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
200                         }
201         } else {
202                 $have_100 = 0;
203         }
204
205         my $have_edt;
206
207         if ( exists $data->{700} ) {
208                         foreach my $auth ( @{ $data->{700} } ) {
209                                 my $authid = $auth->{9} || next;
210                                 my $type   = $auth->{4} || next; #die "no 4 in ",dump($data);
211
212                                 $type_stats->{$type}++;
213
214                                 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
215                                         push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
216                                         push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
217                                 } elsif ( $type =~ m/aut/ ) {
218                                         if ( ! $have_100 ) {
219                                                 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
220                                                 if ( $have_edt ) {
221                                                         $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
222                                                 } else {
223                                                         push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
224                                                 }
225                                         } else {
226                                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
227                                         }
228                                 } else {
229 #                                       warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
230                                         $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
231                                 }
232                         }
233         }
234
235 }
236
237 debug 'authors' => $authors;
238 debug 'type_stats' => $type_stats;
239 debug 'skip' => $skip;
240 debug 'biblio_year' => $biblio_year;
241
242 my $category_label;
243 my $sth_categories = $dbh->prepare(q{
244 select authorised_value, lib from authorised_values where category = 'BIBCAT'
245 });
246 $sth_categories->execute();
247 while( my $row = $sth_categories->fetchrow_hashref ) {
248         $category_label->{ $row->{authorised_value} } = $row->{lib};
249
250 }
251 debug 'category_label' => $category_label;
252
253 sub html_title {
254         return qq|<html>
255 <head>
256 <meta charset="UTF-8">
257 <title>|, join(" ", @_), qq|</title>
258 <link href="style.css" type="text/css" rel="stylesheet" />
259 </head>
260 <body>
261 |;
262 }
263
264 sub html_end {
265         return qq|</body>\n</html\n|;
266 }
267
268 mkdir 'html' unless -d 'html';
269
270 open(my $index, '>:encoding(utf-8)', 'html/index.new');
271 print $index html_title('Bibliografija Filozofskog fakulteta');
272
273 my $first_letter = '';
274
275 debug 'authors' => \@authors;
276
277 sub li_biblio {
278         my ($biblionumber) = @_;
279         return qq|<li>|,
280                 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
281                 biblioitem_html($biblionumber),
282                 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
283                 qq|</li>\n|;
284 }
285
286 sub author_html {
287         my ( $fh, $authid, $type, $label ) = @_;
288
289         return unless exists $authors->{$authid}->{$type};
290
291         print $fh qq|<h2>$label</h2>\n|;
292
293         foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
294                 my $label = $category_label->{$category} || 'Bez kategorije';
295                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
296                 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
297                         print $fh li_biblio( $biblionumber );
298                 }
299                 print $fh qq|</ul>\n|;
300         }
301 }
302
303 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
304
305         my $first = substr( $row->{full_name}, 0, 1 );
306         if ( $first ne $first_letter ) {
307                 print $index qq{</ul>\n} if $first_letter;
308                 $first_letter = $first;
309                 print $index qq{<h1>$first</h1>\n<ul>\n};
310         }
311         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
312
313         my $path = "html/$row->{authid}";
314         open(my $fh, '>:encoding(utf-8)', "$path.new");
315         print $fh html_title($row->{full_name}, "bibliografija");
316         print $fh qq|<h1>$row->{full_name} - bibliografija za razdoblje 2008-2013</h1>|;
317
318         author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
319         author_html( $fh, $row->{authid}, 'sec' => 'Sekundarno autorstvo' );
320
321         print $fh html_end;
322         close($fh);
323         rename "$path.new", "$path.html";
324
325 }
326
327 print $index html_end;
328 close($index);
329 rename 'html/index.new', 'html/index.html';
330
331 debug 'auth_header' => $auth_header;
332
333
334 my $department_category_author;
335 foreach my $department ( sort keys %$auth_department ) {
336         foreach my $authid ( sort @{ $auth_department->{$department} } ) {
337                 my   @categories = keys %{ $authors->{$authid}->{aut} };
338                 push @categories,  keys %{ $authors->{$authid}->{sec} };
339                 foreach my $category ( sort @categories ) {
340                         push @{ $department_category_author->{$department}->{$category} }, $authid;
341                 }
342         }
343 }
344
345 debug 'department_category_author' => $department_category_author;
346
347 mkdir 'html/departments' unless -d 'html/departments';
348
349 sub unique_biblionumber {
350         my @v = @_;
351         my $u;
352         $u->{$_}++ foreach @v;
353         return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
354 }
355
356 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
357 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
358 foreach my $department ( sort keys %$department_category_author ) {
359         my $dep = $department || 'Nema odsjeka';
360         my $dep_file = unac_string('utf-8',$dep);
361         print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
362         open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
363
364         print $fh html_title($department . ' bibliografija');
365         print $fh qq|<h1>$department bibliografija</h1>\n|;
366
367         print $fh qq|<h2>Primarno autorstvo</h2>\n|;
368
369         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
370
371                 my @authids = @{ $department_category_author->{$department}->{$category} };
372                 next unless @authids;
373
374                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
375                 my $unique;
376                 $unique->{$_}++ foreach @biblionumber;
377
378                 next unless @biblionumber;
379
380                 my $label = $category_label->{$category} || 'Bez kategorije';
381                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
382
383                 print $fh li_biblio( $_ ) foreach @biblionumber;
384
385                 print $fh qq|</ul>|;
386         }
387
388
389         print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
390
391         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
392
393                 my @authids = @{ $department_category_author->{$department}->{$category} };
394                 next unless @authids;
395
396                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
397
398                 next unless @biblionumber;
399
400                 my $label = $category_label->{$category} || 'Bez kategorije';
401                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
402
403                 print $fh li_biblio( $_ ) foreach @biblionumber;
404
405                 print $fh qq|</ul>|;
406         }
407
408
409         print $fh html_end;
410         close($fh);
411         rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
412 }
413 print $dep_fh qq|</ul>\n|, html_end;
414 close($dep_fh);
415 rename 'html/departments/index.new', 'html/departments/index.html';
416
417 my $azvo_stat;
418
419 foreach my $department ( sort keys %$department_category_author ) {
420         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
421                 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
422                         foreach my $type ( keys %{ $authors->{$authid} } ) {
423                                 next unless exists $authors->{$authid}->{$type}->{$category};
424                                 $azvo_stat->{ $department }->{ $category }->{ $type } += $#{ $authors->{$authid}->{$type}->{$category} } + 1;
425                         }
426                 }
427         }
428 }
429
430 debug 'azvo_stat' => $azvo_stat;
431
432 =for later
433 open(my $fh, '>', 'html/azvo.new');
434
435
436
437 close($fh);
438 rename 'html/azvo.new', 'html/azvo.html';
439 =cut
440