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