dd395acf3595fb43f4b80874701bbeb807994e4d
[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 $author_count;
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
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}->{ $category } }, $row->{biblionumber};
185                                 $author_count->{aut}->{$authid}++;
186                         }
187         }
188
189         if ( exists $data->{700} ) {
190                         foreach my $auth ( @{ $data->{700} } ) {
191                                 my $authid = $auth->{9} || next;
192                                 my $type   = $auth->{4} || next; #die "no 4 in ",dump($data);
193                         
194                                 push @{ $authors->{$authid}->{ $category } }, $row->{biblionumber};
195                                 if ( $type =~ m/aut/ ) {
196                                         $author_count->{aut}->{ $authid }++;
197                                 } elsif ( $type =~ m/(edt|tr)/ ) {
198                                         $author_count->{$1}->{ $authid }++;
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 'author_count' => $author_count;
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 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
247
248         my $first = substr( $row->{full_name}, 0, 1 );
249         if ( $first ne $first_letter ) {
250                 print $index qq{</ul>\n} if $first_letter;
251                 $first_letter = $first;
252                 print $index qq{<h1>$first</h1>\n<ul>\n};
253         }
254         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
255
256         my $path = "html/$row->{authid}";
257         open(my $fh, '>:encoding(utf-8)', "$path.new");
258         print $fh html_title($row->{full_name}, "bibliografija");
259         print $fh qq|<h1>$row->{full_name} - bibliografija za razdoblje 2008-2013</h1>|;
260         foreach my $category ( sort keys %{ $authors->{ $row->{authid} } } ) {
261                 my $label = $category_label->{$category} || 'Bez kategorije';
262                 print $fh qq|<h2>$label</h2>\n<ul>\n|;
263                 foreach my $biblionumber ( @{ $authors->{ $row->{authid} }->{$category} } ) {
264                         print $fh qq|<li>|,
265                                 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
266                                 biblioitem_html($biblionumber),
267                                 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
268                                 qq|</li>\n|;
269                 }
270                 print $fh qq|</ul>\n|;
271         }
272         print $fh html_end;
273         close($fh);
274         rename "$path.new", "$path.html";
275
276 }
277
278 print $index html_end;
279 close($index);
280 rename 'html/index.new', 'html/index.html';
281
282 debug 'auth_header' => $auth_header;
283
284
285 my $department_category_author;
286 foreach my $department ( sort keys %$auth_department ) {
287         foreach my $authid ( sort @{ $auth_department->{$department} } ) {
288                 foreach my $category ( sort keys %{ $authors->{$authid} } ) {
289                         push @{ $department_category_author->{$department}->{$category} }, $authid;
290                 }
291         }
292 }
293
294 debug 'department_category_author' => $department_category_author;
295
296 mkdir 'html/departments' unless -d 'html/departments';
297
298 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
299 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
300 foreach my $department ( sort keys %$department_category_author ) {
301         my $dep = $department || 'Nema odsjeka';
302         my $dep_file = unac_string('utf-8',$dep);
303         print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
304         open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
305         print $fh html_title($department . ' bibliografija');
306         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
307                 my $label = $category_label->{$category} || 'Bez kategorije';
308                 print $fh qq|<h1>$label</h1>\n<ul>\n|;
309
310                 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
311                         foreach my $biblionumber ( @{ $authors->{$authid}->{$category} } ) {
312                                 print $fh qq|<li>|,
313                                         qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
314                                         biblioitem_html($biblionumber),
315                                         qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
316                                         qq|</li>\n|;
317                         }
318                 }
319
320                 print $fh qq|</ul>|;
321         }
322         print $fh html_end;
323         close($fh);
324         rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
325 }
326 print $dep_fh qq|</ul>\n|, html_end;
327 close($dep_fh);
328 rename 'html/departments/index.new', 'html/departments/index.html';
329
330 debug 'skip' => $skip;
331