blib/lib/MediaWiki/Bot/Plugin/Steward.pm

filter: Statementstmt 16.7% 24 / 143 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 46.6% 7 / 15 Podpod 100% 5 / 5 totaltotal 12.2% 36 / 295 CC 55 SCAR 48.9
CC
55
Cov
9%
g_block
240.0
g_unblock
182.0
ca_lock
156.0
CRAP 132.9
↑ Summary
1 package MediaWiki::Bot::Plugin::Steward;
2 # ABSTRACT: A plugin to MediaWiki::Bot providing steward functions
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Net::CIDR qw(range2cidr cidrvalidate);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use URI::Escape qw(uri_escape_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use WWW::Mechanize 1.30;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = 0.0003;
13  
14  
15  
16 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 our @EXPORT = qw(steward_new g_block g_unblock ca_lock ca_unlock _screenscrape_get _screenscrape_put _screenscrape_error _screenscrape_login);
18  
19  
20 0 ▶sub steward_new {
Subroutine steward_new: not called CC=3 SCAR=24.8
Pod documented
21 0 my $self = shift;
22  
23 my $mech = WWW::Mechanize->new(
24 onerror => \&Carp::carp,
25 stack_depth => 1,
26 0 agent => $self->{'useragent'},
27 );
28 0 my $cookies = ".mediawiki-bot-$self->{'username'}-cookies";
29 0 ▶ if (-r $cookies) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $cookies) { } 0 0 0
30 0 $mech->{'cookie_jar'}->load($cookies);
31 0 $mech->{'cookie_jar'}->{'ignore_discard'} = 1;
32 }
33 else {
34 0 croak "$cookies doesn't exist or isn't readable";
35 }
36 0 $self->{'mech'} = $mech;
37  
38 0 my $check_login = $self->_screenscrape_get('Special:BlankPage');
39 0 ▶ if ($check_login->decoded_content() =~ m/wgUserName="\Q$self->{'username'}\E"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_login->decoded_content =~ /wgUserName="\Q$self->{'username'}\E"/) { } 0 0 0
40 0 return 1;
41 }
42 else {
43 0 croak "Steward plugin couldn't log in";
44 }
45 }
46  
47  
48 0 ▶sub g_block {
Subroutine g_block: not called CC=15 SCAR=54.8
Pod documented
49 0 my $self = shift;
50 0 ▶ my $ip = ref $_[0] eq 'HASH' ? $_[0]->{'ip'} : shift; # Allow giving just an IP
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
51 0 ▶ my $ao = exists($_[0]->{'ao'}) ? $_[0]->{'ao'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{'ao'} ? : 0 0 0
52 0 ▶ my $reason = $_[0]->{'reason'} || 'cross-wiki abuse';
Condition0%0/2
$_[0]{'reason'} || 'cross-wiki abuse'
l!l
00
MC/DC0%0/1
$_[0]{'reason'} || 'cross-wiki abuse'
$_[0]{'reason'}
Truth table0%0/2
A
A= $_[0]{'reason'}
Aresult
11
00
53 0 ▶ my $expiry = $_[0]->{'expiry'} || '31 hours';
Condition0%0/2
$_[0]{'expiry'} || '31 hours'
l!l
00
MC/DC0%0/1
$_[0]{'expiry'} || '31 hours'
$_[0]{'expiry'}
Truth table0%0/2
A
A= $_[0]{'expiry'}
Aresult
11
00
54 0 ▶ my $clobber = exists($_[0]->{'clobber'}) ? $_[0]->{'clobber'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{'clobber'} ? : 0 0 0
55  
56 0 my $start;
57 0 ▶ if ($ip =~ m/-/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ip =~ /-/) { } 0 0 0
elsif ($ip =~ m[/\d\d$]) { } 0 0 0
58 0 $start = (split(/\-/, $ip, 2))[0];
59 0 $ip = range2cidr($start);
60 }
61 elsif ($ip =~ m,/\d\d$,) {
62 0 $start = $ip;
63 0 $start =~ s,/\d\d$,,;
64 }
65 0 ▶ unless ($ip =~ m,/\d\d$, || cidrvalidate($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip =~ m[/\d\d$] or cidrvalidate($ip)) 0 0 0
Condition0%0/3
$ip =~ m[/\d\d$] or cidrvalidate($ip)
l!l&&r!l&&!r
000
MC/DC0%0/2
$ip =~ m[/\d\d$] or cidrvalidate($ip)
$ip =~ m[/\d\d$] cidrvalidate($ip)
Truth table0%0/3
A or B
A= $ip =~ m[/\d\d$]B= cidrvalidate($ip)
ABresult
1X1
011
000
66 0 ▶ carp "Invalid IP $ip" if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
67 }
68  
69 0 my $opts = {
70 'wpAddress' => $ip, # mw-globalblock-address
71 'wpExpiryOther' => $expiry, # mw-globalblock-expiry-other
72 'wpReason' => $reason, # mw-globalblock-reason
73 'wpAnonOnly' => $ao, # mw-globalblock-anon-only
74 };
75 0 my $res = $self->_screenscrape_put('Special:GlobalBlock', $opts, 1);
76 0 ▶ if ($res->decoded_content() =~ m/class="error"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->decoded_content =~ /class="error"/) 0 0 0
77 0 ▶ if ($clobber and $res->decoded_content() =~ 'already blocked globally') {
Branch: 1 branch
BranchTrueFalseTotal
if ($clobber and $res->decoded_content =~ /already blocked globally/) { } 0 0 0
Condition0%0/3
$clobber and $res->decoded_content =~ /already blocked globally/
!ll&&!rl&&r
000
MC/DC0%0/2
$clobber and $res->decoded_content =~ /already blocked globally/
$clobber $res->decoded_content =~ /already blocked globally/
Truth table0%0/3
A and B
A= $clobberB= $res->decoded_content =~ /already blocked globally/
ABresult
0X0
100
111
78 # Resubmit unless noclobber
79 0 $res = $self->{'mech'}->submit_form(
80 with_fields => $opts,
81 );
82 }
83 else {
84 0 my $error = $self->_screenscrape_error($res->decoded_content());
85 0 ▶ carp $error if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
86 0 return;
87 }
88 }
89  
90 0 return $res;
91 }
92  
93  
94 0 ▶sub g_unblock {
Subroutine g_unblock: not called CC=13 SCAR=52.0
Pod documented
95 0 my $self = shift;
96 0 ▶ my $ip = ref $_[0] eq 'HASH' ? $_[0]->{'ip'} : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
97 0 ▶ my $reason = $_[0]->{'reason'} || 'Removing obsolete block';
Condition0%0/2
$_[0]{'reason'} || 'Removing obsolete block'
l!l
00
MC/DC0%0/1
$_[0]{'reason'} || 'Removing obsolete block'
$_[0]{'reason'}
Truth table0%0/2
A
A= $_[0]{'reason'}
Aresult
11
00
98  
99 0 my $start;
100 0 ▶ if ($ip =~ m/-/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ip =~ /-/) { } 0 0 0
elsif ($ip =~ m[/\d\d$]) { } 0 0 0
101 0 $start = (split(/\-/, $ip, 2))[0];
102 0 $ip = range2cidr($start);
103 }
104 elsif ($ip =~ m,/\d\d$,) {
105 0 $start = $ip;
106 0 $start =~ s,/\d\d$,,;
107 }
108 0 ▶ unless ($ip =~ m,/\d\d$, || cidrvalidate($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip =~ m[/\d\d$] or cidrvalidate($ip)) 0 0 0
Condition0%0/3
$ip =~ m[/\d\d$] or cidrvalidate($ip)
l!l&&r!l&&!r
000
MC/DC0%0/2
$ip =~ m[/\d\d$] or cidrvalidate($ip)
$ip =~ m[/\d\d$] cidrvalidate($ip)
Truth table0%0/3
A or B
A= $ip =~ m[/\d\d$]B= cidrvalidate($ip)
ABresult
1X1
011
000
109 0 ▶ carp "Invalid IP $ip" if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
110 }
111  
112 0 ▶ if ($start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start) 0 0 0
113 # When rangeblocks are placed, the CIDR gets normalized - so you cannot unblock
114 # the same range you blocked. You'll need to do some kind of lookup. Probably,
115 # you can convert CIDR to A-B range, take the first IP, see whether it is blocked
116 # and what rangeblock affects it, then unblock that.
117  
118 0 $ip = $self->is_g_blocked($start);
119 0 ▶ unless ($ip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip) 0 0 0
120 0 ▶ carp "Couldn't find the matching rangeblock" if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
121 0 return;
122 }
123 }
124  
125 0 my $opts = {
126 'address' => $ip,
127 'wpReason' => $reason,
128 };
129 0 my $res = $self->_screenscrape_put('Special:GlobalUnblock', $opts, 1);
130  
131 0 ▶ if ($res->decoded_content() =~ m/class="error"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->decoded_content =~ /class="error"/) 0 0 0
132 0 my $error = $self->_screenscrape_error($res->decoded_content());
133 0 ▶ carp $error if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
134 0 return;
135 }
136  
137 0 return 1;
138 }
139  
140  
141 0 ▶sub ca_lock {
Subroutine ca_lock: not called CC=12 SCAR=50.5
Pod documented
142 0 my $self = shift;
143 0 ▶ my $user = ref $_[0] eq 'HASH' ? $_[0]->{'user'} : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
144 0 ▶ my $hide = $_[0]->{'hide'} || 0;
Condition0%0/2
$_[0]{'hide'} || 0
l!l
00
MC/DC0%0/1
$_[0]{'hide'} || 0
$_[0]{'hide'}
Truth table0%0/2
A
A= $_[0]{'hide'}
Aresult
11
00
145 0 ▶ my $reason = $_[0]->{'reason'} || 'cross-wiki abuse';
Condition0%0/2
$_[0]{'reason'} || 'cross-wiki abuse'
l!l
00
MC/DC0%0/1
$_[0]{'reason'} || 'cross-wiki abuse'
$_[0]{'reason'}
Truth table0%0/2
A
A= $_[0]{'reason'}
Aresult
11
00
146 0 ▶ my $lock = defined($_[0]->{'lock'}) ? $_[0]->{'lock'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'lock'} ? : 0 0 0
147  
148 0 ▶ if ($hide == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($hide == 0) { } 0 0 0
elsif ($hide == 1) { } 0 0 0
elsif ($hide == 2) { } 0 0 0
149 0 $hide = '';
150 }
151 elsif ($hide == 1) {
152 0 $hide = 'lists';
153 }
154 elsif ($hide == 2) {
155 0 $hide = 'suppressed';
156 }
157 0 $user =~ s/^User://i;
158 0 $user =~ s/\@global$//i;
159  
160 0 my $res = $self->_screenscrape_put("Special:CentralAuth", {target=>$user}, 1);
161 0 ▶ if ($res->decoded_content() =~ m/class="error"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->decoded_content =~ /class="error"/) 0 0 0
162 0 my $error = $self->_screenscrape_error($res->decoded_content());
163 0 ▶ carp $error if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
164 0 return;
165 }
166  
167 0 $res = $self->{'mech'}->submit_form(
168 with_fields => {
169 wpStatusLocked => $lock,
170 wpStatusHidden => $hide,
171 wpReason => $reason,
172 },
173 );
174 0 ▶ if ($res->decoded_content() =~ m/class="error"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->decoded_content =~ /class="error"/) 0 0 0
175 0 my $error = $self->_screenscrape_error($res->decoded_content());
176 0 ▶ carp $error if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
177 0 return;
178 }
179  
180 0 return $res;
181 }
182  
183  
184 0 ▶sub ca_unlock {
Subroutine ca_unlock: not called CC=5 SCAR=34.0
Pod documented
185 0 my $self = shift;
186 0 ▶ my $user = ref $_[0] eq 'HASH' ? $_[0]->{'user'} : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
187 0 ▶ my $hide = $_[0]->{'hide'} || 0;
Condition0%0/2
$_[0]{'hide'} || 0
l!l
00
MC/DC0%0/1
$_[0]{'hide'} || 0
$_[0]{'hide'}
Truth table0%0/2
A
A= $_[0]{'hide'}
Aresult
11
00
188 0 ▶ my $reason = $_[0]->{'reason'} || 'Removing obsolete account lock';
Condition0%0/2
$_[0]{'reason'} || 'Removing obsolete account lock'
l!l
00
MC/DC0%0/1
$_[0]{'reason'} || 'Removing obsolete account lock'
$_[0]{'reason'}
Truth table0%0/2
A
A= $_[0]{'reason'}
Aresult
11
00
189 0 ▶ my $lock = defined($_[0]->{'lock'}) ? $_[0]->{'lock'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'lock'} ? : 0 0 0
190  
191 0 return $self->ca_lock({
192 user => $user,
193 hide => $hide,
194 reason => $reason,
195 lock => $lock,
196 });
197 }
198  
199  
200 ################
201 # Internal use #
202 ################
203  
204 # Submits a form screenscrape-style (barf!)
205 0 ▶sub _screenscrape_put {
Subroutine _screenscrape_put: not called CC=3 SCAR=24.8
206 0 my $self = shift;
207 0 my $page = shift;
208 0 my $options = shift;
209 0 my $no_esc = shift;
210 0 my $extra = shift;
211  
212 0 my $res = $self->_screenscrape_get($page, $no_esc, $extra);
213 0 ▶ return unless (ref($res) eq 'HTTP::Response' && $res->is_success);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res eq "HTTP::Response" and $res->is_success 0 0 0
Condition0%0/3
ref $res eq "HTTP::Response" and $res->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
ref $res eq "HTTP::Response" and $res->is_success
ref $res eq "HTTP::Response" $res->is_success
Truth table0%0/3
A and B
A= ref $res eq "HTTP::Response"B= $res->is_success
ABresult
0X0
100
111
214  
215 0 $res = $self->{'mech'}->submit_form(
216 with_fields => $options,
217 );
218  
219 0 return $res;
220 }
221  
222 # Gets a page screenscrape-style (barf!)
223 0 ▶sub _screenscrape_get {
Subroutine _screenscrape_get: not called CC=10 SCAR=47.0
224 0 my $self = shift;
225 0 my $page = shift;
226 0 ▶ my $no_escape = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
227 0 ▶ my $extra = shift || '&uselang=en&useskin=monobook';
Condition0%0/2
shift() || '&uselang=en&useskin=monobook'
l!l
00
MC/DC0%0/1
shift() || '&uselang=en&useskin=monobook'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
228  
229 0 ▶ $page = uri_escape_utf8($page) unless $no_escape;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_escape 0 0 0
230  
231 0 my $url = "http://$self->{host}/$self->{path}/index.php?title=$page";
232 0 ▶ $url .= $extra if $extra;
Branch: 1 branch
BranchTrueFalseTotal
if $extra 0 0 0
233 0 ▶ print "Retrieving $url\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
234  
235 0 my $res = $self->{'mech'}->get($url);
236 0 ▶ return unless (ref($res) eq 'HTTP::Response' && $res->is_success());
Branch: 1 branch
BranchTrueFalseTotal
unless ref $res eq "HTTP::Response" and $res->is_success 0 0 0
Condition0%0/3
ref $res eq "HTTP::Response" and $res->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
ref $res eq "HTTP::Response" and $res->is_success
ref $res eq "HTTP::Response" $res->is_success
Truth table0%0/3
A and B
A= ref $res eq "HTTP::Response"B= $res->is_success
ABresult
0X0
100
111
237  
238 0 ▶ if ($res->decoded_content() =~ m/class="error"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->decoded_content =~ /class="error"/) 0 0 0
239 0 my $error = $self->_screenscrape_error($res->decoded_content());
240 0 ▶ carp $error if $self->{'debug'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
241 0 return;
242 }
243  
244 0 return $res;
245 }
246  
247 # Returns the text of the first div with class 'error'
248 0 ▶sub _screenscrape_error {
Subroutine _screenscrape_error: not called CC=1 SCAR=6.9
249 0 my $self = shift;
250 0 my $html = shift;
251  
252 0 require HTML::TreeBuilder;
253 0 my $tree = HTML::TreeBuilder->new_from_content($html);
254 0 my $error = $tree->look_down(
255 '_tag', 'div',
256 'class', 'error'
257 );
258  
259 0 my $error_text = $error->as_text();
260 0 $self->{'error'}->{'details'} = $error_text;
261 0 $self->{'error'}->{'code'} = 3;
262 0 return $error_text;
263 }
264  
265  
266 3 1;
267  
268  
269  
270 =pod
271  
272 =head1 NAME
273  
274 MediaWiki::Bot::Plugin::Steward - A plugin to MediaWiki::Bot providing steward functions
275  
276 =head1 VERSION
277  
278 version 0.0003
279  
280 =head1 SYNOPSIS
281  
282 use MediaWiki::Bot;
283 my $bot = MediaWiki::Bot->new({
284 operator => 'Mike.lifeguard',
285 assert => 'bot',
286 protocol => 'https',
287 host => 'secure.wikimedia.org',
288 path => 'wikipedia/meta/w',
289 login_data => { username => "Mike.lifeguard", password => $pass },
290 });
291 $bot->g_block({
292 ip => '127.0.0.1',
293 ao => 0,
294 summary => 'bloody vandals...',
295 });
296  
297 =head1 DESCRIPTION
298  
299 A plugin to the MediaWiki::Bot framework to provide steward functions to a bot.
300  
301 =head1 METHODS
302  
303 =head2 import()
304  
305 Calling import from any module will, quite simply, transfer these subroutines into that module's namespace. This is possible from any module which is compatible with MediaWiki::Bot.
306  
307 =head2 steward_new($data_hashref)
308  
309 =head2 g_block($data_hashref)
310  
311 This places a global block on an IP or IP range. You can provide either CIDR or classful ranges. To easily place a vandalism block, pass just the IP.
312  
313 =over 4
314  
315 =item *
316 ip - the IP or IP range to block. Use a single IP, CIDR range, or classful range.
317  
318 =item *
319 ao - whether to block anon-only; default is true.
320  
321 =item *
322 reason - the log summary. Default is 'cross-wiki abuse'.
323  
324 =item *
325 expiry - the expiry setting. Default is 31 hours.
326  
327 =back
328  
329 $bot->g_block({
330 ip => '127.0.0.1',
331 ao => 0,
332 reason => 'silly vandals',
333 expiry => '1 week',
334 });
335  
336 # Or, use defaults
337 $bot->g_block('127.0.0.0-127.0.0.255');
338  
339 =head2 g_unblock($data)
340  
341 Remove the global block affecting an IP or range. The hashref is:
342  
343 =over 4
344  
345 =item *
346 ip - the IP or range to unblock. You don't need to convert your range into a CIDR, just pass in your range in xxx.xxx.xxx.xxx-yyy.yyy.yyy.yyy format and let this method do the work.
347  
348 =item *
349 reason - the log reason. Default is 'Removing obsolete block'.
350  
351 =back
352  
353 If you pass only the IP, a generic reason will be used.
354  
355 $bot->g_unblock({
356 ip => '127.0.0.0-127.0.0.255',
357 reason => 'oops',
358 });
359 # Or
360 $bot->g_unblock('127.0.0.1');
361  
362 =head2 ca_lock($data)
363  
364 Locks and hides a user with CentralAuth. $data is a hash:
365  
366 =over 4
367  
368 =item *
369 user - the user to target
370  
371 =item *
372 lock - whether to lock or unlock the account - default is lock (0=unlocked, 1=locked)
373  
374 =item *
375 hide - how hard to hide the account - default is not at all (0=none, 1=lists, 2=oversight)
376  
377 =item *
378 reason - default is 'cross-wiki abuse'
379  
380 =back
381  
382 If you pass in only a username, the account will be locked but not hidden, and the default reason will be used:
383  
384 $bot->ca_lock("Mike.lifeguard");
385 # Or, the more complete call:
386 $bot->ca_lock({
387 user => "Mike.lifeguard",
388 reason => "test",
389 });
390  
391 =head2 ca_unlock($data)
392  
393 Same parameters as ca_lock(), but with the default setting for lock reversed (ie, default is I<unlock>).
394  
395 =head1 AUTHORS
396  
397 =over 4
398  
399 =item *
400  
401 Mike.lifeguard <mike.lifeguard@gmail.com>
402  
403 =item *
404  
405 patch and bug report contributors
406  
407 =back
408  
409 =head1 COPYRIGHT AND LICENSE
410  
411 This software is Copyright (c) 2010 by the MediaWiki::Bot team <perlwikibot@googlegroups.com>.
412  
413 This is free software, licensed under:
414  
415 The GNU General Public License, Version 3, June 2007
416  
417 =cut
418  
419  
420 __END__
↑ Summary