Thursday, December 16, 2010

Mediawiki foo

Some time ago Kristian LyngstĂžl blogged about his hack which provides commandline "wikiget", "wikipost" and "wikiedit" interfaces to mediawikis. I planned to use it for a client project I'm doing right now. It turns out the restriction "supports only basic auth" was rather hard to overcome, at least for this one particular mediawiki instance I needed to make pages on.

So, in the good tradition of one-up-man-ship here is how I do mediawiki:

I was programming my project in perl in any case, so the python inferface suggested by a python-colleague would be non-optimal. As well as not included in Ubuntu afakt. But Mediawiki::API is included. The code below is mostly cut and paste from the documentation:

use Mediawiki::API;

my $wh;

sub wiki_upload {
my ($page,$content) = @_;

# First time use, generate object and log in.
if (!defined($wh)) {
$wh = MediaWiki::API->new( { api_url => $wikiapi } );
$wh->login( { lgname => $user, lgpassword => $pw } ) or
die "Wiki error: ".$wh->{error}->{code}.': '.$wh->{error}->{details};

my $ref = $wh->get_page( { 'title' => $page } ) or
die $wh->{error}->{code} . ': ' . $wh->{error}->{details};

die if $ref->{missing};

# At this time $ref->{*} contains the page contents

my $timestamp = $ref->{timestamp};

$wh->edit( {
'action' => 'edit',
'title' => $page,
'basetimestamp' => $timestamp,
'text' => $content
} )
or die $wh->{error}->{code} . ': ' . $wh->{error}->{details};

wiki_upload("Sandbox","= Sandbox=\n Foobar! Gazonk!\n");