2ba0d2c667ae9aa917212f18cf0cf083def8a8f5
[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 sub html_title {
65         return qq|<html>
66 <head>
67 <meta charset="UTF-8">
68 <title>|, join(" ", @_), qq|</title>
69 </head>
70 <body>
71 |;
72 }
73
74 sub html_end {
75         return qq|</body>\n</html\n|;
76 }
77
78
79 my $sth_marcxml = $dbh->prepare(q{
80 select marcxml from biblioitems where biblionumber = ?
81 });
82
83 sub biblioitem_html {
84         my $biblionumber = shift;
85
86         $sth_marcxml->execute( $biblionumber );
87         my $xmlrecord = $sth_marcxml->fetchrow_arrayref->[0];
88
89         my $parser = XML::LibXML->new();
90         $parser->recover_silently(0); # don't die when you find &, >, etc
91     my $source = $parser->parse_string($xmlrecord);
92         my $style_doc = $parser->parse_file($xslfilename);
93
94         my $xslt = XML::LibXSLT->new();
95         my $parsed = $xslt->parse_stylesheet($style_doc);
96         my $transformed = $parsed->transform($source);
97         return $parsed->output_string( $transformed );
98 }
99
100
101 mkdir 'html' unless -d 'html';
102
103 open(my $index, '>:encoding(utf-8)', 'html/index.html');
104 print $index html_title('Bibliografija Filozogskog fakulteta');
105
106 my $first_letter;
107
108 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
109
110         my $first = substr( $row->{full_name}, 0, 1 );
111         if ( $first ne $first_letter ) {
112                 print $index qq{</ul>\n} if $first_letter;
113                 $first_letter = $first;
114                 print $index qq{<h1>$first</h1>\n<ul>\n};
115         }
116         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
117
118         open(my $fh, '>:encoding(utf-8)', "html/$row->{authid}.html");
119         print $fh html_title($row->{full_name}, "bibliografija");
120         foreach my $category ( sort keys %{ $authors->{ $row->{authid} } } ) {
121                 print $fh qq|<h1>$category</h1>\n<ul>\n|;
122                 foreach my $biblionumber ( @{ $authors->{ $row->{authid} }->{$category} } ) {
123                         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|;
124                 }
125                 print $fh qq|</ul>\n|;
126         }
127         print $fh html_end;
128         close($fh);
129
130 }
131
132 print $index html_end;
133
134 print dump( $authors );
135
136 print dump( $auth_header );
137
138
139