EditFramework/Page.pm

filter: Statementstmt 39.4% 15 / 38 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod 50.0% 2 / 4 totaltotal 37.9% 22 / 58 CC 5 SCAR 21.1
CC
5
Cov
33%
edit
20.0
_dump_text
6.0
get_text
2.0
CRAP 8.2
↑ Summary EditFramework.pm »
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<edit> 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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 2 ▶sub new ($$$) {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
35 2 my ($class,$api,$title) = @_;
36 2 my $page = $api->{0}->get_page({title=>$title});
37 2 bless [$api,$page], $class;
38 }
39  
40 0 ▶sub _dump_text {
Subroutine _dump_text: not called CC=2 SCAR=17.9
41 0 my ($self,$prefix,$text) = @_;
42 0 my ($mw,$page) = @$self;
43  
44 0 ▶ if (defined $mw->{text_dump_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mw->{"text_dump_dir"}) 0 0 0
45 0 my $title = $page->{title};
46 0 $title =~ s:/:-:g;
47
48 0 open my($fh), '>', "$mw->{text_dump_dir}/$prefix-$title";
49 0 print $fh $text;
50 0 close $fh;
51 }
52 }
53 =item B<get_text>
54  
55 Get the current page text.
56  
57 =cut
58  
59 0 ▶sub get_text( $ ) {
Subroutine get_text: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my $self = shift;
61 0 my ($mw,$page) = @$self;
62 0 $self->_dump_text(read=>$page->{'*'});
63 0 return $page->{'*'};
64 }
65  
66 =item B<edit>(I<TEXT>,I<SUMMARY>)
67  
68 Replace the text of the page with the specified I<TEXT>. Using the
69 specified I<SUMMARY> 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<MediaWiki::EditFramework> object has an I<edit_prefix> 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 0 ▶sub edit( $$$ ) {
Subroutine edit: not called CC=4 SCAR=30.0
Pod documented
83 0 my ($self,$text,$summary) = @_;
84 0 my $mw = $self->[0];
85 0 my $page = $self->[1];
86 0 my $p = $mw->{write_prefix};
87  
88 0 $self->_dump_text(write=>$text);
89
90 my %qh = (
91 action=>'edit',
92 bot=>1,
93 0 title=>($p.$page->{title}),
94 text=>$text,
95 summary=>$summary,
96 );
97  
98 0 ▶ $qh{basetimestamp}=$page->{timestamp} if $p eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $p eq "" 0 0 0
99  
100 0 eval {
101 0 warn "edit $qh{title}";
102 $mw->{0}->edit(\%qh)
103 0 ▶ or croak $mw->{error}->{code} . ': ' . $mw->{error}->{details};
Branch: 1 branch
BranchTrueFalseTotal
$mw->{0}->edit(\%qh) or $mw->croak->{"error"}{"code"} . ': ' . $mw->{"error"}{"details"} 0 0 0
104 };
105
106 0 ▶ die "Edit failed: ". Data::Dumper::Dumper($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
107 }
108
109 =item B<exists>
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 2 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
119 2 my ($self) = @_;
120 2 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 1;
↑ Summary EditFramework.pm »