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