line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
MediaWiki::EditFramework - a framework for editing MediaWiki pages. |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use MediaWiki::EditFramework; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $wiki = MediaWiki::EditFramework->new('example.com', 'wiki'); |
10
|
|
|
|
|
|
|
my $page = $wiki->get_page('Main_Page'); |
11
|
|
|
|
|
|
|
my $text = $page->get_text; |
12
|
|
|
|
|
|
|
$text =~ s/old thing/new update/g; |
13
|
|
|
|
|
|
|
$page->edit($text, 'update page'); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head2 DESCRIPTION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
This is a higher level framework for editing MediaWiki pages. It depends on |
18
|
|
|
|
|
|
|
another module for lower level API access, and doesn't provide functionality |
19
|
|
|
|
|
|
|
unrelated to editing. By using a higher-level abstraction layer in scripts, |
20
|
|
|
|
|
|
|
it becomes simpler to change out backend modules as needed. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
This is the framework that I've been using for the past few years to run an |
23
|
|
|
|
|
|
|
archiving bot. The main features that it has over lower-level frameworks |
24
|
|
|
|
|
|
|
are: |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=over |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=item * |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
Pages are represented as objects. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
This allows the page to store additional information, such as the last |
33
|
|
|
|
|
|
|
updated timestamp when they were retrieved. The timestamp is then passed |
34
|
|
|
|
|
|
|
back to the server when the page is edited, to allow it to properly detect |
35
|
|
|
|
|
|
|
edit conflicts. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=item * |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
The module supports specified a write_prefix, which is appended to pages |
40
|
|
|
|
|
|
|
titles when editing a page. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
This makes it easier to create a test mode for a bot script. By specifying |
43
|
|
|
|
|
|
|
the write prefix in your own user space, the bot will retrieve the normal |
44
|
|
|
|
|
|
|
pages, but the modifications will be written to user space so you can review |
45
|
|
|
|
|
|
|
them without impacting others. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=back |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=cut |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
package MediaWiki::EditFramework; |
52
|
|
|
|
|
|
|
|
53
|
1
|
|
|
1
|
|
774
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
54
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
32
|
|
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
73
|
|
57
|
1
|
|
|
1
|
|
998
|
use MediaWiki::API; |
|
1
|
|
|
|
|
102732
|
|
|
1
|
|
|
|
|
35
|
|
58
|
1
|
|
|
1
|
|
3295
|
use Data::Dumper; |
|
1
|
|
|
|
|
5827
|
|
|
1
|
|
|
|
|
70
|
|
59
|
1
|
|
|
1
|
|
586
|
use MediaWiki::EditFramework::Page; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
60
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
409
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
63
|
|
|
|
|
|
|
our $ABSTRACT = 'framework for editing MediaWiki pages'; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head2 CONSTRUCTOR |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=over |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=item B(I,I) |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Create a new instance pointing to the specified I and I (default |
72
|
|
|
|
|
|
|
I). The underling API object points to http://I/I/api.php. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=back |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 METHODS |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=over |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub new ($;$$) { |
83
|
1
|
|
|
1
|
1
|
453
|
my ($class,$site,$path)=@_; |
84
|
1
|
|
|
|
|
7
|
my $mw = MediaWiki::API->new(); |
85
|
1
|
50
|
|
|
|
19065
|
if (defined $site) { |
86
|
1
|
50
|
|
|
|
5
|
$path = 'w' unless defined $path; |
87
|
1
|
|
|
|
|
8
|
$mw->{config}->{api_url} = "http://$site/$path/api.php"; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
#$mw->{ua}->cookie_jar({file=>"$ENV{HOME}/.cookies.txt", autosave=>1}); |
90
|
1
|
|
|
|
|
12
|
bless {0=>$mw, write_prefix=>''}, $class; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item B(I) |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Passes I to L's I method, to store cookies |
96
|
|
|
|
|
|
|
for a persistent login. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub cookie_jar( $$ ) { |
101
|
|
|
|
|
|
|
#temporary method for persistent login. |
102
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
103
|
0
|
|
|
|
|
0
|
my $file = shift; |
104
|
0
|
|
|
|
|
0
|
$self->{0}{ua}->cookie_jar($file, autosave=>1); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=item B(I,I) |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Log in the specified I. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub login ($$$) { |
114
|
0
|
|
|
0
|
1
|
0
|
my ($self,$user,$pass) = @_; |
115
|
0
|
|
|
|
|
0
|
my $mw = $self->{0}; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
0
|
my $state = $mw->api({ action=>'query', meta=>'userinfo', }); |
118
|
|
|
|
|
|
|
|
119
|
0
|
0
|
0
|
|
|
0
|
if ( |
120
|
|
|
|
|
|
|
! exists $state->{query}{userinfo}{anon} |
121
|
|
|
|
|
|
|
and |
122
|
|
|
|
|
|
|
$state->{query}{userinfo}{name} eq $user |
123
|
|
|
|
|
|
|
){ |
124
|
0
|
|
|
|
|
0
|
warn "already logged in as $user"; |
125
|
|
|
|
|
|
|
} else { |
126
|
0
|
0
|
|
|
|
0
|
$mw->login( { lgname => $user, lgpassword => $pass } ) |
127
|
|
|
|
|
|
|
or confess $mw->{error}->{code} . ': ' . |
128
|
|
|
|
|
|
|
Dumper($mw->{error}->{details}); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=item B(I) |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Get the wiki page with the specified I. Returns an instance of |
136
|
|
|
|
|
|
|
L, which has methods to get/edit the page text. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub get_page( $$ ) { |
141
|
2
|
|
|
2
|
1
|
19
|
MediaWiki::EditFramework::Page->new(@_); |
142
|
|
|
|
|
|
|
}; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item B(I) |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Get the wiki page with the specified I; then croak if it already |
147
|
|
|
|
|
|
|
exists. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=cut |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub create_page( $$ ) { |
152
|
0
|
|
|
0
|
1
|
|
my $page = MediaWiki::EditFramework::Page->new(@_); |
153
|
0
|
0
|
|
|
|
|
croak "$_[1]: exists" if $page->exists; |
154
|
0
|
|
|
|
|
|
return $page; |
155
|
|
|
|
|
|
|
}; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item B(I) |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
When writing pages, prepend the specified I to the page title. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
This makes it easier to create a test mode for a bot script. By specifying |
162
|
|
|
|
|
|
|
the write prefix in your own user space, the bot will retrieve the normal |
163
|
|
|
|
|
|
|
pages, but the modifications will be written to user space so you can review |
164
|
|
|
|
|
|
|
them without impacting others. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=cut |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub write_prefix { |
169
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
170
|
0
|
|
|
|
|
|
my $prefix = shift; |
171
|
0
|
|
|
|
|
|
$self->{write_prefix} = $prefix; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=back |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 SEE ALSO |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
L |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head1 COPYRIGHT |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Copyright (C) 2012 by Steve Sanbeg |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
185
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.10.1 or, |
186
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=cut |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
1; |