Bug 14648: Take advantage of I18N to deal with plural
[koha.git] / Koha / Sitemapper / Writer.pm
blob63f147aff8d2c819b50e6e7dfac2e4c2a9d4c502
1 package Koha::Sitemapper::Writer;
4 # Copyright 2015 Tamil s.a.r.l.
6 # This file is part of Koha.
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.
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.
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>.
22 use Moo;
23 use Modern::Perl;
24 use XML::Writer;
25 use IO::File;
26 use Koha::DateUtils;
29 my $MAX = 50000;
32 has sitemapper => (is => 'rw', );
34 has current => ( is => 'rw', default => sub { $MAX } );
36 has count => ( is => 'rw', default => sub { 0 } );
38 has writer => ( is => 'rw', );
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;
50 my $writer = XML::Writer->new(
51 OUTPUT => $fh,
52 DATA_MODE => 1,
53 DATA_INDENT => 2,
55 $writer->xmlDecl("UTF-8");
56 return $writer;
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();
69 sub write {
70 my ($self, $biblionumber, $timestamp) = @_;
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);
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();
102 sub end {
103 my $self = shift;
105 $self->_writer_end();
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();
121 $w->endTag();