EditFramework.pm

filter: Statementstmt 67.3% 31 / 46 Branchbran 20.0% 2 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 69.2% 9 / 13 Podpod 100% 6 / 6 totaltotal 60.0% 48 / 80 CC 7 SCAR 17.8
CC
7
Cov
56%
login
20.0
create_page
6.0
new
3.1
CRAP 6.0
« EditFramework/Page.pm ↑ Summary
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use MediaWiki::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use MediaWiki::EditFramework::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 1 our $VERSION = '0.02';
63 1 our $ABSTRACT = 'framework for editing MediaWiki pages';
64  
65 =head2 CONSTRUCTOR
66  
67 =over
68  
69 =item B<new>(I<SITE>,I<PATH>)
70  
71 Create a new instance pointing to the specified I<SITE> and I<PATH> (default
72 I<w>). The underling API object points to http://I<SITE>/I<PATH>/api.php.
73  
74 =back
75  
76 =head2 METHODS
77  
78 =over
79  
80 =cut
81  
82 1 ▶sub new ($;$$) {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
83 1 my ($class,$site,$path)=@_;
84 1 my $mw = MediaWiki::API->new();
85 1 ▶ if (defined $site) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $site) 1 0 1
86 1 ▶ $path = 'w' unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 1 0 1
87 1 $mw->{config}->{api_url} = "http://$site/$path/api.php";
88 }
89 #$mw->{ua}->cookie_jar({file=>"$ENV{HOME}/.cookies.txt", autosave=>1});
90 1 bless {0=>$mw, write_prefix=>''}, $class;
91 }
92  
93 =item B<cookie_jar>(I<FILE>)
94  
95 Passes I<FILE> to L<LWP::UserAgent>'s I<cookie_jar> method, to store cookies
96 for a persistent login.
97  
98 =cut
99  
100 0 ▶sub cookie_jar( $$ ) {
Subroutine cookie_jar: not called CC=1 SCAR=6.9
Pod documented
101 #temporary method for persistent login.
102 0 my $self = shift;
103 0 my $file = shift;
104 0 $self->{0}{ua}->cookie_jar($file, autosave=>1);
105 }
106  
107 =item B<login>(I<USER>,I<PASS>)
108  
109 Log in the specified I<USER>.
110  
111 =cut
112  
113 0 ▶sub login ($$$) {
Subroutine login: not called CC=4 SCAR=30.0
Pod documented
114 0 my ($self,$user,$pass) = @_;
115 0 my $mw = $self->{0};
116  
117 0 my $state = $mw->api({ action=>'query', meta=>'userinfo', });
118
119 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $state->{'query'}{'userinfo'}{'anon'} and $state->{'query'}{'userinfo'}{'name'} eq $user) { } 0 0 0
Condition0%0/3
not exists $state->{"query"}{"userinfo"}{"anon"} and $state->{"query"}{"userinfo"}{"name"} eq $user
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $state->{"query"}{"userinfo"}{"anon"} and $state->{"query"}{"userinfo"}{"name"} eq $user
not exists $state->{"query"}{"userinfo"}{"anon"} $state->{"query"}{"userinfo"}{"name"} eq $user
Truth table0%0/3
not(A) and B
A= not exists $state->{"query"}{"userinfo"}{"anon"}B= $state->{"query"}{"userinfo"}{"name"} eq $user
ABresult
0X0
100
111
120 ! exists $state->{query}{userinfo}{anon}
121 and
122 $state->{query}{userinfo}{name} eq $user
123 ){
124 0 warn "already logged in as $user";
125 } else {
126 $mw->login( { lgname => $user, lgpassword => $pass } )
127 or confess $mw->{error}->{code} . ': ' .
128 0 ▶ Dumper($mw->{error}->{details});
Branch: 1 branch
BranchTrueFalseTotal
$mw->login({"lgname", $user, "lgpassword", $pass}) or confess($mw->{"error"}{"code"} . ': ' . Dumper($mw->{"error"}{"details"})) 0 0 0
129 }
130 }
131  
132  
133 =item B<get_page>(I<TITLE>)
134  
135 Get the wiki page with the specified I<TITLE>. Returns an instance of
136 L<MediaWiki::EditFramework::Page>, which has methods to get/edit the page text.
137  
138 =cut
139  
140 2 ▶sub get_page( $$ ) {
Subroutine get_page: called CC=1 SCAR=0.0
Pod documented
141 2 MediaWiki::EditFramework::Page->new(@_);
142 };
143  
144 =item B<create_page>(I<TITLE>)
145  
146 Get the wiki page with the specified I<TITLE>; then croak if it already
147 exists.
148  
149 =cut
150  
151 0 ▶sub create_page( $$ ) {
Subroutine create_page: not called CC=2 SCAR=17.9
Pod documented
152 0 my $page = MediaWiki::EditFramework::Page->new(@_);
153 0 ▶ croak "$_[1]: exists" if $page->exists;
Branch: 1 branch
BranchTrueFalseTotal
if $page->exists 0 0 0
154 0 return $page;
155 };
156  
157 =item B<write_prefix>(I<PREFIX>)
158  
159 When writing pages, prepend the specified I<PREFIX> 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 0 ▶sub write_prefix {
Subroutine write_prefix: not called CC=1 SCAR=6.9
Pod documented
169 0 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<MediaWiki::API>
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 1;
« EditFramework/Page.pm ↑ Summary