Bug 28959: Add virtualshelves.public as a boolean
[koha.git] / Koha / Sitemapper / Writer.pm
1 package Koha::Sitemapper::Writer;
2
3 #
4 # Copyright 2015 Tamil s.a.r.l.
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
22 use Moo;
23 use Modern::Perl;
24 use XML::Writer;
25 use IO::File;
26 use Koha::DateUtils qw( dt_from_string );
27
28
29 my $MAX = 50000;
30
31
32 has sitemapper => (is => 'rw', );
33
34 has current => ( is => 'rw', default => sub { $MAX } );
35
36 has count => ( is => 'rw', default => sub { 0 } );
37
38 has writer => ( is => 'rw',  );
39
40
41
42 sub _writer_create {
43     my ($self, $name) = @_;
44     $name = $self->sitemapper->dir . "/$name";
45     my $fh = IO::File->new(">$name");
46     unless ($fh) {
47         say "Impossible to create file: $name";
48         exit;
49     }
50     my $writer = XML::Writer->new(
51         OUTPUT => $fh,
52         DATA_MODE => 1,
53         DATA_INDENT => 2,
54     );
55     $writer->xmlDecl("UTF-8");
56     return $writer;
57 }
58
59
60 sub _writer_end {
61     my $self = shift;
62     return unless $self->writer;
63     $self->writer->endTag();
64     $self->writer->end();
65     $self->writer->getOutput()->close();
66 }
67
68
69 sub write {
70     my ($self, $biblionumber, $timestamp) = @_;
71
72     if ( $self->current == $MAX ) {
73         $self->_writer_end();
74         $self->count( $self->count + 1 );
75         my $w = $self->_writer_create( sprintf("sitemap%04d.xml", $self->count) );
76         $w->startTag(
77             'urlset',
78             'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
79             'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
80             'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
81         $self->writer($w);
82         $self->current(0);
83     }
84
85     $self->current( $self->current + 1 );
86     my $writer = $self->writer;
87     my $url = $self->sitemapper->url .
88               ($self->sitemapper->short ? '/bib/' : '/cgi-bin/koha/opac-detail.pl?biblionumber=') .
89               $biblionumber;
90     $writer->startTag('url');
91         $writer->startTag('loc');
92             $writer->characters($url);
93         $writer->endTag();
94         $writer->startTag('lastmod');
95             $timestamp = substr($timestamp, 0, 10);
96             $writer->characters($timestamp);
97         $writer->endTag();
98     $writer->endTag();
99 }
100
101
102 sub end {
103     my $self = shift;
104
105     $self->_writer_end();
106
107     my $w = $self->_writer_create("sitemapindex.xml");
108     $w->startTag('sitemapindex', 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9');
109     my $now = dt_from_string()->ymd;
110     for my $i ( 1..$self->count ) {
111         $w->startTag('sitemap');
112             $w->startTag('loc');
113                 my $name = sprintf("sitemap%04d.xml", $i);
114                 $w->characters($self->sitemapper->url . "/$name");
115             $w->endTag();
116             $w->startTag('lastmod');
117                 $w->characters($now);
118             $w->endTag();
119         $w->endTag();
120     }
121     $w->endTag();
122 }
123
124
125 1;