| 1 |
Parse-MediaWikiDump |
| 2 |
|
| 3 |
Parse::MediaWikiDump is a collection of classes for processing various |
| 4 |
MediaWiki dump files such as those at |
| 5 |
http://download.wikimedia.org/wikipedia/en/; the package requires XML::Parser. |
| 6 |
Using this software it is nearly trivial to get access to the information in |
| 7 |
supported dump files. |
| 8 |
|
| 9 |
Currently the following dump files are supported: |
| 10 |
* Current page dumps for all languages |
| 11 |
* Current links dumps for all languages |
| 12 |
|
| 13 |
INSTALLATION |
| 14 |
|
| 15 |
To install this module, run the following commands: |
| 16 |
|
| 17 |
perl Makefile.PL |
| 18 |
make |
| 19 |
make test |
| 20 |
make install |
| 21 |
|
| 22 |
EXAMPLE |
| 23 |
|
| 24 |
Extract the text for a given article from the given dump file: |
| 25 |
|
| 26 |
#!/usr/bin/perl |
| 27 |
|
| 28 |
use strict; |
| 29 |
use warnings; |
| 30 |
use Parse::MediaWikiDump; |
| 31 |
|
| 32 |
my $file = shift(@ARGV) or die "must specify a MediaWiki dump of the current pages"; |
| 33 |
my $title = shift(@ARGV) or die "must specify an article title"; |
| 34 |
my $dump = Parse::MediaWikiDump::Pages->new($file); |
| 35 |
|
| 36 |
binmode(STDOUT, ':utf8'); |
| 37 |
binmode(STDERR, ':utf8'); |
| 38 |
|
| 39 |
#this is the only currently known value but there could be more in the future |
| 40 |
if ($dump->case ne 'first-letter') { |
| 41 |
die "unable to handle any case setting besides 'first-letter'"; |
| 42 |
} |
| 43 |
|
| 44 |
#enforce the MediaWiki case rules |
| 45 |
$title = case_fixer($title); |
| 46 |
|
| 47 |
#iterate over the entire dump file, article by article |
| 48 |
while(my $page = $dump->next) { |
| 49 |
if ($page->title eq $title) { |
| 50 |
print STDERR "Located text for $title\n"; |
| 51 |
my $text = $page->text; |
| 52 |
print $$text; |
| 53 |
exit 0; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
print STDERR "Unable to find article text for $title\n"; |
| 58 |
exit 1; |
| 59 |
|
| 60 |
#removes any case sensativity from the very first letter of the title |
| 61 |
#but not from the optional namespace name |
| 62 |
sub case_fixer { |
| 63 |
my $title = shift; |
| 64 |
|
| 65 |
#check for namespace |
| 66 |
if ($title =~ /^(.+?):(.+)/) { |
| 67 |
$title = $1 . ':' . ucfirst($2); |
| 68 |
} else { |
| 69 |
$title = ucfirst($title); |
| 70 |
} |
| 71 |
|
| 72 |
return $title; |
| 73 |
} |
| 74 |
|
| 75 |
COPYRIGHT & LICENSE |
| 76 |
Copyright 2005 Tyler Riddle, all rights reserved. |
| 77 |
|
| 78 |
This program is free software; you can redistribute it and/or modify it |
| 79 |
under the same terms as Perl itself. |
| 80 |
|