line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perlwikipedia; |
2
|
|
|
|
|
|
|
|
3
|
21
|
|
|
21
|
|
634713
|
use strict; |
|
21
|
|
|
|
|
57
|
|
|
21
|
|
|
|
|
1011
|
|
4
|
21
|
|
|
21
|
|
120
|
use warnings; |
|
21
|
|
|
|
|
43
|
|
|
21
|
|
|
|
|
650
|
|
5
|
21
|
|
|
21
|
|
41771
|
use WWW::Mechanize; |
|
21
|
|
|
|
|
4604224
|
|
|
21
|
|
|
|
|
884
|
|
6
|
21
|
|
|
21
|
|
317
|
use HTML::Entities; |
|
21
|
|
|
|
|
48
|
|
|
21
|
|
|
|
|
1700
|
|
7
|
21
|
|
|
21
|
|
138
|
use URI::Escape; |
|
21
|
|
|
|
|
40
|
|
|
21
|
|
|
|
|
1321
|
|
8
|
21
|
|
|
21
|
|
40301
|
use XML::Simple; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Carp; |
10
|
|
|
|
|
|
|
use Encode; |
11
|
|
|
|
|
|
|
use URI::Escape qw(uri_escape_utf8); |
12
|
|
|
|
|
|
|
use MediaWiki::API; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use Module::Pluggable search_path => [ qw(Perlwikipedia::Plugin) ], |
15
|
|
|
|
|
|
|
'require' => 1; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
foreach my $plugin (__PACKAGE__->plugins) { |
18
|
|
|
|
|
|
|
print "Found plugin $plugin\n"; |
19
|
|
|
|
|
|
|
$plugin->import(); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
our $VERSION = '1.5.2'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 NAME |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Perlwikipedia - a Wikipedia bot framework written in Perl |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 SYNOPSIS |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use Perlwikipedia; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $editor = Perlwikipedia->new('Account'); |
34
|
|
|
|
|
|
|
$editor->login('Account', 'password'); |
35
|
|
|
|
|
|
|
$editor->revert('Wikipedia:Sandbox', 'Reverting vandalism', '38484848'); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 DESCRIPTION |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Perlwikipedia is a framework that can be used to write Wikipedia bots. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Many of the methods use the MediaWiki API (L). |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 AUTHOR |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
The Perlwikipedia team (Alex Rowe, Jmax, Oleg Alexandrov) and others. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 METHODS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=over 4 |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item new([$agent[, $assert[, $operator]]]) |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Calling Perlwikipedia->new will create a new Perlwikipedia object. $agent sets a custom useragent, $assert sets a parameter for the assertedit extension, common is "&assert=bot", $operator allows the bot to send you a message when it fails an assert. The message will tell you that $agent is logged out, so use a descriptive $agent. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub new { |
58
|
|
|
|
|
|
|
my $package = shift; |
59
|
|
|
|
|
|
|
my $agent = shift || 'Perlwikipedia'; #user-specified agent or default to 'Perlwikipedia' |
60
|
|
|
|
|
|
|
my $assert = shift || undef; |
61
|
|
|
|
|
|
|
my $operator= shift || undef; |
62
|
|
|
|
|
|
|
my $maxlag = shift || 5; |
63
|
|
|
|
|
|
|
if ($operator) {$operator=~s/User://i;} #strip off namespace |
64
|
|
|
|
|
|
|
$assert=~s/\&?assert=// if $assert; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $self = bless {}, $package; |
67
|
|
|
|
|
|
|
$self->{mech} = WWW::Mechanize->new( cookie_jar => {}, onerror => \&Carp::carp, stack_depth => 1 ); |
68
|
|
|
|
|
|
|
$self->{mech}->agent("$agent/$VERSION"); |
69
|
|
|
|
|
|
|
$self->{host} = 'en.wikipedia.org'; |
70
|
|
|
|
|
|
|
$self->{path} = 'w'; |
71
|
|
|
|
|
|
|
$self->{debug} = 0; |
72
|
|
|
|
|
|
|
$self->{errstr} = ''; |
73
|
|
|
|
|
|
|
$self->{assert} = $assert; |
74
|
|
|
|
|
|
|
$self->{operator}=$operator; |
75
|
|
|
|
|
|
|
$self->{api} = MediaWiki::API->new(); |
76
|
|
|
|
|
|
|
$self->{api}->{config}->{api_url} = 'http://en.wikipedia.org/w/api.php'; |
77
|
|
|
|
|
|
|
$self->{api}->{config}->{max_lag} = $maxlag; |
78
|
|
|
|
|
|
|
$self->{api}->{config}->{max_lag_delay} = 1; |
79
|
|
|
|
|
|
|
$self->{api}->{config}->{retries} = 5; |
80
|
|
|
|
|
|
|
$self->{api}->{config}->{max_lag_retries} = -1; |
81
|
|
|
|
|
|
|
$self->{api}->{config}->{retry_delay} = 30; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
return $self; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=item set_highlimits([$flag]) |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Tells Perlwikipedia to start using the APIHighLimits for certain queries. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=cut |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub set_highlimits { |
93
|
|
|
|
|
|
|
my $self = shift; |
94
|
|
|
|
|
|
|
my $highlimits = shift; |
95
|
|
|
|
|
|
|
unless (defined($highlimits)) {$highlimits=1} |
96
|
|
|
|
|
|
|
$self->{highlimits}=1; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _get { |
100
|
|
|
|
|
|
|
my $self = shift; |
101
|
|
|
|
|
|
|
my $page = shift; |
102
|
|
|
|
|
|
|
my $action = shift || 'view'; |
103
|
|
|
|
|
|
|
my $extra = shift; |
104
|
|
|
|
|
|
|
my $no_escape = shift || 0; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
$page = uri_escape_utf8($page) unless $no_escape; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my $url = |
109
|
|
|
|
|
|
|
"http://$self->{host}/$self->{path}/index.php?title=$page&action=$action"; |
110
|
|
|
|
|
|
|
$url .= $extra if $extra; |
111
|
|
|
|
|
|
|
print "Retrieving $url\n" if $self->{debug}; |
112
|
|
|
|
|
|
|
my $res = $self->{mech}->get($url); |
113
|
|
|
|
|
|
|
if ( ref($res) eq 'HTTP::Response' && $res->is_success() ) { |
114
|
|
|
|
|
|
|
if ( $res->decoded_content =~ |
115
|
|
|
|
|
|
|
m/The action you have requested is limited to users in the group (.+)\./ |
116
|
|
|
|
|
|
|
) { |
117
|
|
|
|
|
|
|
my $group = $1; |
118
|
|
|
|
|
|
|
$group =~ s/<.+?>//g; |
119
|
|
|
|
|
|
|
$self->{errstr} = qq/Error requesting $page: You must be in the user group "$group"/; |
120
|
|
|
|
|
|
|
carp $self->{errstr} if $self->{debug}; |
121
|
|
|
|
|
|
|
return 1; |
122
|
|
|
|
|
|
|
} else { |
123
|
|
|
|
|
|
|
return $res; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} else { |
126
|
|
|
|
|
|
|
$self->{errstr} = "Error requesting $page: " . $res->status_line(); |
127
|
|
|
|
|
|
|
carp $self->{errstr} if $self->{debug}; |
128
|
|
|
|
|
|
|
return 1; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub _get_api { |
133
|
|
|
|
|
|
|
my $self = shift; |
134
|
|
|
|
|
|
|
my $query = shift; |
135
|
|
|
|
|
|
|
print "Retrieving http://$self->{host}/$self->{path}/api.php?$query\n" |
136
|
|
|
|
|
|
|
if $self->{debug}; |
137
|
|
|
|
|
|
|
my $res = |
138
|
|
|
|
|
|
|
$self->{mech}->get("http://$self->{host}/$self->{path}/api.php?$query"); |
139
|
|
|
|
|
|
|
if ( ref($res) eq 'HTTP::Response' && $res->is_success() ) { |
140
|
|
|
|
|
|
|
return $res; |
141
|
|
|
|
|
|
|
} else { |
142
|
|
|
|
|
|
|
$self->{errstr} = "Error requesting api.php?$query: " . $res->status_line(); |
143
|
|
|
|
|
|
|
carp $self->{errstr} if $self->{debug}; |
144
|
|
|
|
|
|
|
return 1; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub _put { |
149
|
|
|
|
|
|
|
my $self = shift; |
150
|
|
|
|
|
|
|
my $page = shift; |
151
|
|
|
|
|
|
|
my $options = shift; |
152
|
|
|
|
|
|
|
my $extra = shift; |
153
|
|
|
|
|
|
|
my $type = shift; |
154
|
|
|
|
|
|
|
my $res = $self->_get( $page, 'edit', $extra ); |
155
|
|
|
|
|
|
|
unless (ref($res) eq 'HTTP::Response' && $res->is_success) { return; } |
156
|
|
|
|
|
|
|
if ( ( $res->decoded_content ) =~ m/ |
157
|
|
|
|
|
|
|
$self->{errstr} = "Error editing $page: Page is protected"; |
158
|
|
|
|
|
|
|
carp $self->{errstr} if $self->{debug}; |
159
|
|
|
|
|
|
|
return 1; |
160
|
|
|
|
|
|
|
} elsif ( ($res->decoded_content) =~ m/The specified assertion \(.+?\) failed/) { |
161
|
|
|
|
|
|
|
$self->{errstr} = "Error editing $page: Assertion failed"; |
162
|
|
|
|
|
|
|
return 2; |
163
|
|
|
|
|
|
|
} elsif ( ($res->decoded_content) !~ /class=\"diff-lineno\">/ and $type eq 'undo') { |
164
|
|
|
|
|
|
|
$self->{errstr} = "Error editing $page: Undo failed"; |
165
|
|
|
|
|
|
|
return 3; |
166
|
|
|
|
|
|
|
} else { |
167
|
|
|
|
|
|
|
$res = $self->{mech}->submit_form( %{$options} ); |
168
|
|
|
|
|
|
|
return $res; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item set_wiki([$wiki_host[,$wiki_path]]) |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
set_wiki will cause the Perlwikipedia object to use the wiki specified, e.g set_wiki('de.wikipedia.org','w') will tell Perlwikipedia to use http://de.wikipedia.org/w/index.php. The Perlwikipedia default settings are 'en.wikipedia.org' with a path of 'w'. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub set_wiki { |
179
|
|
|
|
|
|
|
my $self = shift; |
180
|
|
|
|
|
|
|
my $host = shift || 'en.wikipedia.org'; |
181
|
|
|
|
|
|
|
my $path = shift || 'w'; |
182
|
|
|
|
|
|
|
$self->{host} = $host if $host; |
183
|
|
|
|
|
|
|
$self->{path} = $path if $path; |
184
|
|
|
|
|
|
|
$self->{api}->{config}->{api_url} = "http://$host/$path/api.php"; |
185
|
|
|
|
|
|
|
print "Wiki set to http://$self->{host}/$self->{path}\n" if $self->{debug}; |
186
|
|
|
|
|
|
|
return 0; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=item login($username,$password) |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Logs the Perlwikipedia object into the specified wiki. If the login was a success, it will return 'Success', otherwise, 'Fail'. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=cut |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub login { |
196
|
|
|
|
|
|
|
my $self = shift; |
197
|
|
|
|
|
|
|
my $editor = shift; |
198
|
|
|
|
|
|
|
my $password = shift; |
199
|
|
|
|
|
|
|
my $cookies = ".perlwikipedia-$editor-cookies"; |
200
|
|
|
|
|
|
|
$self->{mech}->cookie_jar( |
201
|
|
|
|
|
|
|
{ file => $cookies, autosave => 1 } ); |
202
|
|
|
|
|
|
|
if ( !defined $password ) { |
203
|
|
|
|
|
|
|
$self->{mech}->{cookie_jar}->load($cookies); |
204
|
|
|
|
|
|
|
my $cookies_exist = $self->{mech}->{cookie_jar}->as_string; |
205
|
|
|
|
|
|
|
if ($cookies_exist) { |
206
|
|
|
|
|
|
|
$self->{mech}->{cookie_jar}->load($cookies); |
207
|
|
|
|
|
|
|
print "Loaded MediaWiki cookies from file $cookies\n" if $self->{debug}; |
208
|
|
|
|
|
|
|
$self->{api}->{ua}->{cookie_jar} = $self->{mech}->{cookie_jar}; |
209
|
|
|
|
|
|
|
return 0; |
210
|
|
|
|
|
|
|
} else { |
211
|
|
|
|
|
|
|
$self->{errstr} = "Cannot load MediaWiki cookies from file $cookies"; |
212
|
|
|
|
|
|
|
carp $self->{errstr}; |
213
|
|
|
|
|
|
|
return 1; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
my $res = $self->{api}->api( { |
218
|
|
|
|
|
|
|
action=>'login', |
219
|
|
|
|
|
|
|
lgname=>$editor, |
220
|
|
|
|
|
|
|
lgpassword=>$password } ); |
221
|
|
|
|
|
|
|
use Data::Dumper; print Dumper($res); |
222
|
|
|
|
|
|
|
# unless (ref($res) eq 'HTTP::Response' && $res->is_success) { return; } |
223
|
|
|
|
|
|
|
$self->{mech}->{cookie_jar}->extract_cookies($self->{api}->{response}); |
224
|
|
|
|
|
|
|
my $result = $res->{login}->{result}; |
225
|
|
|
|
|
|
|
if ($result eq "Success") { |
226
|
|
|
|
|
|
|
return 0; |
227
|
|
|
|
|
|
|
} else { |
228
|
|
|
|
|
|
|
return 1; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=item edit($pagename,$page_text,[$edit_summary],[$is_minor],[$assert]) |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Edits the specified page $pagename and replaces it with $page_text with an edit summary of $edit_summary, optionally marking the edit as minor if specified, and adding an assertion, if requested. Assertions should be of the form "user". |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=cut |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
sub edit { |
239
|
|
|
|
|
|
|
my $self = shift; |
240
|
|
|
|
|
|
|
my $page = shift; |
241
|
|
|
|
|
|
|
my $text = shift; |
242
|
|
|
|
|
|
|
my $summary = shift; |
243
|
|
|
|
|
|
|
my $is_minor = shift || 0; |
244
|
|
|
|
|
|
|
my $assert = shift || $self->{assert}; |
245
|
|
|
|
|
|
|
my $res; |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
$assert=~s/\&?assert=// if $assert; |
248
|
|
|
|
|
|
|
# $text = encode( 'utf8', $text ) if $text; |
249
|
|
|
|
|
|
|
# $summary = encode( 'utf8', $summary ) if $summary; |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
$res = $self->{api}->api( { |
252
|
|
|
|
|
|
|
action=>'query', |
253
|
|
|
|
|
|
|
titles=>$page, |
254
|
|
|
|
|
|
|
prop=>'info|revisions', |
255
|
|
|
|
|
|
|
intoken=>'edit' } ); |
256
|
|
|
|
|
|
|
# use Data::Dumper; print Dumper($res); |
257
|
|
|
|
|
|
|
my ($id, $data)=%{$res->{query}->{pages}}; |
258
|
|
|
|
|
|
|
my $edittoken=$data->{edittoken}; |
259
|
|
|
|
|
|
|
my $lastedit=$data->{revisions}[0]->{timestamp}; |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
my $savehash = { |
262
|
|
|
|
|
|
|
action=>'edit', |
263
|
|
|
|
|
|
|
title=>$page, |
264
|
|
|
|
|
|
|
token=>$edittoken, |
265
|
|
|
|
|
|
|
text=>$text, |
266
|
|
|
|
|
|
|
summary=>$summary, |
267
|
|
|
|
|
|
|
minor=>$is_minor, |
268
|
|
|
|
|
|
|
basetimestamp=>$lastedit, |
269
|
|
|
|
|
|
|
bot=>1}; |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
$savehash->{assert}=$assert if ($assert); |
272
|
|
|
|
|
|
|
# use Data::Dumper; print Dumper($savehash); |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
$res = $self->{api}->api( $savehash ); |
275
|
|
|
|
|
|
|
# use Data::Dumper; print Dumper($res); |
276
|
|
|
|
|
|
|
if (!$res) { |
277
|
|
|
|
|
|
|
carp "API returned null result or error for edit"; |
278
|
|
|
|
|
|
|
carp "Error code: " . $self->{api}->{error}->{code}; |
279
|
|
|
|
|
|
|
carp $self->{api}->{error}->{details}; |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
if ($res->{edit}->{result} && $res->{edit}->{result} eq 'Failure') { |
282
|
|
|
|
|
|
|
carp "edit failed as ".$self->{mech}->{agent}; |
283
|
|
|
|
|
|
|
if ($self->{operator}) { |
284
|
|
|
|
|
|
|
my $optalk=$self->get_text("User talk:".$self->{operator}); |
285
|
|
|
|
|
|
|
unless ($optalk=~/Error with \Q$self->{mech}->{agent}\E/) { |
286
|
|
|
|
|
|
|
print "Sending warning!\n"; |
287
|
|
|
|
|
|
|
$self->edit("User talk:$self->{operator}", $optalk."\n\n==Error with ".$self->{mech}->{agent}."==\n".$self->{mech}->{agent}." needs to be logged in! ~~~~", 'bot issue', 0, 'assert='); |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
return 2; |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
return $res; |
294
|
|
|
|
|
|
|
} |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=item get_history($pagename,$limit) |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
Returns an array containing the history of the specified page, with $limit number of revisions. The array structure contains 'revid','user','comment','timestamp_date', and 'timestamp_time'. |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=cut |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
sub get_history { |
303
|
|
|
|
|
|
|
my $self = shift; |
304
|
|
|
|
|
|
|
my $pagename = shift; |
305
|
|
|
|
|
|
|
my $limit = shift || 5; |
306
|
|
|
|
|
|
|
my $rvstartid = shift || ''; |
307
|
|
|
|
|
|
|
my $direction = shift; |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
my @return; |
310
|
|
|
|
|
|
|
my @revisions; |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
if ( $limit > 50 ) { |
313
|
|
|
|
|
|
|
$self->{errstr} = "Error requesting history for $pagename: Limit may not be set to values above 50"; |
314
|
|
|
|
|
|
|
carp $self->{errstr} if $self->{debug}; |
315
|
|
|
|
|
|
|
return 1; |
316
|
|
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
my $hash = { |
319
|
|
|
|
|
|
|
action=>'query', |
320
|
|
|
|
|
|
|
prop=>'revisions', |
321
|
|
|
|
|
|
|
titles=>$pagename, |
322
|
|
|
|
|
|
|
rvprop=>'ids|timestamp|user|comment', |
323
|
|
|
|
|
|
|
rvlimit=>$limit |
324
|
|
|
|
|
|
|
}; |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
$hash->{rvstartid}=$rvstartid if ($rvstartid); |
327
|
|
|
|
|
|
|
$hash->{direction}=$direction if ($direction); |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
my $res = $self->{api}->api( $hash ); |
330
|
|
|
|
|
|
|
my ($id)=keys %{$res->{query}->{pages}}; |
331
|
|
|
|
|
|
|
my $array=$res->{query}->{pages}->{$id}->{revisions}; |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
foreach my $hash ( @{$array} ) { |
334
|
|
|
|
|
|
|
my $revid = $hash->{revid}; |
335
|
|
|
|
|
|
|
my $user = $hash->{user}; |
336
|
|
|
|
|
|
|
my ( $timestamp_date, $timestamp_time ) = split( /T/, $hash->{timestamp} ); |
337
|
|
|
|
|
|
|
$timestamp_time=~s/Z$//; |
338
|
|
|
|
|
|
|
my $comment = $hash->{comment}; |
339
|
|
|
|
|
|
|
push ( @return, { |
340
|
|
|
|
|
|
|
revid => $revid, |
341
|
|
|
|
|
|
|
user => $user, |
342
|
|
|
|
|
|
|
timestamp_date => $timestamp_date, |
343
|
|
|
|
|
|
|
timestamp_time => $timestamp_time, |
344
|
|
|
|
|
|
|
comment => $comment, |
345
|
|
|
|
|
|
|
} ); |
346
|
|
|
|
|
|
|
} |
347
|
|
|
|
|
|
|
return @return; |
348
|
|
|
|
|
|
|
} |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
=item get_text($pagename,[$revid,$section_number]) |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
Returns the text of the specified page. If $revid is defined, it will return the text of that revision; if $section_number is defined, it will return the text of that section. Returns 2 if page does not exist. |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
=cut |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
sub get_text { |
357
|
|
|
|
|
|
|
my $self = shift; |
358
|
|
|
|
|
|
|
my $pagename = shift; |
359
|
|
|
|
|
|
|
my $revid = shift || ''; |
360
|
|
|
|
|
|
|
my $section = shift || ''; |
361
|
|
|
|
|
|
|
my $recurse = shift || 0; |
362
|
|
|
|
|
|
|
my $dontescape=shift || 0; |
363
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
my $hash = { |
365
|
|
|
|
|
|
|
action=>'query', |
366
|
|
|
|
|
|
|
titles=>$pagename, |
367
|
|
|
|
|
|
|
prop=>'revisions', |
368
|
|
|
|
|
|
|
rvprop=>'content', |
369
|
|
|
|
|
|
|
}; |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
$hash->{rvsection}=$section if ($section); |
372
|
|
|
|
|
|
|
$hash->{rvstartid}=$revid if ($revid); |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
my $res = $self->{api}->api( $hash ); |
375
|
|
|
|
|
|
|
if (!$res) { |
376
|
|
|
|
|
|
|
carp "API returned null result or error for get_text"; |
377
|
|
|
|
|
|
|
carp "Error code: " . $self->{api}->{error}->{code}; |
378
|
|
|
|
|
|
|
carp $self->{api}->{error}->{details}; |
379
|
|
|
|
|
|
|
#use Data::Dumper; print Dumper($hash); |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
# use Data::Dumper; print Dumper($res); |
382
|
|
|
|
|
|
|
my ($id, $data)=%{$res->{query}->{pages}}; |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
if ($id==-1) {return 2} |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
my $wikitext=$data->{revisions}[0]->{'*'}; |
387
|
|
|
|
|
|
|
# use Data::Dumper;print Dumper($data); |
388
|
|
|
|
|
|
|
return $wikitext; |
389
|
|
|
|
|
|
|
} |
390
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
=item get_pages(@pages) |
392
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
Returns the text of the specified pages in a hashref. Content of '2' means page does not exist. |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
=cut |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
sub get_pages { |
398
|
|
|
|
|
|
|
my $self = shift; |
399
|
|
|
|
|
|
|
my @pages = @_; |
400
|
|
|
|
|
|
|
my %return; |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
my $hash = { |
403
|
|
|
|
|
|
|
action=>'query', |
404
|
|
|
|
|
|
|
titles=>join('|', @pages), |
405
|
|
|
|
|
|
|
prop=>'revisions', |
406
|
|
|
|
|
|
|
rvprop=>'content', |
407
|
|
|
|
|
|
|
}; |
408
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
# use Data::Dumper; print Dumper($hash); |
410
|
|
|
|
|
|
|
my $res = $self->{api}->api( $hash ); |
411
|
|
|
|
|
|
|
if (!$res) { |
412
|
|
|
|
|
|
|
carp "API returned null result or error for get_pages"; |
413
|
|
|
|
|
|
|
carp "Error code: " . $self->{api}->{error}->{code}; |
414
|
|
|
|
|
|
|
carp $self->{api}->{error}->{details}; |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
# use Data::Dumper; print Dumper($res); |
417
|
|
|
|
|
|
|
foreach my $id (keys %{$res->{query}->{pages}}) { |
418
|
|
|
|
|
|
|
if (defined($res->{query}->{pages}->{$id}->{missing})) { |
419
|
|
|
|
|
|
|
$return{$res->{query}->{pages}->{$id}->{title}}= |
420
|
|
|
|
|
|
|
2; |
421
|
|
|
|
|
|
|
next; |
422
|
|
|
|
|
|
|
} |
423
|
|
|
|
|
|
|
if (defined($res->{query}->{pages}->{$id}->{revisions})) { |
424
|
|
|
|
|
|
|
my @revisions=@{$res->{query}->{pages}->{$id}->{revisions}}; |
425
|
|
|
|
|
|
|
$return{$res->{query}->{pages}->{$id}->{title}}= |
426
|
|
|
|
|
|
|
$revisions[0]->{'*'}; |
427
|
|
|
|
|
|
|
next; |
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
} |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
# use Data::Dumper;print Dumper(\%return); |
432
|
|
|
|
|
|
|
return \%return; |
433
|
|
|
|
|
|
|
} |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
=item revert($pagename,$edit_summary,$old_revision_id) |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
Reverts the specified page to $old_revision_id, with an edit summary of $edit_summary. |
438
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
=cut |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
sub revert { |
442
|
|
|
|
|
|
|
my $self = shift; |
443
|
|
|
|
|
|
|
my $pagename = shift; |
444
|
|
|
|
|
|
|
my $summary = shift; |
445
|
|
|
|
|
|
|
my $revid = shift; |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
return $self->_put( |
448
|
|
|
|
|
|
|
$pagename, |
449
|
|
|
|
|
|
|
{ |
450
|
|
|
|
|
|
|
form_name => 'editform', |
451
|
|
|
|
|
|
|
fields => { wpSummary => $summary, }, |
452
|
|
|
|
|
|
|
}, |
453
|
|
|
|
|
|
|
"&oldid=$revid" |
454
|
|
|
|
|
|
|
); |
455
|
|
|
|
|
|
|
} |
456
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
=item undo($pagename,$edit_summary,$revision_id,$after) |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
Reverts the specified page to $revision_id, with an edit summary of $edit_summary, using the undo function. To use old revision id instead of new, set last param to 'after'. |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
=cut |
462
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
sub undo { |
464
|
|
|
|
|
|
|
my $self = shift; |
465
|
|
|
|
|
|
|
my $pagename = shift; |
466
|
|
|
|
|
|
|
my $summary = shift; |
467
|
|
|
|
|
|
|
my $revid = shift; |
468
|
|
|
|
|
|
|
my $after = shift || ''; |
469
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
return $self->_put( |
471
|
|
|
|
|
|
|
$pagename, |
472
|
|
|
|
|
|
|
{ |
473
|
|
|
|
|
|
|
form_name => 'editform', |
474
|
|
|
|
|
|
|
fields => { wpSummary => $summary, }, |
475
|
|
|
|
|
|
|
}, |
476
|
|
|
|
|
|
|
"&undo$after=$revid", |
477
|
|
|
|
|
|
|
"undo" #For the error detection in _put. |
478
|
|
|
|
|
|
|
); |
479
|
|
|
|
|
|
|
} |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
=item get_last($pagename,$username) |
482
|
|
|
|
|
|
|
|
483
|
|
|
|
|
|
|
Returns the number of the last revision not made by $username. |
484
|
|
|
|
|
|
|
|
485
|
|
|
|
|
|
|
=cut |
486
|
|
|
|
|
|
|
|
487
|
|
|
|
|
|
|
sub get_last { |
488
|
|
|
|
|
|
|
my $self = shift; |
489
|
|
|
|
|
|
|
my $pagename = shift; |
490
|
|
|
|
|
|
|
my $editor = shift; |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
my $revertto = 0; |
493
|
|
|
|
|
|
|
$pagename = uri_escape_utf8( $pagename ); |
494
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
my $res = $self->{api}->api( { |
496
|
|
|
|
|
|
|
action=>'query', |
497
|
|
|
|
|
|
|
titles=>$pagename, |
498
|
|
|
|
|
|
|
prop=>'revisions', |
499
|
|
|
|
|
|
|
rvlimit=>20, |
500
|
|
|
|
|
|
|
rvprop=>'ids|user', |
501
|
|
|
|
|
|
|
rvexcludeuser=>$editor } ); |
502
|
|
|
|
|
|
|
my ($id, $data)=%{$res->{query}->{pages}}; |
503
|
|
|
|
|
|
|
return $data->{revisions}[0]->{revid}; |
504
|
|
|
|
|
|
|
} |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
=item update_rc([$limit]) |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
Returns an array containing the Recent Changes to the wiki Main namespace. The array structure contains 'pagename', 'revid', 'oldid', 'timestamp_date', and 'timestamp_time'. |
509
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
=cut |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
sub update_rc { |
513
|
|
|
|
|
|
|
my $self = shift; |
514
|
|
|
|
|
|
|
my $limit = shift || 5; |
515
|
|
|
|
|
|
|
my @rc_table; |
516
|
|
|
|
|
|
|
|
517
|
|
|
|
|
|
|
my $res = $self->{api}->list( { |
518
|
|
|
|
|
|
|
action=>'query', |
519
|
|
|
|
|
|
|
list=>'recentchanges', |
520
|
|
|
|
|
|
|
rcnamespace=>0, |
521
|
|
|
|
|
|
|
rclimit=>$limit }, |
522
|
|
|
|
|
|
|
{ max=>$limit } ); |
523
|
|
|
|
|
|
|
foreach my $hash (@{$res}) { |
524
|
|
|
|
|
|
|
my ( $timestamp_date, $timestamp_time ) = split( /T/, $hash->{timestamp} ); |
525
|
|
|
|
|
|
|
$timestamp_time =~ s/Z$//; |
526
|
|
|
|
|
|
|
push( @rc_table, { |
527
|
|
|
|
|
|
|
pagename => $hash->{title}, |
528
|
|
|
|
|
|
|
revid => $hash->{revid}, |
529
|
|
|
|
|
|
|
oldid => $hash->{old_revid}, |
530
|
|
|
|
|
|
|
timestamp_date => $timestamp_date, |
531
|
|
|
|
|
|
|
timestamp_time => $timestamp_time, |
532
|
|
|
|
|
|
|
} |
533
|
|
|
|
|
|
|
); |
534
|
|
|
|
|
|
|
} |
535
|
|
|
|
|
|
|
return @rc_table; |
536
|
|
|
|
|
|
|
} |
537
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
=item what_links_here($pagename) |
539
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
Returns an array containing a list of all pages linking to the given page. The array structure contains 'title' and 'type', the type being a transclusion, redirect, or neither. |
541
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
=cut |
543
|
|
|
|
|
|
|
|
544
|
|
|
|
|
|
|
sub what_links_here { |
545
|
|
|
|
|
|
|
my $self = shift; |
546
|
|
|
|
|
|
|
my $article = shift; |
547
|
|
|
|
|
|
|
my @links; |
548
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
$article = uri_escape_utf8( $article ); |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
my $res = |
552
|
|
|
|
|
|
|
$self->_get( 'Special:Whatlinkshere', 'view', |
553
|
|
|
|
|
|
|
"&target=$article&limit=5000" ); |
554
|
|
|
|
|
|
|
unless (ref($res) eq 'HTTP::Response' && $res->is_success) { return 1; } |
555
|
|
|
|
|
|
|
my $content = $res->decoded_content; |
556
|
|
|
|
|
|
|
while ( |
557
|
|
|
|
|
|
|
$content =~ m{[^<]+([^<]*)}g ) { |
558
|
|
|
|
|
|
|
my $title = $1; |
559
|
|
|
|
|
|
|
my $type = $2; |
560
|
|
|
|
|
|
|
if ( $type !~ /\(redirect page\)/ && $type !~ /\(transclusion\)/ ) { |
561
|
|
|
|
|
|
|
$type = ""; |
562
|
|
|
|
|
|
|
} |
563
|
|
|
|
|
|
|
if ( $type =~ /\(redirect page\)/ ) { $type = "redirect"; } |
564
|
|
|
|
|
|
|
if ( $type =~ /\(transclusion\)/ ) { $type = "transclusion"; } |
565
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
push @links, { title => $title, type => $type }; |
567
|
|
|
|
|
|
|
} |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
return @links; |
570
|
|
|
|
|
|
|
} |
571
|
|
|
|
|
|
|
|
572
|
|
|
|
|
|
|
=item get_pages_in_category($category_name) |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
Returns an array containing the names of all pages in the specified category. Does not go into sub-categories. |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
=cut |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
sub get_pages_in_category { |
579
|
|
|
|
|
|
|
my $self = shift; |
580
|
|
|
|
|
|
|
my $category = shift; |
581
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
my @return; |
583
|
|
|
|
|
|
|
my $res = $self->{api}->list( { |
584
|
|
|
|
|
|
|
action=>'query', |
585
|
|
|
|
|
|
|
list=>'categorymembers', |
586
|
|
|
|
|
|
|
cmtitle=>$category, |
587
|
|
|
|
|
|
|
cmlimit=>500 }, |
588
|
|
|
|
|
|
|
# { max=>100 } |
589
|
|
|
|
|
|
|
); |
590
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
foreach (@{$res}) { |
592
|
|
|
|
|
|
|
push @return, $_->{title}; |
593
|
|
|
|
|
|
|
} |
594
|
|
|
|
|
|
|
return @return; |
595
|
|
|
|
|
|
|
} |
596
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
=item get_all_pages_in_category($category_name) |
598
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
Returns an array containing the names of ALL pages in the specified category, including sub-categories. |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
=cut |
602
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
sub get_all_pages_in_category { |
604
|
|
|
|
|
|
|
my $self = shift; |
605
|
|
|
|
|
|
|
my $base_category = shift; |
606
|
|
|
|
|
|
|
my @first = $self->get_pages_in_category($base_category); |
607
|
|
|
|
|
|
|
my %data; |
608
|
|
|
|
|
|
|
foreach my $page (@first) { |
609
|
|
|
|
|
|
|
$data{$page} = ''; |
610
|
|
|
|
|
|
|
if ( $page =~ /^Category:/ ) { |
611
|
|
|
|
|
|
|
my @pages = $self->get_all_pages_in_category($page); |
612
|
|
|
|
|
|
|
foreach (@pages) { |
613
|
|
|
|
|
|
|
$data{$_} = ''; |
614
|
|
|
|
|
|
|
} |
615
|
|
|
|
|
|
|
} |
616
|
|
|
|
|
|
|
} |
617
|
|
|
|
|
|
|
return keys %data; |
618
|
|
|
|
|
|
|
} |
619
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
=item linksearch($link) |
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
Runs a linksearch on the specified link and returns an array containing anonymous hashes with keys "link" for the outbound link name, and "page" for the page the link is on. |
623
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
=cut |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
sub linksearch { |
627
|
|
|
|
|
|
|
my $self = shift; |
628
|
|
|
|
|
|
|
my $link = shift; |
629
|
|
|
|
|
|
|
my @links; |
630
|
|
|
|
|
|
|
my $res = |
631
|
|
|
|
|
|
|
$self->_get( "Special:Linksearch", "edit", "&target=$link&limit=500" ); |
632
|
|
|
|
|
|
|
unless (ref($res) eq 'HTTP::Response' && $res->is_success) { return 1; } |
633
|
|
|
|
|
|
|
my $content = $res->decoded_content; |
634
|
|
|
|
|
|
|
while ( $content =~ |
635
|
|
|
|
|
|
|
m{(.+?) linked from (.+)}g ) { |
636
|
|
|
|
|
|
|
push( @links, { link => $1, page => $2 } ); |
637
|
|
|
|
|
|
|
} |
638
|
|
|
|
|
|
|
while ( my $res = $self->{mech}->follow_link( text => 'next 500' ) && ref($res) eq 'HTTP::Response' && $res->is_success ) { |
639
|
|
|
|
|
|
|
sleep 2; |
640
|
|
|
|
|
|
|
my $content = $res->decoded_content; |
641
|
|
|
|
|
|
|
while ( $content =~ |
642
|
|
|
|
|
|
|
m{(.+?) linked from (.+)}g ) { |
643
|
|
|
|
|
|
|
push( @links, { link => $1, page => $2 } ); |
644
|
|
|
|
|
|
|
} |
645
|
|
|
|
|
|
|
} |
646
|
|
|
|
|
|
|
return @links; |
647
|
|
|
|
|
|
|
} |
648
|
|
|
|
|
|
|
|
649
|
|
|
|
|
|
|
=item purge_page($pagename) |
650
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
Purges the server cache of the specified page. |
652
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
=cut |
654
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
sub purge_page { |
656
|
|
|
|
|
|
|
my $self = shift; |
657
|
|
|
|
|
|
|
my $page = shift; |
658
|
|
|
|
|
|
|
my $res = $self->_get( $page, 'purge' ); |
659
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
} |
661
|
|
|
|
|
|
|
|
662
|
|
|
|
|
|
|
=item get_namespace_names |
663
|
|
|
|
|
|
|
|
664
|
|
|
|
|
|
|
get_namespace_names returns a hash linking the namespace id, such as 1, to its named equivalent, such as "Talk". |
665
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
=cut |
667
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
sub get_namespace_names { |
669
|
|
|
|
|
|
|
my $self = shift; |
670
|
|
|
|
|
|
|
my %return; |
671
|
|
|
|
|
|
|
my $res = $self->{api}->api( { |
672
|
|
|
|
|
|
|
action=>'query', |
673
|
|
|
|
|
|
|
meta=>'siteinfo', |
674
|
|
|
|
|
|
|
siprop=>'namespaces'} ); |
675
|
|
|
|
|
|
|
foreach my $id (keys %{$res->{query}->{namespaces}}) { |
676
|
|
|
|
|
|
|
$return{$id} = $res->{query}->{namespaces}->{$id}->{'*'}; |
677
|
|
|
|
|
|
|
} |
678
|
|
|
|
|
|
|
if ($return{1} or $_[0]>1) { |
679
|
|
|
|
|
|
|
return %return; |
680
|
|
|
|
|
|
|
} else { |
681
|
|
|
|
|
|
|
return $self->get_namespace_names($_[0]+1); |
682
|
|
|
|
|
|
|
} |
683
|
|
|
|
|
|
|
} |
684
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
=item links_to_image($page) |
686
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
Gets a list of pages which include a certain image. |
688
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
=cut |
690
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
sub links_to_image { |
692
|
|
|
|
|
|
|
my $self = shift; |
693
|
|
|
|
|
|
|
my $page = shift; |
694
|
|
|
|
|
|
|
my $url = "http://$self->{host}/$self->{path}/index.php?title=$page"; |
695
|
|
|
|
|
|
|
print "Retrieving $url\n" if $self->{debug}; |
696
|
|
|
|
|
|
|
my $res = $self->{mech}->get($url); |
697
|
|
|
|
|
|
|
$res->decoded_content=~/div class=\"linkstoimage\" id=\"linkstoimage\"(.+?)\<\/ul\>/is; |
698
|
|
|
|
|
|
|
my $list=$1; |
699
|
|
|
|
|
|
|
my @list; |
700
|
|
|
|
|
|
|
while ($list=~/title=\"(.+?)\"/ig) { |
701
|
|
|
|
|
|
|
push @list, $1; |
702
|
|
|
|
|
|
|
} |
703
|
|
|
|
|
|
|
return @list; |
704
|
|
|
|
|
|
|
} |
705
|
|
|
|
|
|
|
|
706
|
|
|
|
|
|
|
=item test_blocked($user) |
707
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
Checks if a user is currently blocked. |
709
|
|
|
|
|
|
|
|
710
|
|
|
|
|
|
|
=cut |
711
|
|
|
|
|
|
|
|
712
|
|
|
|
|
|
|
sub test_blocked { |
713
|
|
|
|
|
|
|
my $self = shift; |
714
|
|
|
|
|
|
|
my $user = shift; |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
my $res = $self->_get("Special%3AIpblocklist&ip=$user", "", "", 1); |
717
|
|
|
|
|
|
|
if ($res->decoded_content=~/not blocked/i) { |
718
|
|
|
|
|
|
|
return 0; |
719
|
|
|
|
|
|
|
} else { |
720
|
|
|
|
|
|
|
return 1; |
721
|
|
|
|
|
|
|
} |
722
|
|
|
|
|
|
|
} |
723
|
|
|
|
|
|
|
|
724
|
|
|
|
|
|
|
=item test_image_exists($page) |
725
|
|
|
|
|
|
|
|
726
|
|
|
|
|
|
|
Checks if an image exists at $page. 0 means no, 1 means yes, local, 2 means on commons, 3 means doesn't exist but there is text on the page. |
727
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
=cut |
729
|
|
|
|
|
|
|
|
730
|
|
|
|
|
|
|
sub test_image_exists { |
731
|
|
|
|
|
|
|
my $self = shift; |
732
|
|
|
|
|
|
|
my @pages = @_; |
733
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
my $titles=join('|', @pages); |
735
|
|
|
|
|
|
|
my $return; |
736
|
|
|
|
|
|
|
$titles=~s/\|{2,}/\|/g; |
737
|
|
|
|
|
|
|
$titles=~s/\|$//; |
738
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
my $hash = { |
740
|
|
|
|
|
|
|
action => 'query', |
741
|
|
|
|
|
|
|
titles => $titles, |
742
|
|
|
|
|
|
|
iilimit => 1, |
743
|
|
|
|
|
|
|
prop => 'imageinfo'}; |
744
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
# use Data::Dumper; print Dumper($hash); |
746
|
|
|
|
|
|
|
my $res = $self->{api}->api($hash); |
747
|
|
|
|
|
|
|
# use Data::Dumper; print Dumper($res); |
748
|
|
|
|
|
|
|
foreach my $id (keys %{$res->{query}->{pages}}) { |
749
|
|
|
|
|
|
|
my $title=$res->{query}->{pages}->{$id}->{title}; |
750
|
|
|
|
|
|
|
if ($res->{query}->{pages}->{$id}->{imagerepository} eq 'shared') { |
751
|
|
|
|
|
|
|
$return->{$title}=2; |
752
|
|
|
|
|
|
|
} elsif (defined($res->{query}->{pages}->{$id}->{missing})) { |
753
|
|
|
|
|
|
|
$return->{$title}=0; |
754
|
|
|
|
|
|
|
} elsif ($res->{query}->{pages}->{$id}->{imagerepository} eq '') { |
755
|
|
|
|
|
|
|
$return->{$title}=3; |
756
|
|
|
|
|
|
|
} elsif ($res->{query}->{pages}->{$id}->{imagerepository} eq 'local') { |
757
|
|
|
|
|
|
|
$return->{$title}=1; |
758
|
|
|
|
|
|
|
} |
759
|
|
|
|
|
|
|
} |
760
|
|
|
|
|
|
|
if (scalar(@pages)==1) { |
761
|
|
|
|
|
|
|
return $return->{$pages[0]}; |
762
|
|
|
|
|
|
|
} else { |
763
|
|
|
|
|
|
|
return $return; |
764
|
|
|
|
|
|
|
} |
765
|
|
|
|
|
|
|
} |
766
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
=item delete_page($page[, $summary]) |
768
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
Deletes the page with the specified summary. |
770
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
=cut |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
sub delete_page { |
774
|
|
|
|
|
|
|
my $self = shift; |
775
|
|
|
|
|
|
|
my $page = shift; |
776
|
|
|
|
|
|
|
my $summary = shift; |
777
|
|
|
|
|
|
|
|
778
|
|
|
|
|
|
|
my $res = $self->{api}->api( { |
779
|
|
|
|
|
|
|
action=>'query', |
780
|
|
|
|
|
|
|
titles=>$page, |
781
|
|
|
|
|
|
|
prop=>'info|revisions', |
782
|
|
|
|
|
|
|
intoken=>'delete' } ); |
783
|
|
|
|
|
|
|
my ($id, $data)=%{$res->{query}->{pages}}; |
784
|
|
|
|
|
|
|
my $edittoken=$data->{deletetoken}; |
785
|
|
|
|
|
|
|
$res = $self->{api}->api( { |
786
|
|
|
|
|
|
|
action=>'delete', |
787
|
|
|
|
|
|
|
title=>$page, |
788
|
|
|
|
|
|
|
token=>$edittoken, |
789
|
|
|
|
|
|
|
reason=>$summary } ); |
790
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
return $res; |
792
|
|
|
|
|
|
|
} |
793
|
|
|
|
|
|
|
|
794
|
|
|
|
|
|
|
=item delete_old_image($page, $revision[, $summary]) |
795
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
Deletes the specified revision of the image with the specified summary. |
797
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
=cut |
799
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
sub delete_old_image { |
801
|
|
|
|
|
|
|
my $self = shift; |
802
|
|
|
|
|
|
|
my $page = shift; |
803
|
|
|
|
|
|
|
my $id = shift; |
804
|
|
|
|
|
|
|
my $summary = shift; |
805
|
|
|
|
|
|
|
my $image = $page; |
806
|
|
|
|
|
|
|
$image=~s/\s/_/g; |
807
|
|
|
|
|
|
|
$image=~s/\%20/_/g; |
808
|
|
|
|
|
|
|
$image=~s/Image://gi; |
809
|
|
|
|
|
|
|
my $res = $self->_get( $page, 'delete', "&oldimage=$id%21$image" ); |
810
|
|
|
|
|
|
|
unless ($res) { return; } |
811
|
|
|
|
|
|
|
my $options = { |
812
|
|
|
|
|
|
|
fields => { |
813
|
|
|
|
|
|
|
wpReason => $summary, |
814
|
|
|
|
|
|
|
}, |
815
|
|
|
|
|
|
|
}; |
816
|
|
|
|
|
|
|
$res = $self->{mech}->submit_form( %{$options}); |
817
|
|
|
|
|
|
|
#use Data::Dumper;print Dumper($res); |
818
|
|
|
|
|
|
|
#print $res->decoded_content."\n"; |
819
|
|
|
|
|
|
|
return $res; |
820
|
|
|
|
|
|
|
} |
821
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
=item block($user, $length, $summary, $anononly, $autoblock, $blockaccountcreation, $blockemail) |
823
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
Blocks the user with the specified options. All options optional except $user and $length. Last four are true/false. Defaults to empty summary, all options disabled. |
825
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
=cut |
827
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
sub block { |
829
|
|
|
|
|
|
|
my $self = shift; |
830
|
|
|
|
|
|
|
my $user = shift; |
831
|
|
|
|
|
|
|
my $length = shift; |
832
|
|
|
|
|
|
|
my $summary = shift; |
833
|
|
|
|
|
|
|
my $anononly= shift; |
834
|
|
|
|
|
|
|
my $autoblock=shift; |
835
|
|
|
|
|
|
|
my $blockac = shift; |
836
|
|
|
|
|
|
|
my $blockemail=shift; |
837
|
|
|
|
|
|
|
my $res = $self->_get( "Special:Blockip/$user" ); |
838
|
|
|
|
|
|
|
unless ($res) { return; } |
839
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
$res = $self->{api}->api( { |
841
|
|
|
|
|
|
|
action=>'query', |
842
|
|
|
|
|
|
|
titles=>'Main_Page', |
843
|
|
|
|
|
|
|
prop=>'info|revisions', |
844
|
|
|
|
|
|
|
intoken=>'block' } ); |
845
|
|
|
|
|
|
|
my ($id, $data)=%{$res->{query}->{pages}}; |
846
|
|
|
|
|
|
|
my $edittoken=$data->{blocktoken}; |
847
|
|
|
|
|
|
|
my $hash = { |
848
|
|
|
|
|
|
|
action=>'block', |
849
|
|
|
|
|
|
|
user=>$user, |
850
|
|
|
|
|
|
|
token=>$edittoken, |
851
|
|
|
|
|
|
|
expiry=>$length, |
852
|
|
|
|
|
|
|
reason=>$summary }; |
853
|
|
|
|
|
|
|
$hash->{anononly}=$anononly if ($anononly); |
854
|
|
|
|
|
|
|
$hash->{autoblock}=$autoblock if ($autoblock); |
855
|
|
|
|
|
|
|
$hash->{nocreate}=$blockac if ($blockac); |
856
|
|
|
|
|
|
|
$hash->{noemail}=$blockemail if ($blockemail); |
857
|
|
|
|
|
|
|
$res = $self->{api}->api( $hash ); |
858
|
|
|
|
|
|
|
|
859
|
|
|
|
|
|
|
return $res; |
860
|
|
|
|
|
|
|
} |
861
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
=item protect($page, $reason, $editlvl, $movelvl, $time, $cascade) |
863
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
Protects (or unprotects) the page. $editlvl and $movelvl may be '', 'autoconfirmed', or 'sysop'. $cascade is true/false. |
865
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
=cut |
867
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
sub protect { |
869
|
|
|
|
|
|
|
my $self = shift; |
870
|
|
|
|
|
|
|
my $page = shift; |
871
|
|
|
|
|
|
|
my $reason = shift; |
872
|
|
|
|
|
|
|
my $editlvl = shift || 'all'; |
873
|
|
|
|
|
|
|
my $movelvl = shift || 'all'; |
874
|
|
|
|
|
|
|
my $time = shift || 'infinite'; |
875
|
|
|
|
|
|
|
my $cascade = shift; |
876
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
if ($cascade and ($editlvl ne 'sysop' or $movelvl ne 'sysop')) { |
878
|
|
|
|
|
|
|
carp "Can't set cascading unless both editlvl and movelvl are sysop." |
879
|
|
|
|
|
|
|
} |
880
|
|
|
|
|
|
|
my $res = $self->{api}->api( { |
881
|
|
|
|
|
|
|
action=>'query', |
882
|
|
|
|
|
|
|
titles=>$page, |
883
|
|
|
|
|
|
|
prop=>'info|revisions', |
884
|
|
|
|
|
|
|
intoken=>'protect' } ); |
885
|
|
|
|
|
|
|
#use Data::Dumper;print STDERR Dumper($res); |
886
|
|
|
|
|
|
|
my ($id, $data)=%{$res->{query}->{pages}}; |
887
|
|
|
|
|
|
|
my $edittoken=$data->{protecttoken}; |
888
|
|
|
|
|
|
|
my $hash={ action=>'protect', |
889
|
|
|
|
|
|
|
title=>$page, |
890
|
|
|
|
|
|
|
token=>$edittoken, |
891
|
|
|
|
|
|
|
reason=>$reason, |
892
|
|
|
|
|
|
|
protections=>"edit=$editlvl|move=$movelvl", |
893
|
|
|
|
|
|
|
expiry=>$time }; |
894
|
|
|
|
|
|
|
$hash->{'cascade'}=$cascade if ($cascade); |
895
|
|
|
|
|
|
|
$res = $self->{api}->api( $hash ); |
896
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
return $res; |
898
|
|
|
|
|
|
|
} |
899
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
=item get_pages_in_namespace($namespace_id,$page_limit) |
901
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
Returns an array containing the names of all pages in the specified namespace. The $namespace_id must be a number, not a namespace name. Setting $page_limit is optional. If $page_limit is over 500, it will be rounded up to the next multiple of 500. |
903
|
|
|
|
|
|
|
|
904
|
|
|
|
|
|
|
=cut |
905
|
|
|
|
|
|
|
|
906
|
|
|
|
|
|
|
sub get_pages_in_namespace { |
907
|
|
|
|
|
|
|
my $self = shift; |
908
|
|
|
|
|
|
|
my $namespace = shift; |
909
|
|
|
|
|
|
|
my $page_limit = shift || 500; |
910
|
|
|
|
|
|
|
my $apilimit=500; |
911
|
|
|
|
|
|
|
if ($self->{highlimits}) { |
912
|
|
|
|
|
|
|
$apilimit=5000; |
913
|
|
|
|
|
|
|
} |
914
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
my @return; |
916
|
|
|
|
|
|
|
my $max; |
917
|
|
|
|
|
|
|
|
918
|
|
|
|
|
|
|
if ($page_limit<=$apilimit) { |
919
|
|
|
|
|
|
|
$max=1; |
920
|
|
|
|
|
|
|
} else { |
921
|
|
|
|
|
|
|
$max=($page_limit-1)/$apilimit+1; |
922
|
|
|
|
|
|
|
$page_limit=$apilimit; |
923
|
|
|
|
|
|
|
} |
924
|
|
|
|
|
|
|
|
925
|
|
|
|
|
|
|
my $res = $self->{api}->list( { |
926
|
|
|
|
|
|
|
action=>'query', |
927
|
|
|
|
|
|
|
list=>'allpages', |
928
|
|
|
|
|
|
|
apnamespace=>$namespace, |
929
|
|
|
|
|
|
|
aplimit=>$page_limit }, |
930
|
|
|
|
|
|
|
{ max=>$max } ); |
931
|
|
|
|
|
|
|
|
932
|
|
|
|
|
|
|
foreach (@{$res}) { |
933
|
|
|
|
|
|
|
push @return, $_->{title}; |
934
|
|
|
|
|
|
|
} |
935
|
|
|
|
|
|
|
return @return; |
936
|
|
|
|
|
|
|
} |
937
|
|
|
|
|
|
|
|
938
|
|
|
|
|
|
|
=item count_contributions($user) |
939
|
|
|
|
|
|
|
|
940
|
|
|
|
|
|
|
Uses the API to count $user's contributions. |
941
|
|
|
|
|
|
|
|
942
|
|
|
|
|
|
|
=cut |
943
|
|
|
|
|
|
|
|
944
|
|
|
|
|
|
|
sub count_contributions { |
945
|
|
|
|
|
|
|
my $self=shift; |
946
|
|
|
|
|
|
|
my $username=shift; |
947
|
|
|
|
|
|
|
$username=~s/User://i; #strip namespace |
948
|
|
|
|
|
|
|
my $res = $self->{api}->list( { |
949
|
|
|
|
|
|
|
action=>'query', |
950
|
|
|
|
|
|
|
list=>'users', |
951
|
|
|
|
|
|
|
ususers=>$username, |
952
|
|
|
|
|
|
|
usprop=>'editcount' }, |
953
|
|
|
|
|
|
|
{ max=>1 } ); |
954
|
|
|
|
|
|
|
my $return = ${$res}[0]->{'editcount'}; |
955
|
|
|
|
|
|
|
|
956
|
|
|
|
|
|
|
if ($return or $_[0]>1) { |
957
|
|
|
|
|
|
|
return $return; |
958
|
|
|
|
|
|
|
} else { |
959
|
|
|
|
|
|
|
return $self->count_contributions($username, $_[0]+1); |
960
|
|
|
|
|
|
|
} |
961
|
|
|
|
|
|
|
} |
962
|
|
|
|
|
|
|
|
963
|
|
|
|
|
|
|
=item last_active($user) |
964
|
|
|
|
|
|
|
|
965
|
|
|
|
|
|
|
Returns the last active time of $user in YYYY-MM-DDTHH:MM:SSZ |
966
|
|
|
|
|
|
|
|
967
|
|
|
|
|
|
|
=cut |
968
|
|
|
|
|
|
|
|
969
|
|
|
|
|
|
|
sub last_active { |
970
|
|
|
|
|
|
|
my $self=shift; |
971
|
|
|
|
|
|
|
my $username=shift; |
972
|
|
|
|
|
|
|
unless ($username=~/User:/i) {$username="User:".$username;} |
973
|
|
|
|
|
|
|
my $res = $self->{api}->list( { |
974
|
|
|
|
|
|
|
action=>'query', |
975
|
|
|
|
|
|
|
list=>'usercontribs', |
976
|
|
|
|
|
|
|
ucuser=>$username, |
977
|
|
|
|
|
|
|
uclimit=>1 }, |
978
|
|
|
|
|
|
|
{ max=>1 } ); |
979
|
|
|
|
|
|
|
return ${$res}[0]->{'timestamp'}; |
980
|
|
|
|
|
|
|
} |
981
|
|
|
|
|
|
|
|
982
|
|
|
|
|
|
|
=item recent_edit_to_page($page) |
983
|
|
|
|
|
|
|
|
984
|
|
|
|
|
|
|
Returns timestamp and username for most recent edit to $page. |
985
|
|
|
|
|
|
|
|
986
|
|
|
|
|
|
|
=cut |
987
|
|
|
|
|
|
|
|
988
|
|
|
|
|
|
|
sub recent_edit_to_page { |
989
|
|
|
|
|
|
|
my $self=shift; |
990
|
|
|
|
|
|
|
my $page=shift; |
991
|
|
|
|
|
|
|
my $res = $self->{api}->api( { |
992
|
|
|
|
|
|
|
action=>'query', |
993
|
|
|
|
|
|
|
prop=>'revisions', |
994
|
|
|
|
|
|
|
titles=>$page, |
995
|
|
|
|
|
|
|
rvlimit=>1 }, |
996
|
|
|
|
|
|
|
{ max=>1 } ); |
997
|
|
|
|
|
|
|
my ($id, $data)=%{$res->{query}->{pages}}; |
998
|
|
|
|
|
|
|
return $data->{revisions}[0]->{timestamp}; |
999
|
|
|
|
|
|
|
} |
1000
|
|
|
|
|
|
|
|
1001
|
|
|
|
|
|
|
=item get_users($page, $limit, $revision, $direction) |
1002
|
|
|
|
|
|
|
|
1003
|
|
|
|
|
|
|
Gets the most recent editors to $page, up to $limit, starting from $revision and goint in $direction. |
1004
|
|
|
|
|
|
|
|
1005
|
|
|
|
|
|
|
=cut |
1006
|
|
|
|
|
|
|
|
1007
|
|
|
|
|
|
|
sub get_users { |
1008
|
|
|
|
|
|
|
my $self = shift; |
1009
|
|
|
|
|
|
|
my $pagename = shift; |
1010
|
|
|
|
|
|
|
my $limit = shift || 5; |
1011
|
|
|
|
|
|
|
my $rvstartid = shift; |
1012
|
|
|
|
|
|
|
my $direction = shift; |
1013
|
|
|
|
|
|
|
|
1014
|
|
|
|
|
|
|
my @return; |
1015
|
|
|
|
|
|
|
my @revisions; |
1016
|
|
|
|
|
|
|
|
1017
|
|
|
|
|
|
|
if ( $limit > 50 ) { |
1018
|
|
|
|
|
|
|
$self->{errstr} = "Error requesting history for $pagename: Limit may not be set to values above 50"; |
1019
|
|
|
|
|
|
|
carp $self->{errstr}; |
1020
|
|
|
|
|
|
|
return 1; |
1021
|
|
|
|
|
|
|
} |
1022
|
|
|
|
|
|
|
my $hash = { |
1023
|
|
|
|
|
|
|
action=>'query', |
1024
|
|
|
|
|
|
|
prop=>'revisions', |
1025
|
|
|
|
|
|
|
titles=>$pagename, |
1026
|
|
|
|
|
|
|
rvprop=>'ids|timestamp|user|comment', |
1027
|
|
|
|
|
|
|
rvlimit=>$limit |
1028
|
|
|
|
|
|
|
}; |
1029
|
|
|
|
|
|
|
|
1030
|
|
|
|
|
|
|
$hash->{rvstartid}=$rvstartid if ($rvstartid); |
1031
|
|
|
|
|
|
|
$hash->{rvdir}=$direction if ($direction); |
1032
|
|
|
|
|
|
|
|
1033
|
|
|
|
|
|
|
my $res = $self->{api}->api( $hash ); |
1034
|
|
|
|
|
|
|
my ($id)=keys %{$res->{query}->{pages}}; |
1035
|
|
|
|
|
|
|
my $array=$res->{query}->{pages}->{$id}->{revisions}; |
1036
|
|
|
|
|
|
|
foreach (@{$array}) { |
1037
|
|
|
|
|
|
|
push @return, $_->{user}; |
1038
|
|
|
|
|
|
|
} |
1039
|
|
|
|
|
|
|
return @return; |
1040
|
|
|
|
|
|
|
} |
1041
|
|
|
|
|
|
|
|
1042
|
|
|
|
|
|
|
=item test_block_hist($user) |
1043
|
|
|
|
|
|
|
|
1044
|
|
|
|
|
|
|
Returns 1 if $user has been blocked. |
1045
|
|
|
|
|
|
|
|
1046
|
|
|
|
|
|
|
=cut |
1047
|
|
|
|
|
|
|
|
1048
|
|
|
|
|
|
|
sub test_block_hist { |
1049
|
|
|
|
|
|
|
my $self = shift; |
1050
|
|
|
|
|
|
|
my $user = shift; |
1051
|
|
|
|
|
|
|
|
1052
|
|
|
|
|
|
|
$user=~s/User://i; |
1053
|
|
|
|
|
|
|
my $res = $self->_get("Special:Log&type=block&page=User:$user", "", "", 1); |
1054
|
|
|
|
|
|
|
if ($res->decoded_content=~/no matching/i) { |
1055
|
|
|
|
|
|
|
return 0; |
1056
|
|
|
|
|
|
|
} else { |
1057
|
|
|
|
|
|
|
return 1; |
1058
|
|
|
|
|
|
|
} |
1059
|
|
|
|
|
|
|
} |
1060
|
|
|
|
|
|
|
|
1061
|
|
|
|
|
|
|
=item expandtemplates($page[, $text]) |
1062
|
|
|
|
|
|
|
|
1063
|
|
|
|
|
|
|
Expands templates on $page, using $text if provided, otherwise loading the page text automatically. |
1064
|
|
|
|
|
|
|
|
1065
|
|
|
|
|
|
|
=cut |
1066
|
|
|
|
|
|
|
|
1067
|
|
|
|
|
|
|
sub expandtemplates { |
1068
|
|
|
|
|
|
|
my $self = shift; |
1069
|
|
|
|
|
|
|
my $page = shift; |
1070
|
|
|
|
|
|
|
my $text = shift || undef; |
1071
|
|
|
|
|
|
|
|
1072
|
|
|
|
|
|
|
unless ($text) { |
1073
|
|
|
|
|
|
|
$text=$self->get_text($page); |
1074
|
|
|
|
|
|
|
} |
1075
|
|
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
my $res = $self->_get( "Special:ExpandTemplates" ); |
1077
|
|
|
|
|
|
|
my $options = { |
1078
|
|
|
|
|
|
|
fields => { |
1079
|
|
|
|
|
|
|
contexttitle => $page, |
1080
|
|
|
|
|
|
|
input => $text, |
1081
|
|
|
|
|
|
|
removecomments => undef, |
1082
|
|
|
|
|
|
|
}, |
1083
|
|
|
|
|
|
|
}; |
1084
|
|
|
|
|
|
|
$res = $self->{mech}->submit_form( %{$options}); |
1085
|
|
|
|
|
|
|
$res->decoded_content=~/\ |
1086
|
|
|
|
|
|
|
return $1; |
1087
|
|
|
|
|
|
|
} |
1088
|
|
|
|
|
|
|
|
1089
|
|
|
|
|
|
|
=item undelete($page, $summary) |
1090
|
|
|
|
|
|
|
|
1091
|
|
|
|
|
|
|
Undeletes $page with $summary. |
1092
|
|
|
|
|
|
|
|
1093
|
|
|
|
|
|
|
=cut |
1094
|
|
|
|
|
|
|
|
1095
|
|
|
|
|
|
|
sub undelete { |
1096
|
|
|
|
|
|
|
my $self = shift; |
1097
|
|
|
|
|
|
|
my $page = shift; |
1098
|
|
|
|
|
|
|
my $summary = shift; |
1099
|
|
|
|
|
|
|
my $res = $self->_get( "Special:Undelete", "", "&target=$page" ); |
1100
|
|
|
|
|
|
|
unless ($res) { return; } |
1101
|
|
|
|
|
|
|
if ($res->decoded_content=~/There is no revision history for this page/i) { |
1102
|
|
|
|
|
|
|
return 1; |
1103
|
|
|
|
|
|
|
} |
1104
|
|
|
|
|
|
|
my $options = { |
1105
|
|
|
|
|
|
|
fields => { |
1106
|
|
|
|
|
|
|
wpComment => $summary, |
1107
|
|
|
|
|
|
|
}, |
1108
|
|
|
|
|
|
|
}; |
1109
|
|
|
|
|
|
|
$res = $self->{mech}->submit_form( %{$options}, button=>"restore"); |
1110
|
|
|
|
|
|
|
return $res; |
1111
|
|
|
|
|
|
|
} |
1112
|
|
|
|
|
|
|
|
1113
|
|
|
|
|
|
|
=item get_allusers($limit) |
1114
|
|
|
|
|
|
|
|
1115
|
|
|
|
|
|
|
Returns an array of all users. Default limit is 500. |
1116
|
|
|
|
|
|
|
|
1117
|
|
|
|
|
|
|
=cut |
1118
|
|
|
|
|
|
|
|
1119
|
|
|
|
|
|
|
sub get_allusers { |
1120
|
|
|
|
|
|
|
my $self = shift; |
1121
|
|
|
|
|
|
|
my $limit = shift; |
1122
|
|
|
|
|
|
|
my @return = (); |
1123
|
|
|
|
|
|
|
|
1124
|
|
|
|
|
|
|
$limit = 500 unless $limit; |
1125
|
|
|
|
|
|
|
|
1126
|
|
|
|
|
|
|
my $res = $self->{api}->api( { action =>'query', |
1127
|
|
|
|
|
|
|
list =>'allusers', |
1128
|
|
|
|
|
|
|
aulimit => $limit } ); |
1129
|
|
|
|
|
|
|
|
1130
|
|
|
|
|
|
|
for my $ref ( @{$res->{query}->{allusers}} ) { |
1131
|
|
|
|
|
|
|
push @return, $ref->{name}; |
1132
|
|
|
|
|
|
|
} |
1133
|
|
|
|
|
|
|
return @return; |
1134
|
|
|
|
|
|
|
} |
1135
|
|
|
|
|
|
|
|
1136
|
|
|
|
|
|
|
|
1137
|
|
|
|
|
|
|
1; |
1138
|
|
|
|
|
|
|
|
1139
|
|
|
|
|
|
|
=back |
1140
|
|
|
|
|
|
|
|
1141
|
|
|
|
|
|
|
=head1 ERROR HANDLING |
1142
|
|
|
|
|
|
|
|
1143
|
|
|
|
|
|
|
All Perlwikipedia functions will return either 0 or 1 if they do not return data. If an error occurs in a function, $perlwikipedia_object->{errstr} is set to the error message and the function will return 1. A robust bot should check $perlwikipedia_object->{errstr} for messages after performing any action with the object. |
1144
|
|
|
|
|
|
|
|
1145
|
|
|
|
|
|
|
=cut |
1146
|
|
|
|
|
|
|
|
1147
|
|
|
|
|
|
|
__END__ |