« blib/lib/WWW/SitemapIndex/XML/Sitemap.pm
↑ Summary
blib/lib/WWW/Sitemap/XML/Google/Image/Interface.pm »
| 1 | #ABSTRACT: XML Sitemap index protocol | ||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | package WWW::SitemapIndex::XML; | ||||||||||
| 5 | BEGIN { | ||||||||||
| 6 | 6 | ▶ | $WWW::SitemapIndex::XML::AUTHORITY = 'cpan:AJGB'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | } | ||||||||||
| 8 | 6 | $WWW::SitemapIndex::XML::VERSION = '2.01'; | |||||||||
| 9 | 6 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 6 | extends qw( WWW::Sitemap::XML ); | |||||||||
| 11 | |||||||||||
| 12 | 6 | ▶ | use WWW::SitemapIndex::XML::Sitemap; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 6 | ▶ | use XML::LibXML; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 6 | ▶ | use Scalar::Util qw( blessed ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 6 | ▶ | use WWW::Sitemap::XML::Types qw( SitemapIndexSitemap ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | has '+_check_req_interface' => ( | ||||||||||
| 20 | is => 'ro', | ||||||||||
| 21 | default => sub { | ||||||||||
| 22 | sub { | ||||||||||
| 23 | 75 | ▶ | die 'object does not implement WWW::SitemapIndex::XML::Sitemap::Interface' | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 24 | unless is_SitemapIndexSitemap($_[0]); | ||||||||||
| 25 | } | ||||||||||
| 26 | 9 | ▶ | } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 6 | ); | |||||||||
| 28 | |||||||||||
| 29 | 6 | has '+_entry_class' => ( | |||||||||
| 30 | is => 'ro', | ||||||||||
| 31 | default => 'WWW::SitemapIndex::XML::Sitemap' | ||||||||||
| 32 | ); | ||||||||||
| 33 | |||||||||||
| 34 | has '+_root_ns' => ( | ||||||||||
| 35 | is => 'ro', | ||||||||||
| 36 | default => sub { | ||||||||||
| 37 | { | ||||||||||
| 38 | 9 | ▶ | 'xmlns' => "http://www.sitemaps.org/schemas/sitemap/0.9", | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", | ||||||||||
| 40 | 'xsi:schemaLocation' => join(' ', | ||||||||||
| 41 | 'http://www.sitemaps.org/schemas/sitemap/0.9', | ||||||||||
| 42 | 'http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd' | ||||||||||
| 43 | ), | ||||||||||
| 44 | } | ||||||||||
| 45 | }, | ||||||||||
| 46 | 6 | ); | |||||||||
| 47 | |||||||||||
| 48 | 6 | has '+_root_elem' => ( | |||||||||
| 49 | is => 'ro', | ||||||||||
| 50 | default => 'sitemapindex', | ||||||||||
| 51 | ); | ||||||||||
| 52 | |||||||||||
| 53 | 6 | has '+_entry_elem' => ( | |||||||||
| 54 | is => 'ro', | ||||||||||
| 55 | default => 'sitemap', | ||||||||||
| 56 | ); | ||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | 21 | ▶ | sub sitemaps { shift->_entries } | ||||||||
|
Subroutine
sitemaps: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 60 | |||||||||||
| 61 | |||||||||||
| 62 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||
| 63 | |||||||||||
| 64 | 6 | 1; | |||||||||
| 65 | |||||||||||
| 66 | __END__ | ||||||||||
| 67 | |||||||||||
| 68 | =pod | ||||||||||
| 69 | |||||||||||
| 70 | =encoding UTF-8 | ||||||||||
| 71 | |||||||||||
| 72 | =head1 NAME | ||||||||||
| 73 | |||||||||||
| 74 | WWW::SitemapIndex::XML - XML Sitemap index protocol | ||||||||||
| 75 | |||||||||||
| 76 | =head1 VERSION | ||||||||||
| 77 | |||||||||||
| 78 | version 2.01 | ||||||||||
| 79 | |||||||||||
| 80 | =head1 SYNOPSIS | ||||||||||
| 81 | |||||||||||
| 82 | use WWW::SitemapIndex::XML; | ||||||||||
| 83 | |||||||||||
| 84 | my $index = WWW::SitemapIndex::XML->new(); | ||||||||||
| 85 | |||||||||||
| 86 | # add new sitemaps | ||||||||||
| 87 | $index->add( 'http://mywebsite.com/sitemap1.xml.gz' ); | ||||||||||
| 88 | |||||||||||
| 89 | # or | ||||||||||
| 90 | $index->add( | ||||||||||
| 91 | loc => 'http://mywebsite.com/sitemap1.xml.gz', | ||||||||||
| 92 | lastmod => '2010-11-26', | ||||||||||
| 93 | ); | ||||||||||
| 94 | |||||||||||
| 95 | # or | ||||||||||
| 96 | $index->add( | ||||||||||
| 97 | WWW::SitemapIndex::XML::Sitemap->new( | ||||||||||
| 98 | loc => 'http://mywebsite.com/sitemap1.xml.gz', | ||||||||||
| 99 | lastmod => '2010-11-26', | ||||||||||
| 100 | ) | ||||||||||
| 101 | ); | ||||||||||
| 102 | |||||||||||
| 103 | # read sitemaps from existing sitemap_index.xml file | ||||||||||
| 104 | my @sitemaps = $index->read( 'sitemap_index.xml' ); | ||||||||||
| 105 | |||||||||||
| 106 | # load sitemaps from existing sitemap_index.xml file | ||||||||||
| 107 | $index->load( 'sitemap_index.xml' ); | ||||||||||
| 108 | |||||||||||
| 109 | # get XML::LibXML object | ||||||||||
| 110 | my $xml = $index->as_xml; | ||||||||||
| 111 | |||||||||||
| 112 | print $xml->toString(1); | ||||||||||
| 113 | |||||||||||
| 114 | # write to file | ||||||||||
| 115 | $index->write( 'sitemap_index.xml', my $pretty_print = 1 ); | ||||||||||
| 116 | |||||||||||
| 117 | # write compressed | ||||||||||
| 118 | $index->write( 'sitemap_index.xml.gz' ); | ||||||||||
| 119 | |||||||||||
| 120 | =head1 DESCRIPTION | ||||||||||
| 121 | |||||||||||
| 122 | Read and write sitemap index xml files as defined at L<http://www.sitemaps.org/>. | ||||||||||
| 123 | |||||||||||
| 124 | =head1 METHODS | ||||||||||
| 125 | |||||||||||
| 126 | =head2 add($sitemap|%attrs) | ||||||||||
| 127 | |||||||||||
| 128 | $index->add( | ||||||||||
| 129 | WWW::SitemapIndex::XML::Sitemap->new( | ||||||||||
| 130 | loc => 'http://mywebsite.com/sitemap1.xml.gz', | ||||||||||
| 131 | lastmod => '2010-11-26', | ||||||||||
| 132 | ) | ||||||||||
| 133 | ); | ||||||||||
| 134 | |||||||||||
| 135 | Add the C<$sitemap> object representing single sitemap in the sitemap index. | ||||||||||
| 136 | |||||||||||
| 137 | Accepts blessed objects implementing L<WWW::SitemapIndex::XML::Sitemap::Interface>. | ||||||||||
| 138 | |||||||||||
| 139 | Otherwise the arguments C<%attrs> are passed as-is to create new | ||||||||||
| 140 | L<WWW::SitemapIndex::XML::Sitemap> object. | ||||||||||
| 141 | |||||||||||
| 142 | $index->add( | ||||||||||
| 143 | loc => 'http://mywebsite.com/sitemap1.xml.gz', | ||||||||||
| 144 | lastmod => '2010-11-26', | ||||||||||
| 145 | ); | ||||||||||
| 146 | |||||||||||
| 147 | # single url argument | ||||||||||
| 148 | $index->add( 'http://mywebsite.com/' ); | ||||||||||
| 149 | |||||||||||
| 150 | # is same as | ||||||||||
| 151 | $index->add( loc => 'http://mywebsite.com/sitemap1.xml.gz' ); | ||||||||||
| 152 | |||||||||||
| 153 | Performs basic validation of sitemaps added: | ||||||||||
| 154 | |||||||||||
| 155 | =over | ||||||||||
| 156 | |||||||||||
| 157 | =item * maximum of 50 000 sitemaps in single sitemap | ||||||||||
| 158 | |||||||||||
| 159 | =item * URL no longer then 2048 characters | ||||||||||
| 160 | |||||||||||
| 161 | =item * all URLs should use the same protocol and reside on same host | ||||||||||
| 162 | |||||||||||
| 163 | =back | ||||||||||
| 164 | |||||||||||
| 165 | =head2 sitemaps | ||||||||||
| 166 | |||||||||||
| 167 | my @sitemaps = $index->sitemaps; | ||||||||||
| 168 | |||||||||||
| 169 | Returns a list of all Sitemap objects added to sitemap index. | ||||||||||
| 170 | |||||||||||
| 171 | =head2 load(%sitemap_index_location) | ||||||||||
| 172 | |||||||||||
| 173 | $index->load( location => $sitemap_index_file ); | ||||||||||
| 174 | |||||||||||
| 175 | It is a shortcut for: | ||||||||||
| 176 | |||||||||||
| 177 | $index->add($_) for $index->read( location => $sitemap_index_file ); | ||||||||||
| 178 | |||||||||||
| 179 | Please see L<"read"> for details. | ||||||||||
| 180 | |||||||||||
| 181 | =head2 read(%sitemap_index_location) | ||||||||||
| 182 | |||||||||||
| 183 | # file or url to sitemap index | ||||||||||
| 184 | my @sitemaps = $index->read( location => $file_or_url ); | ||||||||||
| 185 | |||||||||||
| 186 | # file handle | ||||||||||
| 187 | my @sitemaps = $index->read( IO => $fh ); | ||||||||||
| 188 | |||||||||||
| 189 | # xml string | ||||||||||
| 190 | my @sitemaps = $index->read( string => $xml ); | ||||||||||
| 191 | |||||||||||
| 192 | Read the sitemap index from file, URL, open file handle or string and return | ||||||||||
| 193 | the list of L<WWW::SitemapIndex::XML::Sitemap> objects representing | ||||||||||
| 194 | C<E<lt>sitemapE<gt>> elements. | ||||||||||
| 195 | |||||||||||
| 196 | =head2 write($file, $format = 0) | ||||||||||
| 197 | |||||||||||
| 198 | # write to file | ||||||||||
| 199 | $index->write( 'sitemap_index.xml', my $pretty_print = 1); | ||||||||||
| 200 | |||||||||||
| 201 | # or | ||||||||||
| 202 | my $fh = IO::File->new(); | ||||||||||
| 203 | $fh->open('sitemap_index.xml', 'w'); | ||||||||||
| 204 | $index->write( $fh, my $pretty_print = 1); | ||||||||||
| 205 | $cfh->close; | ||||||||||
| 206 | |||||||||||
| 207 | # write compressed | ||||||||||
| 208 | $index->write( 'sitemap_index.xml.gz' ); | ||||||||||
| 209 | |||||||||||
| 210 | Write XML sitemap index to C<$file> - a file name or L<IO::Handle> object. | ||||||||||
| 211 | |||||||||||
| 212 | If file names ends in C<.gz> then the output file will be compressed by | ||||||||||
| 213 | setting compression on xml object - please note that it requires I<libxml2> to | ||||||||||
| 214 | be compiled with I<zlib> support. | ||||||||||
| 215 | |||||||||||
| 216 | Optional C<$format> is passed to C<toFH> or C<toFile> methods | ||||||||||
| 217 | (depending on the type of C<$file>, respectively for file handle and file name) | ||||||||||
| 218 | as described in L<XML::LibXML>. | ||||||||||
| 219 | |||||||||||
| 220 | =head2 as_xml | ||||||||||
| 221 | |||||||||||
| 222 | my $xml = $index->as_xml; | ||||||||||
| 223 | |||||||||||
| 224 | # pretty print | ||||||||||
| 225 | print $xml->toString(1); | ||||||||||
| 226 | |||||||||||
| 227 | # write compressed | ||||||||||
| 228 | $xml->setCompression(8); | ||||||||||
| 229 | $xml->toFile( "sitemap_index.xml.gz" ); | ||||||||||
| 230 | |||||||||||
| 231 | Returns L<XML::LibXML::Document> object representing the sitemap index in XML | ||||||||||
| 232 | format. | ||||||||||
| 233 | |||||||||||
| 234 | The C<E<lt>sitemapE<gt>> elements are built by calling I<as_xml> on all Sitemap | ||||||||||
| 235 | objects added into sitemap index. | ||||||||||
| 236 | |||||||||||
| 237 | =head1 SEE ALSO | ||||||||||
| 238 | |||||||||||
| 239 | L<http://www.sitemaps.org/> | ||||||||||
| 240 | |||||||||||
| 241 | =head1 AUTHOR | ||||||||||
| 242 | |||||||||||
| 243 | Alex J. G. Burzyński <ajgb@cpan.org> | ||||||||||
| 244 | |||||||||||
| 245 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 246 | |||||||||||
| 247 | This software is copyright (c) 2014 by Alex J. G. Burzyński <ajgb@cpan.org>. | ||||||||||
| 248 | |||||||||||
| 249 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 250 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 251 | |||||||||||
| 252 | =cut | ||||||||||