blib/lib/WWW/Sitemap/XML.pm

filter: Statementstmt 100% 112 / 112 Branchbran 96.6% 29 / 30 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 19 / 19 Podpod 100% 6 / 6 totaltotal 99.4% 174 / 175 CC 24 SCAR 14.9
CC
24
Cov
99%
read
10.0
_pre_check_add
5.0
write
4.0
CRAP 4.4
« blib/lib/WWW/Sitemap/XML/Google/Video/Player.pm ↑ Summary blib/lib/WWW/Sitemap/XML/Google/Image.pm »
1 #ABSTRACT: XML Sitemap protocol
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package WWW::Sitemap::XML;
5 BEGIN {
6 13 ▶ $WWW::Sitemap::XML::AUTHORITY = 'cpan:AJGB';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 }
8 13 $WWW::Sitemap::XML::VERSION = '2.01';
9 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use WWW::Sitemap::XML::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use XML::LibXML qw(XML_ELEMENT_NODE);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 13 ▶use WWW::Sitemap::XML::Types qw( SitemapURL );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 has '_rootcontainer' => (
19 is => 'ro',
20 traits => [qw( Array )],
21 isa => 'ArrayRef',
22 19 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 13 handles => {
24 _add_entry => 'push',
25 _count_entries => 'count',
26 _entries => 'elements',
27 }
28 );
29  
30 13 has '_first_loc' => (
31 is => 'rw',
32 );
33  
34 has '_check_req_interface' => (
35 is => 'ro',
36 default => sub {
37 sub {
38 75 ▶ die 'object does not implement WWW::Sitemap::XML::URL::Interface'
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless is_SitemapURL($_[0]) 1 74 75
39 unless is_SitemapURL($_[0]);
40 }
41 10 ▶ }
Subroutine __ANON__: called CC=1 SCAR=0.0
42 13 );
43  
44 13 has '_entry_class' => (
45 is => 'ro',
46 default => 'WWW::Sitemap::XML::URL'
47 );
48  
49 has '_root_ns' => (
50 is => 'ro',
51 default => sub {
52 {
53 10 ▶ 'xmlns' => "http://www.sitemaps.org/schemas/sitemap/0.9",
Subroutine __ANON__: called CC=1 SCAR=0.0
54 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
55 'xsi:schemaLocation' => join(' ',
56 'http://www.sitemaps.org/schemas/sitemap/0.9',
57 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'
58 ),
59 'xmlns:image' => "http://www.google.com/schemas/sitemap-image/1.1",
60 'xmlns:video' => "http://www.google.com/schemas/sitemap-video/1.1",
61 'xmlns:mobile' => "http://www.google.com/schemas/sitemap-mobile/1.0",
62 }
63 },
64 13 );
65  
66 13 has '_root_elem' => (
67 is => 'ro',
68 default => 'urlset',
69 );
70  
71 13 has '_entry_elem' => (
72 is => 'ro',
73 default => 'url',
74 );
75  
76 sub _pre_check_add {
77 150 ▶ my ($self, $entry) = @_;
Subroutine _pre_check_add: called CC=5 SCAR=16.1
78  
79 150 $self->_check_req_interface->($entry);
80  
81 148 ▶ die "Single file cannot contain more then 50 000 entries"
Branch: 1 branch
BranchTrueFalseTotal
if $self->_count_entries >= 50000 0 148 148
82 if $self->_count_entries >= 50_000;
83  
84 148 my $loc = $entry->loc;
85  
86 148 ▶ die "URL cannot be longer then 2048 characters"
Branch: 1 branch
BranchTrueFalseTotal
unless length $loc < 2048 2 146 148
87 unless length $loc < 2048;
88  
89 146 my($scheme, $authority) = $loc =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?|;
90 146 my $new = "$scheme://$authority";
91 146 ▶ if ( $self->_count_entries ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_count_entries) { } 130 16 146
92 130 my $first = $self->_first_loc;
93  
94 130 ▶ die "All URLs in same file should use the same protocol and reside on the "
Branch: 1 branch
BranchTrueFalseTotal
unless $first eq $new 6 124 130
95 ."same host: $first, not $new" unless $first eq $new;
96 } else {
97 16 $self->_first_loc( $new );
98 }
99 }
100  
101  
102 sub add {
103 152 ▶ my $self = shift;
Subroutine add: called CC=3 SCAR=11.0
Pod documented
104  
105 152 my $class = $self->_entry_class;
106  
107 152 ▶ my $arg = @_ == 1 && blessed $_[0] ?
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && builtin::blessed($_[0]) ? : 132 20 152
Condition100%3/3
@_ == 1 and builtin::blessed($_[0])
!ll&&!rl&&r
146132
MC/DC100%2/2
@_ == 1 and builtin::blessed($_[0])
@_ == 1 builtin::blessed($_[0])
Truth table100%3/3
A and B
A= @_ == 1B= builtin::blessed($_[0])
ABresult
0X0
100
111
108 shift @_ : $class->new(@_);
109  
110 150 $self->_pre_check_add($arg);
111  
112 140 $self->_add_entry( $arg );
113 }
114  
115  
116 21 ▶sub urls { shift->_entries }
Subroutine urls: called CC=1 SCAR=0.0
Pod documented
117  
118  
119 sub load {
120 14 ▶ my $self = shift;
Subroutine load: called CC=2 SCAR=6.9
Pod documented
121  
122 14 $self->add($_) for $self->read(@_);
123 }
124  
125  
126 sub read {
127 17 ▶ my ($self, %args) = @_;
Subroutine read: called CC=10 SCAR=23.0
Pod documented
128  
129 17 my @entries;
130 17 my $class = $self->_entry_class;
131  
132 17 my $xml = XML::LibXML->load_xml( %args );
133 17 my $doc = $xml->getDocumentElement;
134  
135 17 my @ns = $doc->getNamespaces();
136 17 for my $ns ( @ns ) {
137 48 my $name = $ns->localname;
138 48 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 17 31 48
139  
140 31 $self->_root_ns->{ $ns->nodeName } = $ns->href;
141 }
142  
143 17 for my $url ( $doc->getChildrenByLocalName( $self->_entry_elem ) ) {
144 146 my @childNodes = grep { $_->nodeType == XML_ELEMENT_NODE() } $url->nonBlankChildNodes;
145 146 my %args;
146 146 for my $n ( @childNodes ) {
147 447 my $localname = $n->localname;
148  
149 447 ▶ if ( $localname eq 'image' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($localname eq 'image') { } 4 443 447
elsif ($localname eq 'video') { } 2 441 443
elsif ($localname eq 'mobile') { } 1 440 441
150 4 push @{ $args{images} }, {
151 map {
152 20 $_->localname => $_->textContent
153 }
154 4 grep { $_->nodeType == XML_ELEMENT_NODE() }
155 $n->nonBlankChildNodes
156 };
157 }
158 elsif ( $localname eq 'video' ) {
159 2 my $video = {};
160 2 my @videoChildNodes = grep { $_->nodeType == XML_ELEMENT_NODE() } $n->nonBlankChildNodes;
161  
162 2 for my $cn ( @videoChildNodes ) {
163 10 my $vname = $cn->localname;
164  
165 10 ▶ if ( $vname eq 'player_loc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vname eq 'player_loc') { } 2 8 10
166 $video->{player} = {
167 loc => $cn->textContent,
168 (
169 map {
170 4 $_ => $cn->getAttribute($_)
171 }
172 grep {
173 2 $cn->hasAttribute($_)
174 } qw( allow_embed autoplay )
175 )
176 };
177 }
178 else {
179 8 $video->{ $vname } = $cn->textContent;
180 }
181 }
182  
183 2 push @{ $args{videos} }, $video;
184 }
185 elsif ( $localname eq 'mobile' ) {
186 1 $args{mobile} = 1;
187 }
188 else {
189 440 $args{ $n->localname } = $n->textContent;
190 }
191 }
192 146 push @entries,
193 $class->new( %args );
194 }
195  
196 17 return @entries;
197 }
198  
199  
200 sub write {
201 8 ▶ my ($self, $fh, $format) = @_;
Subroutine write: called CC=4 SCAR=13.9
Pod documented
202  
203 8 ▶ $format ||= 0;
Condition100%2/2
$format ||= 0
l!l
62
MC/DC100%1/1
$format ||= 0
$format
Truth table100%2/2
A
A= $format
Aresult
11
00
204  
205 8 my $writer = 'toFH';
206 8 my $xml = $self->as_xml;
207  
208 8 ▶ unless ( ref $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 4 4 8
209 4 $writer = 'toFile';
210 4 ▶ if ( $fh =~ /\.gz$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh =~ /\.gz$/i) 2 2 4
211 2 $xml->setCompression(8);
212 }
213 }
214  
215 8 $xml->$writer( $fh, $format );
216 }
217  
218  
219 sub as_xml {
220 26 ▶ my $self = shift;
Subroutine as_xml: called CC=4 SCAR=13.9
Pod documented
221  
222 26 my $xml = XML::LibXML->createDocument('1.0','UTF-8');
223 26 my $root = $xml->createElement($self->_root_elem);
224  
225 26 ▶ while (my ($k, $v) = each %{ $self->_root_ns() } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->_root_ns;}) 117 26 143
226 117 $root->setAttribute($k, $v);
227 };
228  
229 26 $root->appendChild($_) for
230 map {
231 164 my $xml = $_->as_xml;
232 164 ▶ blessed $xml ? $xml : XML::LibXML->load_xml(string => $xml)->documentElement()
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($xml) ? : 161 3 164
233 } $self->_entries;
234  
235 26 $xml->setDocumentElement($root);
236  
237 26 return $xml;
238 }
239  
240  
241 13 __PACKAGE__->meta->make_immutable;
242  
243 13 1;
244  
245 __END__
246  
247 =pod
248  
249 =encoding UTF-8
250  
251 =head1 NAME
252  
253 WWW::Sitemap::XML - XML Sitemap protocol
254  
255 =head1 VERSION
256  
257 version 2.01
258  
259 =head1 SYNOPSIS
260  
261 use WWW::Sitemap::XML;
262  
263 my $map = WWW::Sitemap::XML->new();
264  
265 # add new url
266 $map->add( 'http://mywebsite.com/' );
267  
268 # or
269 $map->add(
270 loc => 'http://mywebsite.com/',
271 lastmod => '2010-11-22',
272 changefreq => 'monthly',
273 priority => 1.0,
274 mobile => 1,
275 images => [
276 {
277 loc => 'http://mywebsite.com/image1.jpg',
278 caption => 'Caption 1',
279 title => 'Title 1',
280 license => 'http://www.mozilla.org/MPL/2.0/',
281 geo_location => 'Town, Region',
282 },
283 {
284 loc => 'http://mywebsite.com/image2.jpg',
285 caption => 'Caption 2',
286 title => 'Title 2',
287 license => 'http://www.mozilla.org/MPL/2.0/',
288 geo_location => 'Town, Region',
289 }
290 ],
291 videos => {
292 content_loc => 'http://mywebsite.com/video1.flv',
293 player => {
294 loc => 'http://mywebsite.com/video_player.swf?video=1',
295 allow_embed => "yes",
296 autoplay => "ap=1",
297 },
298 thumbnail_loc => 'http://mywebsite.com/thumbs/1.jpg',
299 title => 'Video Title 1',
300 description => 'Video Description 1',
301 }
302 );
303  
304 # or
305 $map->add(
306 WWW::Sitemap::XML::URL->new(
307 loc => 'http://mywebsite.com/',
308 lastmod => '2010-11-22',
309 changefreq => 'monthly',
310 priority => 1.0,
311 mobile => 1,
312 images => [
313 WWW::Sitemap::XML::Google::Image->new(
314 {
315 loc => 'http://mywebsite.com/image1.jpg',
316 caption => 'Caption 1',
317 title => 'Title 1',
318 license => 'http://www.mozilla.org/MPL/2.0/',
319 geo_location => 'Town, Region',
320 },
321 ),
322 WWW::Sitemap::XML::Google::Image->new(
323 {
324 loc => 'http://mywebsite.com/image2.jpg',
325 caption => 'Caption 2',
326 title => 'Title 2',
327 license => 'http://www.mozilla.org/MPL/2.0/',
328 geo_location => 'Town, Region',
329 }
330 ),
331 ],
332 videos => [
333 WWW::Sitemap::XML::Google::Video->new(
334 content_loc => 'http://mywebsite.com/video1.flv',
335 player => WWW::Sitemap::XML::Google::Video::Player->new(
336 {
337 loc => 'http://mywebsite.com/video_player.swf?video=1',
338 allow_embed => "yes",
339 autoplay => "ap=1",
340 }
341 ),
342 thumbnail_loc => 'http://mywebsite.com/thumbs/1.jpg',
343 title => 'Video Title 1',
344 description => 'Video Description 1',
345 ),
346 ],
347 )
348 );
349  
350 # read URLs from existing sitemap.xml file
351 my @urls = $map->read( location => 'sitemap.xml' );
352  
353 # load urls from existing sitemap.xml file
354 $map->load( location => 'sitemap.xml' );
355  
356 # get XML::LibXML object
357 my $xml = $map->as_xml;
358  
359 print $xml->toString(1);
360  
361 # write to file
362 $map->write( 'sitemap.xml', my $pretty_print = 1 );
363  
364 # write compressed
365 $map->write( 'sitemap.xml.gz' );
366  
367 =head1 DESCRIPTION
368  
369 Read and write sitemap XML files as defined at L<http://www.sitemaps.org/> and
370 with support of Google video, image and mobile extensions described at L<https://support.google.com/webmasters/answer/183668>.
371  
372 =head1 METHODS
373  
374 =head2 add($url|%attrs)
375  
376 $map->add(
377 WWW::Sitemap::XML::URL->new(
378 loc => 'http://mywebsite.com/',
379 lastmod => '2010-11-22',
380 changefreq => 'monthly',
381 priority => 1.0,
382 )
383 );
384  
385 Add the C<$url> object representing single page in the sitemap.
386  
387 Accepts blessed objects implementing L<WWW::Sitemap::XML::URL::Interface>.
388  
389 Otherwise the arguments C<%attrs> are passed as-is to create new
390 L<WWW::Sitemap::XML::URL> object.
391  
392 $map->add(
393 loc => 'http://mywebsite.com/',
394 lastmod => '2010-11-22',
395 changefreq => 'monthly',
396 priority => 1.0,
397 );
398  
399 # single url argument
400 $map->add( 'http://mywebsite.com/' );
401  
402 # is same as
403 $map->add( loc => 'http://mywebsite.com/' );
404  
405 Performs basic validation of URLs added:
406  
407 =over
408  
409 =item * maximum of 50 000 URLs in single sitemap
410  
411 =item * URL no longer then 2048 characters
412  
413 =item * all URLs should use the same protocol and reside on same host
414  
415 =back
416  
417 =head2 urls
418  
419 my @urls = $map->urls;
420  
421 Returns a list of all URL objects added to sitemap.
422  
423 =head2 load(%sitemap_location)
424  
425 $map->load( location => $sitemap_file );
426  
427 It is a shortcut for:
428  
429 $map->add($_) for $map->read( location => $sitemap_file );
430  
431 Please see L<"read"> for details.
432  
433 =head2 read(%sitemap_location)
434  
435 # file or url to sitemap
436 my @urls = $map->read( location => $file_or_url );
437  
438 # file handle
439 my @urls = $map->read( IO => $fh );
440  
441 # XML string
442 my @urls = $map->read( string => $xml );
443  
444 Read the sitemap from file, URL, open file handle or string and return the list of
445 L<WWW::Sitemap::XML::URL> objects representing C<E<lt>urlE<gt>> elements.
446  
447 =head2 write($file, $format = 0)
448  
449 # write to file
450 $map->write( 'sitemap.xml', my $pretty_print = 1);
451  
452 # or
453 my $fh = IO::File->new();
454 $fh->open('sitemap.xml', 'w');
455 $map->write( $fh, my $pretty_print = 1);
456 $cfh->close;
457  
458 # write compressed
459 $map->write( 'sitemap.xml.gz' );
460  
461 Write XML sitemap to C<$file> - a file name or L<IO::Handle> object.
462  
463 If file names ends in C<.gz> then the output file will be compressed by
464 setting compression on XML object - please note that it requires I<libxml2> to
465 be compiled with I<zlib> support.
466  
467 Optional C<$format> is passed to C<toFH> or C<toFile> methods
468 (depending on the type of C<$file>, respectively for file handle and file name)
469 as described in L<XML::LibXML>.
470  
471 =head2 as_xml
472  
473 my $xml = $map->as_xml;
474  
475 # pretty print
476 print $xml->toString(1);
477  
478 # write compressed
479 $xml->setCompression(8);
480 $xml->toFile( 'sitemap.xml.gz' );
481  
482 Returns L<XML::LibXML::Document> object representing the sitemap in XML format.
483  
484 The C<E<lt>urlE<gt>> elements are built by calling I<as_xml> on all URL objects
485 added into sitemap.
486  
487 =head1 SEE ALSO
488  
489 L<WWW::SitemapIndex::XML>
490  
491 L<http://www.sitemaps.org/>
492  
493 L<https://support.google.com/webmasters/answer/183668>
494  
495 =head1 AUTHOR
496  
497 Alex J. G. Burzyński <ajgb@cpan.org>
498  
499 =head1 COPYRIGHT AND LICENSE
500  
501 This software is copyright (c) 2014 by Alex J. G. Burzyński <ajgb@cpan.org>.
502  
503 This is free software; you can redistribute it and/or modify it under
504 the same terms as the Perl 5 programming language system itself.
505  
506 =cut
« blib/lib/WWW/Sitemap/XML/Google/Video/Player.pm ↑ Summary blib/lib/WWW/Sitemap/XML/Google/Image.pm »