Parse:...::Links(3pm)

πŸ“¦
version 1.0.6 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

okay bestie, you found it β€” this is a page straight out of the User Contributed Perl Documentation, and it's all about Parse::MediaWikiDump::Links. pull up a chair, we're doing this 🫢


✨ NAME ✨

Parse::MediaWikiDump::Links β€” an object that's literally built to process link dump files. no cap.


🌸 ABOUT 🌸

so like, this object is your girl for getting at the content of those SQL-based category dump files. she hands you an iterative interface so you can pull out the individual article links one at a time, no drama, no loading the whole thing into your brain at once πŸ’…

and the objects she gives you back? those are instances of Parse::MediaWikiDump::link. matching set, periodt.


πŸŽ€ SYNOPSIS πŸŽ€

here's the vibe, literally copy-paste core:

$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 hand her a filename or a filehandle? versatile queen πŸ‘‘


πŸ’€ STATUS πŸ’€

bestie. sit down. i need to tell you something.

this software is being RETIRED. 😭

MediaWiki::DumpFile is the official successor to Parse::MediaWikiDump, and it ships with a compatibility library called MediaWiki::DumpFile::Compat that is 100% API compatible β€” like, a near perfect stand-in for this module. it's faster in every single instance where it actually counts, and it's actively maintained. glow-up fr fr ✨

and here's the messy part: any undocumented deviation of MediaWiki::DumpFile::Compat from Parse::MediaWikiDump is considered a bug and will be fixed. so the new girl is contractually obligated to behave. we love accountability πŸ’œ


πŸ¦‹ METHODS πŸ¦‹

spawn a brand new instance of a page links dump file parser. fresh out the box, main character energy πŸ‘‘

hand over the next available Parse::MediaWikiDump::link object, or undef if there's genuinely no more data left. when she's done, she's done β€” no crumbs πŸ’…


🌈 EXAMPLE 🌈

#!/usr/bin/perl

use strict;
use warnings;

use Parse::MediaWikiDump;

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";
}

not her casually building two whole lookup hashes and yeeting $dump the second she's done with it πŸ’€ efficient behavior honestly


and that's the page bestie!! πŸŽ€ this whole thing comes from perl v5.36.0, dated 2022-10-14 β€” go forth and parse those links, you absolute icon πŸ‘‘πŸ’–βœ¨

πŸ“¦ where this page comes from