| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $ao = exists($_[0]->{'ao'}) ? $_[0]->{'ao'} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my $reason = $_[0]->{'reason'} || 'cross-wiki abuse'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_[0]{'reason'} || 'cross-wiki abuse'
MC/DC0%0/1
$_[0]{'reason'} || 'cross-wiki abuse'
$_[0]{'reason'}
Truth table0%0/2
A
A= $_[0]{'reason'}
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $expiry = $_[0]->{'expiry'} || '31 hours'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_[0]{'expiry'} || '31 hours'
MC/DC0%0/1
$_[0]{'expiry'} || '31 hours'
$_[0]{'expiry'}
Truth table0%0/2
A
A= $_[0]{'expiry'}
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $clobber = exists($_[0]->{'clobber'}) ? $_[0]->{'clobber'} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | my $start; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ($ip =~ m/-/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$ip =~ m[/\d\d$] or cidrvalidate($ip)
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)
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | carp "Invalid IP $ip" if $self->{'debug'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ($clobber and $res->decoded_content() =~ 'already blocked globally') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$clobber and $res->decoded_content =~ /already blocked globally/
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/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my $reason = $_[0]->{'reason'} || 'Removing obsolete block'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_[0]{'reason'} || 'Removing obsolete block'
MC/DC0%0/1
$_[0]{'reason'} || 'Removing obsolete block'
$_[0]{'reason'}
Truth table0%0/2
A
A= $_[0]{'reason'}
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | my $start; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($ip =~ m/-/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$ip =~ m[/\d\d$] or cidrvalidate($ip)
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)
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | carp "Invalid IP $ip" if $self->{'debug'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if ($start) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | carp "Couldn't find the matching rangeblock" if $self->{'debug'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 132 | 0 | my $error = $self->_screenscrape_error($res->decoded_content()); | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | carp $error if $self->{'debug'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | my $hide = $_[0]->{'hide'} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_[0]{'hide'} || 0
MC/DC0%0/1
$_[0]{'hide'} || 0
$_[0]{'hide'}
Truth table0%0/2
A
A= $_[0]{'hide'}
| |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | my $reason = $_[0]->{'reason'} || 'cross-wiki abuse'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_[0]{'reason'} || 'cross-wiki abuse'
MC/DC0%0/1
$_[0]{'reason'} || 'cross-wiki abuse'
$_[0]{'reason'}
Truth table0%0/2
A
A= $_[0]{'reason'}
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | my $lock = defined($_[0]->{'lock'}) ? $_[0]->{'lock'} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ($hide == 0) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 162 | 0 | my $error = $self->_screenscrape_error($res->decoded_content()); | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | carp $error if $self->{'debug'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 175 | 0 | my $error = $self->_screenscrape_error($res->decoded_content()); | |||||||||||||||||||||||||||
| 176 | 0 | ▶ | carp $error if $self->{'debug'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | my $hide = $_[0]->{'hide'} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_[0]{'hide'} || 0
MC/DC0%0/1
$_[0]{'hide'} || 0
$_[0]{'hide'}
Truth table0%0/2
A
A= $_[0]{'hide'}
| |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | my $reason = $_[0]->{'reason'} || 'Removing obsolete account lock'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_[0]{'reason'} || 'Removing obsolete account lock'
MC/DC0%0/1
$_[0]{'reason'} || 'Removing obsolete account lock'
$_[0]{'reason'}
Truth table0%0/2
A
A= $_[0]{'reason'}
| |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | my $lock = defined($_[0]->{'lock'}) ? $_[0]->{'lock'} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
ref $res eq "HTTP::Response" and $res->is_success
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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 227 | 0 | ▶ | my $extra = shift || '&uselang=en&useskin=monobook'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || '&uselang=en&useskin=monobook'
MC/DC0%0/1
shift() || '&uselang=en&useskin=monobook'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 0 | ▶ | $page = uri_escape_utf8($page) unless $no_escape; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 0 | my $url = "http://$self->{host}/$self->{path}/index.php?title=$page"; | |||||||||||||||||||||||||||
| 232 | 0 | ▶ | $url .= $extra if $extra; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 0 | ▶ | print "Retrieving $url\n" if $self->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | my $res = $self->{'mech'}->get($url); | |||||||||||||||||||||||||||
| 236 | 0 | ▶ | return unless (ref($res) eq 'HTTP::Response' && $res->is_success()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $res eq "HTTP::Response" and $res->is_success
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
| |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | ▶ | if ($res->decoded_content() =~ m/class="error"/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 0 | my $error = $self->_screenscrape_error($res->decoded_content()); | |||||||||||||||||||||||||||
| 240 | 0 | ▶ | carp $error if $self->{'debug'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||