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 |
has url => ( is => 'rw', isa => 'Str'); |
32 |
|
33 |
has short => ( is => 'rw', isa => 'Bool', default => 1); |
34 |
|
35 |
has current => ( is => 'rw', isa => 'Int', default => $MAX ); |
36 |
|
37 |
has count => ( is => 'rw', isa => 'Int', default => 0 ); |
38 |
|
39 |
has writer => ( is => 'rw', isa => 'XML::Writer' ); |
40 |
|
41 |
|
42 |
|
43 |
sub _writer_create { |
44 |
my ($self, $name) = @_; |
45 |
my $fh = IO::File->new(">$name"); |
46 |
my $writer = XML::Writer->new( |
47 |
OUTPUT => $fh, |
48 |
DATA_MODE => 1, |
49 |
DATA_INDENT => 2, |
50 |
); |
51 |
$writer->xmlDecl("UTF-8"); |
52 |
return $writer; |
53 |
} |
54 |
|
55 |
|
56 |
sub _writer_end { |
57 |
my $self = shift; |
58 |
return unless $self->writer; |
59 |
$self->writer->endTag(); |
60 |
$self->writer->end(); |
61 |
} |
62 |
|
63 |
|
64 |
sub write { |
65 |
my ($self, $biblionumber, $timestamp) = @_; |
66 |
|
67 |
if ( $self->current == $MAX ) { |
68 |
$self->_writer_end(); |
69 |
$self->count( $self->count + 1 ); |
70 |
my $w = $self->_writer_create( sprintf("sitemap%04d.xml", $self->count) ); |
71 |
$w->startTag( |
72 |
'urlset', |
73 |
'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9', |
74 |
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', |
75 |
'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'); |
76 |
$self->writer($w); |
77 |
$self->current(0); |
78 |
} |
79 |
|
80 |
$self->current( $self->current + 1 ); |
81 |
my $writer = $self->writer; |
82 |
my $url = $self->url . |
83 |
($self->short ? '/bib/' : '/cgi-bin/koha/opac-detail.pl?biblionumber=') . |
84 |
$biblionumber; |
85 |
$writer->startTag('url'); |
86 |
$writer->startTag('loc'); |
87 |
$writer->characters($url); |
88 |
$writer->endTag(); |
89 |
$writer->startTag('lastmod'); |
90 |
$timestamp = substr($timestamp, 0, 10); |
91 |
$writer->characters($timestamp); |
92 |
$writer->endTag(); |
93 |
$writer->endTag(); |
94 |
} |
95 |
|
96 |
|
97 |
sub end { |
98 |
my $self = shift; |
99 |
|
100 |
$self->_writer_end(); |
101 |
|
102 |
my $w = $self->_writer_create("sitemapindex.xml"); |
103 |
$w->startTag('sitemapindex', 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9'); |
104 |
my $now = DateTime->now()->ymd; |
105 |
for my $i ( 1..$self->count ) { |
106 |
$w->startTag('sitemap'); |
107 |
$w->startTag('loc'); |
108 |
my $name = sprintf("sitemap%04d.xml", $i); |
109 |
$w->characters($self->url . "/$name"); |
110 |
$w->endTag(); |
111 |
$w->startTag('lastmod'); |
112 |
$w->characters($now); |
113 |
$w->endTag(); |
114 |
$w->endTag(); |
115 |
} |
116 |
$w->endTag(); |
117 |
} |
118 |
|
119 |
|
120 |
no Moose; |
121 |
1; |
122 |
|