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>.
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', );
43 my ($self, $name) = @_;
44 $name = $self->sitemapper->dir . "/$name";
45 my $fh = IO
::File
->new(">$name");
47 say "Impossible to create file: $name";
50 my $writer = XML
::Writer
->new(
55 $writer->xmlDecl("UTF-8");
62 return unless $self->writer;
63 $self->writer->endTag();
65 $self->writer->getOutput()->close();
70 my ($self, $biblionumber, $timestamp) = @_;
72 if ( $self->current == $MAX ) {
74 $self->count( $self->count + 1 );
75 my $w = $self->_writer_create( sprintf("sitemap%04d.xml", $self->count) );
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');
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=') .
90 $writer->startTag('url');
91 $writer->startTag('loc');
92 $writer->characters($url);
94 $writer->startTag('lastmod');
95 $timestamp = substr($timestamp, 0, 10);
96 $writer->characters($timestamp);
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');
113 my $name = sprintf("sitemap%04d.xml", $i);
114 $w->characters($self->sitemapper->url . "/$name");
116 $w->startTag('lastmod');
117 $w->characters($now);