File Coverage

blib/lib/MediaWiki/EditFramework/Page.pm
Criterion Covered Total %
statement 14 37 37.8
branch 0 8 0.0
condition n/a
subroutine 5 8 62.5
pod 2 4 50.0
total 21 57 36.8


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             MediaWiki::EditFramework::Page - a MediaWiki page object.
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             =head1 DESCRIPTION
16              
17             This module represents a MediaWiki page. When instantiated, it gets the
18             page information from the site. The last modified timestamp is stored when
19             the page is retrieved, and passed back via the I method to allow the
20             server to properly detect edit conflicts.
21              
22             =head2 METHODS
23              
24             =over
25              
26             =cut
27              
28             package MediaWiki::EditFramework::Page;
29 1     1   6 use strict;
  1         2  
  1         29  
30 1     1   6 use warnings;
  1         2  
  1         28  
31 1     1   5 use Data::Dumper;
  1         2  
  1         440  
32              
33              
34             sub new ($$$) {
35 2     2 0 5 my ($class,$api,$title) = @_;
36 2         13 my $page = $api->{0}->get_page({title=>$title});
37 2         375 bless [$api,$page], $class;
38             }
39              
40             sub _dump_text {
41 0     0   0 my ($self,$prefix,$text) = @_;
42 0         0 my ($mw,$page) = @$self;
43              
44 0 0       0 if (defined $mw->{text_dump_dir}) {
45 0         0 my $title = $page->{title};
46 0         0 $title =~ s:/:-:g;
47            
48 0         0 open my($fh), '>', "$mw->{text_dump_dir}/$prefix-$title";
49 0         0 print $fh $text;
50 0         0 close $fh;
51             }
52             }
53             =item B
54              
55             Get the current page text.
56              
57             =cut
58              
59             sub get_text( $ ) {
60 0     0 0 0 my $self = shift;
61 0         0 my ($mw,$page) = @$self;
62 0         0 $self->_dump_text(read=>$page->{'*'});
63 0         0 return $page->{'*'};
64             }
65              
66             =item B(I,I)
67              
68             Replace the text of the page with the specified I. Using the
69             specified I as the edit summary to describe the edit.
70              
71             The original timestamp will be passed back; if the page was editing between
72             when it was retrieved and when edit is called, and the server cannot
73             automatically merge our edit, then the edit will be rejected in order to
74             prevent clobbering the other edit.
75              
76             If the L object has an I in effect,
77             that will be prepended to the page name. This allow redirecting the output
78             from an edit into user space, to facilitate testing bot code.
79              
80             =cut
81              
82             sub edit( $$$ ) {
83 0     0 1 0 my ($self,$text,$summary) = @_;
84 0         0 my $mw = $self->[0];
85 0         0 my $page = $self->[1];
86 0         0 my $p = $mw->{write_prefix};
87              
88 0         0 $self->_dump_text(write=>$text);
89            
90 0         0 my %qh = (
91             action=>'edit',
92             bot=>1,
93             title=>($p.$page->{title}),
94             text=>$text,
95             summary=>$summary,
96             );
97              
98 0 0       0 $qh{basetimestamp}=$page->{timestamp} if $p eq '';
99              
100 0         0 eval {
101 0         0 warn "edit $qh{title}";
102 0 0       0 $mw->{0}->edit(\%qh)
103             or croak $mw->{error}->{code} . ': ' . $mw->{error}->{details};
104             };
105            
106 0 0       0 die "Edit failed: ". Data::Dumper::Dumper($@) if $@;
107             }
108            
109             =item B
110              
111             Returns true if the underlying page existed on the server when the page was
112             retrieved, false if it is a new page.
113              
114             =back
115              
116             =cut
117              
118             sub exists {
119 2     2 1 4 my ($self) = @_;
120 2         11 return exists $self->[1]->{'*'};
121             }
122              
123             =head1 COPYRIGHT
124              
125             Copyright (C) 2012 by Steve Sanbeg
126              
127             This library is free software; you can redistribute it and/or modify
128             it under the same terms as Perl itself, either Perl version 5.10.1 or,
129             at your option, any later version of Perl 5 you may have available.
130              
131             =cut
132              
133             1;