| Line 0
          
      
      
        Link Here | 
            
              |  |  | 1 | package Koha::Sitemaper::Writer; | 
            
              | 2 |  | 
            
              | 3 | # | 
            
              | 4 | # Copyright 2013 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 current => ( is => 'rw', isa => 'Int', default => $MAX ); | 
            
              | 34 |  | 
            
              | 35 | has count => ( is => 'rw', isa => 'Int', default => 0 ); | 
            
              | 36 |  | 
            
              | 37 | has writer => ( is => 'rw', isa => 'XML::Writer' ); | 
            
              | 38 |  | 
            
              | 39 |  | 
            
              | 40 |  | 
            
              | 41 | sub _writer_create { | 
            
              | 42 |     my ($self, $name) = @_; | 
            
              | 43 |     my $fh = IO::File->new(">$name"); | 
            
              | 44 |     my $writer = XML::Writer->new( | 
            
              | 45 |         OUTPUT => $fh, | 
            
              | 46 |         DATA_MODE => 1, | 
            
              | 47 |         DATA_INDENT => 2, | 
            
              | 48 |     ); | 
            
              | 49 |     return $writer; | 
            
              | 50 | } | 
            
              | 51 |  | 
            
              | 52 |  | 
            
              | 53 | sub _writer_end { | 
            
              | 54 |     my $self = shift; | 
            
              | 55 |     return unless $self->writer; | 
            
              | 56 |     $self->writer->endTag(); | 
            
              | 57 |     $self->writer->end(); | 
            
              | 58 | } | 
            
              | 59 |  | 
            
              | 60 |  | 
            
              | 61 | sub write { | 
            
              | 62 |     my ($self, $biblionumber, $timestamp) = @_; | 
            
              | 63 |  | 
            
              | 64 |     if ( $self->current == $MAX ) { | 
            
              | 65 |         $self->_writer_end(); | 
            
              | 66 |         $self->count( $self->count + 1 ); | 
            
              | 67 |         my $w = $self->_writer_create( sprintf("sitemap%04d.xml", $self->count) ); | 
            
              | 68 |         $w->startTag('urlset', 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9'); | 
            
              | 69 |         $self->writer($w); | 
            
              | 70 |         $self->current(0); | 
            
              | 71 |     } | 
            
              | 72 |  | 
            
              | 73 |     $self->current( $self->current + 1 ); | 
            
              | 74 |     my $writer = $self->writer; | 
            
              | 75 |     $writer->startTag('url'); | 
            
              | 76 |         $writer->startTag('loc'); | 
            
              | 77 |             $writer->characters($self->url . "/bib/$biblionumber"); | 
            
              | 78 |         $writer->endTag(); | 
            
              | 79 |         $writer->startTag('lastmod'); | 
            
              | 80 |             $timestamp = substr($timestamp, 0, 10); | 
            
              | 81 |             $writer->characters($timestamp); | 
            
              | 82 |         $writer->endTag(); | 
            
              | 83 |     $writer->endTag(); | 
            
              | 84 | } | 
            
              | 85 |  | 
            
              | 86 |  | 
            
              | 87 | sub end { | 
            
              | 88 |     my $self = shift; | 
            
              | 89 |  | 
            
              | 90 |     $self->_writer_end(); | 
            
              | 91 |  | 
            
              | 92 |     my $w = $self->_writer_create("sitemapindex.xml"); | 
            
              | 93 |     $w->startTag('sitemapindex', 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9'); | 
            
              | 94 |     my $now = DateTime->now()->ymd; | 
            
              | 95 |     for my $i ( 1..$self->count ) { | 
            
              | 96 |         $w->startTag('sitemap'); | 
            
              | 97 |             $w->startTag('loc'); | 
            
              | 98 |                 my $name = sprintf("sitemap%04d.xml", $i); | 
            
              | 99 |                 $w->characters($self->url . "/$name"); | 
            
              | 100 |             $w->endTag(); | 
            
              | 101 |             $w->startTag('lastmod'); | 
            
              | 102 |                 $w->characters($now); | 
            
              | 103 |             $w->endTag(); | 
            
              | 104 |         $w->endTag(); | 
            
              | 105 |     } | 
            
              | 106 |     $w->endTag(); | 
            
              | 107 | } | 
            
              | 108 |  | 
            
              | 109 |  | 
            
              | 110 | no Moose; | 
            
              | 111 | 1; | 
            
              | 112 |  |