Bug 23151: DBRev 19.06.00.013
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21
22 use Moo;
23 use Modern::Perl;
24 use XML::Writer;
25 use IO::File;
26 use DateTime;
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 = DateTime->now()->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;