MediaW...:Links(3pm)

πŸ“¦
version 0.2.2 Β· 2 versions
  • 0.2.2 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.2.2 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

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 available Parse::MediaWikiDump::link object, or undef if there's no more data left. when it's undef, it's over over. πŸ’€

πŸ“ EXAMPLE πŸ“

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'll die if you forget either one 😭. also it sets binmode(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