blib/lib/WWW/Wikipedia/LangTitles.pm

filter: Statementstmt 51.5% 33 / 64 Branchbran 21.4% 3 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 51.1% 45 / 88 CC 10 SCAR 35.5
CC
10
Cov
46%
get_wiki_titles
72.0
make_wiki_url
3.0
BEGIN
1.0
CRAP 34.8
↑ Summary
1 package WWW::Wikipedia::LangTitles;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 require Exporter;
6 1 our @ISA = qw(Exporter);
7 1 our @EXPORT_OK = qw/get_wiki_titles make_wiki_url/;
8 1 our %EXPORT_TAGS = (
9 all => \@EXPORT_OK,
10 );
11 1 our $VERSION = '0.04';
12  
13 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use URI::Escape 'uri_escape_utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use JSON::Parse 'parse_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub make_wiki_url
18 2 ▶{
Subroutine make_wiki_url: called CC=3 SCAR=11.0
Pod documented
19 2 my ($name, $lang) = @_;
20 2 ▶ if (! $lang) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lang) 1 1 2
21 # Defaults to English.
22 1 $lang = 'en';
23 }
24 # Have to say "enwiki" or "jawiki" in the URL, since it can be
25 # "enquote" or something.
26 2 ▶ if ($lang !~ /wiki$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lang =~ /wiki$/) 2 0 2
27 2 $lang .= 'wiki';
28 }
29 2 my $safe_name = $name;
30 2 $safe_name = uri_escape_utf8 ($safe_name);
31 # The URL to get the information from.
32 2 my $url = "https://www.wikidata.org/w/api.php?action=wbgetentities&sites=$lang&titles=$safe_name&props=sitelinks/urls|datatype&format=json";
33 2 return $url;
34 }
35  
36 sub get_wiki_titles
37 0 ▶{
Subroutine get_wiki_titles: not called CC=8 SCAR=42.8
Pod documented
38 0 my ($name, %options) = @_;
39 0 my $lang = $options{lang};
40 0 my $verbose = $options{verbose};
41 0 my $url = make_wiki_url ($name, $lang);
42 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
43 0 print "Getting $name from '$url'.\n";
44 }
45 0 my $ua = LWP::UserAgent->new ();
46 # Tell the server from what software this request originates, in
47 # case this module turns out to be problematic for them somehow.
48 0 my $agent = __PACKAGE__;
49 0 $ua = LWP::UserAgent->new (agent => $agent);
50 0 $ua->default_header (
51 'Accept-Encoding' => scalar HTTP::Message::decodable()
52 );
53 0 my $response = $ua->get ($url);
54 0 ▶ if (! $response->is_success ()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
55 0 carp "Get $url failed: " . $response->status_line ();
56 0 return;
57 }
58 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
59 0 print "$name data was retrieved successfully.\n";
60 }
61 0 my $json = $response->decoded_content ();
62 0 my $data = parse_json ($json);
63 0 my $array = $data->{entities};
64 0 my %lang2title;
65 0 for my $k (keys %$array) {
66 0 my $sitelinks = $array->{$k}->{sitelinks};
67 0 for my $k (keys %$sitelinks) {
68 0 my $lang = $k;
69 # Reject these? This is a legacy of the script that this
70 # used to be, it might be more useful for the CPAN module
71 # not to reject these.
72 0 ▶ if ($lang =~ /wikiversity|simple|commons|wikiquote|wikibooks/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang =~ /wikiversity|simple|commons|wikiquote|wikibooks/) 0 0 0
73 0 next;
74 }
75 0 $lang =~ s/wiki$//;
76 0 $lang2title{$lang} = $sitelinks->{$k}->{title};
77 }
78 }
79 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
80 0 print "$name operations complete.\n";
81 }
82 0 return \%lang2title;
83 }
84  
85 1 1;
↑ Summary