DumpFile/Compat.pm

filter: Statementstmt 98.0% 196 / 200 Branchbran 88.1% 37 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 98.1% 52 / 53 Podpod n/a totaltotal 96.6% 285 / 295 CC 26 SCAR 8.0
CC
26
Cov
96%
namespace
5.0
categories
5.0
get_category_anchor
4.0
CRAP 2.2
« DumpFile/SQL.pm ↑ Summary DumpFile.pm »
1 #!/usr/bin/env perl
2  
3 #Parse::MediaWikiDump compatibility
4  
5 package MediaWiki::DumpFile::Compat;
6  
7 6 our $VERSION = '0.2.0';
8  
9 package #go away indexer!
10 Parse::MediaWikiDump;
11  
12 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
16 0 my ($class) = @_;
17 0 return bless({}, $class);
18 }
19  
20 3 ▶sub pages {
Subroutine pages: called CC=1 SCAR=0.0
21 3 shift(@_);
22 3 return Parse::MediaWikiDump::Pages->new(@_);
23 }
24  
25 2 ▶sub revisions {
Subroutine revisions: called CC=1 SCAR=0.0
26 2 shift(@_);
27 2 return Parse::MediaWikiDump::Revisions->new(@_);
28 }
29  
30 1 ▶sub links {
Subroutine links: called CC=1 SCAR=0.0
31 1 shift(@_);
32 1 return Parse::MediaWikiDump::Links->new(@_);
33 }
34  
35 package #go away indexer!
36 Parse::MediaWikiDump::Links;
37  
38 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 6 ▶use MediaWiki::DumpFile::SQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
44 2 my ($class, $source) = @_;
45 2 my $self = {};
46 2 my $sql;
47
48 2 $Carp::CarpLevel++;
49 2 $sql = MediaWiki::DumpFile::SQL->new($source);
50 2 $Carp::CarpLevel--;
51
52 2 ▶ if (! defined($sql)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sql) 0 2 2
53 0 die "could not create SQL parser";
54 }
55
56 2 $self->{sql} = $sql;
57
58 2 return bless($self, $class);
59 }
60  
61 18 ▶sub next {
Subroutine next: called CC=2 SCAR=6.9
62 18 my ($self) = @_;
63 18 my $next = $self->{sql}->next;
64
65 18 ▶ unless(defined($next)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $next) 1 17 18
66 1 return undef;
67 }
68
69 17 return Parse::MediaWikiDump::link->new($next);
70 }
71  
72 package #go away indexer!
73 Parse::MediaWikiDump::link;
74  
75 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 17 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
81 17 my ($class, $self) = @_;
82
83 17 bless($self, $class);
84 }
85  
86 18 ▶sub from {
Subroutine from: called CC=1 SCAR=0.0
87 18 return $_[0]->{pl_from};
88 }
89  
90 1 ▶sub namespace {
Subroutine namespace: called CC=1 SCAR=0.0
91 1 return $_[0]->{pl_namespace};
92 }
93  
94 1 ▶sub to {
Subroutine to: called CC=1 SCAR=0.0
95 1 return $_[0]->{pl_title};
96 }
97  
98 package #go away indexer!
99 Parse::MediaWikiDump::Revisions;
100  
101 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 6 ▶use MediaWiki::DumpFile::Pages;
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
108 7 my ($class, @args) = @_;
109 7 my $self = { queue => [] };
110 7 my $mediawiki;
111
112 7 $Carp::CarpLevel++;
113 7 $mediawiki = MediaWiki::DumpFile::Pages->new(@args);
114 7 $Carp::CarpLevel--;
115
116 7 $self->{mediawiki} = $mediawiki;
117
118 7 return bless($self, $class);
119 }
120  
121 4 ▶sub version {
Subroutine version: called CC=1 SCAR=0.0
122 4 return $_[0]->{mediawiki}->version;
123 }
124  
125 4 ▶sub sitename {
Subroutine sitename: called CC=1 SCAR=0.0
126 4 return $_[0]->{mediawiki}->sitename;
127 }
128  
129 4 ▶sub base {
Subroutine base: called CC=1 SCAR=0.0
130 4 return $_[0]->{mediawiki}->base;
131 }
132  
133 4 ▶sub generator {
Subroutine generator: called CC=1 SCAR=0.0
134 4 return $_[0]->{mediawiki}->generator;
135 }
136  
137 4 ▶sub case {
Subroutine case: called CC=1 SCAR=0.0
138 4 return $_[0]->{mediawiki}->case;
139 }
140  
141 58 ▶sub namespaces {
Subroutine namespaces: called CC=3 SCAR=11.0
142 58 my $cache = $_[0]->{cache}->{namespaces};
143
144 58 ▶ if(defined($cache)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache) 53 5 58
145 53 return $cache;
146 }
147
148 5 my %namespaces = $_[0]->{mediawiki}->namespaces;
149 5 my @temp;
150
151 5 ▶ while(my ($key, $val) = each(%namespaces)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %namespaces) 90 5 95
152 90 push(@temp, [$key, $val]);
153 }
154
155 5 @temp = sort({$a->[0] <=> $b->[0]} @temp);
156
157 5 $_[0]->{cache}->{namespaces} = \@temp;
158
159 5 return \@temp;
160 }
161  
162 4 ▶sub namespaces_names {
Subroutine namespaces_names: called CC=3 SCAR=11.0
163 4 my ($self) = @_;
164 4 my @result;
165
166 4 ▶ return $self->{cache}->{namespaces_names} if defined $self->{cache}->{namespaces_names};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"cache"}{"namespaces_names"} 0 4 4
167
168 4 foreach (@{ $_[0]->namespaces }) {
169 72 push(@result, $_->[1]);
170 }
171
172 4 $self->{cache}->{namespaces_names} = \@result;
173
174 4 return \@result;
175 }
176  
177 4 ▶sub current_byte {
Subroutine current_byte: called CC=1 SCAR=0.0
178 4 return $_[0]->{mediawiki}->current_byte;
179 }
180  
181 4 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
182 4 return $_[0]->{mediawiki}->size;
183 }
184  
185 25 ▶sub get_category_anchor {
Subroutine get_category_anchor: called CC=4 SCAR=13.9
186 25 my ($self) = @_;
187 25 my $namespaces = $self->namespaces;
188 25 my $cache = $self->{cache};
189 25 my $ret = undef;
190
191 25 ▶ if (defined($cache->{category_anchor})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache->{"category_anchor"}) 20 5 25
192 20 return $cache->{category_anchor};
193 }
194  
195 5 foreach (@$namespaces) {
196 90 my ($id, $name) = @$_;
197 90 ▶ if ($id == 14) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id == 14) 5 85 90
198 5 $ret = $name;
199 }
200 }
201
202 5 $self->{cache}->{category_anchor} = $ret;
203
204 5 return $ret;
205 }
206  
207 16 ▶sub next {
Subroutine next: called CC=4 SCAR=13.9
208 16 my $self = $_[0];
209 16 my $queue = $_[0]->{queue};
210 16 my $next = shift(@$queue);
211 16 my @results;
212  
213 16 ▶ return $next if defined $next;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next 4 12 16
214
215 12 $next = $self->{mediawiki}->next;
216
217 12 ▶ return undef unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 2 10 12
218  
219 10 foreach ($next->revision) {
220 14 push(@$queue, Parse::MediaWikiDump::page->new($next, $self->namespaces, $self->get_category_anchor, $_));
221 }
222
223 10 return shift(@$queue);
224 }
225  
226 package #go away indexer!
227 Parse::MediaWikiDump::Pages;
228  
229 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
230 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
231  
232 6 our @ISA = qw(Parse::MediaWikiDump::Revisions);
233  
234 14 ▶sub next {
Subroutine next: called CC=3 SCAR=11.0
235 14 my $self = $_[0];
236 14 my $next = $self->{mediawiki}->next;
237 14 my $revision_count;
238
239 14 ▶ return undef unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 2 12 14
240
241 12 $revision_count = scalar(@{[$next->revision]});
242 #^^^^^ because scalar($next->revision) doesn't work
243
244 12 ▶ if ($revision_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($revision_count > 1) 1 11 12
245 1 die "only one revision per page is allowed\n";
246 }
247  
248 11 return Parse::MediaWikiDump::page->new($next, $self->namespaces, $self->get_category_anchor);
249 }
250  
251  
252 package #go away indexer!
253 Parse::MediaWikiDump::page;
254  
255 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
256 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
257  
258 6 our %REGEX_CACHE_CATEGORIES;
259  
260 25 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
261 25 my ($class, $page, $namespaces, $category_anchor, $revision) = @_;
262 25 my $self = {page => $page, namespaces => $namespaces, category_anchor => $category_anchor};
263
264 25 $self->{revision} = $revision;
265
266 25 return bless($self, $class);
267 }
268  
269 120 ▶sub _revision {
Subroutine _revision: called CC=2 SCAR=6.9
270 120 ▶ if (defined($_[0]->{revision})) { return $_[0]->{revision}};
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]{"revision"}) 64 56 120
271
272 56 return $_[0]->{page}->revision;
273 }
274  
275 36 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
276 36 my $text = $_[0]->_revision->text;
277 36 return \$text;
278 }
279  
280 36 ▶sub title {
Subroutine title: called CC=1 SCAR=0.0
281 36 return $_[0]->{page}->title;
282 }
283  
284 18 ▶sub id {
Subroutine id: called CC=1 SCAR=0.0
285 18 return $_[0]->{page}->id;
286 }
287  
288 8 ▶sub revision_id {
Subroutine revision_id: called CC=1 SCAR=0.0
289 8 return $_[0]->_revision->id;
290 }
291  
292 22 ▶sub username {
Subroutine username: called CC=1 SCAR=0.0
293 22 return $_[0]->_revision->contributor->username;
294 }
295  
296 22 ▶sub userid {
Subroutine userid: called CC=1 SCAR=0.0
297 22 return $_[0]->_revision->contributor->id;
298 }
299  
300 6 ▶sub userip {
Subroutine userip: called CC=1 SCAR=0.0
301 6 return $_[0]->_revision->contributor->ip;
302 }
303  
304 16 ▶sub timestamp {
Subroutine timestamp: called CC=1 SCAR=0.0
305 16 return $_[0]->_revision->timestamp;
306 }
307  
308 10 ▶sub minor {
Subroutine minor: called CC=1 SCAR=0.0
309 10 return $_[0]->_revision->minor;
310 }
311  
312 12 ▶sub namespace {
Subroutine namespace: called CC=5 SCAR=16.1
313 12 my ($self) = @_;
314 12 my $title = $self->title;
315 12 my $namespace = '';
316
317 12 ▶ if (defined($self->{cache}->{namespace})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cache"}{"namespace"}) 0 12 12
318 0 return $self->{cache}->{namespace};
319 }
320
321 12 ▶ if ($title =~ m/^([^:]+):(.*)/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ /^([^:]+):(.*)/o) 8 4 12
322 8 foreach (@{ $self->{namespaces} } ) {
323 88 my ($num, $name) = @$_;
324 88 ▶ if ($1 eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq $name) 4 84 88
325 4 $namespace = $1;
326 4 last;
327 }
328 }
329 }
330  
331 12 $self->{cache}->{namespace} = $namespace;
332  
333 12 return $namespace;
334 }
335  
336 10 ▶sub redirect {
Subroutine redirect: called CC=3 SCAR=11.0
337 10 my ($self) = @_;
338 10 my $text = $self->text;
339 10 my $ret;
340
341 10 ▶ return $self->{cache}->{redirect} if defined $self->{cache}->{redirect};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"cache"}{"redirect"} 0 10 10
342  
343 10 ▶ if ($$text =~ m/^#redirect\s*:?\s*\[\[([^\]]*)\]\]/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /^#redirect\s*:?\s*\[\[([^\]]*)\]\]/io) { } 6 4 10
344 6 $ret = $1;
345 } else {
346 4 $ret = undef;
347 }
348
349 10 $self->{cache}->{redirect} = $ret;
350
351 10 return $ret;
352 }
353  
354 12 ▶sub categories {
Subroutine categories: called CC=5 SCAR=16.1
355 12 my ($self) = @_;
356 12 my $anchor = $$self{category_anchor};
357 12 my $text = $self->text;
358 12 my @cats;
359 my $ret;
360
361 12 ▶ return $self->{cache}->{categories} if defined $self->{cache}->{categories};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"cache"}{"categories"} 0 12 12
362
363 12 ▶ if (! defined($REGEX_CACHE_CATEGORIES{$anchor})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Parse::MediaWikiDump::page::REGEX_CACHE_CATEGORIES{$anchor}) 2 10 12
364 2 $REGEX_CACHE_CATEGORIES{$anchor} = qr/\[\[$anchor:\s*([^\]]+)\]\]/i;
365 }
366
367 12 ▶ while($$text =~ /$REGEX_CACHE_CATEGORIES{$anchor}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /$Parse::MediaWikiDump::page::REGEX_CACHE_CATEGORIES{$anchor}/g) 4 12 16
368 4 my $buf = $1;
369
370 #deal with the pipe trick
371 4 $buf =~ s/\|.*$//;
372 4 push(@cats, $buf);
373 }
374  
375 12 ▶ if (scalar(@cats) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @cats == 0) { } 8 4 12
376 8 $ret = undef;
377 } else {
378 4 $ret = \@cats;
379 }
380  
381 12 return $ret;
382 }
383  
384  
385 6 1;
386  
387 __END__
« DumpFile/SQL.pm ↑ Summary DumpFile.pm »