Bot.pm

filter: Statementstmt 74.5% 870 / 1167 Branchbran 52.2% 332 / 636 Conditioncond 37.6% 73 / 194 MC/DCmcdc 13.7% 15 / 109 Subroutinesub 82.5% 66 / 80 Podpod 100% 60 / 60 totaltotal 63.0% 1416 / 2246 CC 421 SCAR 36.2
CC
421
Cov
64%
_do_autoconfig
272.0
login
192.5
upload
90.0
CRAP 37.2
↑ Summary Bot/Constants.pm »
1 package MediaWiki::Bot;
2 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 # ABSTRACT: a high-level bot framework for interacting with MediaWiki wikis
5 51 our $VERSION = '5.007000'; # VERSION
6  
7 51 ▶use HTML::Entities 3.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 51 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 51 ▶use Digest::MD5 2.39 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 51 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 51 ▶use MediaWiki::API 0.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 51 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 51 ▶use MediaWiki::Bot::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 51 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 51 our @EXPORT_OK = @{ $MediaWiki::Bot::Constants::EXPORT_TAGS{all} };
17 51 our %EXPORT_TAGS = ( constants => \@EXPORT_OK );
18  
19 51 ▶use Module::Pluggable search_path => [qw(MediaWiki::Bot::Plugin)], 'require' => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 51 foreach my $plugin (__PACKAGE__->plugins) {
21 #print "Found plugin $plugin\n";
22 0 $plugin->import();
23 }
24  
25  
26 47 ▶sub new {
Subroutine new: called CC=17 SCAR=34.1
Pod documented
27 47 my $package = shift;
28 47 my $agent;
29 my $assert;
30 47 my $operator;
31 47 my $maxlag;
32 47 my $protocol;
33 47 my $host;
34 47 my $path;
35 47 my $login_data;
36 47 my $debug;
37  
38 47 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 47 0 47
39 47 $agent = $_[0]->{agent};
40 47 $assert = $_[0]->{assert};
41 47 $operator = $_[0]->{operator};
42 47 $maxlag = $_[0]->{maxlag};
43 47 $protocol = $_[0]->{protocol};
44 47 $host = $_[0]->{host};
45 47 $path = $_[0]->{path};
46 47 $login_data = $_[0]->{login_data};
47 47 $debug = $_[0]->{debug};
48 }
49 else {
50 0 ▶ warnings::warnif('deprecated', 'Please pass a hashref; this method of calling '
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
51 . 'the constructor is deprecated and will be removed in a future release')
52 if @_;
53 0 $agent = shift;
54 0 $assert = shift;
55 0 $operator = shift;
56 0 $maxlag = shift;
57 0 $protocol = shift;
58 0 $host = shift;
59 0 $path = shift;
60 0 $debug = shift;
61 }
62  
63 47 ▶ $assert =~ s/[&?]assert=// if $assert; # Strip out param part, leaving just the value
Branch: 1 branch
BranchTrueFalseTotal
if $assert 1 46 47
64 47 ▶ $operator =~ s/^User://i if $operator;
Branch: 1 branch
BranchTrueFalseTotal
if $operator 4 43 47
65  
66 47 ▶ if (not $agent and not $operator) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $agent || $operator) { } 0 47 47
elsif (not $agent and $operator) { } 3 88 91
Condition66%2/3
$agent || $operator
l!l&&r!l&&!r
4430
MC/DC0%0/2
$agent || $operator
$agent $operator
Truth table66%2/3
A || B
A= $agentB= $operator
ABresult
1X1
011
000
67 0 carp q{You should provide either a customized user agent string }
68 . q{(see https://meta.wikimedia.org/wiki/User-agent_policy) }
69 . q{or provide your username as `operator'.};
70 }
71 ▶ elsif (not $agent and $operator) {
Condition66%2/3
not $agent and $operator
!ll&&!rl&&r
4403
MC/DC50%1/2
not $agent and $operator
not $agent $operator
Truth table66%2/3
not(A) and B
A= not $agentB= $operator
ABresult
0X0
100
111
72 3 $operator =~ s{^User:}{};
73 3 ▶ $agent = sprintf(
Branch: 1 branch
BranchTrueFalseTotal
defined __PACKAGE__->VERSION ? : 3 0 3
74 'Perl MediaWiki::Bot/%s (%s; [[User:%s]])',
75 (defined __PACKAGE__->VERSION ? __PACKAGE__->VERSION : 'dev'),
76 'https://metacpan.org/MediaWiki::Bot',
77 $operator
78 );
79 }
80  
81 47 my $self = bless({}, $package);
82 47 $self->{errstr} = '';
83 47 ▶ $self->{assert} = $assert if $assert;
Branch: 1 branch
BranchTrueFalseTotal
if $assert 1 46 47
84 47 $self->{operator} = $operator;
85 47 ▶ $self->{debug} = $debug || 0;
Condition50%1/2
$debug || 0
l!l
047
MC/DC0%0/1
$debug || 0
$debug
Truth table50%1/2
A
A= $debug
Aresult
11
00
86 47 ▶ $self->{api} = MediaWiki::API->new({
Branch: 1 branch
BranchTrueFalseTotal
defined $maxlag ? : 0 47 47
87 max_lag => (defined $maxlag ? $maxlag : 5),
88 max_lag_delay => 5,
89 max_lag_retries => 5,
90 retries => 5,
91 retry_delay => 10, # no infinite loops
92 use_http_get => 1, # use HTTP GET to make certain requests cacheable
93 });
94 47 ▶ $self->{api}->{ua}->agent($agent) if defined $agent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $agent 47 0 47
95 47 $self->{mw_version} = undef; # will be set in get_mw_version
96  
97 # Set wiki (handles setting $self->{host} etc)
98 47 $self->set_wiki({
99 protocol => $protocol,
100 host => $host,
101 path => $path,
102 });
103  
104 # Log-in, and maybe autoconfigure
105 47 ▶ if ($login_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($login_data) 0 47 47
106 0 my $success = $self->login($login_data);
107 0 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 0 0
108 0 return $self;
109 }
110 else {
111 0 ▶ carp "Couldn't log in with supplied settings" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
112 0 return;
113 }
114 }
115  
116 47 return $self;
117 }
118  
119  
120 47 ▶sub set_wiki {
Subroutine set_wiki: called CC=20 SCAR=38.3
Pod documented
121 47 my $self = shift;
122 47 my $host;
123 my $path;
124 47 my $protocol;
125  
126 47 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 47 0 47
127 47 $host = $_[0]->{host};
128 47 $path = $_[0]->{path};
129 47 $protocol = $_[0]->{protocol};
130 }
131 else {
132 0 warnings::warnif('deprecated', 'Please pass a hashref; this method of calling '
133 . 'set_wiki is deprecated, and will be removed in a future release');
134 0 $host = shift;
135 0 $path = shift;
136 }
137  
138 # Set defaults
139 47 ▶ $protocol = $self->{protocol} || 'https' unless defined($protocol);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $protocol 43 4 47
Condition50%1/2
$self->{'protocol'} || 'https'
l!l
043
MC/DC0%0/1
$self->{'protocol'} || 'https'
$self->{'protocol'}
Truth table50%1/2
A
A= $self->{'protocol'}
Aresult
11
00
140 47 ▶ $host = $self->{host} || 'en.wikipedia.org' unless defined($host);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 1 46 47
Condition50%1/2
$self->{'host'} || 'en.wikipedia.org'
l!l
01
MC/DC0%0/1
$self->{'host'} || 'en.wikipedia.org'
$self->{'host'}
Truth table50%1/2
A
A= $self->{'host'}
Aresult
11
00
141 47 ▶ $path = $self->{path} || 'w' unless defined($path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 46 1 47
Condition50%1/2
$self->{'path'} || 'w'
l!l
046
MC/DC0%0/1
$self->{'path'} || 'w'
$self->{'path'}
Truth table50%1/2
A
A= $self->{'path'}
Aresult
11
00
142  
143 # Clean up the parts we will build a URL with
144 47 $protocol =~ s,://$,,;
145 47 ▶ if ($host =~ m,^(http|https)(://)?, && !$protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ m[^(http|https)(://)?] and not $protocol) 0 47 47
Condition33%1/3
$host =~ m[^(http|https)(://)?] and not $protocol
!ll&&!rl&&r
4700
MC/DC0%0/2
$host =~ m[^(http|https)(://)?] and not $protocol
$host =~ m[^(http|https)(://)?] not $protocol
Truth table33%1/3
A and not(B)
A= $host =~ m[^(http|https)(://)?]B= not $protocol
ABresult
0X0
100
111
146 0 $protocol = $1;
147 }
148 47 $host =~ s,^https?://,,;
149 47 $host =~ s,/$,,;
150 47 $path =~ s,/$,,;
151  
152 # Invalidate wiki-specific cached data
153 47 ▶ if ( ((defined($self->{host})) and ($self->{host} ne $host))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"host"} and $self->{"host"} ne $host or defined $self->{"path"} and $self->{"path"} ne $path or defined $self->{"protocol"} and $self->{"protocol"} ne $protocol) 0 47 47
Condition33%5/15
defined $self->{"host"} and $self->{"host"} ne $host or defined $self->{"path"} and $self->{"path"} ne $path or defined $self->{"protocol"} and $self->{"protocol"} ne $protocol
l!l&&r!l&&!r
0047
defined $self->{"host"} and $self->{"host"} ne $host or defined $self->{"path"} and $self->{"path"} ne $path
l!l&&r!l&&!r
0047
defined $self->{"host"} and $self->{"host"} ne $host
!ll&&!rl&&r
4700
defined $self->{"path"} and $self->{"path"} ne $path
!ll&&!rl&&r
4700
defined $self->{"protocol"} and $self->{"protocol"} ne $protocol
!ll&&!rl&&r
4700
MC/DC0%0/6
defined $self->{"host"} and $self->{"host"} ne $host or defined $self->{"path"} and $self->{"path"} ne $path or defined $self->{"protocol"} and $self->{"protocol"} ne $protocol
defined $self->{"host"} $self->{"host"} ne $host defined $self->{"path"} $self->{"path"} ne $path defined $self->{"protocol"} $self->{"protocol"} ne $protocol
Truth table6%1/15
A and B or C and D or E and F
A= defined $self->{"host"}B= $self->{"host"} ne $hostC= defined $self->{"path"}D= $self->{"path"} ne $pathE= defined $self->{"protocol"}F= $self->{"protocol"} ne $protocol
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
154 or ((defined($self->{path})) and ($self->{path} ne $path))
155 or ((defined($self->{protocol})) and ($self->{protocol} ne $protocol))
156 ) {
157 0 ▶ delete $self->{ns_data} if $self->{ns_data};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ns_data"} 0 0 0
158 0 ▶ delete $self->{ns_alias_data} if $self->{ns_alias_data};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ns_alias_data"} 0 0 0
159 }
160  
161 47 $self->{protocol} = $protocol;
162 47 $self->{host} = $host;
163 47 $self->{path} = $path;
164  
165 47 ▶ $self->{api}->{config}->{api_url} = $path
Branch: 1 branch
BranchTrueFalseTotal
$path ? : 46 1 47
166 ? "$protocol://$host/$path/api.php"
167 : "$protocol://$host/api.php"; # $path is '', so don't use http://domain.com//api.php
168 47 ▶ warn "Wiki set to " . $self->{api}->{config}{api_url} . "\n" if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 47 47
169  
170 47 return RET_TRUE;
171 }
172  
173  
174 1 ▶sub login {
Subroutine login: called CC=30 SCAR=52.6
Pod documented
175 1 my $self = shift;
176 1 my $username;
177 my $password;
178 1 my $lgdomain;
179 1 my $autoconfig;
180 1 my $basic_auth;
181 1 my $do_sul;
182 1 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 1 0 1
183 1 $username = $_[0]->{username};
184 1 $password = $_[0]->{password};
185 1 ▶ $autoconfig = defined($_[0]->{autoconfig}) ? $_[0]->{autoconfig} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'autoconfig'} ? : 0 1 1
186 1 $basic_auth = $_[0]->{basic_auth};
187 1 ▶ $do_sul = $_[0]->{do_sul} || 0;
Condition50%1/2
$_[0]{'do_sul'} || 0
l!l
01
MC/DC0%0/1
$_[0]{'do_sul'} || 0
$_[0]{'do_sul'}
Truth table50%1/2
A
A= $_[0]{'do_sul'}
Aresult
11
00
188 1 $lgdomain = $_[0]->{lgdomain};
189 }
190 else {
191 0 warnings::warnif('deprecated', 'Please pass a hashref; this method of calling '
192 . 'login is deprecated and will be removed in a future release');
193 0 $username = shift;
194 0 $password = shift;
195 0 $autoconfig = 0;
196 0 $do_sul = 0;
197 }
198  
199 # strip off the "@bot_password_label" suffix, if any
200 1 $self->{username} = (split /@/, $username, 2)[0]; # normal human-readable username
201 1 $self->{login_username} = $username; # to be used for login (includes "@bot_password_label")
202  
203 carp "Logging in over plain HTTP is a bad idea, we would be sending secrets"
204 . " (passwords or cookies) in plaintext over an insecure connection."
205 . " To protect against eavesdroppers, set protocol => 'https'"
206 1 ▶ unless $self->{protocol} eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"protocol"} eq "https" 0 1 1
207  
208 # Handle basic auth first, if needed
209 1 ▶ if ($basic_auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basic_auth) 0 1 1
210 0 ▶ warn 'Applying basic auth credentials' if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 0 0
211 $self->{api}->{ua}->credentials(
212 $basic_auth->{netloc},
213 $basic_auth->{realm},
214 $basic_auth->{uname},
215 $basic_auth->{pass}
216 0 );
217 }
218  
219 1 ▶ if ($self->{host} eq 'secure.wikimedia.org') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"host"} eq "secure.wikimedia.org") 0 1 1
220 0 warnings::warnif('deprecated', 'SSL is now supported on the main Wikimedia Foundation sites. '
221 . 'Use en.wikipedia.org (or whatever) instead of secure.wikimedia.org.');
222 0 return;
223 }
224  
225 1 ▶ if($do_sul) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_sul) 0 1 1
226 0 my $sul_success = $self->_do_sul($password);
227 0 ▶ warn 'Some or all SUL logins failed' if $self->{debug} > 1 and !$sul_success;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 and not $sul_success 0 0 0
Condition0%0/3
$self->{"debug"} > 1 and not $sul_success
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"debug"} > 1 and not $sul_success
$self->{"debug"} > 1 not $sul_success
Truth table0%0/3
A and not(B)
A= $self->{"debug"} > 1B= not $sul_success
ABresult
0X0
100
111
228 }
229  
230 1 my $cookies = ".mediawiki-bot-$username-cookies";
231 1 ▶ if (-r $cookies) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $cookies) 0 1 1
232 0 $self->{api}->{ua}->{cookie_jar}->load($cookies);
233 0 $self->{api}->{ua}->{cookie_jar}->{ignore_discard} = 1;
234 # $self->{api}->{ua}->add_handler("request_send", sub { shift->dump; return });
235  
236 0 ▶ if ($self->_is_loggedin()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_loggedin) 0 0 0
237 0 ▶ $self->_do_autoconfig() if $autoconfig;
Branch: 1 branch
BranchTrueFalseTotal
if $autoconfig 0 0 0
238 0 ▶ warn 'Logged in successfully with cookies' if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 0 0
239 0 return 1; # If we're already logged in, nothing more is needed
240 }
241 }
242  
243 1 ▶ unless ($password) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($password) 0 1 1
244 0 ▶ carp q{Cookies didn't get us logged in, and no password to continue with authentication} if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
245 0 return;
246 }
247  
248 1 my $res;
249 1 RETRY: for (1..2) {
250 # Fetch a login token
251 $res = $self->{api}->api({
252 2 ▶ action => 'query',
Branch: 1 branch
BranchTrueFalseTotal
$res = $self->{"api"}->api({"action", "query", "meta", "tokens", "type", "login"}) or return $self->_handle_api_error 2 0 2
253 meta => 'tokens',
254 type => 'login',
255 }) or return $self->_handle_api_error();
256 2 my $token = $res->{query}->{tokens}->{logintoken};
257  
258 # Do the login
259 $res = $self->{api}->api({
260 action => 'login',
261 lgname => $self->{login_username},
262 2 ▶ lgpassword => $password,
Branch: 1 branch
BranchTrueFalseTotal
$res = $self->{"api"}->api({"action", "login", "lgname", $self->{"login_username"}, "lgpassword", $password, "lgdomain", $lgdomain, "lgtoken", $token}) or return $self->_handle_api_error 2 0 2
263 lgdomain => $lgdomain,
264 lgtoken => $token,
265 }) or return $self->_handle_api_error();
266  
267 2 ▶ last RETRY if $res->{login}->{result} eq 'Success';
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"login"}{"result"} eq "Success" 0 2 2
268 };
269  
270 1 $self->{api}->{ua}->{cookie_jar}->extract_cookies($self->{api}->{response});
271 1 ▶ $self->{api}->{ua}->{cookie_jar}->save($cookies) if (-w($cookies) or -w('.'));
Branch: 1 branch
BranchTrueFalseTotal
if -w $cookies or -w "." 1 0 1
Condition33%1/3
-w $cookies or -w "."
l!l&&r!l&&!r
010
MC/DC0%0/2
-w $cookies or -w "."
-w $cookies -w "."
Truth table33%1/3
A or B
A= -w $cookiesB= -w "."
ABresult
1X1
011
000
272  
273 1 ▶ if ($res->{login}->{result} eq 'Success') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"login"}{"result"} eq "Success") 0 1 1
274 0 ▶ if ($res->{login}->{lgusername} eq $self->{username}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"login"}{"lgusername"} eq $self->{"username"}) 0 0 0
275 0 ▶ $self->_do_autoconfig() if $autoconfig;
Branch: 1 branch
BranchTrueFalseTotal
if $autoconfig 0 0 0
276 0 ▶ warn 'Logged in successfully with password' if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 0 0
277 }
278 }
279  
280 return ((defined($res->{login}->{lgusername})) and
281 (defined($res->{login}->{result})) and
282 ($res->{login}->{lgusername} eq $self->{username}) and
283 1 ▶ ($res->{login}->{result} eq 'Success'));
Condition33%3/9
defined $res->{'login'}{'lgusername'} && defined $res->{'login'}{'result'} && $res->{'login'}{'lgusername'} eq $self->{'username'} && $res->{'login'}{'result'} eq 'Success'
!ll&&!rl&&r
100
defined $res->{"login"}{"lgusername"} and defined $res->{"login"}{"result"} and $res->{"login"}{"lgusername"} eq $self->{"username"}
!ll&&!rl&&r
100
defined $res->{"login"}{"lgusername"} and defined $res->{"login"}{"result"}
!ll&&!rl&&r
100
MC/DC0%0/4
defined $res->{'login'}{'lgusername'} && defined $res->{'login'}{'result'} && $res->{'login'}{'lgusername'} eq $self->{'username'} && $res->{'login'}{'result'} eq 'Success'
defined $res->{"login"}{"lgusername"} defined $res->{"login"}{"result"} $res->{"login"}{"lgusername"} eq $self->{"username"} $res->{'login'}{'result'} eq 'Success'
Truth table20%1/5
A and B and C && D
A= defined $res->{"login"}{"lgusername"}B= defined $res->{"login"}{"result"}C= $res->{"login"}{"lgusername"} eq $self->{"username"}D= $res->{'login'}{'result'} eq 'Success'
ABCDresult
0XXX0
10XX0
110X0
11100
11111
284 }
285  
286 0 ▶sub _do_sul {
Subroutine _do_sul: not called CC=8 SCAR=42.8
287 0 my $self = shift;
288 0 my $password = shift;
289 0 my $debug = $self->{debug}; # Remember these for later
290 0 my $host = $self->{host};
291 0 my $path = $self->{path};
292 0 my $protocol = $self->{protocol};
293 0 my $username = $self->{login_username};
294  
295 0 $self->{debug} = 0; # Turn off debugging for these internal calls
296 0 my @logins; # Keep track of our successes
297 0 my @WMF_projects = qw(
298 en.wikipedia.org
299 en.wiktionary.org
300 en.wikibooks.org
301 en.wikinews.org
302 en.wikiquote.org
303 en.wikisource.org
304 en.wikiversity.org
305 meta.wikimedia.org
306 commons.wikimedia.org
307 species.wikimedia.org
308 incubator.wikimedia.org
309 );
310  
311 0 SUL: foreach my $project (@WMF_projects) { # Could maybe be parallelized
312 0 ▶ print STDERR "Logging in on $project..." if $debug > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 1 0 0 0
313 0 $self->set_wiki({
314 host => $project,
315 });
316 0 my $success = $self->login({
317 username => $username,
318 password => $password,
319 do_sul => 0,
320 autoconfig => 0,
321 });
322 0 ▶ warn ($success ? " OK\n" : " FAILED:\n") if $debug > 1;
Branch: 2 branches
BranchTrueFalseTotal
if $debug > 1 0 0 0
$success ? : 0 0 0
323 warn $self->{api}->{error}->{code} . ': ' . $self->{api}->{error}->{details}
324 0 ▶ if $debug > 1 and !$success;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 1 and not $success 0 0 0
Condition0%0/3
$debug > 1 and not $success
!ll&&!rl&&r
000
MC/DC0%0/2
$debug > 1 and not $success
$debug > 1 not $success
Truth table0%0/3
A and not(B)
A= $debug > 1B= not $success
ABresult
0X0
100
111
325 0 push(@logins, $success);
326 }
327 $self->set_wiki({ # Switch back to original wiki
328 0 protocol => $protocol,
329 host => $host,
330 path => $path,
331 });
332  
333 0 my $sum = sum 0, @logins;
334 0 my $total = scalar @WMF_projects;
335 0 ▶ warn "$sum/$total logins succeeded" if $debug > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $debug > 1 0 0 0
336 0 $self->{debug} = $debug; # Reset debug to it's old value
337  
338 0 return $sum == $total;
339 }
340  
341  
342 0 ▶sub logout {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
343 0 my $self = shift;
344  
345 0 $self->{api}->api({ action => 'logout' });
346 0 return RET_TRUE;
347 }
348  
349  
350 5 ▶sub edit {
Subroutine edit: called CC=20 SCAR=37.9
Pod documented
351 5 my $self = shift;
352 5 my $page;
353 my $text;
354 5 my $summary;
355 5 my $is_minor;
356 5 my $assert;
357 5 my $markasbot;
358 5 my $section;
359 5 my $captcha_id;
360 5 my $captcha_solution;
361  
362 5 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 5 0 5
363 5 $page = $_[0]->{page};
364 5 $text = $_[0]->{text};
365 5 $summary = $_[0]->{summary};
366 5 $is_minor = $_[0]->{minor};
367 5 $assert = $_[0]->{assert};
368 5 $markasbot = $_[0]->{markasbot};
369 5 $section = $_[0]->{section};
370 5 $captcha_id = $_[0]->{captcha_id};
371 5 $captcha_solution = $_[0]->{captcha_solution};
372 }
373 else {
374 0 warnings::warnif('deprecated', 'Please pass a hashref; this method of calling '
375 . 'edit is deprecated, and will be removed in a future release.');
376 0 $page = shift;
377 0 $text = shift;
378 0 $summary = shift;
379 0 $is_minor = shift;
380 0 $assert = shift;
381 0 $markasbot = shift;
382 0 $section = shift;
383 }
384  
385 # Set defaults
386 5 ▶ $summary = 'BOT: Changing page text' unless $summary;
Branch: 1 branch
BranchTrueFalseTotal
unless $summary 1 4 5
387 5 ▶ if ($assert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($assert) { } 1 4 5
388 1 $assert =~ s/^[&?]assert=//;
389 }
390 else {
391 4 $assert = $self->{assert};
392 }
393 5 ▶ $is_minor = 1 unless defined($is_minor);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $is_minor 4 1 5
394 5 ▶ $markasbot = 1 unless defined($markasbot);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $markasbot 5 0 5
395  
396 # Clear any captcha data that might remain from a previous edit attempt
397 5 delete $self->{error}->{captcha};
398 5 ▶ carp 'Need both captcha_id and captcha_solution when editing with a solved CAPTCHA'
Branch: 1 branch
BranchTrueFalseTotal
if defined $captcha_id and not defined $captcha_solution or defined $captcha_solution and not defined $captcha_id 0 5 5
Condition33%3/9
defined $captcha_id and not defined $captcha_solution or defined $captcha_solution and not defined $captcha_id
l!l&&r!l&&!r
005
defined $captcha_id and not defined $captcha_solution
!ll&&!rl&&r
500
defined $captcha_solution and not defined $captcha_id
!ll&&!rl&&r
500
MC/DC0%0/4
defined $captcha_id and not defined $captcha_solution or defined $captcha_solution and not defined $captcha_id
defined $captcha_id not defined $captcha_solution defined $captcha_solution not defined $captcha_id
Truth table14%1/7
A and not(B) or C and not(D)
A= defined $captcha_idB= not defined $captcha_solutionC= defined $captcha_solutionD= not defined $captcha_id
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
399 if (defined $captcha_id and not defined $captcha_solution)
400 or (defined $captcha_solution and not defined $captcha_id);
401  
402 5 my ($edittoken, $lastedit, $tokentime) = $self->_get_edittoken($page);
403 5 ▶ return $self->_handle_api_error() unless $edittoken;
Branch: 1 branch
BranchTrueFalseTotal
unless $edittoken 0 5 5
404  
405 # HTTP::Message will do this eventually as of 6.03 (RT#75592), so we need
406 # to do it here - otherwise, the md5 won't match what eventually is sent to
407 # the server, and the edit will fail - GH#39.
408 # If HTTP::Message becomes unbroken in the future, might have to keep this
409 # workaround for people using 6.03 and other future broken versions.
410 5 $text =~ s{(?<!\r)\n}{\r\n}g;
411 5 my $md5 = md5_hex(encode_utf8($text)); # Pass only bytes to md5_hex()
412 5 ▶ my $hash = {
Branch: 5 branches
BranchTrueFalseTotal
$section ? : 0 5 5
$assert ? : 1 4 5
$is_minor ? : 5 0 5
$captcha_id ? : 0 5 5
$captcha_solution ? : 0 5 5
413 action => 'edit',
414 title => $page,
415 token => $edittoken,
416 text => $text,
417 md5 => $md5, # Guard against data corruption
418 summary => $summary,
419 basetimestamp => $lastedit, # Guard against edit conflicts
420 starttimestamp => $tokentime, # Guard against the page being deleted/moved
421 bot => $markasbot,
422 ( $section ? (section => $section) : ()),
423 ( $assert ? (assert => $assert) : ()),
424 ( $is_minor ? (minor => 1) : (notminor => 1)),
425 ( $captcha_id ? (captchaid => $captcha_id) : ()),
426 ( $captcha_solution ? (captchaword => $captcha_solution) : ()),
427 };
428  
429 ### Actually do the edit
430 5 my $res = $self->{api}->api($hash);
431 5 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 5 0 5
432  
433 0 ▶ if ($res->{edit}->{result} && $res->{edit}->{result} eq 'Failure') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"edit"}{"result"} and $res->{"edit"}{"result"} eq "Failure") 0 0 0
Condition0%0/3
$res->{"edit"}{"result"} and $res->{"edit"}{"result"} eq "Failure"
!ll&&!rl&&r
000
MC/DC0%0/2
$res->{"edit"}{"result"} and $res->{"edit"}{"result"} eq "Failure"
$res->{"edit"}{"result"} $res->{"edit"}{"result"} eq "Failure"
Truth table0%0/3
A and B
A= $res->{"edit"}{"result"}B= $res->{"edit"}{"result"} eq "Failure"
ABresult
0X0
100
111
434 # https://www.mediawiki.org/wiki/API:Edit#CAPTCHAs_and_extension_errors
435 # You need to solve the CAPTCHA, then retry the request with the ID in
436 # this error response and the solution.
437 0 ▶ if (exists $res->{edit}->{captcha}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $res->{"edit"}{"captcha"}) 0 0 0
438 return $self->_handle_api_error({
439 code => ERR_CAPTCHA,
440 details => 'captcharequired: This action requires that a CAPTCHA be solved',
441 captcha => $res->{edit}->{captcha},
442 0 });
443 }
444 0 return $self->_handle_api_error();
445 }
446  
447 0 return $res;
448 }
449  
450  
451 0 ▶sub move {
Subroutine move: not called CC=5 SCAR=34.0
Pod documented
452 0 my $self = shift;
453 0 my $from = shift;
454 0 my $to = shift;
455 0 my $reason = shift;
456 0 my $opts = shift;
457  
458 0 my $hash = {
459 action => 'move',
460 from => $from,
461 to => $to,
462 reason => $reason,
463 };
464 0 ▶ $hash->{movetalk} = $opts->{movetalk} if defined($opts->{movetalk});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"movetalk"} 0 0 0
465 0 ▶ $hash->{noredirect} = $opts->{noredirect} if defined($opts->{noredirect});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"noredirect"} 0 0 0
466 0 ▶ $hash->{movesubpages} = $opts->{movesubpages} if defined($opts->{movesubpages});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"movesubpages"} 0 0 0
467  
468 0 my $res = $self->{api}->edit($hash);
469 0 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
470 0 return $res; # should we return something more useful?
471 }
472  
473  
474 6 ▶sub get_history {
Subroutine get_history: called CC=10 SCAR=23.2
Pod documented
475 6 my $self = shift;
476 6 my $pagename = shift;
477 6 my $additional_params = shift;
478 # for backward-compatibility check for textual params
479 6 ▶ if(ref $additional_params eq '' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $additional_params eq "") 4 2 6
480 4 ▶ if(@_ > 0 || defined $additional_params){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0 or defined $additional_params) { } 4 0 4
Condition66%2/3
@_ > 0 or defined $additional_params
l!l&&r!l&&!r
130
MC/DC0%0/2
@_ > 0 or defined $additional_params
@_ > 0 defined $additional_params
Truth table66%2/3
A or B
A= @_ > 0B= defined $additional_params
ABresult
1X1
011
000
481 4 warnings::warnif('deprecated', 'Please pass a hashref; this method of calling '
482 . 'get_history is deprecated and will be removed in a future release');
483 4 my $rvlimit = $additional_params;
484 4 my $rvstartid = shift;
485 4 my $rvdir = shift;
486 4 $additional_params = {};
487 4 ▶ $additional_params->{'rvlimit'} = $rvlimit if $rvlimit;
Branch: 1 branch
BranchTrueFalseTotal
if $rvlimit 3 1 4
488 4 ▶ $additional_params->{'rvstartid'} = $rvstartid if $rvstartid;
Branch: 1 branch
BranchTrueFalseTotal
if $rvstartid 1 3 4
489 4 ▶ $additional_params->{'rvdir'} = $rvdir if $rvdir;
Branch: 1 branch
BranchTrueFalseTotal
if $rvdir 0 4 4
490 }else{
491 0 $additional_params = {};
492 }
493 }
494 6 my $ready;
495 6 my $filter_params = {%$additional_params};
496 6 my @full_hist;
497 6 ▶ while(!$ready){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ready) 5847 6 5853
498 5847 my @hist = $self->get_history_step_by_step($pagename, $filter_params);
499 5847 ▶ if(@hist == 0 || !defined($filter_params->{'continue'})){
Branch: 1 branch
BranchTrueFalseTotal
if (@hist == 0 or not defined $filter_params->{"continue"}) 6 5841 5847
Condition66%2/3
@hist == 0 or not defined $filter_params->{"continue"}
l!l&&r!l&&!r
065841
MC/DC50%1/2
@hist == 0 or not defined $filter_params->{"continue"}
@hist == 0 not defined $filter_params->{"continue"}
Truth table66%2/3
A or not(B)
A= @hist == 0B= not defined $filter_params->{"continue"}
ABresult
1X1
011
000
500 6 $ready = 1;
501 }
502 5847 push @full_hist, @hist;
503 }
504 6 return @full_hist;
505 }
506  
507  
508 5847 ▶sub get_history_step_by_step {
Subroutine get_history_step_by_step: called CC=6 SCAR=17.9
Pod documented
509 5847 my $self = shift;
510 5847 my $pagename = shift;
511 5847 ▶ my $additional_params = shift // {};
Condition50%1/2
shift() // {}
l!l
58470
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
512 5847 my $query = {
513 action => 'query',
514 prop => 'revisions',
515 titles => $pagename,
516 rvprop => 'ids|timestamp|user|comment|flags',
517 };
518 5847 ▶ while(my ($key, $value) = each %$additional_params){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$additional_params) 17529 5847 23376
519 17529 $query->{$key} = $value;
520 }
521 5847 ▶ $query->{'rvlimit'} = 'max' unless defined $query->{'rvlimit'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query->{"rvlimit"} 1 5846 5847
522  
523 5847 my $res = $self->{api}->api($query);
524 5847 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 5847 5847
525 5847 my ($id) = keys %{ $res->{query}->{pages} };
526 5847 my $array = $res->{query}->{pages}->{$id}->{revisions};
527  
528 5847 my @return;
529 5847 for my $hash (@{$array}) {
530 21254 my $revid = $hash->{revid};
531 21254 my $user = $hash->{user};
532 21254 my ($timestamp_date, $timestamp_time) = split(/T/, $hash->{timestamp});
533 21254 $timestamp_time =~ s/Z$//;
534 21254 my $comment = $hash->{comment};
535 push(
536 @return,
537 {
538 revid => $revid,
539 user => $user,
540 timestamp_date => $timestamp_date,
541 timestamp_time => $timestamp_time,
542 comment => $comment,
543 minor => exists $hash->{minor},
544 21254 });
545 }
546 5847 $additional_params->{'continue'} = $res->{'continue'}{'continue'};
547 5847 $additional_params->{'rvcontinue'} = $res->{'continue'}{'rvcontinue'};
548 5847 return @return;
549 }
550  
551  
552 16 ▶sub get_text {
Subroutine get_text: called CC=12 SCAR=24.9
Pod documented
553 16 my $self = shift;
554 16 my $pagename = shift;
555 16 ▶ unless(defined $pagename){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pagename) 1 15 16
556 1 ▶ warn "get_text(): param \$pagename is not defined.\n" if $self->{'debug'} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 1 1
557 1 return;
558 }
559 15 my $options = shift;
560 # for backward-compatibility: try to read scalars
561 15 ▶ if(ref $options eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $options eq "") 13 2 15
562 13 ▶ if(@_ > 0 || defined $options){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0 or defined $options) { } 2 11 13
Condition100%3/3
@_ > 0 or defined $options
l!l&&r!l&&!r
1111
MC/DC100%2/2
@_ > 0 or defined $options
@_ > 0 defined $options
Truth table100%3/3
A or B
A= @_ > 0B= defined $options
ABresult
1X1
011
000
563 2 warnings::warnif('deprecated', 'Please pass a hashref; this method of calling '
564 . 'get_text is deprecated and will be removed in a future release');
565 2 $options = {
566 'rvstartid' => $options,
567 'rvsection' => shift,
568 };
569 2 ▶ delete $options->{'rvstartid'} unless defined $options->{'rvstartid'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"rvstartid"} 1 1 2
570 2 ▶ delete $options->{'rvsection'} unless defined $options->{'rvsection'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"rvsection"} 1 1 2
571 }else{
572 11 $options = {};
573 }
574 }
575  
576 15 my $hash = {
577 action => 'query',
578 titles => $pagename,
579 prop => 'revisions',
580 rvprop => 'content',
581 };
582 15 for my $key(keys %$options){
583 3 ▶ if(substr($key, 0, 2) eq 'rv'){
Branch: 1 branch
BranchTrueFalseTotal
if (substr($key, 0, 2) eq "rv") 3 0 3
584 3 $hash->{$key} = $options->{$key};
585 }
586 }
587 15 my $res = $self->{api}->api($hash);
588 15 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 15 15
589 15 ($options->{'pageid'}, my $data) = %{ $res->{query}->{pages} };
590  
591 15 ▶ return if $options->{'pageid'} == PAGE_NONEXISTENT;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"pageid"} == -1 1 14 15
592 14 return $data->{revisions}[0]->{'*'}; # the wikitext
593 }
594  
595  
596 2 ▶sub get_id {
Subroutine get_id: called CC=3 SCAR=11.0
Pod documented
597 2 my $self = shift;
598 2 my $pagename = shift;
599  
600 2 my $hash = {
601 action => 'query',
602 titles => $pagename,
603 };
604  
605 2 my $res = $self->{api}->api($hash);
606 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
607 2 my ($id) = %{ $res->{query}->{pages} };
608 2 ▶ return if $id == PAGE_NONEXISTENT;
Branch: 1 branch
BranchTrueFalseTotal
if $id == -1 1 1 2
609 1 return $id;
610 }
611  
612  
613 2 ▶sub get_pages {
Subroutine get_pages: called CC=17 SCAR=30.1
Pod documented
614 2 my $self = shift;
615 2 ▶ my @pages = (ref $_[0] eq 'ARRAY') ? @{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 1 2
616 2 my %return;
617  
618 2 my $hash = {
619 action => 'query',
620 titles => join('|', @pages),
621 prop => 'revisions',
622 rvprop => 'content',
623 };
624  
625 2 my $diff; # Used to track problematic article names
626 2 map { $diff->{$_} = 1; } @pages;
627  
628 2 my $res = $self->{api}->api($hash);
629 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
630  
631 2 foreach my $id (keys %{ $res->{query}->{pages} }) {
632 8 my $page = $res->{query}->{pages}->{$id};
633 8 ▶ if ($diff->{ $page->{title} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$diff{$page->{'title'}}) { } 6 2 8
634 6 $diff->{ $page->{title} }++;
635 }
636 else {
637 2 next;
638 }
639  
640 6 ▶ if (defined($page->{missing})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page->{"missing"}) 2 4 6
641 2 $return{ $page->{title} } = undef;
642 2 next;
643 }
644 4 ▶ if (defined($page->{revisions})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page->{"revisions"}) 4 0 4
645 4 my $revisions = @{ $page->{revisions} }[0]->{'*'};
646 4 ▶ if (!defined $revisions) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $revisions) { } 0 4 4
elsif (length $revisions < 150 and $revisions =~ /\#REDIRECT\s\[\[([^\[\]]+)\]\]/) { } 0 4 4
647 0 $return{ $page->{title} } = $revisions;
648 }
649 ▶ elsif (length($revisions) < 150 && $revisions =~ m/\#REDIRECT\s\[\[([^\[\]]+)\]\]/) { # FRAGILE!
Condition33%1/3
length $revisions < 150 and $revisions =~ /\#REDIRECT\s\[\[([^\[\]]+)\]\]/
!ll&&!rl&&r
400
MC/DC0%0/2
length $revisions < 150 and $revisions =~ /\#REDIRECT\s\[\[([^\[\]]+)\]\]/
length $revisions < 150 $revisions =~ /\#REDIRECT\s\[\[([^\[\]]+)\]\]/
Truth table33%1/3
A and B
A= length $revisions < 150B= $revisions =~ /\#REDIRECT\s\[\[([^\[\]]+)\]\]/
ABresult
0X0
100
111
650 0 my $redirect_to = $1;
651 0 $return{ $page->{title} } = $self->get_text($redirect_to);
652 }
653 else {
654 4 $return{ $page->{title} } = $revisions;
655 }
656 }
657 }
658  
659 2 my $expand = $self->_get_ns_alias_data();
660 # Only for those article names that remained after the first part
661 # If we're here we are dealing most likely with a WP:CSD type of article name
662 2 for my $title (keys %$diff) {
663 8 ▶ if ($diff->{$title} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff->{$title} == 1) 2 6 8
664 2 my @pieces = split(/:/, $title);
665 2 ▶ if (@pieces > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pieces > 1) 2 0 2
666 2 ▶ $pieces[0] = ($expand->{ $pieces[0] } || $pieces[0]);
Condition33%1/3
$$expand{$pieces[0]} || $pieces[0]
l!l&&r!l&&!r
200
MC/DC0%0/2
$$expand{$pieces[0]} || $pieces[0]
$$expand{$pieces[0]} $pieces[0]
Truth table33%1/3
A || B
A= $$expand{$pieces[0]}B= $pieces[0]
ABresult
1X1
011
000
667 2 my $v = $self->get_text(join ':', @pieces);
668 2 ▶ warn "Detected article name that needed expanding $title\n" if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 2 2
669  
670 2 $return{$title} = $v;
671 2 ▶ if (defined $v and $v =~ m/\#REDIRECT\s\[\[([^\[\]]+)\]\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and $v =~ /\#REDIRECT\s\[\[([^\[\]]+)\]\]/) 2 0 2
Condition33%1/3
defined $v and $v =~ /\#REDIRECT\s\[\[([^\[\]]+)\]\]/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $v and $v =~ /\#REDIRECT\s\[\[([^\[\]]+)\]\]/
defined $v $v =~ /\#REDIRECT\s\[\[([^\[\]]+)\]\]/
Truth table33%1/3
A and B
A= defined $vB= $v =~ /\#REDIRECT\s\[\[([^\[\]]+)\]\]/
ABresult
0X0
100
111
672 2 $v = $self->get_text($1);
673 2 $return{$title} = $v;
674 }
675 }
676 }
677 }
678 2 return \%return;
679 }
680  
681  
682 0 ▶sub get_image{
Subroutine get_image: not called CC=8 SCAR=42.8
Pod documented
683 0 my $self = shift;
684 0 my $name = shift;
685 0 my $options = shift;
686  
687 0 my %sizeparams;
688 0 ▶ $sizeparams{iiurlwidth} = $options->{width} if $options->{width};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"width"} 0 0 0
689 0 ▶ $sizeparams{iiurlheight} = $options->{height} if $options->{height};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"height"} 0 0 0
690  
691 my $ref = $self->{api}->api({
692 0 action => 'query',
693 titles => $name,
694 prop => 'imageinfo',
695 iiprop => 'url|size',
696 %sizeparams
697 });
698 0 ▶ return $self->_handle_api_error() unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 0 0
699 0 my ($pageref) = values %{ $ref->{query}->{pages} };
700 0 ▶ return unless defined $pageref->{imageinfo}; # if the image is missing
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pageref->{"imageinfo"} 0 0 0
701  
702 0 ▶ my $url = @{ $pageref->{imageinfo} }[0]->{thumburl} || @{ $pageref->{imageinfo} }[0]->{url};
Condition0%0/3
@{$$pageref{'imageinfo'};}[0]->{'thumburl'} || @{$$pageref{'imageinfo'};}[0]->{'url'}
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$$pageref{'imageinfo'};}[0]->{'thumburl'} || @{$$pageref{'imageinfo'};}[0]->{'url'}
@{$$pageref{'imageinfo'};}[0]->{'thumburl'} @{$$pageref{'imageinfo'};}[0]->{'url'}
Truth table0%0/3
A || B
A= @{$$pageref{'imageinfo'};}[0]->{'thumburl'}B= @{$$pageref{'imageinfo'};}[0]->{'url'}
ABresult
1X1
011
000
703 0 ▶ die "$url should be absolute or something." unless ( $url =~ m{^https?://} );
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^https?://] 0 0 0
704  
705 0 my $response = $self->{api}->{ua}->get($url);
706 0 ▶ return $self->_handle_api_error() unless ( $response->code == 200 );
Branch: 1 branch
BranchTrueFalseTotal
unless $response->code == 200 0 0 0
707 0 return $response->decoded_content;
708 }
709  
710  
711 1 ▶sub revert {
Subroutine revert: called CC=2 SCAR=7.0
Pod documented
712 1 my $self = shift;
713 1 my $pagename = shift;
714 1 my $revid = shift;
715 1 ▶ my $summary = shift || "Reverting to old revision $revid";
Condition50%1/2
shift() || "Reverting to old revision $revid"
l!l
10
MC/DC0%0/1
shift() || "Reverting to old revision $revid"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
716  
717 1 my $text = $self->get_text($pagename, $revid);
718 1 my $res = $self->edit({
719 page => $pagename,
720 text => $text,
721 summary => $summary,
722 });
723  
724 1 return $res;
725 }
726  
727  
728 1 ▶sub undo {
Subroutine undo: called CC=5 SCAR=17.0
Pod documented
729 1 my $self = shift;
730 1 my $page = shift;
731 1 ▶ my $revid = shift || croak "No revid given";
Condition50%1/2
shift() || croak('No revid given')
l!l
10
MC/DC0%0/1
shift() || croak('No revid given')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
732 1 ▶ my $summary = shift || "Reverting revision #$revid";
Condition50%1/2
shift() || "Reverting revision #$revid"
l!l
01
MC/DC0%0/1
shift() || "Reverting revision #$revid"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
733 1 my $after = shift;
734 1 ▶ $summary = "Reverting edits between #$revid & #$after" if defined($after); # Is that clear? Correct?
Branch: 1 branch
BranchTrueFalseTotal
if defined $after 0 1 1
735  
736 1 my ($edittoken, $basetimestamp, $starttimestamp) = $self->_get_edittoken($page);
737 1 my $hash = {
738 action => 'edit',
739 title => $page,
740 undo => $revid,
741 (undoafter => $after)x!! defined $after,
742 summary => $summary,
743 token => $edittoken,
744 starttimestamp => $starttimestamp,
745 basetimestamp => $basetimestamp,
746 };
747  
748 1 my $res = $self->{api}->api($hash);
749 1 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 1 0 1
750 0 return $res;
751 }
752  
753  
754 3 ▶sub get_last {
Subroutine get_last: called CC=3 SCAR=11.0
Pod documented
755 3 my $self = shift;
756 3 my $page = shift;
757 3 my $user = shift;
758  
759 my $res = $self->{api}->api({
760 3 ▶ action => 'query',
Condition100%2/2
$user || ''
l!l
21
MC/DC100%1/1
$user || ''
$user
Truth table100%2/2
A
A= $user
Aresult
11
00
761 titles => $page,
762 prop => 'revisions',
763 rvlimit => 1,
764 rvprop => 'ids|user',
765 rvexcludeuser => $user || '',
766 });
767 3 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 1 2 3
768  
769 2 my (undef, $data) = %{ $res->{query}->{pages} };
770 2 my $revid = $data->{revisions}[0]->{revid};
771 2 return $revid;
772 }
773  
774  
775 1 ▶sub update_rc {
Subroutine update_rc: called CC=6 SCAR=18.3
Pod documented
776 1 warnings::warnif('deprecated', 'update_rc is deprecated, and may be removed '
777 . 'in a future release. Please use recentchanges(), which provides more '
778 . 'data, including rcid');
779 1 my $self = shift;
780 1 ▶ my $limit = shift || 'max';
Condition50%1/2
shift() || 'max'
l!l
10
MC/DC0%0/1
shift() || 'max'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
781 1 my $options = shift;
782  
783 1 my $hash = {
784 action => 'query',
785 list => 'recentchanges',
786 rcnamespace => 0,
787 rclimit => $limit,
788 };
789 1 ▶ $options->{max} = 1 unless $options->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"max"} 1 0 1
790  
791 1 my $res = $self->{api}->list($hash, $options);
792 1 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 1 1
793 1 ▶ return RET_TRUE if not ref $res; # Not a ref when using callback
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 0 1 1
794  
795 1 my @rc_table;
796 1 foreach my $hash (@{$res}) {
797 push(
798 @rc_table,
799 {
800 title => $hash->{title},
801 revid => $hash->{revid},
802 old_revid => $hash->{old_revid},
803 timestamp => $hash->{timestamp},
804 }
805 2 );
806 }
807 1 return @rc_table;
808 }
809  
810  
811 5 ▶sub recentchanges {
Subroutine recentchanges: called CC=12 SCAR=26.2
Pod documented
812 5 my $self = shift;
813 5 my $ns;
814 my $limit;
815 5 my $options;
816 5 my $user;
817 5 my $show;
818 5 ▶ if (ref $_[0] eq 'HASH') { # unpack for new args
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 2 3 5
819 2 my %args = %{ +shift };
820 2 $ns = delete $args{ns};
821 2 $limit = delete $args{limit};
822 2 $user = delete $args{user};
823  
824 2 ▶ if (ref $args{show} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'show'} eq 'HASH') { } 0 2 2
825 0 my @show;
826 0 ▶ while (my ($k, $v) = each %{ $args{show} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$args{"show"};}) 0 0 0
827 0 push @show, '!'x!$v . $k;
828 }
829 0 $show = join '|', @show;
830 }
831 else {
832 2 $show = delete $args{show};
833 }
834  
835 2 $options = shift;
836 }
837 else {
838 3 ▶ $ns = shift || 0;
Condition100%2/2
shift() || 0
l!l
21
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
839 3 ▶ $limit = shift || 50;
Condition100%2/2
shift() || 50
l!l
21
MC/DC100%1/1
shift() || 50
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
840 3 $options = shift;
841 }
842 5 ▶ $ns = join('|', @$ns) if ref $ns eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ns eq "ARRAY" 2 3 5
843  
844 5 my $hash = {
845 action => 'query',
846 list => 'recentchanges',
847 rcnamespace => $ns,
848 rclimit => $limit,
849 rcprop => 'user|comment|timestamp|title|ids',
850 };
851 5 ▶ $hash->{rcuser} = $user if defined $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user 0 5 5
852 5 ▶ $hash->{rcshow} = $show if defined $show;
Branch: 1 branch
BranchTrueFalseTotal
if defined $show 0 5 5
853  
854 5 ▶ $options->{max} = 1 unless $options->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"max"} 5 0 5
855  
856 5 ▶ my $res = $self->{api}->list($hash, $options)
Branch: 1 branch
BranchTrueFalseTotal
my $res = $self->{"api"}->list($hash, $options) or return $self->_handle_api_error 5 0 5
857 or return $self->_handle_api_error();
858 5 ▶ return RET_TRUE unless ref $res; # Not a ref when using callback
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 2 3 5
859 3 return @$res;
860 }
861  
862  
863 2 ▶sub what_links_here {
Subroutine what_links_here: called CC=10 SCAR=23.6
Pod documented
864 2 my $self = shift;
865 2 my $page = shift;
866 2 my $filter = shift;
867 2 my $ns = shift;
868 2 my $options = shift;
869  
870 2 ▶ $ns = join('|', @$ns) if (ref $ns eq 'ARRAY'); # Allow array of namespaces
Branch: 1 branch
BranchTrueFalseTotal
if ref $ns eq "ARRAY" 0 2 2
871 2 ▶ if (defined($filter) and $filter =~ m/(all|redirects|nonredirects)/) { # Verify $filter
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter and $filter =~ /(all|redirects|nonredirects)/) 2 0 2
Condition33%1/3
defined $filter and $filter =~ /(all|redirects|nonredirects)/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $filter and $filter =~ /(all|redirects|nonredirects)/
defined $filter $filter =~ /(all|redirects|nonredirects)/
Truth table33%1/3
A and B
A= defined $filterB= $filter =~ /(all|redirects|nonredirects)/
ABresult
0X0
100
111
872 2 $filter = $1;
873 }
874  
875 # http://en.wikipedia.org/w/api.php?action=query&list=backlinks&bltitle=template:tlx
876 2 my $hash = {
877 action => 'query',
878 list => 'backlinks',
879 bltitle => $page,
880 bllimit => 'max',
881 };
882 2 ▶ $hash->{blnamespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 1 1 2
883 2 ▶ $hash->{blfilterredir} = $filter if $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $filter 2 0 2
884 2 ▶ $options->{max} = 1 unless $options->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"max"} 0 2 2
885  
886 2 my $res = $self->{api}->list($hash, $options);
887 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
888 2 ▶ return RET_TRUE if not ref $res; # When using a callback hook, this won't be a reference
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 1 1 2
889 1 my @links;
890 1 foreach my $hashref (@$res) {
891 34 my $title = $hashref->{title};
892 34 my $redirect = defined($hashref->{redirect});
893 34 push @links, { title => $title, redirect => $redirect };
894 }
895  
896 1 return @links;
897 }
898  
899  
900 2 ▶sub list_transclusions {
Subroutine list_transclusions: called CC=10 SCAR=23.9
Pod documented
901 2 my $self = shift;
902 2 my $page = shift;
903 2 my $filter = shift;
904 2 my $ns = shift;
905 2 my $options = shift;
906  
907 2 ▶ $ns = join('|', @$ns) if (ref $ns eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $ns eq "ARRAY" 0 2 2
908 2 ▶ if (defined($filter) and $filter =~ m/(all|redirects|nonredirects)/) { # Verify $filter
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter and $filter =~ /(all|redirects|nonredirects)/) 2 0 2
Condition33%1/3
defined $filter and $filter =~ /(all|redirects|nonredirects)/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $filter and $filter =~ /(all|redirects|nonredirects)/
defined $filter $filter =~ /(all|redirects|nonredirects)/
Truth table33%1/3
A and B
A= defined $filterB= $filter =~ /(all|redirects|nonredirects)/
ABresult
0X0
100
111
909 2 $filter = $1;
910 }
911  
912 # http://en.wikipedia.org/w/api.php?action=query&list=embeddedin&eititle=Template:Stub
913 2 my $hash = {
914 action => 'query',
915 list => 'embeddedin',
916 eititle => $page,
917 eilimit => 'max',
918 };
919 2 ▶ $hash->{eifilterredir} = $filter if $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $filter 2 0 2
920 2 ▶ $hash->{einamespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 0 2 2
921 2 ▶ $options->{max} = 1 unless $options->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"max"} 0 2 2
922  
923 2 my $res = $self->{api}->list($hash, $options);
924 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
925 2 ▶ return RET_TRUE if not ref $res; # When using a callback hook, this won't be a reference
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 1 1 2
926 1 my @links;
927 1 foreach my $hashref (@$res) {
928 500 my $title = $hashref->{title};
929 500 my $redirect = defined($hashref->{redirect});
930 500 push @links, { title => $title, redirect => $redirect };
931 }
932  
933 1 return @links;
934 }
935  
936  
937 14 ▶sub get_pages_in_category {
Subroutine get_pages_in_category: called CC=9 SCAR=24.8
Pod documented
938 14 my $self = shift;
939 14 my $category = shift;
940 14 my $options = shift;
941  
942 14 ▶ if ($category =~ m/:/) { # It might have a namespace name
Branch: 1 branch
BranchTrueFalseTotal
if ($category =~ /:/) { } 14 0 14
943 14 my ($cat) = split(/:/, $category, 2);
944 14 ▶ if ($cat ne 'Category') { # 'Category' is a canonical name for ns14
Branch: 1 branch
BranchTrueFalseTotal
if ($cat ne "Category") 0 14 14
945 0 my $ns_data = $self->_get_ns_data();
946 0 my $cat_ns_name = $ns_data->{+NS_CATEGORY};
947 0 ▶ if ($cat ne $cat_ns_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cat ne $cat_ns_name) 0 0 0
948 0 $category = "$cat_ns_name:$category";
949 }
950 }
951 }
952 else { # Definitely no namespace name, since there's no colon
953 0 $category = "Category:$category";
954 }
955 14 ▶ warn "Category to fetch is [[$category]]" if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 14 14
956  
957 14 my $hash = {
958 action => 'query',
959 list => 'categorymembers',
960 cmtitle => $category,
961 cmlimit => 'max',
962 };
963 14 ▶ $options->{max} = 1 unless defined($options->{max});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"max"} 0 14 14
964 14 ▶ delete($options->{max}) if $options->{max} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"max"} == 0 1 13 14
965  
966 14 my $res = $self->{api}->list($hash, $options);
967 14 ▶ return RET_TRUE if not ref $res; # Not a hashref when using callback
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 1 13 14
968 13 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 13 13
969  
970 13 return map { $_->{title} } @$res;
971 }
972  
973  
974 { # Instead of using the state pragma, use a bare block
975 51 my %data;
976  
977 14 ▶ sub get_all_pages_in_category {
Subroutine get_all_pages_in_category: called CC=7 SCAR=19.5
Pod documented
978 14 my $self = shift;
979 14 my $base_category = shift;
980 14 my $options = shift;
981 14 ▶ $options->{max} = 0 unless defined($options->{max});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"max"} 1 13 14
982  
983 14 my @first = $self->get_pages_in_category($base_category, $options);
984 14 ▶ %data = () unless $_[0]; # This is a special flag for internal use.
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 3 11 14
985 # It marks a call to this method as being
986 # internal. Since %data is a fake state variable,
987 # it needs to be cleared for every *external*
988 # call, but not cleared when the call is recursive.
989  
990 14 my $ns_data = $self->_get_ns_data();
991 14 my $cat_ns_name = $ns_data->{+NS_CATEGORY};
992  
993 14 foreach my $page (@first) {
994 6470 ▶ if ($page =~ m/^$cat_ns_name:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page =~ /^$cat_ns_name:/) { } 18 6452 6470
995 18 ▶ if (!exists($data{$page})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $data{$page}) { } 11 7 18
996 11 $data{$page} = '';
997 11 my @pages = $self->get_all_pages_in_category($page, $options, 1);
998 11 foreach (@pages) {
999 25289 $data{$_} = '';
1000 }
1001 }
1002 else {
1003 7 $data{$page} = '';
1004 }
1005 }
1006 else {
1007 6452 $data{$page} = '';
1008 }
1009 }
1010 14 return keys %data;
1011 }
1012 } # This ends the bare block around get_all_pages_in_category()
1013  
1014  
1015 51 ▶sub get_all_categories {
Subroutine get_all_categories: called CC=4 SCAR=13.9
Pod documented
1016 2 my $self = shift;
1017 2 my $options = shift;
1018  
1019 2 my $query = {
1020 action => 'query',
1021 list => 'allcategories',
1022 };
1023  
1024 2 ▶ if ( defined $options && $options->{'max'} == '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options and $options->{"max"} == 0) 1 1 2
Condition66%2/3
defined $options and $options->{"max"} == 0
!ll&&!rl&&r
101
MC/DC50%1/2
defined $options and $options->{"max"} == 0
defined $options $options->{"max"} == 0
Truth table66%2/3
A and B
A= defined $optionsB= $options->{"max"} == 0
ABresult
0X0
100
111
1025 1 $query->{'aclimit'} = 'max';
1026 }
1027  
1028 2 my $res = $self->{api}->api($query);
1029 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
1030  
1031 2 return map { $_->{'*'} } @{ $res->{'query'}->{'allcategories'} };
1032 }
1033  
1034  
1035 2 ▶sub linksearch {
Subroutine linksearch: called CC=7 SCAR=19.9
Pod documented
1036 2 my $self = shift;
1037 2 my $link = shift;
1038 2 my $ns = shift;
1039 2 my $prot = shift;
1040 2 my $options = shift;
1041  
1042 2 ▶ $ns = join('|', @$ns) if (ref $ns eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $ns eq "ARRAY" 0 2 2
1043  
1044 2 my $hash = {
1045 action => 'query',
1046 list => 'exturlusage',
1047 euprop => 'url|title',
1048 euquery => $link,
1049 eulimit => 'max',
1050 };
1051 2 ▶ $hash->{eunamespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 0 2 2
1052 2 ▶ $hash->{euprotocol} = $prot if $prot;
Branch: 1 branch
BranchTrueFalseTotal
if $prot 0 2 2
1053 2 ▶ $options->{max} = 1 unless $options->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"max"} 0 2 2
1054  
1055 2 my $res = $self->{api}->list($hash, $options);
1056 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
1057 2 ▶ return RET_TRUE if not ref $res; # When using a callback hook, this won't be a reference
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 1 1 2
1058  
1059 1 return map {{
1060 url => $_->{url},
1061 title => $_->{title},
1062 147 }} @$res;
1063  
1064 }
1065  
1066  
1067 0 ▶sub purge_page {
Subroutine purge_page: not called CC=5 SCAR=34.0
Pod documented
1068 0 my $self = shift;
1069 0 my $page = shift;
1070  
1071 0 my $hash;
1072 0 ▶ if (ref $page eq 'ARRAY') { # If it is an array reference...
Branch: 1 branch
BranchTrueFalseTotal
if (ref $page eq 'ARRAY') { } 0 0 0
1073 0 $hash = {
1074 action => 'purge',
1075 titles => join('|', @$page), # dereference it and purge all those titles
1076 };
1077 }
1078 else { # Just one page
1079 0 $hash = {
1080 action => 'purge',
1081 titles => $page,
1082 };
1083 }
1084  
1085 0 my $res = $self->{api}->api($hash);
1086 0 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
1087 0 my $success = 0;
1088 0 foreach my $hashref (@{ $res->{purge} }) {
1089 0 ▶ $success++ if exists $hashref->{purged};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hashref->{"purged"} 0 0 0
1090 }
1091 0 return $success;
1092 }
1093  
1094  
1095 5 ▶sub get_namespace_names {
Subroutine get_namespace_names: called CC=2 SCAR=7.0
Pod documented
1096 5 my $self = shift;
1097 my $res = $self->{api}->api({
1098 5 action => 'query',
1099 meta => 'siteinfo',
1100 siprop => 'namespaces',
1101 });
1102 5 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 5 5
1103 225 return map { $_ => $res->{query}->{namespaces}->{$_}->{'*'} }
1104 5 keys %{ $res->{query}->{namespaces} };
1105 }
1106  
1107  
1108 3 ▶sub image_usage {
Subroutine image_usage: called CC=11 SCAR=26.5
Pod documented
1109 3 my $self = shift;
1110 3 my $image = shift;
1111 3 my $ns = shift;
1112 3 my $filter = shift;
1113 3 my $options = shift;
1114  
1115 3 ▶ if ($image !~ m/^File:|Image:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image =~ /^File:|Image:/) 0 3 3
1116 0 warnings::warnif('deprecated', q{Please include the canonical File: }
1117 . q{namespace in the image name. If you don't, MediaWiki::Bot might }
1118 . q{incur a network round-trip to get the localized namespace name});
1119 0 my $ns_data = $self->_get_ns_data();
1120 0 my $file_ns_name = $ns_data->{+NS_FILE};
1121 0 ▶ if ($image !~ m/^\Q$file_ns_name\E:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image =~ /^\Q$file_ns_name\E:/) 0 0 0
1122 0 $image = "$file_ns_name:$image";
1123 }
1124 }
1125  
1126 3 ▶ $options->{max} = 1 unless defined($options->{max});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"max"} 1 2 3
1127 3 ▶ delete($options->{max}) if $options->{max} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"max"} == 0 0 3 3
1128  
1129 3 ▶ $ns = join('|', @$ns) if (ref $ns eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $ns eq "ARRAY" 0 3 3
1130  
1131 3 my $hash = {
1132 action => 'query',
1133 list => 'imageusage',
1134 iutitle => $image,
1135 iulimit => 'max',
1136 };
1137 3 ▶ $hash->{iunamespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 0 3 3
1138 3 ▶ if (defined($filter) and $filter =~ m/(all|redirects|nonredirects)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter and $filter =~ /(all|redirects|nonredirects)/) 1 2 3
Condition66%2/3
defined $filter and $filter =~ /(all|redirects|nonredirects)/
!ll&&!rl&&r
201
MC/DC50%1/2
defined $filter and $filter =~ /(all|redirects|nonredirects)/
defined $filter $filter =~ /(all|redirects|nonredirects)/
Truth table66%2/3
A and B
A= defined $filterB= $filter =~ /(all|redirects|nonredirects)/
ABresult
0X0
100
111
1139 1 $hash->{'iufilterredir'} = $1;
1140 }
1141 3 my $res = $self->{api}->list($hash, $options);
1142 3 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 3 3
1143 3 ▶ return RET_TRUE if not ref $res; # When using a callback hook, this won't be a reference
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 1 2 3
1144  
1145 2 return map { $_->{title} } @$res;
1146 }
1147  
1148  
1149 3 ▶sub global_image_usage {
Subroutine global_image_usage: called CC=13 SCAR=26.4
Pod documented
1150 3 my $self = shift;
1151 3 my $image = shift;
1152 3 my $limit = shift;
1153 3 my $filterlocal = shift;
1154 3 ▶ $limit = defined $limit ? $limit : 500;
Branch: 1 branch
BranchTrueFalseTotal
defined $limit ? : 2 1 3
1155  
1156 3 ▶ if ($image !~ m/^File:|Image:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image =~ /^File:|Image:/) 1 2 3
1157 1 my $ns_data = $self->_get_ns_data();
1158 1 my $image_ns_name = $ns_data->{+NS_FILE};
1159 1 ▶ if ($image !~ m/^\Q$image_ns_name\E:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image =~ /^\Q$image_ns_name\E:/) 1 0 1
1160 1 $image = "$image_ns_name:$image";
1161 }
1162 }
1163  
1164 3 my @data;
1165 my $cont;
1166 3 ▶ while ($limit ? scalar @data < $limit : 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($limit ? scalar @data < $limit : 1) 3 0 3
$limit ? : 3 0 3
1167 3 my $hash = {
1168 action => 'query',
1169 prop => 'globalusage',
1170 titles => $image,
1171 # gufilterlocal => $filterlocal,
1172 gulimit => 'max',
1173 };
1174 3 ▶ $hash->{gufilterlocal} = $filterlocal if $filterlocal;
Branch: 1 branch
BranchTrueFalseTotal
if $filterlocal 1 2 3
1175 3 ▶ $hash->{gucontinue} = $cont if $cont;
Branch: 1 branch
BranchTrueFalseTotal
if $cont 0 3 3
1176  
1177 3 my $res = $self->{api}->api($hash);
1178 3 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 3 3
1179  
1180 3 $cont = $res->{'query-continue'}->{globalusage}->{gucontinue};
1181 3 ▶ warn "gucontinue: $cont\n" if $cont and $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $cont and $self->{"debug"} > 1 0 3 3
Condition33%1/3
$cont and $self->{"debug"} > 1
!ll&&!rl&&r
300
MC/DC0%0/2
$cont and $self->{"debug"} > 1
$cont $self->{"debug"} > 1
Truth table33%1/3
A and B
A= $contB= $self->{"debug"} > 1
ABresult
0X0
100
111
1182 3 my $page_id = (keys %{ $res->{query}->{pages} })[0];
1183 3 my $results = $res->{query}->{pages}->{$page_id}->{globalusage};
1184 3 push @data, @$results;
1185 3 ▶ last unless $cont;
Branch: 1 branch
BranchTrueFalseTotal
unless $cont 3 0 3
1186 }
1187  
1188 3 ▶ return @data > $limit
Branch: 1 branch
BranchTrueFalseTotal
@data > $limit ? : 1 2 3
1189 ? @data[0 .. $limit-1]
1190 : @data;
1191 }
1192  
1193  
1194 1 ▶sub links_to_image {
Subroutine links_to_image: called CC=1 SCAR=0.0
Pod documented
1195 1 warnings::warnif('deprecated', 'links_to_image is an alias of image_usage; '
1196 . 'please use the new name');
1197 1 my $self = shift;
1198 1 return $self->image_usage($_[0]);
1199 }
1200  
1201  
1202 4 ▶sub is_blocked {
Subroutine is_blocked: called CC=4 SCAR=14.1
Pod documented
1203 4 my $self = shift;
1204 4 my $user = shift;
1205  
1206 # http://en.wikipedia.org/w/api.php?action=query&meta=blocks&bkusers=$user&bklimit=1&bkprop=id
1207 4 my $hash = {
1208 action => 'query',
1209 list => 'blocks',
1210 bkusers => $user,
1211 bklimit => 1,
1212 bkprop => 'id',
1213 };
1214 4 my $res = $self->{api}->api($hash);
1215 4 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 4 4
1216  
1217 4 my $number = scalar @{ $res->{query}->{blocks} }; # The number of blocks returned
1218 4 ▶ if ($number == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($number == 1) { } 2 2 4
elsif ($number == 0) { } 2 0 2
1219 2 return RET_TRUE;
1220 }
1221 elsif ($number == 0) {
1222 2 return RET_FALSE;
1223 }
1224 else {
1225 0 confess "This query should return at most one result, but the API returned more than that.";
1226 }
1227 }
1228  
1229  
1230 2 ▶sub test_blocked { # For backwards-compatibility
Subroutine test_blocked: called CC=1 SCAR=0.0
Pod documented
1231 2 warnings::warnif('deprecated', 'test_blocked is an alias of is_blocked; '
1232 . 'please use the new name. This alias might be removed in a future release');
1233 2 return (is_blocked(@_));
1234 }
1235  
1236  
1237 7 ▶sub test_image_exists {
Subroutine test_image_exists: called CC=15 SCAR=27.5
Pod documented
1238 7 my $self = shift;
1239 7 my $image = shift;
1240  
1241 7 my $multi;
1242 7 ▶ if (ref $image eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $image eq "ARRAY") 1 6 7
1243 1 $multi = $image; # so we know to return a hash/scalar & keep track of order
1244 1 $image = join('|', @$image);
1245 }
1246  
1247 my $res = $self->{api}->api({
1248 7 action => 'query',
1249 titles => $image,
1250 iilimit => 1,
1251 prop => 'imageinfo'
1252 });
1253 7 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 7 7
1254  
1255 7 my @sorted_ids;
1256 7 ▶ if ($multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi) { } 1 6 7
1257 1 my %mapped;
1258 $mapped{ $res->{query}->{pages}->{$_}->{title} } = $_
1259 1 for (keys %{ $res->{query}->{pages} });
1260 1 foreach my $file ( @$multi ) {
1261 3 unshift @sorted_ids, $mapped{$file};
1262 }
1263 }
1264 else {
1265 6 push @sorted_ids, keys %{ $res->{query}->{pages} };
1266 }
1267 7 my @return;
1268 7 foreach my $id (@sorted_ids) {
1269 9 ▶ if ($res->{query}->{pages}->{$id}->{imagerepository} eq 'shared') {
Branch: 4 branches
BranchTrueFalseTotal
if ($res->{'query'}{'pages'}{$id}{'imagerepository'} eq 'shared') { } 3 6 9
elsif (exists $res->{'query'}{'pages'}{$id}{'missing'}) { } 3 3 6
elsif ($res->{'query'}{'pages'}{$id}{'imagerepository'} eq '') { } 0 3 3
elsif ($res->{'query'}{'pages'}{$id}{'imagerepository'} eq 'local') { } 3 0 3
1270 3 ▶ if ($multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi) { } 1 2 3
1271 1 unshift @return, FILE_SHARED;
1272 }
1273 else {
1274 2 return FILE_SHARED;
1275 }
1276 }
1277 elsif (exists($res->{query}->{pages}->{$id}->{missing})) {
1278 3 ▶ if ($multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi) { } 1 2 3
1279 1 unshift @return, FILE_NONEXISTENT;
1280 }
1281 else {
1282 2 return FILE_NONEXISTENT;
1283 }
1284 }
1285 elsif ($res->{query}->{pages}->{$id}->{imagerepository} eq '') {
1286 0 ▶ if ($multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi) { } 0 0 0
1287 0 unshift @return, FILE_PAGE_TEXT_ONLY;
1288 }
1289 else {
1290 0 return FILE_PAGE_TEXT_ONLY;
1291 }
1292 }
1293 elsif ($res->{query}->{pages}->{$id}->{imagerepository} eq 'local') {
1294 3 ▶ if ($multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi) { } 1 2 3
1295 1 unshift @return, FILE_LOCAL;
1296 }
1297 else {
1298 2 return FILE_LOCAL;
1299 }
1300 }
1301 }
1302  
1303 1 return \@return;
1304 }
1305  
1306  
1307 4 ▶sub get_pages_in_namespace {
Subroutine get_pages_in_namespace: called CC=7 SCAR=19.5
Pod documented
1308 4 my $self = shift;
1309 4 my $namespace = shift;
1310 4 ▶ my $limit = shift || 'max';
Condition100%2/2
shift() || 'max'
l!l
22
MC/DC100%1/1
shift() || 'max'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1311 4 my $options = shift;
1312  
1313 4 my $hash = {
1314 action => 'query',
1315 list => 'allpages',
1316 apnamespace => $namespace,
1317 aplimit => $limit,
1318 };
1319 4 ▶ $options->{max} = 1 unless defined $options->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"max"} 3 1 4
1320 4 ▶ delete $options->{max} if exists $options->{max} and $options->{max} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"max"} and $options->{"max"} == 0 1 3 4
Condition66%2/3
exists $options->{"max"} and $options->{"max"} == 0
!ll&&!rl&&r
031
MC/DC50%1/2
exists $options->{"max"} and $options->{"max"} == 0
exists $options->{"max"} $options->{"max"} == 0
Truth table66%2/3
A and B
A= exists $options->{"max"}B= $options->{"max"} == 0
ABresult
0X0
100
111
1321  
1322 4 my $res = $self->{api}->list($hash, $options);
1323 4 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 1 3 4
1324 3 ▶ return RET_TRUE if not ref $res; # Not a ref when using callback
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 0 3 3
1325 3 return map { $_->{title} } @$res;
1326 }
1327  
1328  
1329 2 ▶sub count_contributions {
Subroutine count_contributions: called CC=2 SCAR=7.0
Pod documented
1330 2 my $self = shift;
1331 2 my $username = shift;
1332 2 $username =~ s/User://i; # Strip namespace
1333  
1334 my $res = $self->{api}->list({
1335 2 action => 'query',
1336 list => 'users',
1337 ususers => $username,
1338 usprop => 'editcount'
1339 },
1340 { max => 1 });
1341 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
1342 2 return ${$res}[0]->{editcount};
1343 }
1344  
1345  
1346 0 ▶sub timed_count_contributions {
Subroutine timed_count_contributions: not called CC=2 SCAR=17.9
Pod documented
1347 0 my $self = shift;
1348 0 my $username = shift;
1349 0 my $days = shift;
1350 0 $username =~ s/User://i; # Strip namespace
1351  
1352 my $res = $self->{api}->api({
1353 0 action => 'userdailycontribs',
1354 user => $username,
1355 daysago => $days,
1356 },
1357 { max => 1 });
1358 0 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
1359 0 return ($res->{userdailycontribs}->{timeFrameEdits}, $res->{userdailycontribs}->{totalEdits});
1360 }
1361  
1362  
1363 2 ▶sub last_active {
Subroutine last_active: called CC=2 SCAR=7.0
Pod documented
1364 2 my $self = shift;
1365 2 my $username = shift;
1366 my $res = $self->{api}->list({
1367 2 action => 'query',
1368 list => 'usercontribs',
1369 ucuser => $username,
1370 uclimit => 1
1371 },
1372 { max => 1 });
1373 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
1374 2 return ${$res}[0]->{timestamp};
1375 }
1376  
1377  
1378 1 ▶sub recent_edit_to_page {
Subroutine recent_edit_to_page: called CC=2 SCAR=7.0
Pod documented
1379 1 my $self = shift;
1380 1 my $page = shift;
1381 my $res = $self->{api}->api({
1382 1 action => 'query',
1383 prop => 'revisions',
1384 titles => $page,
1385 rvlimit => 1
1386 },
1387 { max => 1 });
1388 1 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 1 1
1389 1 my $data = ( %{ $res->{query}->{pages} } )[1];
1390 return ($data->{revisions}[0]->{timestamp},
1391 1 $data->{revisions}[0]->{user});
1392 }
1393  
1394  
1395 1 ▶sub get_users {
Subroutine get_users: called CC=6 SCAR=19.1
Pod documented
1396 1 my $self = shift;
1397 1 my $pagename = shift;
1398 1 ▶ my $limit = shift || 'max';
Condition50%1/2
shift() || 'max'
l!l
10
MC/DC0%0/1
shift() || 'max'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1399 1 my $rvstartid = shift;
1400 1 my $direction = shift;
1401  
1402 1 ▶ if ($limit > 50) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > 50) 0 1 1
1403 0 $self->{errstr} = "Error requesting history for $pagename: Limit may not be set to values above 50";
1404 0 carp $self->{errstr};
1405 0 return;
1406 }
1407 1 my $hash = {
1408 action => 'query',
1409 prop => 'revisions',
1410 titles => $pagename,
1411 rvprop => 'ids|timestamp|user|comment',
1412 rvlimit => $limit,
1413 };
1414 1 ▶ $hash->{rvstartid} = $rvstartid if ($rvstartid);
Branch: 1 branch
BranchTrueFalseTotal
if $rvstartid 0 1 1
1415 1 ▶ $hash->{rvdir} = $direction if ($direction);
Branch: 1 branch
BranchTrueFalseTotal
if $direction 0 1 1
1416  
1417 1 my $res = $self->{api}->api($hash);
1418 1 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 1 1
1419  
1420 1 my ($id) = keys %{ $res->{query}->{pages} };
1421 1 return map { $_->{user} } @{$res->{query}->{pages}->{$id}->{revisions}};
1422 }
1423  
1424  
1425 4 ▶sub was_blocked {
Subroutine was_blocked: called CC=4 SCAR=14.0
Pod documented
1426 4 my $self = shift;
1427 4 my $user = shift;
1428 4 $user =~ s/User://i; # Strip User: prefix, if present
1429  
1430 # http://en.wikipedia.org/w/api.php?action=query&list=logevents&letype=block&letitle=User:127.0.0.1&lelimit=1&leprop=ids
1431 4 my $hash = {
1432 action => 'query',
1433 list => 'logevents',
1434 letype => 'block',
1435 letitle => "User:$user", # Ensure the User: prefix is there!
1436 lelimit => 1,
1437 leprop => 'ids',
1438 };
1439  
1440 4 my $res = $self->{api}->api($hash);
1441 4 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 4 4
1442  
1443 4 my $number = scalar @{ $res->{query}->{logevents} }; # The number of blocks returned
1444 4 ▶ if ($number == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($number == 1) { } 2 2 4
elsif ($number == 0) { } 2 0 2
1445 2 return RET_TRUE;
1446 }
1447 elsif ($number == 0) {
1448 2 return RET_FALSE;
1449 }
1450 else {
1451 0 confess "This query should return at most one result, but the API returned more than that.";
1452 }
1453 }
1454  
1455  
1456 2 ▶sub test_block_hist { # Backwards compatibility
Subroutine test_block_hist: called CC=1 SCAR=0.0
Pod documented
1457 2 warnings::warnif('deprecated', 'test_block_hist is an alias of was_blocked; '
1458 . 'please use the new method name. This alias might be removed in a future release');
1459 2 return (was_blocked(@_));
1460 }
1461  
1462  
1463 2 ▶sub expandtemplates {
Subroutine expandtemplates: called CC=6 SCAR=18.1
Pod documented
1464 2 my $self = shift;
1465 2 my $page = shift;
1466 2 my $text = shift;
1467  
1468 2 ▶ unless ($text) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text) 1 1 2
1469 1 ▶ croak q{You must provide a page title} unless $page;
Branch: 1 branch
BranchTrueFalseTotal
unless $page 0 1 1
1470 1 $text = $self->get_text($page);
1471 }
1472  
1473 2 ▶ my $hash = {
Branch: 1 branch
BranchTrueFalseTotal
$page ? : 1 1 2
1474 action => 'expandtemplates',
1475 prop => 'wikitext',
1476 ( $page ? (title => $page) : ()),
1477 text => $text,
1478 };
1479 2 my $res = $self->{api}->api($hash);
1480 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
1481  
1482 return exists $res->{expandtemplates}->{'*'}
1483 ? $res->{expandtemplates}->{'*'}
1484 2 ▶ : $res->{expandtemplates}->{wikitext};
Branch: 1 branch
BranchTrueFalseTotal
exists $res->{'expandtemplates'}{'*'} ? : 0 2 2
1485 }
1486  
1487  
1488 2 ▶sub get_allusers {
Subroutine get_allusers: called CC=8 SCAR=22.0
Pod documented
1489 2 my $self = shift;
1490 2 ▶ my $limit = shift || 'max';
Condition50%1/2
shift() || 'max'
l!l
20
MC/DC0%0/1
shift() || 'max'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1491 2 my $group = shift;
1492 2 my $opts = shift;
1493  
1494 2 my $hash = {
1495 action => 'query',
1496 list => 'allusers',
1497 aulimit => $limit,
1498 };
1499 2 ▶ $hash->{augroup} = $group if defined $group;
Branch: 1 branch
BranchTrueFalseTotal
if defined $group 1 1 2
1500 2 ▶ $opts->{max} = 1 unless exists $opts->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"max"} 2 0 2
1501 2 ▶ delete $opts->{max} if exists $opts->{max} and $opts->{max} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"max"} and $opts->{"max"} == 0 0 2 2
Condition33%1/3
exists $opts->{"max"} and $opts->{"max"} == 0
!ll&&!rl&&r
020
MC/DC0%0/2
exists $opts->{"max"} and $opts->{"max"} == 0
exists $opts->{"max"} $opts->{"max"} == 0
Truth table33%1/3
A and B
A= exists $opts->{"max"}B= $opts->{"max"} == 0
ABresult
0X0
100
111
1502 2 my $res = $self->{api}->list($hash, $opts);
1503 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
1504 2 ▶ return RET_TRUE if not ref $res; # Not a ref when using callback
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 0 2 2
1505  
1506 2 return map { $_->{name} } @$res;
1507 }
1508  
1509  
1510 1 ▶sub db_to_domain {
Subroutine db_to_domain: called CC=8 SCAR=23.9
Pod documented
1511 1 my $self = shift;
1512 1 my $wiki = shift;
1513  
1514 1 ▶ if (!$self->{sitematrix}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sitematrix"}) 1 0 1
1515 1 $self->_get_sitematrix();
1516 }
1517  
1518 1 ▶ if (ref $wiki eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $wiki eq 'ARRAY') { } 1 0 1
1519 1 my @return;
1520 1 foreach my $w (@$wiki) {
1521 6 $wiki =~ s/_p$//; # Strip off a _p suffix, if present
1522 6 ▶ my $domain = $self->{sitematrix}->{$w} || undef;
Condition100%2/2
$self->{'sitematrix'}{$w} || undef
l!l
33
MC/DC100%1/1
$self->{'sitematrix'}{$w} || undef
$self->{'sitematrix'}{$w}
Truth table100%2/2
A
A= $self->{'sitematrix'}{$w}
Aresult
11
00
1523 6 ▶ $domain =~ s/^https\:\/\/// if (defined $domain); # Strip off a https:// prefix, if present
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain 3 3 6
1524 6 push(@return, $domain);
1525 }
1526 1 return \@return;
1527 }
1528 else {
1529 0 $wiki =~ s/_p$//; # Strip off a _p suffix, if present
1530 0 ▶ my $domain = $self->{sitematrix}->{$wiki} || undef;
Condition0%0/2
$self->{'sitematrix'}{$wiki} || undef
l!l
00
MC/DC0%0/1
$self->{'sitematrix'}{$wiki} || undef
$self->{'sitematrix'}{$wiki}
Truth table0%0/2
A
A= $self->{'sitematrix'}{$wiki}
Aresult
11
00
1531 0 ▶ $domain =~ s/^https\:\/\/// if (defined $domain); # Strip off a https:// prefix, if present
Branch: 1 branch
BranchTrueFalseTotal
if defined $domain 0 0 0
1532 0 return $domain;
1533 }
1534 }
1535  
1536  
1537 1 ▶sub domain_to_db {
Subroutine domain_to_db: called CC=8 SCAR=25.5
Pod documented
1538 1 my $self = shift;
1539 1 my $wiki = shift;
1540  
1541 1 ▶ if (!$self->{sitematrix}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sitematrix"}) 0 1 1
1542 0 $self->_get_sitematrix();
1543 }
1544  
1545 1 ▶ if (ref $wiki eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $wiki eq 'ARRAY') { } 1 0 1
1546 1 my @return;
1547 1 foreach my $w (@$wiki) {
1548 6 ▶ $w = "https://".$w if ($w !~ /^https\:\//); # Prepend a https:// prefix, if not present
Branch: 1 branch
BranchTrueFalseTotal
unless $w =~ m[^https\:/] 6 0 6
1549 6 ▶ my $db = $self->{sitematrix}->{$w} || undef;
Condition100%2/2
$self->{'sitematrix'}{$w} || undef
l!l
42
MC/DC100%1/1
$self->{'sitematrix'}{$w} || undef
$self->{'sitematrix'}{$w}
Truth table100%2/2
A
A= $self->{'sitematrix'}{$w}
Aresult
11
00
1550 6 push(@return, $db);
1551 }
1552 1 return \@return;
1553 }
1554 else {
1555 0 ▶ $wiki = "https://".$wiki if ($wiki !~ /^https\:\//); # Prepend a https:// prefix, if not present
Branch: 1 branch
BranchTrueFalseTotal
unless $wiki =~ m[^https\:/] 0 0 0
1556 0 ▶ my $db = $self->{sitematrix}->{$wiki} || undef;
Condition0%0/2
$self->{'sitematrix'}{$wiki} || undef
l!l
00
MC/DC0%0/1
$self->{'sitematrix'}{$wiki} || undef
$self->{'sitematrix'}{$wiki}
Truth table0%0/2
A
A= $self->{'sitematrix'}{$wiki}
Aresult
11
00
1557 0 return $db;
1558 }
1559 }
1560  
1561  
1562 1 ▶sub diff {
Subroutine diff: called CC=5 SCAR=17.5
Pod documented
1563 1 my $self = shift;
1564 1 my $title;
1565 my $revid;
1566 1 my $oldid;
1567  
1568 1 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 1 0 1
1569 1 $title = $_[0]->{title};
1570 1 $revid = $_[0]->{revid};
1571 1 $oldid = $_[0]->{oldid};
1572 }
1573 else {
1574 0 $title = shift;
1575 0 $revid = shift;
1576 0 $oldid = shift;
1577 }
1578  
1579 1 my $hash = {
1580 action => 'query',
1581 prop => 'revisions',
1582 rvdiffto => $oldid,
1583 };
1584 1 ▶ if ($title) {
Branch: 2 branches
BranchTrueFalseTotal
if ($title) { } 0 1 1
elsif ($revid) { } 1 0 1
1585 0 $hash->{titles} = $title;
1586 0 $hash->{rvlimit} = 1;
1587 }
1588 elsif ($revid) {
1589 1 $hash->{'revids'} = $revid;
1590 }
1591  
1592 1 my $res = $self->{api}->api($hash);
1593 1 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 1 1
1594 1 my @revids = keys %{ $res->{query}->{pages} };
1595 1 my $diff = $res->{query}->{pages}->{ $revids[0] }->{revisions}->[0]->{diff}->{'*'};
1596  
1597 1 return $diff;
1598 }
1599  
1600  
1601 1 ▶sub prefixindex {
Subroutine prefixindex: called CC=12 SCAR=27.6
Pod documented
1602 1 my $self = shift;
1603 1 my $prefix = shift;
1604 1 my $ns = shift;
1605 1 my $filter = shift;
1606 1 my $options = shift;
1607  
1608 1 ▶ if (defined($filter) and $filter =~ m/(all|redirects|nonredirects)/) { # Verify
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter and $filter =~ /(all|redirects|nonredirects)/) 0 1 1
Condition33%1/3
defined $filter and $filter =~ /(all|redirects|nonredirects)/
!ll&&!rl&&r
100
MC/DC0%0/2
defined $filter and $filter =~ /(all|redirects|nonredirects)/
defined $filter $filter =~ /(all|redirects|nonredirects)/
Truth table33%1/3
A and B
A= defined $filterB= $filter =~ /(all|redirects|nonredirects)/
ABresult
0X0
100
111
1609 0 $filter = $1;
1610 }
1611  
1612 1 ▶ if (!defined $ns && $prefix =~ m/:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ns and $prefix =~ /:/) 1 0 1
Condition33%1/3
not defined $ns and $prefix =~ /:/
!ll&&!rl&&r
001
MC/DC0%0/2
not defined $ns and $prefix =~ /:/
not defined $ns $prefix =~ /:/
Truth table33%1/3
not(A) and B
A= not defined $nsB= $prefix =~ /:/
ABresult
0X0
100
111
1613 1 ▶ print STDERR "Converted '$prefix' to..." if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 1 1
1614 1 my ($name) = split(/:/, $prefix, 2);
1615 1 my $ns_data = $self->_get_ns_data();
1616 1 $ns = $ns_data->{$name};
1617 1 $prefix =~ s/^$name://;
1618 1 ▶ warn "'$prefix' with a namespace filter $ns" if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 1 1
1619 }
1620  
1621 1 my $hash = {
1622 action => 'query',
1623 list => 'allpages',
1624 apprefix => $prefix,
1625 aplimit => 'max',
1626 };
1627 1 ▶ $hash->{apnamespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 1 0 1
1628 1 ▶ $hash->{apfilterredir} = $filter if $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $filter 0 1 1
1629 1 ▶ $options->{max} = 1 unless $options->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"max"} 1 0 1
1630  
1631 1 my $res = $self->{api}->list($hash, $options);
1632  
1633 1 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 1 1
1634 1 ▶ return RET_TRUE if not ref $res; # Not a ref when using callback hook
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 0 1 1
1635  
1636 return map {
1637 1 { title => $_->{title}, redirect => defined $_->{redirect} }
1638 3 } @$res;
1639 }
1640  
1641  
1642 2 ▶sub search {
Subroutine search: called CC=6 SCAR=18.9
Pod documented
1643 2 my $self = shift;
1644 2 my $term = shift;
1645 2 ▶ my $ns = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1646 2 my $options = shift;
1647  
1648 2 ▶ if (ref $ns eq 'ARRAY') { # Accept a hashref
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ns eq "ARRAY") 0 2 2
1649 0 $ns = join('|', @$ns);
1650 }
1651  
1652 2 my $hash = {
1653 action => 'query',
1654 list => 'search',
1655 srnamespace => $ns,
1656 srsearch => $term,
1657 srwhat => 'text',
1658 srlimit => 'max',
1659  
1660 #srinfo => 'totalhits',
1661 srprop => 'size',
1662 srredirects => 0,
1663 };
1664 2 ▶ $options->{max} = 1 unless $options->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"max"} 2 0 2
1665  
1666 2 my $res = $self->{api}->list($hash, $options);
1667 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
1668 2 ▶ return RET_TRUE if not ref $res; # Not a ref when used with callback
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 0 2 2
1669  
1670 2 return map { $_->{title} } @$res;
1671 }
1672  
1673  
1674 1 ▶sub get_log {
Subroutine get_log: called CC=8 SCAR=21.5
Pod documented
1675 1 my $self = shift;
1676 1 my $data = shift;
1677 1 my $options = shift;
1678  
1679 1 my $log_type = $data->{type};
1680 1 my $user = $data->{user};
1681 1 my $target = $data->{target};
1682  
1683 1 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 1 0 1
1684 1 my $ns_data = $self->_get_ns_data();
1685 1 my $user_ns_name = $ns_data->{+NS_USER};
1686 1 $user =~ s/^$user_ns_name://;
1687 }
1688  
1689 1 my $hash = {
1690 action => 'query',
1691 list => 'logevents',
1692 lelimit => 'max',
1693 };
1694 1 ▶ $hash->{letype} = $log_type if $log_type;
Branch: 1 branch
BranchTrueFalseTotal
if $log_type 1 0 1
1695 1 ▶ $hash->{leuser} = $user if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 1 0 1
1696 1 ▶ $hash->{letitle} = $target if $target;
Branch: 1 branch
BranchTrueFalseTotal
if $target 1 0 1
1697 1 ▶ $options->{max} = 1 unless $options->{max};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"max"} 1 0 1
1698  
1699 1 my $res = $self->{api}->list($hash, $options);
1700 1 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 1 1
1701 1 ▶ return RET_TRUE if not ref $res; # Not a ref when using callback
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 0 1 1
1702  
1703 1 return $res;
1704 }
1705  
1706  
1707 1 ▶sub is_g_blocked {
Subroutine is_g_blocked: called CC=3 SCAR=11.8
Pod documented
1708 1 my $self = shift;
1709 1 my $ip = shift;
1710  
1711 # http://en.wikipedia.org/w/api.php?action=query&list=globalblocks&bglimit=1&bgprop=address&bgip=127.0.0.1
1712 my $res = $self->{api}->api({
1713 1 action => 'query',
1714 list => 'globalblocks',
1715 bglimit => 1,
1716 bgprop => 'address',
1717 # So handy! It searches for blocks affecting this IP or IP range,
1718 # including rangeblocks! Can't get that from UI.
1719 bgip => $ip,
1720 });
1721 1 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 1 1
1722 1 ▶ return RET_FALSE unless ($res->{query}->{globalblocks}->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"query"}{"globalblocks"}[0] 1 0 1
1723  
1724 0 return $res->{query}->{globalblocks}->[0]->{address};
1725 }
1726  
1727  
1728 2 ▶sub was_g_blocked {
Subroutine was_g_blocked: called CC=6 SCAR=19.5
Pod documented
1729 2 my $self = shift;
1730 2 my $ip = shift;
1731 2 $ip =~ s/User://i; # Strip User: prefix, if present
1732  
1733 # This query should always go to Meta
1734 2 ▶ unless ( $self->{host} eq 'meta.wikimedia.org' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"host"} eq "meta.wikimedia.org") 0 2 2
1735 0 ▶ carp "GlobalBlocking queries should probably be sent to Meta; it doesn't look like you're doing so" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1736 }
1737  
1738 # http://meta.wikimedia.org/w/api.php?action=query&list=logevents&letype=gblblock&letitle=User:127.0.0.1&lelimit=1&leprop=ids
1739 my $res = $self->{api}->api({
1740 2 action => 'query',
1741 list => 'logevents',
1742 letype => 'gblblock',
1743 letitle => "User:$ip", # Ensure the User: prefix is there!
1744 lelimit => 1,
1745 leprop => 'ids',
1746 });
1747  
1748 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
1749 2 my $number = scalar @{ $res->{query}->{logevents} }; # The number of blocks returned
1750  
1751 2 ▶ if ($number == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($number == 1) { } 1 1 2
elsif ($number == 0) { } 1 0 1
1752 1 return RET_TRUE;
1753 }
1754 elsif ($number == 0) {
1755 1 return RET_FALSE;
1756 }
1757 else {
1758 0 confess "This query should return at most one result, but the API gave more than that.";
1759 }
1760 }
1761  
1762  
1763 2 ▶sub was_locked {
Subroutine was_locked: called CC=6 SCAR=18.4
Pod documented
1764 2 my $self = shift;
1765 2 my $user = shift;
1766  
1767 # This query should always go to Meta
1768 2 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"api"}{"config"}{"api_url"} =~ m[ http\:\/\/meta\.wikimedia\.org\/w\/api\.php | https\:\/\/secure\.wikimedia\.org\/wikipedia\/meta\/w\/api\.php ]x) 2 0 2
1769 $self->{api}->{config}->{api_url} =~ m,
1770 \Qhttp://meta.wikimedia.org/w/api.php\E
1771 |
1772 \Qhttps://secure.wikimedia.org/wikipedia/meta/w/api.php\E
1773 ,x # /x flag is pretty awesome :)
1774 )
1775 {
1776 2 ▶ carp "CentralAuth queries should probably be sent to Meta; it doesn't look like you're doing so" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
1777 }
1778  
1779 2 $user =~ s/^User://i;
1780 2 $user =~ s/\@global$//i;
1781 my $res = $self->{api}->api({
1782 2 action => 'query',
1783 list => 'logevents',
1784 letype => 'globalauth',
1785 letitle => "User:$user\@global",
1786 lelimit => 1,
1787 leprop => 'ids',
1788 });
1789 2 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
1790 2 my $number = scalar @{ $res->{query}->{logevents} };
1791 2 ▶ if ($number == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($number == 1) { } 1 1 2
elsif ($number == 0) { } 1 0 1
1792 1 return RET_TRUE;
1793 }
1794 elsif ($number == 0) {
1795 1 return RET_FALSE;
1796 }
1797 else {
1798 0 confess "This query should return at most one result, but the API returned more than that.";
1799 }
1800 }
1801  
1802  
1803 3 ▶sub get_protection {
Subroutine get_protection: called CC=6 SCAR=17.9
Pod documented
1804 3 my $self = shift;
1805 3 my $page = shift;
1806 3 ▶ if (ref $page eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $page eq "ARRAY") 1 2 3
1807 1 $page = join('|', @$page);
1808 }
1809  
1810 3 my $hash = {
1811 action => 'query',
1812 titles => $page,
1813 prop => 'info',
1814 inprop => 'protection',
1815 };
1816 3 my $res = $self->{api}->api($hash);
1817 3 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 3 3
1818  
1819 3 my $data = $res->{query}->{pages};
1820  
1821 3 my $out_data;
1822 3 foreach my $item (keys %$data) {
1823 4 my $title = $data->{$item}->{title};
1824 4 my $protection = $data->{$item}->{protection};
1825 4 ▶ if (@$protection == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$protection == 0) 3 1 4
1826 3 $protection = undef;
1827 }
1828 4 $out_data->{$title} = $protection;
1829 }
1830  
1831 3 ▶ if (scalar keys %$out_data == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$out_data == 1) { } 2 1 3
1832 2 return $out_data->{$page};
1833 }
1834 else {
1835 1 return $out_data;
1836 }
1837 }
1838  
1839  
1840 1 ▶sub is_protected {
Subroutine is_protected: called CC=1 SCAR=0.0
Pod documented
1841 1 warnings::warnif('deprecated', 'is_protected is deprecated, and might be '
1842 . 'removed in a future release; please use get_protection instead');
1843 1 my $self = shift;
1844 1 return $self->get_protection(@_);
1845 }
1846  
1847  
1848 0 ▶sub patrol {
Subroutine patrol: not called CC=6 SCAR=37.4
Pod documented
1849 0 my $self = shift;
1850 0 my $rcid = shift;
1851  
1852 0 ▶ if (ref $rcid eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rcid eq 'ARRAY') { } 0 0 0
1853 0 my @return;
1854 0 foreach my $id (@$rcid) {
1855 0 my $res = $self->patrol($id);
1856 0 push(@return, $res);
1857 }
1858 0 return @return;
1859 }
1860 else {
1861 0 my ($token) = $self->_get_edittoken('patrol');
1862 my $res = $self->{api}->api({
1863 0 action => 'patrol',
1864 rcid => $rcid,
1865 token => $token,
1866 });
1867 return $self->_handle_api_error()
1868 if !$res
1869 0 ▶ or $self->{error}->{details} && $self->{error}->{details} =~ m/^(?:permissiondenied|badtoken)/;
Branch: 1 branch
BranchTrueFalseTotal
if not $res or $self->{"error"}{"details"} and $self->{"error"}{"details"} =~ /^(?:permissiondenied|badtoken)/ 0 0 0
Condition0%0/6
not $res or $self->{"error"}{"details"} and $self->{"error"}{"details"} =~ /^(?:permissiondenied|badtoken)/
l!l&&r!l&&!r
000
$self->{"error"}{"details"} and $self->{"error"}{"details"} =~ /^(?:permissiondenied|badtoken)/
!ll&&!rl&&r
000
MC/DC0%0/3
not $res or $self->{"error"}{"details"} and $self->{"error"}{"details"} =~ /^(?:permissiondenied|badtoken)/
not $res $self->{"error"}{"details"} $self->{"error"}{"details"} =~ /^(?:permissiondenied|badtoken)/
Truth table0%0/4
not(A) or B and C
A= not $resB= $self->{"error"}{"details"}C= $self->{"error"}{"details"} =~ /^(?:permissiondenied|badtoken)/
ABCresult
1XX1
0111
00X0
0100
1870  
1871 0 return $res;
1872 }
1873 }
1874  
1875  
1876 0 ▶sub email {
Subroutine email: not called CC=5 SCAR=34.0
Pod documented
1877 0 my $self = shift;
1878 0 my $user = shift;
1879 0 my $subject = shift;
1880 0 my $body = shift;
1881  
1882 0 ▶ if (ref $user eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $user eq "ARRAY") 0 0 0
1883 0 my @return;
1884 0 foreach my $target (@$user) {
1885 0 my $res = $self->email($target, $subject, $body);
1886 0 push(@return, $res);
1887 }
1888 0 return @return;
1889 }
1890  
1891 0 $user =~ s/^User://;
1892 0 ▶ if ($user =~ m/:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user =~ /:/) 0 0 0
1893 0 my $user_ns_name = $self->_get_ns_data()->{+NS_USER};
1894 0 $user =~ s/^$user_ns_name://;
1895 }
1896  
1897 0 my ($token) = $self->_get_edittoken;
1898 my $res = $self->{api}->api({
1899 0 action => 'emailuser',
1900 target => $user,
1901 subject => $subject,
1902 text => $body,
1903 token => $token,
1904 });
1905 0 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
1906 0 return $res;
1907 }
1908  
1909  
1910 0 ▶sub top_edits {
Subroutine top_edits: not called CC=5 SCAR=34.0
Pod documented
1911 0 my $self = shift;
1912 0 my $user = shift;
1913 0 my $options = shift;
1914  
1915 0 $user =~ s/^User://;
1916  
1917 0 ▶ $options->{max} = 1 unless defined($options->{max});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"max"} 0 0 0
1918 0 ▶ delete($options->{max}) if $options->{max} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"max"} == 0 0 0 0
1919  
1920 0 my $res = $self->{'api'}->list({
1921 action => 'query',
1922 list => 'usercontribs',
1923 ucuser => $user,
1924 ucprop => 'title|flags',
1925 uclimit => 'max',
1926 }, $options);
1927 0 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
1928 0 ▶ return RET_TRUE if not ref $res; # Not a ref when using callback
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 0 0 0
1929  
1930 return
1931 0 map { $_->{title} }
1932 0 grep { exists $_->{top} }
1933 @$res;
1934 }
1935  
1936  
1937 3 ▶sub contributions {
Subroutine contributions: called CC=10 SCAR=23.2
Pod documented
1938 3 my $self = shift;
1939 3 my $user = shift;
1940 3 my $ns = shift;
1941 3 my $opts = shift;
1942 3 my $from = shift; # ucend
1943 3 my $to = shift; # ucstart
1944  
1945 3 ▶ if (ref $user eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $user eq 'ARRAY') { } 1 2 3
1946 1 $user = join '|', map { my $u = $_; $u =~ s{^User:}{}; $u } @$user;
1947 }
1948 else {
1949 2 $user =~ s{^User:}{};
1950 }
1951 3 ▶ $ns = join '|', @$ns
Branch: 1 branch
BranchTrueFalseTotal
if ref $ns eq "ARRAY" 0 3 3
1952 if ref $ns eq 'ARRAY';
1953  
1954 3 ▶ $opts->{max} = 1 unless defined($opts->{max});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"max"} 3 0 3
1955 3 ▶ delete($opts->{max}) if $opts->{max} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"max"} == 0 0 3 3
1956  
1957 3 ▶ my $query = {
Branch: 1 branch
BranchTrueFalseTotal
defined $ns ? : 1 2 3
1958 action => 'query',
1959 list => 'usercontribs',
1960 ucuser => $user,
1961 ( defined $ns ? (ucnamespace => $ns) : ()),
1962 ucprop => 'ids|title|timestamp|comment|flags',
1963 uclimit => 'max',
1964 };
1965 3 ▶ $query->{'ucstart'} = $to if defined $to;
Branch: 1 branch
BranchTrueFalseTotal
if defined $to 1 2 3
1966 3 ▶ $query->{'ucend'} = $from if defined $from;
Branch: 1 branch
BranchTrueFalseTotal
if defined $from 1 2 3
1967 3 my $res = $self->{api}->list($query, $opts);
1968 3 ▶ return $self->_handle_api_error() unless $res->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] 0 3 3
1969 3 ▶ return RET_TRUE if not ref $res; # Not a ref when using callback
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res 0 3 3
1970  
1971 3 return @$res;
1972 }
1973  
1974  
1975 0 ▶sub upload {
Subroutine upload: not called CC=9 SCAR=45.0
Pod documented
1976 0 my $self = shift;
1977 0 my $args = shift;
1978  
1979 0 my $data = delete $args->{data};
1980 0 ▶ if (!defined $data and defined $args->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $data and defined $args->{"file"}) 0 0 0
Condition0%0/3
not defined $data and defined $args->{"file"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $data and defined $args->{"file"}
not defined $data defined $args->{"file"}
Truth table0%0/3
not(A) and B
A= not defined $dataB= defined $args->{"file"}
ABresult
0X0
100
111
1981 0 ▶ $data = do { local $/; open my $in, '<:raw', $args->{file} or die $!; <$in> };
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<:raw", $args->{"file"} or die $! 0 0 0
1982 }
1983 0 ▶ unless (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
1984 0 $self->{error}->{code} = ERR_PARAMS;
1985 0 $self->{error}->{details} = q{You must provide either file contents or a filename.};
1986 0 return undef;
1987 }
1988 0 ▶ unless (defined $args->{file} or defined $args->{title}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"file"} or defined $args->{"title"}) 0 0 0
Condition0%0/3
defined $args->{"file"} or defined $args->{"title"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $args->{"file"} or defined $args->{"title"}
defined $args->{"file"} defined $args->{"title"}
Truth table0%0/3
A or B
A= defined $args->{"file"}B= defined $args->{"title"}
ABresult
1X1
011
000
1989 0 $self->{error}->{code} = ERR_PARAMS;
1990 0 $self->{error}->{details} = q{You must specify a title to upload to.};
1991 0 return undef;
1992 }
1993  
1994 0 ▶ my $filename = $args->{title} || do { require File::Basename; File::Basename::basename($args->{file}) };
Condition0%0/3
$args->{'title'} || do { require File::Basename; File::Basename::basename($args->{'file'}) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'title'} || do { require File::Basename; File::Basename::basename($args->{'file'}) }
$args->{'title'} do { require File::Basename; File::Basename::basename($args->{'file'}) }
Truth table0%0/3
A || B
A= $args->{'title'}B= do { require File::Basename; File::Basename::basename($args->{'file'}) }
ABresult
1X1
011
000
1995 my $success = $self->{api}->edit({
1996 action => 'upload',
1997 filename => $filename,
1998 comment => $args->{summary},
1999 0 ▶ file => [ undef, $filename, Content => $data ],
Condition0%0/2
$self->{'api'}->edit({'action', 'upload', 'filename', $filename, 'comment', $args->{'summary'}, 'file', [undef, $filename, 'Content', $data]}) || (return $self->_handle_api_error)
l!l
00
MC/DC0%0/1
$self->{'api'}->edit({'action', 'upload', 'filename', $filename, 'comment', $args->{'summary'}, 'file', [undef, $filename, 'Content', $data]}) || (return $self->_handle_api_error)
$self->{'api'}->edit({'action', 'upload', 'filename', $filename, 'comment', $args->{'summary'}, 'file', [undef, $filename, 'Content', $data]})
Truth table0%0/2
A
A= $self->{'api'}->edit({'action', 'upload', 'filename', $filename, 'comment', $args->{'summary'}, 'file', [undef, $filename, 'Content', $data]})
Aresult
11
00
2000 }) || return $self->_handle_api_error();
2001 0 return $success;
2002 }
2003  
2004  
2005 0 ▶sub upload_from_url {
Subroutine upload_from_url: not called CC=4 SCAR=30.0
Pod documented
2006 0 my $self = shift;
2007 0 my $args = shift;
2008  
2009 0 my $url = delete $args->{url};
2010 0 ▶ unless (defined $url) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $url) 0 0 0
2011 0 $self->{error}->{code} = ERR_PARAMS;
2012 0 $self->{error}->{details} = q{You must provide URL of file to upload.};
2013 0 return undef;
2014 }
2015  
2016 0 ▶ my $filename = $args->{title} || do {
Condition0%0/3
$args->{'title'} || do { require File::Basename; File::Basename::basename($url) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'title'} || do { require File::Basename; File::Basename::basename($url) }
$args->{'title'} do { require File::Basename; File::Basename::basename($url) }
Truth table0%0/3
A || B
A= $args->{'title'}B= do { require File::Basename; File::Basename::basename($url) }
ABresult
1X1
011
000
2017 0 require File::Basename;
2018 0 File::Basename::basename($url)
2019 };
2020 my $success = $self->{api}->edit({
2021 action => 'upload',
2022 filename => $filename,
2023 comment => $args->{summary},
2024 0 ▶ url => $url,
Condition0%0/2
$self->{'api'}->edit({'action', 'upload', 'filename', $filename, 'comment', $args->{'summary'}, 'url', $url, 'ignorewarnings', 1}) || (return $self->_handle_api_error)
l!l
00
MC/DC0%0/1
$self->{'api'}->edit({'action', 'upload', 'filename', $filename, 'comment', $args->{'summary'}, 'url', $url, 'ignorewarnings', 1}) || (return $self->_handle_api_error)
$self->{'api'}->edit({'action', 'upload', 'filename', $filename, 'comment', $args->{'summary'}, 'url', $url, 'ignorewarnings', 1})
Truth table0%0/2
A
A= $self->{'api'}->edit({'action', 'upload', 'filename', $filename, 'comment', $args->{'summary'}, 'url', $url, 'ignorewarnings', 1})
Aresult
11
00
2025 ignorewarnings => 1,
2026 }) || return $self->_handle_api_error();
2027 0 return $success;
2028 }
2029  
2030  
2031 1 ▶sub usergroups {
Subroutine usergroups: called CC=4 SCAR=14.1
Pod documented
2032 1 my $self = shift;
2033 1 my $user = shift;
2034  
2035 1 $user =~ s/^User://;
2036  
2037 my $res = $self->{api}->api({
2038 1 action => 'query',
2039 list => 'users',
2040 ususers => $user,
2041 usprop => 'groups',
2042 ustoken => 'userrights',
2043 });
2044 1 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 1 1
2045  
2046 1 foreach my $res_user (@{ $res->{query}->{users} }) {
2047 1 ▶ next unless $res_user->{name} eq $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $res_user->{"name"} eq $user 0 1 1
2048  
2049 # Cache the userrights token on the assumption that we'll use it shortly to change the rights
2050 $self->{userrightscache} = {
2051 user => $user,
2052 token => $res_user->{userrightstoken},
2053 groups => $res_user->{groups},
2054 1 };
2055  
2056 1 return @{ $res_user->{groups} }; # SUCCESS
2057 }
2058  
2059 0 return $self->_handle_api_error({ code => ERR_API, details => qq{Results for $user weren't returned by the API} });
2060 }
2061  
2062  
2063 1 ▶sub get_mw_version {
Subroutine get_mw_version: called CC=5 SCAR=18.9
Pod documented
2064 1 my $self = shift;
2065 1 my $hash = {
2066 'action' => 'query',
2067 'meta' => 'siteinfo',
2068 'siprop' => 'general',
2069 };
2070 1 my $res = $self->{api}->api($hash);
2071 1 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 1 1
2072 1 my $version = $res->{'query'}{'general'}{'generator'};
2073 1 ▶ if(defined $version && $version =~ /^MediaWiki (([0-9]+)\.([0-9]+)(?:\.([0-9]+))?+)/){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version and $version =~ /^MediaWiki (([0-9]+)\.([0-9]+)(?:\.([0-9]+))?+)/) { } 1 0 1
Condition33%1/3
defined $version and $version =~ /^MediaWiki (([0-9]+)\.([0-9]+)(?:\.([0-9]+))?+)/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $version and $version =~ /^MediaWiki (([0-9]+)\.([0-9]+)(?:\.([0-9]+))?+)/
defined $version $version =~ /^MediaWiki (([0-9]+)\.([0-9]+)(?:\.([0-9]+))?+)/
Truth table33%1/3
A and B
A= defined $versionB= $version =~ /^MediaWiki (([0-9]+)\.([0-9]+)(?:\.([0-9]+))?+)/
ABresult
0X0
100
111
2074 1 $self->{'mw_version'} = {
2075 major => $2,
2076 minor => $3,
2077 patch => $4,
2078 string => $1,
2079 };
2080 }else{
2081 0 ▶ warn "could not fetch MediaWiki version.\n" if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 0 0
2082 0 return;
2083 }
2084 1 return {%{$self->{'mw_version'}}}; # don't return ref to member
2085 }
2086  
2087  
2088 ################
2089 # Internal use #
2090 ################
2091  
2092 6 ▶sub _get_edittoken { # Actually returns ($token, $base_timestamp, $start_timestamp)
Subroutine _get_edittoken: called CC=4 SCAR=14.1
2093 6 my $self = shift;
2094 6 ▶ my $page = shift || 'Main Page';
Condition50%1/2
shift() || 'Main Page'
l!l
60
MC/DC0%0/1
shift() || 'Main Page'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
2095 6 ▶ my $type = shift || 'csrf';
Condition50%1/2
shift() || 'csrf'
l!l
06
MC/DC0%0/1
shift() || 'csrf'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
2096  
2097 my $res = $self->{api}->api({
2098 6 ▶ action => 'query',
Branch: 1 branch
BranchTrueFalseTotal
my $res = $self->{"api"}->api({"action", "query", "meta", "siteinfo|tokens", "titles", $page, "prop", "revisions", "rvprop", "timestamp", "type", $type}) or return $self->_handle_api_error 6 0 6
2099 meta => 'siteinfo|tokens',
2100 titles => $page,
2101 prop => 'revisions',
2102 rvprop => 'timestamp',
2103 type => $type,
2104 }) or return $self->_handle_api_error();
2105  
2106 6 my $data = ( %{ $res->{query}->{pages} })[1];
2107 6 my $base_timestamp = $data->{revisions}[0]->{timestamp};
2108 6 my $start_timestamp = $res->{query}->{general}->{time};
2109 6 my $token = $res->{query}->{tokens}->{"${type}token"};
2110  
2111 6 return ($token, $base_timestamp, $start_timestamp);
2112 }
2113  
2114 8 ▶sub _handle_api_error {
Subroutine _handle_api_error: called CC=6 SCAR=21.6
2115 8 my $self = shift;
2116 8 my $error = shift;
2117  
2118 8 $self->{error} = {};
2119  
2120 carp 'Error code '
2121 . $self->{api}->{error}->{code}
2122 . ': '
2123 8 ▶ . $self->{api}->{error}->{details} if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 8 8
2124 $self->{error} =
2125 (defined $error and ref $error eq 'HASH' and exists $error->{code} and exists $error->{details})
2126 ? $error
2127 8 ▶ : $self->{api}->{error};
Branch: 1 branch
BranchTrueFalseTotal
defined $error && ref $error eq 'HASH' && exists $error->{'code'} && exists $error->{'details'} ? : 0 8 8
Condition33%3/9
defined $error and ref $error eq "HASH" and exists $error->{"code"} and exists $error->{"details"}
!ll&&!rl&&r
800
defined $error and ref $error eq "HASH" and exists $error->{"code"}
!ll&&!rl&&r
800
defined $error and ref $error eq "HASH"
!ll&&!rl&&r
800
MC/DC0%0/4
defined $error and ref $error eq "HASH" and exists $error->{"code"} and exists $error->{"details"}
defined $error ref $error eq "HASH" exists $error->{"code"} exists $error->{"details"}
Truth table20%1/5
A and B and C and D
A= defined $errorB= ref $error eq "HASH"C= exists $error->{"code"}D= exists $error->{"details"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2128  
2129 8 return undef;
2130 }
2131  
2132 0 ▶sub _is_loggedin {
Subroutine _is_loggedin: not called CC=3 SCAR=24.8
2133 0 my $self = shift;
2134  
2135 0 ▶ my $is = $self->_whoami() || return $self->_handle_api_error();
Condition0%0/2
$self->_whoami || (return $self->_handle_api_error)
l!l
00
MC/DC0%0/1
$self->_whoami || (return $self->_handle_api_error)
$self->_whoami
Truth table0%0/2
A
A= $self->_whoami
Aresult
11
00
2136 0 my $ought = $self->{username};
2137 0 ▶ warn "Testing if logged in: we are $is, and we should be $ought" if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 0 0
2138 0 return ($is eq $ought);
2139 }
2140  
2141 0 ▶sub _whoami {
Subroutine _whoami: not called CC=2 SCAR=17.9
2142 0 my $self = shift;
2143  
2144 my $res = $self->{api}->api({
2145 0 ▶ action => 'query',
Branch: 1 branch
BranchTrueFalseTotal
my $res = $self->{"api"}->api({"action", "query", "meta", "userinfo"}) or return $self->_handle_api_error 0 0 0
2146 meta => 'userinfo',
2147 }) or return $self->_handle_api_error();
2148  
2149 0 return $res->{query}->{userinfo}->{name};
2150 }
2151  
2152 0 ▶sub _do_autoconfig {
Subroutine _do_autoconfig: not called CC=16 SCAR=56.1
2153 0 my $self = shift;
2154  
2155 # http://en.wikipedia.org/w/api.php?action=query&meta=userinfo&uiprop=rights|groups
2156 0 my $hash = {
2157 action => 'query',
2158 meta => 'userinfo',
2159 uiprop => 'rights|groups',
2160 };
2161 0 my $res = $self->{api}->api($hash);
2162 0 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
2163 0 ▶ return $self->_handle_api_error() unless $res->{query};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"query"} 0 0 0
2164 0 ▶ return $self->_handle_api_error() unless $res->{query}->{userinfo};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"query"}{"userinfo"} 0 0 0
2165 0 ▶ return $self->_handle_api_error() unless $res->{query}->{userinfo}->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"query"}{"userinfo"}{"name"} 0 0 0
2166  
2167 0 my $is = $res->{query}->{userinfo}->{name};
2168 0 my $ought = $self->{username};
2169  
2170 # Should we try to recover by logging in again? croak?
2171 0 ▶ carp "We're logged in as $is but we should be logged in as $ought" if ($is ne $ought);
Branch: 1 branch
BranchTrueFalseTotal
if $is ne $ought 0 0 0
2172  
2173 0 ▶ my @rights = @{ $res->{query}->{userinfo}->{rights} || [] };
Condition0%0/2
$res->{'query'}{'userinfo'}{'rights'} || []
l!l
00
MC/DC0%0/1
$res->{'query'}{'userinfo'}{'rights'} || []
$res->{'query'}{'userinfo'}{'rights'}
Truth table0%0/2
A
A= $res->{'query'}{'userinfo'}{'rights'}
Aresult
11
00
2174 0 my $has_bot = 0;
2175 0 my $default_assert = 'user'; # At a *minimum*, the bot should be logged in.
2176 0 foreach my $right (@rights) {
2177 0 ▶ if ($right eq 'bot') {
Branch: 1 branch
BranchTrueFalseTotal
if ($right eq "bot") 0 0 0
2178 0 $has_bot = 1;
2179 0 $default_assert = 'bot';
2180 }
2181 }
2182  
2183 0 ▶ my @groups = @{ $res->{query}->{userinfo}->{groups} || [] }; # there may be no groups
Condition0%0/2
$res->{'query'}{'userinfo'}{'groups'} || []
l!l
00
MC/DC0%0/1
$res->{'query'}{'userinfo'}{'groups'} || []
$res->{'query'}{'userinfo'}{'groups'}
Truth table0%0/2
A
A= $res->{'query'}{'userinfo'}{'groups'}
Aresult
11
00
2184 0 my $is_sysop = 0;
2185 0 foreach my $group (@groups) {
2186 0 ▶ if ($group eq 'sysop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($group eq "sysop") 0 0 0
2187 0 $is_sysop = 1;
2188 }
2189 }
2190  
2191 0 ▶ unless ($has_bot && !$is_sysop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_bot and not $is_sysop) 0 0 0
Condition0%0/3
$has_bot and not $is_sysop
!ll&&!rl&&r
000
MC/DC0%0/2
$has_bot and not $is_sysop
$has_bot not $is_sysop
Truth table0%0/3
A and not(B)
A= $has_botB= not $is_sysop
ABresult
0X0
100
111
2192 0 ▶ warn "$is doesn't have a bot flag; edits will be visible in RecentChanges" if $self->{debug} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 1 0 0 0
2193 }
2194 0 ▶ $self->{assert} = $default_assert unless $self->{assert};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"assert"} 0 0 0
2195  
2196 0 return RET_TRUE;
2197 }
2198  
2199 1 ▶sub _get_sitematrix {
Subroutine _get_sitematrix: called CC=13 SCAR=25.8
2200 1 my $self = shift;
2201  
2202 1 my $res = $self->{api}->api({ action => 'sitematrix' });
2203 1 ▶ return $self->_handle_api_error() unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 1 1
2204 1 my %sitematrix = %{ $res->{sitematrix} };
2205  
2206 # This hash is a monstrosity (see http://sprunge.us/dfBD?pl), and needs
2207 # lots of post-processing to have a sane data structure :\
2208 1 my %by_db;
2209 1 SECTION: foreach my $hashref (%sitematrix) {
2210 752 ▶ if (ref $hashref ne 'HASH') { # Yes, there are non-hashrefs in here, wtf?!
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hashref ne "HASH") 378 374 752
2211 378 ▶ if ($hashref eq 'specials') {
Branch: 1 branch
BranchTrueFalseTotal
if ($hashref eq "specials") 1 377 378
2212 1 SPECIAL: foreach my $special (@{ $sitematrix{specials} }) {
2213 next SPECIAL
2214 if (exists($special->{private})
2215 124 ▶ or exists($special->{fishbowl}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $special->{"private"} or exists $special->{"fishbowl"} 63 61 124
Condition100%3/3
exists $special->{"private"} or exists $special->{"fishbowl"}
l!l&&r!l&&!r
441961
MC/DC100%2/2
exists $special->{"private"} or exists $special->{"fishbowl"}
exists $special->{"private"} exists $special->{"fishbowl"}
Truth table100%3/3
A or B
A= exists $special->{"private"}B= exists $special->{"fishbowl"}
ABresult
1X1
011
000
2216  
2217 61 my $db = $special->{code};
2218 61 my $domain = $special->{url};
2219 61 $domain =~ s,^http://,,;
2220  
2221 61 $by_db{$db} = $domain;
2222 }
2223 }
2224 378 next SECTION;
2225 }
2226  
2227 374 my $lang = $hashref->{code};
2228  
2229 374 WIKI: foreach my $wiki_ref ($hashref->{site}) {
2230 374 WIKI2: foreach my $wiki_ref2 (@$wiki_ref) {
2231 949 my $family = $wiki_ref2->{code};
2232 949 my $domain = $wiki_ref2->{url};
2233 949 $domain =~ s,^http://,,;
2234  
2235 949 my $db = $lang . $family; # Is simple concatenation /always/ correct?
2236  
2237 949 $by_db{$db} = $domain;
2238 }
2239 }
2240 }
2241  
2242 # Now filter out closed wikis
2243 1 my $response = $self->{api}->{ua}->get('http://noc.wikimedia.org/conf/closed.dblist');
2244 1 ▶ if ($response->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 0 1 1
2245 0 my @closed_list = split(/\n/, $response->decoded_content);
2246 0 CLOSED: foreach my $closed (@closed_list) {
2247 0 delete($by_db{$closed});
2248 }
2249 }
2250  
2251 # Now merge in the reverse, so you can look up by domain as well as db
2252 1 my %by_domain;
2253 1 ▶ while (my ($key, $value) = each %by_db) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %by_db) 1010 1 1011
2254 1010 $by_domain{$value} = $key;
2255 }
2256 1 %by_db = (%by_db, %by_domain);
2257  
2258 # This could be saved to disk with Storable. Next time you call this
2259 # method, if mtime is less than, say, 14d, you could load it from
2260 # disk instead of over network.
2261 1 $self->{sitematrix} = \%by_db;
2262  
2263 1 return $self->{sitematrix};
2264 }
2265  
2266 17 ▶sub _get_ns_data {
Subroutine _get_ns_data: called CC=2 SCAR=6.9
2267 17 my $self = shift;
2268  
2269 # If we have it already, return the cached data
2270 17 ▶ return $self->{ns_data} if exists $self->{ns_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"ns_data"} 13 4 17
2271  
2272 # If we haven't returned by now, we have to ask the API
2273 4 my %ns_data = $self->get_namespace_names();
2274 4 my %reverse = reverse %ns_data;
2275 4 %ns_data = (%ns_data, %reverse);
2276 4 $self->{ns_data} = \%ns_data; # Save for later use
2277  
2278 4 return $self->{ns_data};
2279 }
2280  
2281 3 ▶sub _get_ns_alias_data {
Subroutine _get_ns_alias_data: called CC=2 SCAR=6.9
2282 3 my $self = shift;
2283  
2284 3 ▶ return $self->{ns_alias_data} if exists $self->{ns_alias_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"ns_alias_data"} 1 2 3
2285  
2286 my $ns_res = $self->{api}->api({
2287 2 action => 'query',
2288 meta => 'siteinfo',
2289 siprop => 'namespacealiases|namespaces',
2290 });
2291  
2292 my %ns_alias_data =
2293 map { # Map namespace alias names like "WP" to the canonical namespace name
2294 # from the "namespaces" part of the response
2295 $_->{ns_alias} => $ns_res->{query}->{namespaces}->{ $_->{ns_number} }->{canonical}
2296 8 }
2297 map { # Map namespace alias names (from the "namespacealiases" part of the response)
2298 # like "WP" to the namespace number (usd to look up canonical data in the
2299 # "namespaces" part of the response)
2300 { ns_alias => $_->{'*'}, ns_number => $_->{id} }
2301 2 } @{ $ns_res->{query}->{namespacealiases} };
2302  
2303 2 $self->{ns_alias_data} = \%ns_alias_data;
2304 2 return $self->{ns_alias_data};
2305 }
2306  
2307  
2308 51 1;
2309  
2310 __END__
↑ Summary Bot/Constants.pm »