/usr/share/perl5/Parse/MediaWikiDump/Revisions.pm is in libparse-mediawikidump-perl 1.0.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | package Parse::MediaWikiDump::Revisions;
our $VERSION = '1.0.4';
use 5.8.0;
use strict;
use warnings;
use Carp;
use List::Util;
use Scalar::Util qw(weaken reftype);
use Data::Dumper;
sub DESTROY {
my ($self) = @_;
if (! $self->{FINISHED}) {
$self->{EXPAT}->parse_done;
}
}
#public methods
sub new {
my ($class, @args) = @_;
my $self = {};
my $source;
if (scalar(@args) == 0) {
die "you must specify an argument to new()";
} elsif (scalar(@args) == 1) {
$source = $args[0];
} else {
my %conf = @args;
if (! defined($conf{input})) {
die "input is a required parameter to new()";
}
$source = $conf{input};
}
bless($self, $class);
$$self{XML} = undef; #holder for XML::Accumulator
$$self{EXPAT} = undef; #holder for expat under XML::Accumulator
$$self{SITEINFO} = {}; #holder for the data from siteinfo
$$self{PAGE_LIST} = []; #place to store articles as they come out of XML::Accumulator
$$self{BYTE} = 0;
$$self{CHUNK_SIZE} = 32768;
$$self{FINISHED} = 0;
$self->open($source);
$self->init;
return $self;
}
sub next {
my ($self) = @_;
my $case = $self->{SITEINFO}->{CASE};
my $namespaces = $self->{SITEINFO}->{namespaces};
my $page;
#look for an available page and if one isn't
#there then parse more XML
while(1) {
$page = shift(@{ $self->{PAGE_LIST} } );
if (defined($page)) {
return Parse::MediaWikiDump::page->new($page, $self->get_category_anchor, $case, $namespaces);
}
return undef unless $self->parse_more;
}
die "should not get here";
}
sub version {
my ($self) = @_;
return $self->{SITEINFO}{version};
}
sub sitename {
my ($self) = @_;
return $$self{SITEINFO}{sitename};
}
sub base {
my ($self) = @_;
return $$self{SITEINFO}{base};
}
sub generator {
my ($self) = @_;
return $$self{SITEINFO}{generator};
}
sub case {
my ($self) = @_;
return $$self{SITEINFO}{case};
}
sub namespaces {
my ($self) = @_;
return $$self{SITEINFO}{namespaces};
}
sub namespaces_names {
my $self = shift;
my @result;
foreach (@{ $$self{SITEINFO}{namespaces} }) {
push(@result, $_->[1]);
}
return \@result;
}
sub current_byte {
my ($self) = @_;
return $$self{BYTE};
}
sub size {
my ($self) = @_;
return undef unless defined $$self{SOURCE_FILE};
my @stat = stat($$self{SOURCE_FILE});
return $stat[7];
}
#private functions with OO interface
sub open {
my ($self, $source) = @_;
if (defined(reftype($source)) && reftype($source) eq 'GLOB') {
$$self{SOURCE} = $source;
} else {
if (! open($$self{SOURCE}, $source)) {
die "could not open $source: $!";
}
$$self{SOURCE_FILE} = $source;
}
binmode($$self{SOURCE}, ':utf8');
return 1;
}
sub init {
my ($self) = @_;
$self->{XML} = $self->new_accumulator_engine;
my $expat_bb = $$self{XML}->parser->parse_start();
$$self{EXPAT} = $expat_bb;
#load the information from the siteinfo section so it is available before
#someone calls ->next
while(scalar(@{$self->{PAGE_LIST}}) < 1) {
die "hit end of document" unless $self->parse_more;
}
}
sub new_accumulator_engine {
my ($self) = @_;
my $f = Parse::MediaWikiDump::XML::Accumulator->new;
my $store_siteinfo = $self->{SITEINFO};
my $store_page = $self->{PAGE_LIST};
my $root = $f->root;
my $mediawiki = $f->node('mediawiki', Start => \&handle_mediawiki_node);
#stuff for siteinfo
my $siteinfo = $f->node('siteinfo', End => sub { %$store_siteinfo = %{ $_[1] } } );
my $sitename = $f->textcapture('sitename');
my $base = $f->textcapture('base');
my $generator = $f->textcapture('generator');
my $case = $f->textcapture('case');
my $namespaces = $f->node('namespaces', Start => sub { $_[1]->{namespaces} = []; } );
my $namespace = $f->node('namespace', Character => \&save_namespace_node);
#stuff for page entries
my $page = $f->node('page', Start => sub { $_[0]->accumulator( {} ) } );
my $title = $f->textcapture('title');
my $id = $f->textcapture('id');
my $revision = $f->node('revision',
Start => \&handle_revision_node_start, End => sub { push(@$store_page, { %{ $_[1] } } ) } );
my $rev_id = $f->textcapture('id', 'revision_id');
my $minor = $f->node('minor', Start => sub { $_[1]->{minor} = 1 } );
my $time = $f->textcapture('timestamp');
my $contributor = $f->node('contributor');
my $username = $f->textcapture('username');
my $ip = $f->textcapture('ip', 'userip');
my $contrib_id = $f->textcapture('id', 'userid');
my $comment = $f->textcapture('comment');
my $text = $f->textcapture('text');
my $restr = $f->textcapture('restrictions');
#put together the tree
$siteinfo->add_child($sitename, $base, $generator, $case, $namespaces);
$namespaces->add_child($namespace);
$page->add_child($title, $id, $revision, $restr);
$revision->add_child($rev_id, $time, $contributor, $minor, $comment, $text);
$contributor->add_child($username, $ip, $contrib_id);
$mediawiki->add_child($siteinfo, $page);
$root->add_child($mediawiki);
my $engine = $f->engine($root, {});
return $engine;
}
sub parse_more {
my ($self) = @_;
my $buf;
my $read = read($$self{SOURCE}, $buf, $$self{CHUNK_SIZE});
if (! defined($read)) {
die "error during read: $!";
} elsif ($read == 0) {
$$self{FINISHED} = 1;
$$self{EXPAT}->parse_done;
return 0;
}
#expat has a bug where the current_byte
#value overflows around 2 gigabytes
#so we track how much data has been
#processed ourselves
$$self{BYTE} += $read;
$$self{EXPAT}->parse_more($buf);
if ($self->{DIE_REQUESTED}) {
die "$self->{DIE_REQUESTED}\n";
}
return 1;
}
sub get_category_anchor {
my ($self) = @_;
my $namespaces = $self->{SITEINFO}->{namespaces};
foreach (@$namespaces) {
my ($id, $name) = @$_;
if ($id == 14) {
return $name;
}
}
return undef;
}
#helper functions that the xml accumulator uses
sub save_namespace_node {
my ($parser, $accum, $text, $element, $attrs) = @_;
my $key = $attrs->{key};
my $namespaces = $accum->{namespaces};
push(@{ $accum->{namespaces} }, [$key, $text] );
}
sub handle_mediawiki_node {
my ($engine, $a, $element, $attrs) = @_;
my $version = $attrs->{version};
#checking versions of the dump file removed in 1.0.6
#see the migration notes for why
# if ($version ne '0.3' && $version ne '0.4') {
# die "Only version 0.3 and 0.4 dump files are supported";
# }
$a->{version} = $version;
}
sub handle_revision_node_start {
my (undef, $a) = @_;
$a->{minor} = 0;
delete($a->{username});
delete($a->{userid});
delete($a->{userip});
}
sub save_siteinfo {
my ($self, $info) = @_;
my %info = %$info;
$self->{SITEINFO} = \%info;
}
1;
__END__
=head1 NAME
Parse::MediaWikiDump::Revisions - Object capable of processing dump files with multiple revisions per article
=head1 ABOUT
This object is used to access the metadata associated with a MediaWiki instance and provide an iterative interface
for extracting the indidivual article revisions out of the same. To gurantee that there is only a single
revision per article use the Parse::MediaWikiDump::Revisions object.
=head1 SYNOPSIS
$pmwd = Parse::MediaWikiDump->new;
$revisions = $pmwd->revisions('pages-articles.xml');
$revisions = $pmwd->revisions(\*FILEHANDLE);
#print the title and id of each article inside the dump file
while(defined($page = $revisions->next)) {
print "title '", $page->title, "' id ", $page->id, "\n";
}
=head1 STATUS
This software is being RETIRED - MediaWiki::DumpFile is the official successor to
Parse::MediaWikiDump and includes a compatibility library called MediaWiki::DumpFile::Compat
that is 100% API compatible and is a near perfect standin for this module. It is faster
in all instances where it counts and is actively maintained. Any undocumented deviation
of MediaWiki::DumpFile::Compat from Parse::MediaWikiDump is considered a bug and will
be fixed.
=head1 METHODS
=over 4
=item $revisions->new
Open the specified MediaWiki dump file. If the single argument to this method
is a string it will be used as the path to the file to open. If the argument
is a reference to a filehandle the contents will be read from the filehandle as
specified.
=item $revisions->next
Returns an instance of the next available Parse::MediaWikiDump::page object or returns undef
if there are no more articles left.
=item $revisions->version
Returns a plain text string of the dump file format revision number
=item $revisions->sitename
Returns a plain text string that is the name of the MediaWiki instance.
=item $revisions->base
Returns the URL to the instances main article in the form of a string.
=item $revisions->generator
Returns a string containing 'MediaWiki' and a version number of the instance that dumped this file.
Example: 'MediaWiki 1.14alpha'
=item $revisions->case
Returns a string describing the case sensitivity configured in the instance.
=item $revisions->namespaces
Returns a reference to an array of references. Each reference is to another array with the first
item being the unique identifier of the namespace and the second element containing a string
that is the name of the namespace.
=item $revisions->namespaces_names
Returns an array reference the array contains strings of all the namespaces each as an element.
=item $revisions->current_byte
Returns the number of bytes that has been processed so far
=item $revisions->size
Returns the total size of the dump file in bytes.
=back
=head1 EXAMPLE
=head2 Extract the article text of each revision of an article using a given title
#!/usr/bin/perl
use strict;
use warnings;
use Parse::MediaWikiDump;
my $file = shift(@ARGV) or die "must specify a MediaWiki dump of the current pages";
my $title = shift(@ARGV) or die "must specify an article title";
my $pmwd = Parse::MediaWikiDump->new;
my $dump = $pmwd->revisions($file);
my $found = 0;
binmode(STDOUT, ':utf8');
binmode(STDERR, ':utf8');
#this is the only currently known value but there could be more in the future
if ($dump->case ne 'first-letter') {
die "unable to handle any case setting besides 'first-letter'";
}
$title = case_fixer($title);
while(my $revision = $dump->next) {
if ($revision->title eq $title) {
print STDERR "Located text for $title revision ", $revision->revision_id, "\n";
my $text = $revision->text;
print $$text;
$found = 1;
}
}
print STDERR "Unable to find article text for $title\n" unless $found;
exit 1;
#removes any case sensativity from the very first letter of the title
#but not from the optional namespace name
sub case_fixer {
my $title = shift;
#check for namespace
if ($title =~ /^(.+?):(.+)/) {
$title = $1 . ':' . ucfirst($2);
} else {
$title = ucfirst($title);
}
return $title;
}
=head1 LIMITATIONS
=head2 Version 0.4
This class was updated to support version 0.4 dump files from
a MediaWiki instance but it does not currently support any of
the new information available in those files.
|