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