| 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 | 13 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 13 | $WWW::Sitemap::XML::AUTHORITY = 'cpan:AJGB'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | 13 | $WWW::Sitemap::XML::VERSION = '2.02'; | |||||||||||||||||||||||||||
| 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 | 10 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 75 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | 75 | ▶ | die 'object does not implement WWW::Sitemap::XML::URL::Interface' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | unless is_SitemapURL($_[0]); | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 10 | } | |||||||||||||||||||||||||||
| 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 | 10 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||
| 53 | 10 | 'xmlns' => "http://www.sitemaps.org/schemas/sitemap/0.9", | |||||||||||||||||||||||||||
| 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 | 150 | ▶ | sub _pre_check_add { | ||||||||||||||||||||||||||
|
Subroutine
_pre_check_add: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 77 | 150 | my ($self, $entry) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 150 | $self->_check_req_interface->($entry); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 148 | ▶ | die "Single file cannot contain more then 50 000 entries" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 95 | ."same host: $first, not $new" unless $first eq $new; | ||||||||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||||||
| 97 | 16 | $self->_first_loc( $new ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 152 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 152 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 152 | my $class = $self->_entry_class; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 152 | ▶ | my $arg = @_ == 1 && blessed $_[0] ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 and builtin::blessed($_[0])
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])
| |||||||||||||||||||||||||||||
| 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 | 14 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 14 | $self->add($_) for $self->read(@_); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 17 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 17 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | 8 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 201 | 8 | my ($self, $fh, $format) = @_; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 8 | ▶ | $format ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$format ||= 0
MC/DC100%1/1
$format ||= 0
$format
Truth table100%2/2
A
A= $format
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 8 | my $writer = 'toFH'; | |||||||||||||||||||||||||||
| 206 | 8 | my $xml = $self->as_xml; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 8 | ▶ | unless ( ref $fh ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 4 | $writer = 'toFile'; | |||||||||||||||||||||||||||
| 210 | 4 | ▶ | if ( $fh =~ /\.gz$/i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 2 | $xml->setCompression(8); | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 8 | $xml->$writer( $fh, $format ); | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 26 | ▶ | sub as_xml { | ||||||||||||||||||||||||||
|
Subroutine
as_xml: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 220 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||