blib/lib/WWW/Sitemap/Simple.pm

filter: Statementstmt 100% 88 / 88 Branchbran 86.3% 19 / 22 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 93.7% 135 / 144 SCAR 29.5
CC
19
Cov
95%
_get_xml
5.0
add
4.0
add_params
4.0
CRAP 19.0
↑ Summary Build »
1 package WWW::Sitemap::Simple;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use Class::Accessor::Lite (
8 4 rw => [qw/ urlset indent fatal /],
9 ro => [qw/ url /],
10 4 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our $VERSION = '0.03';
13  
14 4 my $DEFAULT_XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
15 4 my $DEFAULT_INDENT = "\t";
16 4 my @KEYS = qw/ loc lastmod changefreq priority /;
17  
18 4 our $LIMIT_URL_COUNT = 50000;
19 4 our $LIMIT_URL_SIZE = 10_485_760; # byte
20  
21 sub new {
22 13 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
23 13 my %args = @_;
24  
25 13 bless {
26 urlset => {
27 xmlns => $DEFAULT_XMLNS,
28 },
29 indent => $DEFAULT_INDENT,
30 fatal => 1,
31 %args,
32 count => 0,
33 url => +{},
34 }, $class;
35 }
36  
37 sub add {
38 28 ▶ my ($self, $url, $params) = @_;
Subroutine add: called CC=4 SCAR=14.0
Pod documented
39  
40 28 my $id = $self->get_id($url);
41  
42 $self->url->{$id} = {
43 28 ▶ %{$params || +{}},
Branch: 1 branch
BranchTrueFalseTotal
$params or {} 28 0 28
44 loc => $url,
45 };
46  
47 28 $self->{count}++;
48 28 ▶ if ($self->fatal && $self->{count} > $LIMIT_URL_COUNT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fatal and $self->{"count"} > $WWW::Sitemap::Simple::LIMIT_URL_COUNT) 1 27 28
Condition66%2/3
$self->fatal and $self->{"count"} > $WWW::Sitemap::Simple::LIMIT_URL_COUNT
!ll&&!rl&&r
0271
MC/DC50%1/2
$self->fatal and $self->{"count"} > $WWW::Sitemap::Simple::LIMIT_URL_COUNT
$self->fatal $self->{"count"} > $WWW::Sitemap::Simple::LIMIT_URL_COUNT
Truth table66%2/3
A and B
A= $self->fatalB= $self->{"count"} > $WWW::Sitemap::Simple::LIMIT_URL_COUNT
ABresult
0X0
100
111
49 1 croak "too many URL added: no more than $LIMIT_URL_COUNT URLs";
50 }
51  
52  
53 27 return $id;
54 }
55  
56 sub add_params {
57 2 ▶ my ($self, $id, $params) = @_;
Subroutine add_params: called CC=4 SCAR=13.9
Pod documented
58  
59 2 ▶ croak "key is not exists: $id" unless exists $self->{url}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"url"}{$id} 0 2 2
60  
61 2 for my $key (@KEYS) {
62 8 ▶ $self->url->{$id}{$key} = $params->{$key} if exists $params->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{$key} 4 4 8
63 }
64 }
65  
66 sub get_id {
67 28 ▶ my ($self, $url) = @_;
Subroutine get_id: called CC=1 SCAR=0.0
Pod documented
68  
69 28 return md5_hex(__PACKAGE__ . $url);
70 }
71  
72 sub write {
73 11 ▶ my ($self, $file) = @_;
Subroutine write: called CC=3 SCAR=11.0
Pod documented
74  
75 11 my $xml = $self->_get_xml;
76  
77 11 ▶ if ($self->fatal && length $xml > $LIMIT_URL_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fatal and length $xml > $WWW::Sitemap::Simple::LIMIT_URL_SIZE) 1 10 11
Condition66%2/3
$self->fatal and length $xml > $WWW::Sitemap::Simple::LIMIT_URL_SIZE
!ll&&!rl&&r
0101
MC/DC50%1/2
$self->fatal and length $xml > $WWW::Sitemap::Simple::LIMIT_URL_SIZE
$self->fatal length $xml > $WWW::Sitemap::Simple::LIMIT_URL_SIZE
Truth table66%2/3
A and B
A= $self->fatalB= length $xml > $WWW::Sitemap::Simple::LIMIT_URL_SIZE
ABresult
0X0
100
111
78 1 croak "too large xml: no more than $LIMIT_URL_SIZE bytes";
79 }
80  
81 10 $self->_write($file => $xml);
82 }
83  
84 sub _write {
85 10 ▶ my ($self, $file, $xml) = @_;
Subroutine _write: called CC=4 SCAR=13.9
86  
87 10 ▶ if (!$file) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $file) { } 5 5 10
elsif (my $re = ref $file) { } 3 2 5
88 5 STDOUT->print($xml);
89 }
90 elsif (my $re = ref $file) {
91 3 ▶ if ($re eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($re eq 'GLOB') { } 1 2 3
92 1 print $file $xml;
93 }
94 else {
95 2 $file->print($xml);
96 }
97 }
98 else {
99 2 $self->_write_file($file, $xml);
100 }
101 }
102  
103 sub _write_file {
104 2 ▶ my ($self, $file, $xml) = @_;
Subroutine _write_file: called CC=3 SCAR=11.0
105  
106 2 my $fh;
107 2 ▶ if ($file =~ m!\.gz$!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.gz$/i) { } 1 1 2
108 1 require IO::Zlib;
109 1 IO::Zlib->import;
110 1 $fh = IO::Zlib->new($file => 'wb9');
111 }
112 else {
113 1 $fh = IO::File->new($file => 'w');
114 }
115 2 ▶ croak "Could not create '$file'" unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 2 2
116 2 $fh->print($xml);
117 2 $fh->close;
118 }
119  
120 sub _get_xml {
121 11 ▶ my $self = shift;
Subroutine _get_xml: called CC=5 SCAR=16.1
122  
123 11 ▶ my $indent = $self->{indent} || '';
Condition50%1/2
$self->{'indent'} || ''
l!l
110
MC/DC0%0/1
$self->{'indent'} || ''
$self->{'indent'}
Truth table50%1/2
A
A= $self->{'indent'}
Aresult
11
00
124  
125 11 my $xml = $self->_write_xml_header;
126  
127 11 for my $id (
128 11 sort { $self->url->{$a}{loc} cmp $self->url->{$b}{loc} } keys %{$self->url}
129 ) {
130 17 my $item = "$indent<url>\n";
131 17 for my $key (@KEYS) {
132 68 ▶ if ( my $value = $self->url->{$id}{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $self->url->{$id}{$key}) 21 47 68
133 21 $item .= "$indent$indent<$key>$value</$key>\n";
134 }
135 }
136 17 $xml .= "$item$indent</url>\n";
137 }
138  
139 11 $xml .= $self->_write_xml_footer;
140  
141 11 return $xml;
142 }
143  
144 sub _write_xml_header {
145 11 ▶ my ($self) = @_;
Subroutine _write_xml_header: called CC=2 SCAR=6.9
146  
147 11 my $urlset_attr = '';
148 11 for my $key (sort keys %{$self->urlset}) {
149 13 my $value = $self->urlset->{$key};
150 13 $urlset_attr .= qq| $key="$value"|;
151 }
152 11 my $header = <<"_XML_";
153 <?xml version="1.0" encoding="UTF-8"?>
154 <urlset$urlset_attr>
155 _XML_
156 11 return $header;
157 }
158  
159 sub _write_xml_footer {
160 11 ▶ my ($self) = @_;
Subroutine _write_xml_footer: called CC=1 SCAR=0.0
161  
162 11 my $footer = <<"_XML_";
163 </urlset>
164 _XML_
165 11 return $footer;
166 }
167  
168 4 1;
169  
170 __END__
↑ Summary Build »