| 1 | package Search::Sitemap; | ||
| 2 | $Search::Sitemap::VERSION = '2.13_01'; | ||
| 3 | 2 | ▶ | use 5.008003; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | our $AUTHORITY = 'cpan:JASONK'; | ||
| 7 | 2 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Search::Sitemap::Types qw( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | SitemapUrlStore XMLPrettyPrintValue SitemapURL | ||
| 10 | 2 | ); | |
| 11 | 2 | ▶ | use MooseX::ClassAttribute; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use MooseX::Types::Moose qw( Str HashRef Bool ArrayRef CodeRef ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use Search::Sitemap::URL; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use XML::Twig; | ||
| 15 | use IO::File; | ||
| 16 | use Carp qw( carp croak ); | ||
| 17 | use HTML::Entities qw( decode_entities ); | ||
| 18 | use Class::Load; | ||
| 19 | use namespace::clean -except => 'meta'; | ||
| 20 | |||
| 21 | has 'urls' => ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => SitemapUrlStore, | ||
| 24 | coerce => 1, | ||
| 25 | default => sub { | ||
| 26 | Class::Load::load_class( 'Search::Sitemap::URLStore::Memory' ); | ||
| 27 | return Search::Sitemap::URLStore::Memory->new; | ||
| 28 | }, | ||
| 29 | handles => { | ||
| 30 | get_url => 'get', | ||
| 31 | put_url => 'put', | ||
| 32 | put_urls => 'put', | ||
| 33 | find_url => 'find', | ||
| 34 | }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | has 'pretty' => ( | ||
| 38 | is => 'rw', | ||
| 39 | isa => XMLPrettyPrintValue, | ||
| 40 | coerce => 1, | ||
| 41 | default => 'none', | ||
| 42 | ); | ||
| 43 | |||
| 44 | class_has 'base_element' => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => Str, | ||
| 47 | default => 'urlset' | ||
| 48 | ); | ||
| 49 | |||
| 50 | has 'xmlparser' => ( | ||
| 51 | is => 'rw', | ||
| 52 | isa => 'XML::Twig', | ||
| 53 | lazy => 1, | ||
| 54 | default => sub { | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | XML::Twig->new( | ||
| 58 | twig_roots => { | ||
| 59 | $self->base_element => sub { | ||
| 60 | my ( $twig, $elt ) = @_; | ||
| 61 | foreach my $c ( $elt->children ) { | ||
| 62 | my %url = (); | ||
| 63 | my $var = $c->gi; | ||
| 64 | croak "Unrecognised element $var" | ||
| 65 | unless $var =~ /^(?:url|sitemap)$/; | ||
| 66 | foreach my $e ( $c->children ) { | ||
| 67 | $url{ $e->gi } = decode_entities( $e->text ); | ||
| 68 | } | ||
| 69 | $self->update( \%url ); | ||
| 70 | } | ||
| 71 | $twig->purge; | ||
| 72 | }, | ||
| 73 | }, | ||
| 74 | ); | ||
| 75 | }, | ||
| 76 | handles => [qw( safe_parse )], | ||
| 77 | ); | ||
| 78 | |||
| 79 | has 'have_zlib' => ( | ||
| 80 | is => 'ro', | ||
| 81 | isa => Bool, | ||
| 82 | lazy => 1, | ||
| 83 | default => sub { | ||
| 84 | local $@; | ||
| 85 | eval { Class::Load::load_class( 'IO::Zlib' ) }; | ||
| 86 | return $@ ? 0 : 1; | ||
| 87 | }, | ||
| 88 | ); | ||
| 89 | |||
| 90 | has 'extensions' => ( is => 'ro', isa => HashRef, default => sub { {} } ); | ||
| 91 | |||
| 92 | has 'xml_headers' => ( is => 'rw', isa => HashRef, lazy_build => 1 ); | ||
| 93 | sub _build_xml_headers { | ||
| 94 | my $self = shift; | ||
| 95 | my $ext = $self->extensions; | ||
| 96 | return { | ||
| 97 | 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', | ||
| 98 | 'xsi:schemaLocation' => join( ' ', | ||
| 99 | 'http://www.sitemaps.org/schemas/sitemap/0.9', | ||
| 100 | 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd', | ||
| 101 | ), | ||
| 102 | 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9', | ||
| 103 | ( map { ( "xmlns:$_" => $ext->{ $_ } ) } keys %{ $ext } ), | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | sub xml { | ||
| 108 | my $self = shift; | ||
| 109 | |||
| 110 | my $xml = XML::Twig::Elt->new( | ||
| 111 | $self->base_element => $self->xml_headers, | ||
| 112 | ( map { | ||
| 113 | $_->as_elt( $self->url_type => $self->url_fields ); | ||
| 114 | } $self->urls->all ), | ||
| 115 | ); | ||
| 116 | $xml->set_pretty_print( $self->pretty ); | ||
| 117 | my $header = '<?xml version="1.0" encoding="UTF-8"?>'; | ||
| 118 | if ( $self->pretty ) { $header .= "\n" } | ||
| 119 | return $header.$xml->sprint(); | ||
| 120 | } | ||
| 121 | |||
| 122 | class_has 'url_type' => ( is => 'rw', isa => Str, default => 'url' ); | ||
| 123 | class_has 'url_fields' => ( | ||
| 124 | is => 'rw', | ||
| 125 | isa => ArrayRef[Str], | ||
| 126 | auto_deref => 1, | ||
| 127 | default => sub { [qw( loc lastmod changefreq priority )] } | ||
| 128 | ); | ||
| 129 | |||
| 130 | sub BUILD { | ||
| 131 | my ( $self, $args ) = @_; | ||
| 132 | |||
| 133 | $self->urls->add_trigger( put => sub { | ||
| 134 | my $self = shift; | ||
| 135 | return if $self->extensions->{ 'mobile' }; | ||
| 136 | for my $url ( @_ ) { | ||
| 137 | if ( $url->has_mobile && $url->mobile ) { | ||
| 138 | $self->extensions->{ 'mobile' } = 'http://www.google.com/schemas/sitemap-mobile/1.0'; | ||
| 139 | return; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } ); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub read { | ||
| 146 | my ( $self, $file ) = @_; | ||
| 147 | |||
| 148 | croak "No filename specified for ".ref( $self )."->read" unless $file; | ||
| 149 | |||
| 150 | # don't try to parse missing or empty files | ||
| 151 | # no errors for this, because we might be creating it | ||
| 152 | return unless -f $file && -s _; | ||
| 153 | |||
| 154 | # don't try to parse very small compressed files | ||
| 155 | # (empty .gz files are 20 bytes) | ||
| 156 | return if $file =~ /\.gz/ && -s $file < 50; | ||
| 157 | |||
| 158 | if ( $file =~ /\.gz$/ ) { | ||
| 159 | croak "IO::Zlib not available, cannot read compressed sitemaps" | ||
| 160 | unless $self->have_zlib; | ||
| 161 | $self->safe_parse( IO::Zlib->new( $file => "rb" ) ); | ||
| 162 | } else { | ||
| 163 | $self->safe_parse( IO::File->new( $file => "r" ) ); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | sub write { | ||
| 168 | my ( $self, $file ) = @_; | ||
| 169 | |||
| 170 | croak "No filename specified for ".ref( $self )."->write" unless $file; | ||
| 171 | |||
| 172 | my $fh; | ||
| 173 | if ( $file =~ /\.gz$/i ) { | ||
| 174 | croak "IO::Zlib not available, cannot write compressed sitemaps" | ||
| 175 | unless $self->have_zlib; | ||
| 176 | $fh = IO::Zlib->new( $file => 'wb9' ); | ||
| 177 | } else { | ||
| 178 | $fh = IO::File->new( $file => 'w' ); | ||
| 179 | } | ||
| 180 | croak "Could not create '$file'" unless $fh; | ||
| 181 | $fh->print( $self->xml ); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub update { | ||
| 185 | my $self = shift; | ||
| 186 | my $data = ( @_ == 1 ) ? shift : { @_ }; | ||
| 187 | my $loc = $data->{ 'loc' } or croak "Can't call ->update without 'loc'"; | ||
| 188 | if ( my $obj = $self->get_url( $loc ) ) { | ||
| 189 | for my $key ( keys %{ $data } ) { | ||
| 190 | next if $key eq 'loc'; | ||
| 191 | $obj->$key( $data->{ $key } ); | ||
| 192 | } | ||
| 193 | return $obj; | ||
| 194 | } else { | ||
| 195 | my $obj = Search::Sitemap::URL->new( $data ); | ||
| 196 | $self->put_url( $obj ); | ||
| 197 | return $obj; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | sub add { | ||
| 202 | my $self = shift; | ||
| 203 | |||
| 204 | my @urls = (); | ||
| 205 | if ( ref $_[0] ) { | ||
| 206 | push( @urls, map { to_SitemapURL( $_ ) } @_ ); | ||
| 207 | } elsif ( $_[0] =~ m{://} ) { | ||
| 208 | push( @urls, map { Search::Sitemap::URL->new( loc => $_ ) } @_ ); | ||
| 209 | } else { | ||
| 210 | push( @urls, Search::Sitemap::URL->new( @_ ) ); | ||
| 211 | } | ||
| 212 | $self->put_urls( @urls ); | ||
| 213 | return ( @urls == 1 ) ? $urls[0] : wantarray ? @urls : \@urls; | ||
| 214 | } | ||
| 215 | |||
| 216 | __PACKAGE__->meta->make_immutable; | ||
| 217 | 1; | ||
| 218 | __END__ | ||