File Coverage

blib/lib/WWW/SitemapIndex/XML.pm
Criterion Covered Total %
statement 11 13 84.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 18 88.8


line stmt bran cond sub pod time code
1             #ABSTRACT: XML Sitemap index protocol
2 5     5   383592 use strict;
  5         11  
  5         136  
3 5     5   24 use warnings;
  5         11  
  5         240  
4             package WWW::SitemapIndex::XML;
5             BEGIN {
6 5     5   132 $WWW::SitemapIndex::XML::AUTHORITY = 'cpan:AJGB';
7             }
8             $WWW::SitemapIndex::XML::VERSION = '2.02';
9 5     5   4116 use Moose;
  5         2439410  
  5         35  
10             extends qw( WWW::Sitemap::XML );
11              
12 5     5   39643 use WWW::SitemapIndex::XML::Sitemap;
  0            
  0            
13             use XML::LibXML;
14             use Scalar::Util qw( blessed );
15              
16             use WWW::Sitemap::XML::Types qw( SitemapIndexSitemap );
17              
18              
19             has '+_check_req_interface' => (
20             is => 'ro',
21             default => sub {
22             sub {
23             die 'object does not implement WWW::SitemapIndex::XML::Sitemap::Interface'
24             unless is_SitemapIndexSitemap($_[0]);
25             }
26             }
27             );
28              
29             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             'xmlns' => "http://www.sitemaps.org/schemas/sitemap/0.9",
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             );
47              
48             has '+_root_elem' => (
49             is => 'ro',
50             default => 'sitemapindex',
51             );
52              
53             has '+_entry_elem' => (
54             is => 'ro',
55             default => 'sitemap',
56             );
57              
58              
59             sub sitemaps { shift->_entries }
60              
61              
62             __PACKAGE__->meta->make_immutable;
63              
64             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.02
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