#!/usr/bin/perl
my $username = "USERNAME";
my $password = "PASSWORD";
# Set the pause in seconds after each upload
my $pause = 60;
# List the wiki PHP scripts where you have the username/password pair
my $api_url = 'http://crawl.chaosforge.org/api.php';
my $upload_url = 'http://crawl.chaosforge.org/Special:Upload';
#Then run the script on the command line using
#
# $ perl image.pl dirname
#
# where dirname/ is the name of a directory containing the files to
# be uploaded
use strict;
use warnings;
use MediaWiki::API;
use File::Spec;
use IO::Dir;
my $dir = $ARGV[0] or die "Syntax: perl image.pl dirname\n";
my $d = IO::Dir->new($dir);
die unless defined $d;
my @files = grep {m/\.png/ && ! m/-melee/} $d->read();
undef $d;
my $mw = MediaWiki::API->new( {
api_url => $api_url,
upload_url => $upload_url,
on_error => \&on_error
} );
sub on_error {
print "Error code: " . $mw->{error}->{code} . "\n";
print $mw->{error}->{stacktrace}."\n";
die;
}
my $r = $mw->login( { lgname => $username, lgpassword => $password } );
die unless defined($r);
for (sort(@files)) {
sleep $pause;
my $f = File::Spec->join($dir, $_);
my $page = $_;
print "Uploading $f to the wiki.\n";
my $r = $mw->edit( {
action => 'upload',
filename => $page,
ignorewarnings => 1,
file => [$f]
} );
die unless defined($r);
}
$mw->logout();