MediaW...:Pages(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

πŸ’– 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 available Parse::MediaWikiDump::page object, or returns undef if 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