MediaW...:Links(3pm)
π Parse::MediaWikiDump::Links β the 3pm page, but make it cute π
okay bestie, so this is the manual page for Parse::MediaWikiDump::Links (it lives in section 3pm of the perl docs, iykyk). Let's get into it β¨
πΈ NAME πΈ
Parse::MediaWikiDump::Links</purl> β an object that is literally capable of processing link dump files π¦
β¨ ABOUT β¨
so the vibe here is: this object lets you get at the content of SQL based category dump files by handing you an iterative interface for pulling out the individual article links to the same. and the objects it gives you back? those are instances of Parse::MediaWikiDump::link π
like. it's giving "one link at a time, no cap."
π SYNOPSIS π
use MediaWiki::DumpFile::Compat;
$pmwd = Parse::MediaWikiDump->new;
$links = $pmwd->links('pagelinks.sql');
$links = $pmwd->links(\*FILEHANDLE);
#print the links between articles
while(defined($link = $links->next)) {
print 'from ', $link->from, ' to ', $link->namespace, ':', $link->to, "\n";
}
see how you can pass it a filename or a filehandle? versatility queen π
π METHODS π
Parse::MediaWikiDump::Links->newβ spin up a brand new instance of a page links dump file parser π«Ά like, a fresh one, untouched, main character energy.$links->nextβ gives you the next availableParse::MediaWikiDump::linkobject, orundefif there's no more data left. when it'sundef, it's over over. π
π EXAMPLE π
List all links between articles in a friendly way
okay this one is a whole meal, bestie, so let's go section by section π§
#!/usr/bin/perl
use strict;
use warnings;
use MediaWiki::DumpFile::Compat;
my $pmwd = Parse::MediaWikiDump->new;
my $links = $pmwd->links(shift) or die "must specify a pagelinks dump file";
my $dump = $pmwd->pages(shift) or die "must specify an article dump file";
my %id_to_namespace;
my %id_to_pagename;
binmode(STDOUT, ':utf8');
#build a map between namespace ids to namespace names
foreach (@{$dump->namespaces}) {
my $id = $_->[0];
my $name = $_->[1];
$id_to_namespace{$id} = $name;
}
#build a map between article ids and article titles
while(my $page = $dump->next) {
my $id = $page->id;
my $title = $page->title;
$id_to_pagename{$id} = $title;
}
$dump = undef; #cleanup since we don't need it anymore
while(my $link = $links->next) {
my $namespace = $link->namespace;
my $from = $link->from;
my $to = $link->to;
my $namespace_name = $id_to_namespace{$namespace};
my $fully_qualified;
my $from_name = $id_to_pagename{$from};
if ($namespace_name eq '') {
#default namespace
$fully_qualified = $to;
} else {
$fully_qualified = "$namespace_name:$to";
}
print "Article \"$from_name\" links to \"$fully_qualified\"\n";
}
heads up! this script takes two arguments off the command line β first the pagelinks dump file, then the article dump file β and it'lldieif you forget either one π. also it setsbinmode(STDOUT, ':utf8')so your unicode doesn't get messy, which is so valid of it.
and that's the whole page, babe πβ¨
this little doc was brought to you by perl v5.34.0, dated 2022-06-15, from the User Contributed Perl Documentation shelf. obsessed with her, honestly. go parse some links, bestie π¦πΈ
π¦ 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