added category labels
[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
18 my $dbh = C4::Context->dbh;
19
20 my $xslfilename = 'compact.xsl';
21
22 my $authors;
23
24 my $sth_select_authors  = $dbh->prepare(q{
25 select
26         biblionumber,
27         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
28         ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
29         ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category
30 from biblioitems where agerestriction > 0
31 });
32
33 $sth_select_authors->execute();
34 while( my $row = $sth_select_authors->fetchrow_hashref ) {
35 #       warn dump($row),$/;
36         my $all_authors = join(' ', $row->{first_author}, $row->{other_authors});
37         foreach my $authid ( split(/\s+/, $all_authors) ) {
38                 push @{ $authors->{$authid}->{ $row->{category} } }, $row->{biblionumber};
39         }
40 }
41
42 my $auth_header;
43 my @authors;
44
45 my $all_authids = join(',', grep { length($_) > 0 } keys %$authors);
46 my $sth_auth = $dbh->prepare(q{
47 select
48         authid,
49         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name
50 from auth_header
51 where
52         ExtractValue(marcxml,'//datafield[@tag="024"]/subfield[@code="a"]') <> '' and
53         authid in (} . $all_authids . q{)
54 });
55
56 $sth_auth->execute();
57 while( my $row = $sth_auth->fetchrow_hashref ) {
58         warn dump( $row );
59         $auth_header->{ $row->{authid} } = $row->{full_name};
60         push @authors, $row;
61
62 }
63
64 my $category_label;
65 my $sth_categories = $dbh->prepare(q{
66 select authorised_value, lib from authorised_values where category = 'BIBCAT'
67 });
68 $sth_categories->execute();
69 while( my $row = $sth_categories->fetchrow_hashref ) {
70         $category_label->{ $row->{authorised_value} } = $row->{lib};
71
72 }
73 warn dump( $category_label );
74
75 sub html_title {
76         return qq|<html>
77 <head>
78 <meta charset="UTF-8">
79 <title>|, join(" ", @_), qq|</title>
80 <link href="style.css" type="text/css" rel="stylesheet" />
81 </head>
82 <body>
83 |;
84 }
85
86 sub html_end {
87         return qq|</body>\n</html\n|;
88 }
89
90
91 my $sth_marcxml = $dbh->prepare(q{
92 select marcxml from biblioitems where biblionumber = ?
93 });
94
95 sub biblioitem_html {
96         my $biblionumber = shift;
97
98         $sth_marcxml->execute( $biblionumber );
99         my $xmlrecord = $sth_marcxml->fetchrow_arrayref->[0];
100
101         my $parser = XML::LibXML->new();
102         $parser->recover_silently(0); # don't die when you find &, >, etc
103     my $source = $parser->parse_string($xmlrecord);
104         my $style_doc = $parser->parse_file($xslfilename);
105
106         my $xslt = XML::LibXSLT->new();
107         my $parsed = $xslt->parse_stylesheet($style_doc);
108         my $transformed = $parsed->transform($source);
109         return $parsed->output_string( $transformed );
110 }
111
112
113 mkdir 'html' unless -d 'html';
114
115 open(my $index, '>:encoding(utf-8)', 'html/index.html');
116 print $index html_title('Bibliografija Filozogskog fakulteta');
117
118 my $first_letter;
119
120 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
121
122         my $first = substr( $row->{full_name}, 0, 1 );
123         if ( $first ne $first_letter ) {
124                 print $index qq{</ul>\n} if $first_letter;
125                 $first_letter = $first;
126                 print $index qq{<h1>$first</h1>\n<ul>\n};
127         }
128         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
129
130         open(my $fh, '>:encoding(utf-8)', "html/$row->{authid}.html");
131         print $fh html_title($row->{full_name}, "bibliografija");
132         foreach my $category ( sort keys %{ $authors->{ $row->{authid} } } ) {
133                 my $label = $category_label->{$category} || 'Bez kategorije';
134                 print $fh qq|<h1>$label</h1>\n<ul>\n|;
135                 foreach my $biblionumber ( @{ $authors->{ $row->{authid} }->{$category} } ) {
136                         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|;
137                 }
138                 print $fh qq|</ul>\n|;
139         }
140         print $fh html_end;
141         close($fh);
142
143 }
144
145 print $index html_end;
146
147 print dump( $authors );
148
149 print dump( $auth_header );
150
151
152