Wikipedia.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 85.0% 17 / 20 Conditioncond 69.2% 9 / 13 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 16 / 16 Podpod 100% 7 / 7 totaltotal 90.6% 126 / 139 SCAR 27.8
CC
16
Cov
92%
search
7.0
new
5.2
random
2.1
CRAP 16.1
« Wikipedia/Entry.pm ↑ Summary
1 package WWW::Wikipedia;
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 Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use WWW::Wikipedia::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use base qw( LWP::UserAgent );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 our $VERSION = '2.03';
13  
14 11 use constant WIKIPEDIA_URL =>
15 11 ▶ 'http://%s.wikipedia.org/w/index.php?title=%s&action=raw';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 use constant WIKIPEDIA_RAND_URL =>
17 11 ▶ 'http://%s.wikipedia.org/wiki/Special:Random';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 WWW::Wikipedia - Automated interface to the Wikipedia
22  
23 =head1 SYNOPSIS
24  
25 use WWW::Wikipedia;
26 my $wiki = WWW::Wikipedia->new();
27  
28 ## search for 'perl'
29 my $result = $wiki->search( 'perl' );
30  
31 ## if the entry has some text print it out
32 if ( $result->text() ) {
33 print $result->text();
34 }
35  
36 ## list any related items we can look up
37 print join( "\n", $result->related() );
38  
39 =head1 DESCRIPTION
40  
41 WWW::Wikipedia provides an automated interface to the Wikipedia
42 L<http://www.wikipedia.org>, which is a free, collaborative, online
43 encyclopedia. This module allows you to search for a topic and return the
44 resulting entry. It also gives you access to related topics which are also
45 available via the Wikipedia for that entry.
46  
47  
48 =head1 INSTALLATION
49  
50 To install this module type the following:
51  
52 perl Makefile.PL
53 make
54 make test
55 make install
56  
57 =head1 METHODS
58  
59 =head2 new()
60  
61 The constructor. You can pass it a two letter language code, or nothing
62 to let it default to 'en'.
63  
64 ## Default: English
65 my $wiki = WWW::Wikipedia->new();
66  
67 ## use the French wiki instead
68 my $wiki = WWW::Wikipedia->new( language => 'fr' );
69  
70 WWW::Wikipedia is a subclass of LWP::UserAgent. If you would
71 like to have more control over the user agent (control timeouts, proxies ...)
72 you have full access.
73  
74 ## set HTTP request timeout
75 my $wiki = WWW::Wikipedia->new();
76 $wiki->timeout( 2 );
77  
78  
79 You can turn off the following of wikipedia redirect directives by passing
80 a false value to C<follow_redirects>.
81  
82 Together with the Wiki markup, some entries include HTML tags.
83 They can be stripped out using the C<clean_html> option:
84  
85 my $wiki = WWW::Wikipedia->new( clean_html => 1 );
86  
87 See C<clean_html> documentation bellow for details.
88  
89 =cut
90  
91 sub new {
92 12 ▶ my ( $class, %opts ) = @_;
Subroutine new: called CC=5 SCAR=16.4
Pod documented
93  
94 12 ▶ my $language = delete $opts{ language } || 'en';
Condition100%2/2
delete $opts{'language'} || 'en'
l!l
39
MC/DC100%1/1
delete $opts{'language'} || 'en'
delete $opts{'language'}
Truth table100%2/2
A
A= delete $opts{'language'}
Aresult
11
00
95 12 my $follow = delete $opts{ follow_redirects };
96 12 ▶ $follow = 1 if !defined $follow;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $follow 12 0 12
97 12 ▶ my $clean_html = delete $opts{ clean_html } || 0;
Condition50%1/2
delete $opts{'clean_html'} || 0
l!l
012
MC/DC0%0/1
delete $opts{'clean_html'} || 0
delete $opts{'clean_html'}
Truth table50%1/2
A
A= delete $opts{'clean_html'}
Aresult
11
00
98  
99 12 my $self = LWP::UserAgent->new( %opts );
100 12 $self->agent( 'WWW::Wikipedia' );
101 12 ▶ bless $self, ref( $class ) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
102  
103 12 $self->language( $language );
104 12 $self->follow_redirects( $follow );
105 12 $self->clean_html( $clean_html );
106 12 $self->parse_head( 0 );
107 12 return $self;
108 }
109  
110 =head2 language()
111  
112 This allows you to get and set the language you want to use. Two letter
113 language codes should be used. The default is 'en'.
114  
115 my $wiki = WWW::Wikipedia->new( language => 'es' );
116
117 # Later on...
118 $wiki->language( 'fr' );
119  
120 =cut
121  
122 sub language {
123 30 ▶ my ( $self, $language ) = @_;
Subroutine language: called CC=2 SCAR=6.9
Pod documented
124 30 ▶ $self->{ language } = $language if $language;
Branch: 1 branch
BranchTrueFalseTotal
if $language 13 17 30
125 30 return $self->{ language };
126 }
127  
128  
129 =head2 clean_html()
130  
131 Allows to get/set if HTML is being stripped out.
132  
133 # set HTML strip
134 $wiki->clean_html( 1 );
135  
136 This option removes all tags and attributes they might have.
137 Their contents, however, is maintained (for now). Comments are
138 also removed.
139  
140 =cut
141  
142 sub clean_html {
143 12 ▶ my ( $self, $bool ) = @_;
Subroutine clean_html: called CC=2 SCAR=7.1
Pod documented
144 12 ▶ $self->{ clean_html } = $bool if defined $bool;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bool 12 0 12
145 12 return $self->{ clean_html };
146 }
147  
148 =head2 follow_redirects()
149  
150 By default, wikipeda redirect directives are followed. Set this to false to
151 turn that off.
152  
153 =cut
154  
155 sub follow_redirects {
156 23 ▶ my ( $self, $value ) = @_;
Subroutine follow_redirects: called CC=2 SCAR=6.9
Pod documented
157 23 ▶ $self->{ follow_redirects } = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 13 10 23
158 23 return $self->{ follow_redirects };
159 }
160  
161 =head2 search()
162  
163 Which performs the search and returns a WWW::Wikipedia::Entry object which
164 you can query further. See WWW::Wikipedia::Entry docs for more info.
165  
166 $entry = $wiki->search( 'Perl' );
167 print $entry->text();
168  
169 If there's a problem connecting to Wikipedia, C<undef> will be returned and the
170 error message will be stored in C<error()>.
171  
172 =cut
173  
174 sub search {
175 12 ▶ my ( $self, $string ) = @_;
Subroutine search: called CC=7 SCAR=19.5
Pod documented
176  
177 12 $self->error( undef );
178  
179 12 ▶ croak( "search() requires you pass in a string" ) if !defined( $string );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 1 11 12
180
181 11 ▶ my $enc_string = utf8::is_utf8( $string )
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($string) ? : 2 9 11
182 ? URI::Escape::uri_escape_utf8( $string )
183 : URI::Escape::uri_escape( $string );
184 11 my $src = sprintf( WIKIPEDIA_URL, $self->language(), $enc_string );
185  
186 11 my $response = $self->get( $src );
187 11 ▶ if ( $response->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 10 1 11
188 my $entry = WWW::Wikipedia::Entry->new( $response->decoded_content(), $src,
189 10 clean_html => $self->{ clean_html } );
190  
191 # look for a wikipedia style redirect and process if necessary
192 # try to catch self-redirects
193 10 ▶ return $self->search( $1 )
Branch: 1 branch
BranchTrueFalseTotal
if $self->follow_redirects and $entry->raw =~ /^#REDIRECT\s*\[\[([^|\]]+)/is and $1 ne $string 2 8 10
Condition83%5/6
$self->follow_redirects and $entry->raw =~ /^#REDIRECT\s*\[\[([^|\]]+)/is and $1 ne $string
!ll&&!rl&&r
802
$self->follow_redirects and $entry->raw =~ /^#REDIRECT\s*\[\[([^|\]]+)/is
!ll&&!rl&&r
172
MC/DC66%2/3
$self->follow_redirects and $entry->raw =~ /^#REDIRECT\s*\[\[([^|\]]+)/is and $1 ne $string
$self->follow_redirects $entry->raw =~ /^#REDIRECT\s*\[\[([^|\]]+)/is $1 ne $string
Truth table75%3/4
A and B and C
A= $self->follow_redirectsB= $entry->raw =~ /^#REDIRECT\s*\[\[([^|\]]+)/isC= $1 ne $string
ABCresult
0XX0
10X0
1100
1111
194 if $self->follow_redirects && $entry->raw() =~ /^#REDIRECT\s*\[\[([^|\]]+)/is && $1 ne $string;
195  
196 8 return ( $entry );
197 }
198 else {
199 1 $self->error( "uhoh, WWW::Wikipedia unable to contact " . $src );
200 1 return undef;
201 }
202  
203 }
204  
205 =head2 random()
206  
207 This method fetches a random wikipedia page.
208  
209 =cut
210  
211 sub random {
212 1 ▶ my ( $self ) = @_;
Subroutine random: called CC=2 SCAR=7.2
Pod documented
213 1 my $src = sprintf( WIKIPEDIA_RAND_URL, $self->language() );
214 1 my $response = $self->get( $src );
215  
216 1 ▶ if ( $response->is_success() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 1 0 1
217 # get the raw version of the current url
218 1 my( $title ) = $response->request->uri =~ m{\.org/wiki/(.+)$};
219 1 $src = sprintf( WIKIPEDIA_URL, $self->language(), $title );
220 1 $response = $self->get( $src );
221 return WWW::Wikipedia::Entry->new( $response->decoded_content(), $src,
222 1 clean_html => $self->{ clean_html } );
223 }
224  
225 0 $self->error( "uhoh, WWW::Wikipedia unable to contact " . $src );
226 0 return;
227 }
228  
229 =head2 error()
230  
231 This is a generic error accessor/mutator. You can retrieve any searching error
232 messages here.
233  
234 =cut
235  
236 sub error {
237 14 ▶ my $self = shift;
Subroutine error: called CC=2 SCAR=6.9
Pod documented
238  
239 14 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 13 1 14
240 13 $self->{ _ERROR } = shift;
241 }
242  
243 14 return $self->{ _ERROR };
244 }
245  
246 =head1 TODO
247  
248 =over 4
249  
250 =item *
251  
252 Be more specific on the HTML clean methodology. For now all tags are removed,
253 keeping only their contents. In the future the behaviour might change
254 accordingly with each specific tag.
255  
256 =item *
257  
258 Watch the development of Special:Export XML formatting, eg:
259 http://en.wikipedia.org/wiki/Special:Export/perl
260  
261 =back
262  
263 =head1 SEE ALSO
264  
265 =over 4
266  
267 =item * LWP::UserAgent
268  
269 =back
270  
271 =head1 REPOSITORY
272  
273 L<https://github.com/edsu/www-wikipedia>
274  
275 =head1 AUTHORS
276  
277 Ed Summers E<lt>ehs@pobox.comE<gt>
278  
279 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
280  
281 =head1 COPYRIGHT AND LICENSE
282  
283 Copyright 2003-2015 by Ed Summers
284  
285 This library is free software; you can redistribute it and/or modify
286 it under the same terms as Perl itself.
287  
288 =cut
289  
290 11 1;
« Wikipedia/Entry.pm ↑ Summary