don't filter authors for KNJ itemtype
[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
13 use lib '/srv/koha_ffzg';
14 use C4::Context;
15 use XML::LibXML;
16 use XML::LibXSLT;
17 use XML::Simple;
18
19 my $dbh = C4::Context->dbh;
20
21 my $xslfilename = 'compact.xsl';
22
23 my $authors;
24 my $marcxml;
25
26 my $sth_select_authors  = $dbh->prepare(q{
27 select
28         biblionumber,
29         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
30         ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
31         ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
32         itemtype,
33         marcxml
34 from biblioitems
35 where
36         agerestriction > 0
37         and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
38 order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
39 });
40
41 $sth_select_authors->execute();
42 while( my $row = $sth_select_authors->fetchrow_hashref ) {
43 #       warn dump($row),$/;
44         if ( $row->{first_author} || $row->{itemtype} !~ m/^KNJ/ ) {
45                 my $all_authors = join(' ', $row->{first_author}, $row->{other_authors});
46                 foreach my $authid ( split(/\s+/, $all_authors) ) {
47                         push @{ $authors->{$authid}->{ $row->{category} } }, $row->{biblionumber};
48                         $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
49                 }
50         } else {
51                 my $xml = XMLin( $row->{marcxml}, ForceArray => [ 'subfield' ] );
52                 foreach my $f700 ( map { $_->{subfield} } grep { $_->{tag} eq 700 } @{ $xml->{datafield} } ) {
53                         my $authid = 0;
54                         my $is_edt = 0;
55                         foreach my $sf ( @$f700 ) {
56                                 if ( $sf->{code} eq '4' && $sf->{content} =~ m/^edt/ ) {
57                                         $is_edt++;
58                                 } elsif ( $sf->{code} eq '9' ) {
59                                         $authid = $sf->{content};
60                                 }
61                         }
62                         if ( $authid && $is_edt ) {
63                                 warn "# ++ ", $row->{biblionumber}, " $authid f700 ", dump( $f700 );
64                                 push @{ $authors->{$authid}->{ $row->{category} } }, $row->{biblionumber};
65                                 $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
66                         } else {
67                                 warn "# -- ", $row->{biblionumber}, " f700 ", dump( $f700 );
68                         }
69                 }
70         }
71 }
72
73 my $auth_header;
74 my @authors;
75
76 my $all_authids = join(',', grep { length($_) > 0 } keys %$authors);
77 my $sth_auth = $dbh->prepare(q{
78 select
79         authid,
80         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name
81 from auth_header
82 where
83         ExtractValue(marcxml,'//datafield[@tag="024"]/subfield[@code="a"]') <> '' and
84         authid in (} . $all_authids . q{)
85 });
86
87 $sth_auth->execute();
88 while( my $row = $sth_auth->fetchrow_hashref ) {
89         warn dump( $row );
90         $auth_header->{ $row->{authid} } = $row->{full_name};
91         push @authors, $row;
92
93 }
94
95 my $category_label;
96 my $sth_categories = $dbh->prepare(q{
97 select authorised_value, lib from authorised_values where category = 'BIBCAT'
98 });
99 $sth_categories->execute();
100 while( my $row = $sth_categories->fetchrow_hashref ) {
101         $category_label->{ $row->{authorised_value} } = $row->{lib};
102
103 }
104 warn dump( $category_label );
105
106 sub html_title {
107         return qq|<html>
108 <head>
109 <meta charset="UTF-8">
110 <title>|, join(" ", @_), qq|</title>
111 <link href="style.css" type="text/css" rel="stylesheet" />
112 </head>
113 <body>
114 |;
115 }
116
117 sub html_end {
118         return qq|</body>\n</html\n|;
119 }
120
121
122 sub biblioitem_html {
123         my $biblionumber = shift;
124
125         my $xmlrecord = $marcxml->{$biblionumber} || die "missing $biblionumber marcxml";
126
127         my $parser = XML::LibXML->new();
128         $parser->recover_silently(0); # don't die when you find &, >, etc
129     my $source = $parser->parse_string($xmlrecord);
130         my $style_doc = $parser->parse_file($xslfilename);
131
132         my $xslt = XML::LibXSLT->new();
133         my $parsed = $xslt->parse_stylesheet($style_doc);
134         my $transformed = $parsed->transform($source);
135         return $parsed->output_string( $transformed );
136 }
137
138
139 mkdir 'html' unless -d 'html';
140
141 open(my $index, '>:encoding(utf-8)', 'html/index.html');
142 print $index html_title('Bibliografija Filozogskog fakulteta');
143
144 my $first_letter;
145
146 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
147
148         my $first = substr( $row->{full_name}, 0, 1 );
149         if ( $first ne $first_letter ) {
150                 print $index qq{</ul>\n} if $first_letter;
151                 $first_letter = $first;
152                 print $index qq{<h1>$first</h1>\n<ul>\n};
153         }
154         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
155
156         open(my $fh, '>:encoding(utf-8)', "html/$row->{authid}.html");
157         print $fh html_title($row->{full_name}, "bibliografija");
158         foreach my $category ( sort keys %{ $authors->{ $row->{authid} } } ) {
159                 my $label = $category_label->{$category} || 'Bez kategorije';
160                 print $fh qq|<h1>$label</h1>\n<ul>\n|;
161                 foreach my $biblionumber ( @{ $authors->{ $row->{authid} }->{$category} } ) {
162                         print $fh qq|<li><a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|, biblioitem_html($biblionumber), qq|</li>\n|;
163                 }
164                 print $fh qq|</ul>\n|;
165         }
166         print $fh html_end;
167         close($fh);
168
169 }
170
171 print $index html_end;
172
173 print dump( $authors );
174
175 print dump( $auth_header );
176
177
178