Wikipedia/Entry.pm

filter: Statementstmt 92.6% 101 / 109 Branchbran 90.0% 36 / 40 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 90.0% 18 / 20 Podpod 100% 12 / 12 totaltotal 89.5% 171 / 191 SCAR 31.6
CC
23
Cov
90%
_parse
15.0
new
4.5
language
3.0
CRAP 23.5
↑ Summary Wikipedia.pm »
1 package WWW::Wikipedia::Entry;
2  
3 11 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Text::Autoformat;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use WWW::Wikipedia;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 our $VERSION = '2.03';
10  
11 =head1 NAME
12  
13 WWW::Wikipedia::Entry - A class for representing a Wikipedia Entry
14  
15 =head1 SYNOPSIS
16  
17 my $wiki = WWW::Wikipedia->new();
18 my $entry = $wiki->search( 'Perl' );
19 print $entry->text();
20  
21 my $entry_es = $entry->language( 'es' );
22 print $entry_es->text();
23  
24 =head1 DESCRIPTION
25  
26 WWW::Wikipedia::Entry objects are usually created using the search() method
27 on a WWW::Wikipedia object to search for a term. Once you've got an entry
28 object you can then extract pieces of information from the entry using
29 the following methods.
30  
31 =head1 METHODS
32  
33 =head2 new()
34  
35 You probably won't use this one, it's the constructor that is called
36 behind the scenes with the correct arguments by WWW::Wikipedia::search().
37  
38 =cut
39  
40 sub new {
41 14 ▶ my ( $class, $raw, $src, %ops ) = @_;
Subroutine new: called CC=4 SCAR=15.0
Pod documented
42 14 ▶ return if length( $raw ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $raw == 0 0 14 14
43 14 ▶ my $self = bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0140
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
44 raw => $raw,
45 src => $src,
46 text => '',
47 fulltext => '',
48 cursor => 0,
49 related => [],
50 categories => [],
51 headings => [],
52 languages => {},
53 currentlang => ''
54 },
55 ref( $class ) || $class;
56 14 $self->_parse();
57  
58 # store un-"pretty"-ed version of text
59 14 $self->{ fulltext_basic } = $self->{ fulltext };
60 14 $self->{ text_basic } = $self->{ text };
61  
62 14 ▶ if ($ops{clean_html}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ops{"clean_html"}) 0 14 14
63 0 $self->{ fulltext } = _clean_html( $self->{ fulltext });
64 0 $self->{ text } = _clean_html( $self->{ text });
65 }
66  
67 14 $self->{ fulltext } = _pretty( $self->{ fulltext } );
68 14 $self->{ text } = _pretty( $self->{ text } );
69 14 return ( $self );
70 }
71  
72 =head2 text()
73  
74 The brief text for the entry. This will provide the first paragraph of
75 text; basically everything up to the first heading. Ordinarily this will
76 be what you want to use. When there doesn't appear to be summary text you
77 will be returned the fulltext instead.
78  
79 If text() returns nothing then you probably are looking at a disambiguation
80 entry, and should use related() to lookup more specific entries.
81  
82 =cut
83  
84 sub text {
85 6 ▶ my $self = shift;
Subroutine text: called CC=2 SCAR=6.9
Pod documented
86 6 ▶ return $self->{ text } if $self->{ text };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"text"} 4 2 6
87 2 return $self->fulltext();
88 }
89  
90 =head2 text_basic()
91  
92 The same as C<text()>, but not run through Text::Autoformat.
93  
94 =cut
95  
96 sub text_basic {
97 1 ▶ my $self = shift;
Subroutine text_basic: called CC=2 SCAR=8.1
Pod documented
98 1 ▶ return $self->{ text_basic } if $self->{ text_basic };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"text_basic"} 1 0 1
99 0 return $self->fulltext_basic();
100 }
101  
102 =head2 fulltext()
103  
104 Returns the full text for the entry, which can be extensive.
105  
106 =cut
107  
108 sub fulltext {
109 4 ▶ my $self = shift;
Subroutine fulltext: called CC=1 SCAR=0.0
Pod documented
110 4 return $self->{ fulltext };
111 }
112  
113 =head2 fulltext_basic()
114  
115 The same as C<fulltext()>, but not run through Text::Autoformat.
116  
117 =cut
118  
119 sub fulltext_basic {
120 0 ▶ my $self = shift;
Subroutine fulltext_basic: not called CC=1 SCAR=6.9
Pod documented
121 0 return $self->{ fulltext_basic };
122 }
123  
124  
125 =head2 title()
126  
127 Returns a title of the entry.
128  
129 =cut
130  
131 sub title {
132 2 ▶ my $self = shift;
Subroutine title: called CC=1 SCAR=0.0
Pod documented
133 2 return $self->{ title };
134 }
135  
136 =head2 related()
137  
138 Returns a list of terms in the wikipedia that are mentioned in the
139 entry text.
140  
141 =cut
142  
143 sub related {
144 2 ▶ return ( @{ shift->{ related } } );
Subroutine related: called CC=1 SCAR=0.0
Pod documented
145 }
146  
147 =head2 categories()
148  
149 Returns a list of categories which the entry is part of. So Perl is part
150 of the Programming languages category.
151  
152 =cut
153  
154 sub categories {
155 2 ▶ return ( @{ shift->{ categories } } );
Subroutine categories: called CC=1 SCAR=0.0
Pod documented
156 }
157  
158 =head2 headings()
159  
160 Returns a list of headings used in the entry.
161  
162 =cut
163  
164 sub headings {
165 2 ▶ return ( @{ shift->{ headings } } );
Subroutine headings: called CC=1 SCAR=0.0
Pod documented
166 }
167  
168 =head2 raw()
169  
170 Returns the raw wikitext for the entry.
171  
172 =cut
173  
174 sub raw {
175 11 ▶ my $self = shift;
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
176 11 return $self->{ raw };
177 }
178  
179 =head2 language()
180  
181 With no parameters, it will return the current language of the entry. By
182 specifying a two-letter language code, it will return the same entry in that
183 language, if available.
184  
185 =cut
186  
187 sub language {
188 4 ▶ my $self = shift;
Subroutine language: called CC=3 SCAR=11.0
Pod documented
189 4 my $lang = shift;
190  
191 4 ▶ return $self->{ currentlang } unless defined $lang;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lang 2 2 4
192 2 ▶ return undef unless exists $self->{ languages }->{ $lang };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"languages"}{$lang} 1 1 2
193  
194 1 my $wiki = WWW::Wikipedia->new( language => $lang );
195 1 return $wiki->search( $self->{ languages }->{ $lang } );
196 }
197  
198 =head2 languages()
199  
200 Returns an array of two letter language codes denoting the languages in which
201 this entry is available.
202  
203 =cut
204  
205 sub languages {
206 2 ▶ my $self = shift;
Subroutine languages: called CC=1 SCAR=0.0
Pod documented
207  
208 2 return keys %{ $self->{ languages } };
209 }
210  
211 ## messy internal routine for barebones parsing of wikitext
212  
213 sub _parse {
214 14 ▶ my $self = shift;
Subroutine _parse: called CC=15 SCAR=27.1
215 14 my $raw = $self->{ raw };
216 14 my $src = $self->{ src };
217  
218 # Add current language
219 14 my ( $lang ) = ( $src =~ /http:\/\/(..)/ );
220 14 my $title = ( split( /\//, $src ) )[ -1 ];
221  
222 14 ▶ if( $title =~ m{\?title=} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ /\?title=/) 11 3 14
223 11 ( $title ) = $src =~ m{\?title=([^\&]+)};
224 11 $title =~ s{_}{ }g;
225 }
226  
227 14 $self->{ currentlang } = $lang;
228 14 $self->{ languages }->{ $lang } = $title;
229 14 $self->{ title } = $title;
230  
231 14 ▶ for (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cursor"} < length $raw) 325092 14 325106
232 $self->{ cursor } = 0;
233 $self->{ cursor } < length( $raw );
234 $self->{ cursor }++
235 )
236 {
237  
238 325092 pos( $raw ) = $self->{ cursor };
239  
240 ## [[ ... ]]
241 325092 ▶ if ( $raw =~ /\G\[\[ *(.*?) *\]\]/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($raw =~ /\G\[\[ *(.*?) *\]\]/) { } 2427 322665 325092
elsif ($raw =~ /\G=== *(.*?) *===/) { } 118 322547 322665
elsif ($raw =~ /\G== *(.*?) *==/) { } 98 322449 322547
elsif ($raw =~ /\G'' *(.*?) *''/) { } 543 321906 322449
elsif ($raw =~ /\G\{\{ *(.*?) *\}\}/) { } 1223 320683 321906
242 2427 my $directive = $1;
243 2427 $self->{ cursor } += length( $& ) - 1;
244 2427 ▶ if ( $directive =~ /\:/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($directive =~ /\:/) { } 200 2227 2427
elsif ($directive =~ /\|/) { } 784 1443 2227
245 200 my ( $type, $text ) = split /:/, $directive;
246 200 ▶ if ( lc( $type ) eq 'category' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $type eq "category") 62 138 200
247 62 push( @{ $self->{ categories } }, $text );
248 }
249  
250 # language codes
251 200 ▶ if ( length( $type ) == 2 and lc( $type ) eq $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $type == 2 and lc $type eq $type) 41 159 200
Condition66%2/3
length $type == 2 and lc $type eq $type
!ll&&!rl&&r
159041
MC/DC50%1/2
length $type == 2 and lc $type eq $type
length $type == 2 lc $type eq $type
Truth table66%2/3
A and B
A= length $type == 2B= lc $type eq $type
ABresult
0X0
100
111
252 41 $self->{ languages }->{ $type } = $text;
253 }
254 }
255 elsif ( $directive =~ /\|/ ) {
256 784 my ( $lookup, $name ) = split /\|/, $directive;
257 784 $self->{ fulltext } .= $name;
258 784 ▶ push( @{ $self->{ related } }, $lookup ) if $lookup !~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
unless $lookup =~ /^#/ 779 5 784
259 }
260 else {
261 1443 $self->{ fulltext } .= $directive;
262 1443 push( @{ $self->{ related } }, $directive );
263 }
264 }
265  
266 ## === heading 2 ===
267 elsif ( $raw =~ /\G=== *(.*?) *===/ ) {
268 ### don't bother storing these headings
269 118 $self->{ fulltext } .= $1;
270 118 $self->{ cursor } += length( $& ) - 1;
271 118 next;
272 }
273  
274 ## == heading 1 ==
275 elsif ( $raw =~ /\G== *(.*?) *==/ ) {
276 98 push( @{ $self->{ headings } }, $1 );
277 98 ▶ $self->{ text } = $self->{ fulltext } if !$self->{ seenHeading };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"seenHeading"} 10 88 98
278 98 $self->{ seenHeading } = 1;
279 98 $self->{ fulltext } .= $1;
280 98 $self->{ cursor } += length( $& ) - 1;
281 98 next;
282 }
283  
284 ## '' italics ''
285 elsif ( $raw =~ /\G'' *(.*?) *''/ ) {
286 543 $self->{ fulltext } .= $1;
287 543 $self->{ cursor } += length( $& ) - 1;
288 543 next;
289 }
290  
291 ## {{ disambig }}
292 elsif ( $raw =~ /\G\{\{ *(.*?) *\}\}/ ) {
293 ## ignore for now
294 1223 $self->{ cursor } += length( $& ) - 1;
295 1223 next;
296 }
297  
298 else {
299 320683 $self->{ fulltext } .= substr( $raw, $self->{ cursor }, 1 );
300 }
301 }
302 }
303  
304 # future versions might clean tag contents for some specific ones.
305 sub _clean_html {
306 0 ▶ my $text = shift;
Subroutine _clean_html: not called CC=1 SCAR=6.9
307 # force first letter so that standalone < might be kept
308 0 $text =~ s{<[/a-zA-Z!][^>]+>}{}g;
309 0 return $text;
310 }
311  
312 sub _pretty {
313 28 ▶ my $text = shift;
Subroutine _pretty: called CC=2 SCAR=7.1
314  
315 # Text::Autoformat v1.13 chokes on strings that are one or more "\n"
316 28 ▶ return '' if $text =~ m/^\n+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^\n+$/ 0 28 28
317 28 return autoformat(
318 $text,
319 { left => 0,
320 right => 80,
321 justify => 'left',
322 all => 1
323 }
324 );
325 }
326  
327 =head1 AUTHORS
328  
329 Ed Summers E<lt>ehs@pobox.comE<gt>
330  
331 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
332  
333 =head1 COPYRIGHT AND LICENSE
334  
335 Copyright 2003-2015 by Ed Summers
336  
337 This library is free software; you can redistribute it and/or modify
338 it under the same terms as Perl itself.
339  
340 =cut
341  
342 11 1;
↑ Summary Wikipedia.pm »