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