line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CMS::MediaWiki; |
2
|
|
|
|
|
|
|
####################################################################### |
3
|
|
|
|
|
|
|
# Author: Reto Schär |
4
|
|
|
|
|
|
|
# Copyright (C) by Reto Schär (find details at the end of this script) |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This library is free software; you can redistribute it and/or modify |
7
|
|
|
|
|
|
|
# it under the same terms as Perl itself, either Perl version 5.8.6 or, |
8
|
|
|
|
|
|
|
# at your option, any later version of Perl 5 you may have available. |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# Locations: |
11
|
|
|
|
|
|
|
# http://meta.pgate.net/cms-mediawiki/ |
12
|
|
|
|
|
|
|
# http://search.cpan.org/dist/CMS-MediaWiki/lib/CMS/MediaWiki.pm |
13
|
|
|
|
|
|
|
####################################################################### |
14
|
1
|
|
|
1
|
|
30235
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
62
|
|
15
|
|
|
|
|
|
|
my $package = __PACKAGE__; |
16
|
|
|
|
|
|
|
our $VERSION = '0.8014'; |
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
1232
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
56269
|
|
|
1
|
|
|
|
|
41
|
|
19
|
1
|
|
|
1
|
|
1082
|
use HTTP::Request::Common; |
|
1
|
|
|
|
|
2332
|
|
|
1
|
|
|
|
|
2526
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# GLOBAL VARIABLES |
22
|
|
|
|
|
|
|
my %Var = (); |
23
|
|
|
|
|
|
|
my $contentType = ""; |
24
|
|
|
|
|
|
|
my $ua; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$| = 1; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
#----- FORWARD DECLARATIONS & PROTOTYPING |
29
|
|
|
|
|
|
|
sub Error($); |
30
|
|
|
|
|
|
|
sub Debug($); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub new { |
33
|
0
|
|
|
0
|
0
|
|
my $type = shift; |
34
|
0
|
|
|
|
|
|
my %params = @_; |
35
|
0
|
|
|
|
|
|
my $self = {}; |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
0
|
|
|
|
$self->{'protocol'} = $params{'protocol'} || 'http'; # optional |
38
|
0
|
|
0
|
|
|
|
$self->{'host' } = $params{'host'} || 'localhost'; |
39
|
0
|
|
0
|
|
|
|
$self->{'path' } = $params{'path'} || ''; |
40
|
0
|
|
0
|
|
|
|
$self->{'debug' } = $params{'debug'} || 0; # 0, 1, 2 |
41
|
0
|
|
|
|
|
|
$Var{'SERVER_SIG'} = '*Unknown*'; |
42
|
0
|
|
|
|
|
|
$Var{'EDIT_TIME_BEFORE'} = '*Unknown*'; |
43
|
|
|
|
|
|
|
|
44
|
0
|
0
|
|
|
|
|
Debug "$package V$VERSION" if $self->{'debug'}; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
$ua = LWP::UserAgent->new( |
47
|
|
|
|
|
|
|
agent => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; T312461)' , |
48
|
|
|
|
|
|
|
'cookie_jar' => {file => "lwpcookies.txt", autosave => 1} |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
bless $self, $type; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub login { |
55
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
56
|
0
|
|
|
|
|
|
my %args = @_; |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
if ($self->{'debug'}) { |
59
|
0
|
|
|
|
|
|
Debug "[login] $_ = $args{$_}" foreach keys %args; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
0
|
|
|
|
$args{'protocol'} ||= $self->{'protocol'}; |
63
|
0
|
|
0
|
|
|
|
$args{'path'} ||= $self->{'path'}; |
64
|
0
|
|
|
|
|
|
$self->{'path'} = $args{'path'}; # globalize, if it was set here |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
0
|
|
|
|
$args{'host'} ||= $self->{'host'}; |
67
|
0
|
|
|
|
|
|
$self->{'host'} = $args{'host'}; # globalize |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my %tags = (); |
70
|
0
|
|
0
|
|
|
|
$tags{'wpName' } = $args{'user'} || 'Perlbot'; |
71
|
0
|
|
0
|
|
|
|
$tags{'wpPassword' } = $args{'pass'} || 'barfoo'; |
72
|
0
|
|
|
|
|
|
$tags{'wpLoginattempt'} = 'Log in'; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
my $index_path = "/index.php"; |
75
|
0
|
0
|
|
|
|
|
$index_path = "/$args{'path'}/index.php" if $args{'path'}; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my $login_url = "$args{'protocol'}://$args{'host'}$index_path?title=Special:Userlogin&action=submitlogin"; |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
Debug "[login] POST $login_url\..." if $self->{'debug'}; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
my $resp = $ua->request( |
82
|
|
|
|
|
|
|
POST $login_url , |
83
|
|
|
|
|
|
|
Content_Type => 'application/x-www-form-urlencoded' , |
84
|
|
|
|
|
|
|
Content => [ %tags ] |
85
|
|
|
|
|
|
|
); |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my $login_okay = 0; |
88
|
0
|
|
|
|
|
|
foreach (keys %{$resp->{'_headers'}}) { |
|
0
|
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
|
Debug "(header) $_ = " . $resp->{'_headers'}->{$_} if $self->{'debug'} > 1; |
90
|
0
|
0
|
|
|
|
|
if ($_ =~ /^set-cookie$/i) { |
91
|
0
|
|
|
|
|
|
my $arr = $resp->{'_headers'}->{$_}; |
92
|
0
|
0
|
|
|
|
|
if ($arr =~ /^ARRAY(.+)$/) { |
93
|
0
|
|
|
|
|
|
foreach (@{$arr}) { |
|
0
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
Debug "- (cookie) $_" if $self->{'debug'} > 1; |
95
|
|
|
|
|
|
|
# wikiUserID or wikidbUserID |
96
|
0
|
0
|
|
|
|
|
if ($_ =~ /UserID=\d+\;/i) { |
97
|
|
|
|
|
|
|
# Success! |
98
|
0
|
|
|
|
|
|
$login_okay = 1; |
99
|
|
|
|
|
|
|
} |
100
|
0
|
0
|
|
|
|
|
Debug "(cookie) $_" if $self->{'debug'} > 1; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
else { |
104
|
0
|
0
|
|
|
|
|
Debug "=====> cookie: $arr" if $self->{'debug'}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
0
|
0
|
|
|
|
|
if ($_ =~ /^server$/i) { |
108
|
0
|
|
|
|
|
|
$Var{'SERVER_SIG'} = $resp->{'_headers'}->{$_}; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
0
|
0
|
|
|
|
|
return $login_okay ? 0 : 1; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub editPage { |
116
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
117
|
0
|
|
|
|
|
|
my %args = @_; |
118
|
|
|
|
|
|
|
|
119
|
0
|
0
|
|
|
|
|
if ($self->{'debug'}) { |
120
|
0
|
|
|
|
|
|
Debug "[editPage] $_ = \"$args{$_}\"" foreach keys %args; |
121
|
0
|
|
|
|
|
|
Debug "[editPage] VAR $_ = \"$Var{$_}\"" foreach keys %Var; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
0
|
|
|
|
my $WHOST = $self->{'host'} || 'localhost'; |
125
|
0
|
|
0
|
|
|
|
my $WPATH = $self->{'path'} || ''; |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
0
|
|
|
|
$args{'protocol'} ||= $self->{'protocol'}; |
128
|
0
|
|
0
|
|
|
|
$args{'text '} ||= '* No text *'; |
129
|
0
|
|
0
|
|
|
|
$args{'summary'} ||= 'By CMS::MediaWiki'; |
130
|
0
|
|
0
|
|
|
|
$args{'section'} ||= ''; |
131
|
0
|
|
0
|
|
|
|
$args{'watch'} ||= 0; |
132
|
|
|
|
|
|
|
|
133
|
0
|
0
|
|
|
|
|
Debug "Editing page '$args{'title'}' (section '$args{'section'}')..." if $self->{'debug'}; |
134
|
|
|
|
|
|
|
|
135
|
0
|
0
|
|
|
|
|
my $edit_section = length($args{'section'}) > 0 ? "\§ion=$args{'section'}" : ''; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# (Pre-)fetch page... |
138
|
0
|
|
|
|
|
|
my $resp = $ua->request(GET "$args{'protocol'}://$WHOST/$WPATH/index.php?title=$args{'title'}&action=edit$edit_section"); |
139
|
0
|
|
|
|
|
|
my @lines = split /\n/, $resp->content(); |
140
|
0
|
|
|
|
|
|
my $token = my $edit_time = ''; |
141
|
0
|
|
|
|
|
|
foreach (@lines) { |
142
|
|
|
|
|
|
|
#Debug "X $_"; |
143
|
0
|
0
|
|
|
|
|
if (/wpEditToken/) { |
144
|
0
|
|
|
|
|
|
s/type=.?hidden.? *value="(.+)" *name/$1/i; |
145
|
0
|
|
|
|
|
|
$token = $1; |
146
|
|
|
|
|
|
|
} |
147
|
0
|
0
|
|
|
|
|
if (/wpEdittime/) { |
148
|
0
|
|
|
|
|
|
s/type=.?hidden.? *value="(.+)" *name/$1/i; |
149
|
0
|
|
0
|
|
|
|
$edit_time = $1 || ''; |
150
|
0
|
|
|
|
|
|
$Var{EDIT_TIME_BEFORE} = $edit_time; |
151
|
|
|
|
|
|
|
} |
152
|
0
|
0
|
|
|
|
|
if (//i) { |
153
|
0
|
|
|
|
|
|
s/(.+)<\/title>/$1/i; |
154
|
0
|
|
0
|
|
|
|
$Var{PAGE_TITLE} = $1 || ''; |
155
|
|
|
|
|
|
|
} |
156
|
0
|
0
|
|
|
|
|
if (/index.php\?title=(.+?):Copyright.+/i) { |
157
|
0
|
|
0
|
|
|
|
$Var{WIKI_NAME} = $1 || ''; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
0
|
0
|
|
|
|
|
if ($self->{'debug'}) { |
162
|
0
|
0
|
|
|
|
|
Debug "token = $token" if $self->{'debug'} > 1; |
163
|
0
|
|
|
|
|
|
Debug "edit_time (before update) = $edit_time"; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
my %tags = (); |
167
|
0
|
|
|
|
|
|
$tags{'wpTextbox1' } = $args{'text'}; |
168
|
0
|
|
|
|
|
|
$tags{'wpEdittime' } = $edit_time; |
169
|
0
|
|
|
|
|
|
$tags{'wpSave' } = 'Save page'; |
170
|
0
|
|
|
|
|
|
$tags{'wpSection' } = $args{'section'}; |
171
|
0
|
|
|
|
|
|
$tags{'wpSummary' } = $args{'summary'}; |
172
|
0
|
|
|
|
|
|
$tags{'wpEditToken'} = $token; |
173
|
0
|
|
|
|
|
|
$tags{'wpWatchthis'} = $args{'watch'}; |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
$tags{'title' } = $args{'title'}; |
176
|
0
|
|
|
|
|
|
$tags{'action' } = 'submit'; |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
$resp = $ua->request( |
179
|
|
|
|
|
|
|
POST "$args{'protocol'}://$WHOST/$WPATH/index.php?title=$args{'title'}&action=submit" , |
180
|
|
|
|
|
|
|
Content_Type => 'application/x-www-form-urlencoded' , |
181
|
|
|
|
|
|
|
Content => [ %tags ] |
182
|
|
|
|
|
|
|
); |
183
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
|
foreach (sort keys %{$resp->{'_headers'}}) { |
|
0
|
|
|
|
|
|
|
185
|
0
|
0
|
|
|
|
|
Debug "(header) $_ = " . $resp->{'_headers'}->{$_} if $self->{'debug'} > 1; |
186
|
|
|
|
|
|
|
} |
187
|
0
|
|
0
|
|
|
|
my $response_location = $resp->{'_headers'}->{'location'} || ''; |
188
|
0
|
0
|
|
|
|
|
Debug "Response Location: $response_location" if $self->{'debug'}; |
189
|
0
|
0
|
|
|
|
|
Debug "Comparing with \"/$args{'title'}\"" if $self->{'debug'}; |
190
|
0
|
0
|
|
|
|
|
if ($response_location =~ /[\/=]$args{'title'}/i) { |
191
|
0
|
0
|
|
|
|
|
Debug "Success!" if $self->{'debug'}; |
192
|
0
|
|
|
|
|
|
return 0; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
else { |
195
|
0
|
0
|
|
|
|
|
Debug "NOK!" if $self->{'debug'}; |
196
|
0
|
|
|
|
|
|
return 1; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub get { |
201
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
202
|
0
|
|
|
|
|
|
my $Key = shift; |
203
|
0
|
|
|
|
|
|
$Var{$Key}; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub let { |
207
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
208
|
0
|
|
|
|
|
|
my $Key = shift; |
209
|
0
|
|
|
|
|
|
my $Value = shift; |
210
|
0
|
0
|
|
|
|
|
Debug "[let] $Key = $Value" if $self->{'debug'}; |
211
|
0
|
|
|
|
|
|
$Var{$Key} = $Value; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
sub getPage { |
215
|
|
|
|
|
|
|
# returns arrayref of lines of page source |
216
|
|
|
|
|
|
|
# Function created by Matt Hucke |
217
|
0
|
|
|
0
|
0
|
|
my ($self, %args) = @_; |
218
|
|
|
|
|
|
|
|
219
|
0
|
|
0
|
|
|
|
$args{'protocol'} ||= $self->{'protocol'}; |
220
|
0
|
|
0
|
|
|
|
$args{'section' } ||= 0; |
221
|
|
|
|
|
|
|
|
222
|
0
|
0
|
|
|
|
|
if ($self->{'debug'}) { |
223
|
0
|
|
|
|
|
|
Debug "[getPage] $_ = \"$args{$_}\"" foreach keys %args; |
224
|
0
|
|
|
|
|
|
Debug "[getPage] VAR $_ = \"$Var{$_}\"" foreach keys %Var; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
0
|
|
0
|
|
|
|
my $WHOST = $self->{'host'} || 'localhost'; |
228
|
0
|
|
0
|
|
|
|
my $WPATH = $self->{'path'} || ''; |
229
|
|
|
|
|
|
|
|
230
|
0
|
0
|
|
|
|
|
Debug "Fetching page '$args{'title'}' (section '$args{'section'}')..." if $self->{'debug'}; |
231
|
|
|
|
|
|
|
|
232
|
0
|
0
|
|
|
|
|
my $edit_section = $args{'section'} ? "\§ion=$args{'section'}" : ''; |
233
|
0
|
|
|
|
|
|
my $resp = $ua->request(GET "$args{'protocol'}://$WHOST/$WPATH/index.php?title=$args{'title'}&action=edit$edit_section"); |
234
|
0
|
|
|
|
|
|
my @lines = split /\n/, $resp->content(); |
235
|
|
|
|
|
|
|
|
236
|
0
|
|
|
|
|
|
my @content = (); |
237
|
0
|
|
|
|
|
|
my $saving = 0; |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
# This is a very simple parser - it looks for and |
240
|
|
|
|
|
|
|
# and returns everything in between. |
241
|
0
|
|
|
|
|
|
for (my $jj = 0; $jj <= $#lines; $jj++) { |
242
|
0
|
|
|
|
|
|
my $line = $lines[$jj]; |
243
|
|
|
|
|
|
|
|
244
|
0
|
0
|
|
|
|
|
if ($line =~ m/
|
|
|
0
|
|
|
|
|
|
245
|
0
|
|
|
|
|
|
$saving = 1; |
246
|
|
|
|
|
|
|
|
247
|
0
|
0
|
|
|
|
|
if ($line =~ m/ |
248
|
0
|
|
|
|
|
|
$line = $1; # strip out , keep what's after. |
249
|
|
|
|
|
|
|
} else { |
250
|
|
|
|
|
|
|
# ADVANCE to next line |
251
|
0
|
|
|
|
|
|
++$jj; |
252
|
0
|
|
|
|
|
|
$line = $lines[$jj]; |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
# strip out end of textarea tag at start of line |
255
|
0
|
|
|
|
|
|
$line =~ s#^[^>]+>##; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
# if any of $line remains, fall thru to 'push' part. |
259
|
0
|
0
|
|
|
|
|
next unless ($line); |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
} elsif ($line =~ m#(.*)#) { |
262
|
0
|
0
|
0
|
|
|
|
push (@content, $line) if ($saving && $1); |
263
|
0
|
|
|
|
|
|
$saving = 0; |
264
|
|
|
|
|
|
|
} |
265
|
0
|
0
|
|
|
|
|
push (@content, $line) if ($saving); |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
# Always return an arrayref for later processing |
269
|
0
|
|
|
|
|
|
\@content; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
sub Error ($) { |
273
|
0
|
0
|
|
0
|
0
|
|
print "Content-type: text/html\n\n" unless $contentType; |
274
|
0
|
|
|
|
|
|
print "ERROR ($package): $_[0]\n"; |
275
|
0
|
|
|
|
|
|
exit(1); |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
0
|
|
|
0
|
0
|
|
sub Debug ($) { print "[ $package ] $_[0]\n"; } |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
#### Used Warning / Error Codes ########################## |
281
|
|
|
|
|
|
|
# Next free W Code: 1000 |
282
|
|
|
|
|
|
|
# Next free E Code: 1000 |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
1; |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
__END__ |