|
Line 0
Link Here
|
|
|
1 |
package Koha::Sitemapper::Writer; |
| 2 |
|
| 3 |
# |
| 4 |
# Copyright 2014 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 2 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 Moose; |
| 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', isa => 'Koha::Sitemapper'); |
| 33 |
|
| 34 |
has current => ( is => 'rw', isa => 'Int', default => $MAX ); |
| 35 |
|
| 36 |
has count => ( is => 'rw', isa => 'Int', default => 0 ); |
| 37 |
|
| 38 |
has writer => ( is => 'rw', isa => 'XML::Writer' ); |
| 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 |
} |
| 66 |
|
| 67 |
|
| 68 |
sub write { |
| 69 |
my ($self, $biblionumber, $timestamp) = @_; |
| 70 |
|
| 71 |
if ( $self->current == $MAX ) { |
| 72 |
$self->_writer_end(); |
| 73 |
$self->count( $self->count + 1 ); |
| 74 |
my $w = $self->_writer_create( sprintf("sitemap%04d.xml", $self->count) ); |
| 75 |
$w->startTag( |
| 76 |
'urlset', |
| 77 |
'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9', |
| 78 |
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', |
| 79 |
'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'); |
| 80 |
$self->writer($w); |
| 81 |
$self->current(0); |
| 82 |
} |
| 83 |
|
| 84 |
$self->current( $self->current + 1 ); |
| 85 |
my $writer = $self->writer; |
| 86 |
my $url = $self->sitemapper->url . |
| 87 |
($self->sitemapper->short ? '/bib/' : '/cgi-bin/koha/opac-detail.pl?biblionumber=') . |
| 88 |
$biblionumber; |
| 89 |
$writer->startTag('url'); |
| 90 |
$writer->startTag('loc'); |
| 91 |
$writer->characters($url); |
| 92 |
$writer->endTag(); |
| 93 |
$writer->startTag('lastmod'); |
| 94 |
$timestamp = substr($timestamp, 0, 10); |
| 95 |
$writer->characters($timestamp); |
| 96 |
$writer->endTag(); |
| 97 |
$writer->endTag(); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
sub end { |
| 102 |
my $self = shift; |
| 103 |
|
| 104 |
$self->_writer_end(); |
| 105 |
|
| 106 |
my $w = $self->_writer_create("sitemapindex.xml"); |
| 107 |
$w->startTag('sitemapindex', 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9'); |
| 108 |
my $now = DateTime->now()->ymd; |
| 109 |
for my $i ( 1..$self->count ) { |
| 110 |
$w->startTag('sitemap'); |
| 111 |
$w->startTag('loc'); |
| 112 |
my $name = sprintf("sitemap%04d.xml", $i); |
| 113 |
$w->characters($self->sitemapper->url . "/$name"); |
| 114 |
$w->endTag(); |
| 115 |
$w->startTag('lastmod'); |
| 116 |
$w->characters($now); |
| 117 |
$w->endTag(); |
| 118 |
$w->endTag(); |
| 119 |
} |
| 120 |
$w->endTag(); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
no Moose; |
| 125 |
1; |
| 126 |
|