MediaW...:Pages(3pm)
π Parse::MediaWikiDump::Pages π
okay bestie, welcome to MediaW...:Pages(3pm) β the manual page for Parse::MediaWikiDump::Pages, and no cap, this thing is the β¨ moment β¨ where you get to read a MediaWiki dump that has exactly one revision per article. π
πΈ NAME πΈ
Parse::MediaWikiDump::Pages β an object that is literally built to process dump files with a single revision per article. π
π¦ ABOUT π¦
So here's the vibe: this object gives you access to all the metadata hanging off a MediaWiki instance, AND it hands you an iterative interface for pulling the individual articles out of that same instance. Two jobs, one icon. π
β οΈ but lowkey important: this module does not allow more than one revision for each specific article. If you need to parse a comprehensive dump file, you want the Parse::MediaWikiDump::Revisions object instead, bestie. Don't try to make this one do a job it wasn't hired for. π
β¨ SYNOPSIS β¨
use MediaWiki::DumpFile::Compat;
$pmwd = Parse::MediaWikiDump->new;
$input = 'pages-articles.xml';
$input = \*FILEHANDLE;
$pages = $pmwd->pages();
$pages = $pmwd->pages();
$pages = $pmwd->pages(input => $input, fast_mode => 0);
#print the title and id of each article inside the dump file
while(defined($page = $pages->next)) {
print "title '", $page->title, "' id ", $page->id, "\n";
}
(yes the $pmwd->pages() line is in there twice, that's just how the original serves it, we respect the lore π«Ά)
π METHODS π
$pages->newβ opens the specified MediaWiki dump file. If the single argument you hand it is a string, it gets used as the path to the file to open. If that argument is a reference to a filehandle, then the contents get read from the filehandle as specified. π
If you supply more than one argument, those arguments have to be a hash of configuration options. The input option is required and works exactly the same as described above. The fast_mode option is optional, defaults to off, and if you set it to a true value the parser runs in a mode that is much faster β but it only gives you access to the title and text contents of a page. Head over to MediaWiki::DumpFile::Pages for the details about fast mode. β‘
$pages->nextβ returns an instance of the next availableParse::MediaWikiDump::pageobject, or returnsundefif there are no more articles left. π$pages->versionβ returns a plain text string of the dump file format revision number.$pages->sitenameβ returns a plain text string that is the name of the MediaWiki instance.$pages->baseβ returns the URL to the instance's main article, as a string.$pages->generatorβ returns a string containing'MediaWiki'and a version number of the instance that dumped this file. Example:'MediaWiki 1.14alpha'$pages->caseβ returns a string describing the case sensitivity configured in the instance.$pages->namespacesβ returns a reference to an array of references. Each of those references points to another array where the first item is the unique identifier of the namespace and the second element contains a string that is the name of the namespace. π$pages->namespaces_namesβ returns an array reference; the array contains strings of all the namespaces, each one as an element.$pages->current_byteβ returns the number of bytes that have been processed so far. π$pages->sizeβ returns the total size of the dump file in bytes.
π Scan an article dump file for double redirects that exist in the most recent article revision π
#!/usr/bin/perl
#progress information goes to STDERR, a list of double redirects found
#goes to STDOUT
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
use strict;
use warnings;
use MediaWiki::DumpFile::Compat;
my $file = shift(@ARGV);
my $pmwd = Parse::MediaWikiDump->new;
my $pages;
my $page;
my %redirs;
my $artcount = 0;
my $file_size;
my $start = time;
if (defined($file)) {
$file_size = (stat($file))[7];
$pages = $pmwd->pages($file);
} else {
print STDERR "No file specified, using standard input\n";
$pages = $pmwd->pages(\*STDIN);
}
#the case of the first letter of titles is ignored - force this option
#because the other values of the case setting are unknown
die 'this program only supports the first-letter case setting' unless
$pages->case eq 'first-letter';
print STDERR "Analyzing articles:\n";
while(defined($page = $pages->next)) {
update_ui() if ++$artcount % 500 == 0;
#main namespace only
next unless $page->namespace eq '';
next unless defined($page->redirect);
my $title = case_fixer($page->title);
#create a list of redirects indexed by their original name
$redirs{$title} = case_fixer($page->redirect);
}
my $redir_count = scalar(keys(%redirs));
print STDERR "done; searching $redir_count redirects:\n";
my $count = 0;
#if a redirect location is also a key to the index we have a double redirect
foreach my $key (keys(%redirs)) {
my $redirect = $redirs{$key};
if (defined($redirs{$redirect})) {
print "$key\n";
$count++;
}
}
print STDERR "discovered $count double redirects\n";
#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;
}
sub pretty_bytes {
my $bytes = shift;
my $pretty = int($bytes) . ' bytes';
if (($bytes = $bytes / 1024) > 1) {
$pretty = int($bytes) . ' kilobytes';
}
if (($bytes = $bytes / 1024) > 1) {
$pretty = sprintf("%0.2f", $bytes) . ' megabytes';
}
if (($bytes = $bytes / 1024) > 1) {
$pretty = sprintf("%0.4f", $bytes) . ' gigabytes';
}
return $pretty;
}
sub pretty_number {
my $number = reverse(shift);
$number =~ s/(...)/$1,/g;
$number = reverse($number);
$number =~ s/^,//;
return $number;
}
sub update_ui {
my $seconds = time - $start;
my $bytes = $pages->current_byte;
print STDERR " ", pretty_number($artcount), " articles; ";
print STDERR pretty_bytes($bytes), " processed; ";
if (defined($file_size)) {
my $percent = int($bytes / $file_size * 100);
print STDERR "$percent% completed\n";
} else {
my $bytes_per_second = int($bytes / $seconds);
print STDERR pretty_bytes($bytes_per_second), " per second\n";
}
}
π 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. it's giving "we got the invite but not the full tea" π
and that's the whole page, bestie!! this manual came to you from the User Contributed Perl Documentation for MediaW...:Pages(3pm), courtesy of perl v5.34.0, dated 2022-06-15. stay slaying, stay parsing, and remember: one revision per article, periodt. πβ¨π¦
π¦ where this page comes from
- debian bookworm: libmediawiki-dumpfile-perl 0.2.2-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmediawiki-dumpfile-perl 0.2.2-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmediawiki-dumpfile-perl 0.2.2-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmediawiki-dumpfile-perl 0.2.2-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmediawiki-dumpfile-perl 0.2.2-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms