<?xml version="1.0" encoding="windows-1252"?>
<node id="958466" title="Offline wikipedia using Perl" created="2012-03-08 08:49:10" updated="2012-03-08 08:49:10">
<type id="1042">
CUFP</type>
<author id="946047">
grondilu</author>
<data>
<field name="doctext">
&lt;p&gt;I'm finally happy with the code I wrote to browse wikipedia offline.&lt;/p&gt;

&lt;p&gt;The most tricky part was to keep the database small.  So I made a database with blocks of 256 articles.  Each block is frozen using Storable and then compressed with Bzip2.  Doing so, the created database is only about 15% larger than the original xml.bz2&lt;/p&gt;

&lt;p&gt;I also use XML::Parser to parse wikipedia's database dump.&lt;/p&gt;

&lt;p&gt;Here is the most difficult part:  converting the XML database (see http://download.wikimedia.org) into a usable one:&lt;/p&gt;

&lt;code&gt;
#!/usr/bin/perl -w
use v5.14;
use strict;
use warnings;
die "please provide a database name" unless $ARGV[0];
my $rootname = $ARGV[0] =~ s/\.xml\.bz2\E//r =~ s,.*/,,r;

use Encode;
use XML::Parser;
use IO::Uncompress::Bunzip2;
use IO::Compress::Bzip2 qw(bzip2 $Bzip2Error);

use Digest::MD5 qw(md5);
use Storable qw(freeze thaw);
open my $db, "&gt; $rootname.db";
END { close $db }

open my $t, "&gt; $rootname.titles";
END { close $t }

my ($title, @block, $char);

my %debug;

use DB_File;
tie my %index, 'DB_File', "$rootname.index";
END { untie %index }

$SIG{INT} = sub { die "caught INT signal" };
END { printf "%d entries made\n", scalar keys %index }

sub store {
    my $freeze = freeze shift;
    bzip2 \($freeze, my $z);
    my $start = tell $db;
    print $db pack('L', length $z), $z;
    printf "block %d -&gt; %d, compressed ratio is %2.2f%%\n",
    $start, tell($db), 100*length($z)/length($freeze),
    ;
}

my $parser = new XML::Parser Handlers =&gt; {
    Char =&gt; sub { shift; $char .= shift },
    Start =&gt; sub { undef $char },
    End =&gt; sub {
	shift;
	given( $_[0] ) {
	    when( 'title' ) { $title = encode 'utf8', $char; say $t $title }
	    when( 'text' )  {
		push @block, $char;
		$index{length($title) &gt; 16 ? md5 $title : $title} =
		    pack 'LC', tell($db), scalar(@block) - 1;
		if (@block == 256) {
		    store \@block;
		    undef @block;
		}
	    }
	}
    },
};

$parser-&gt;parse( new IO::Uncompress::Bunzip2 $ARGV[0] );
END { store \@block if @block }
&lt;/code&gt;

&lt;p&gt;I think it works pretty well, even if the rendering of the Text::Mediawiki module is a bit ugly for some pages.  I need to take care of the references for instance.  Still, it does the job, and it's much faster than on-line browsing.&lt;/p&gt;

&lt;p&gt;I posted everything (including the CGI script) on my wikipedia userpage, as it also concerns wikipedia users:&lt;/p&gt;

[http://fr.wikipedia.org/wiki/Utilisateur:Grondilu/Offline_Wikipedia_Perl]


&lt;p&gt;EDIT.  I also set up a github repo:
[https://github.com/grondilu/offline-wikipedia-perl]
&lt;/p&gt;</field>
</data>
</node>
