#!/usr/bin/perl -w # # wiki_upload # $Id: wiki_upload,v 1.4 2005/12/08 06:44:55 johnh Exp $ # # Copyright (C) 2005 by John Heidemann # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License, # version 2, as published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. # sub usage { print STDERR < -u -p -w -P also protect the page -v verbose output Currently only tested with Mediawiki-1.5. Ideally this should be replaced by something built on WWW::Mediawiki::Client. END exit 1; } use strict; my $progname = $0; use Carp; use WWW::Mechanize; use Getopt::Long qw(:config no_ignore_case bundling); &usage if ($#ARGV >= 0 && $ARGV[0] eq '-?'); my(%opts); &GetOptions(\%opts, qw(D d=s u=s m=s P p=s w=s v)); # v a e=s o=s@ &usage if ($#ARGV < 0); my($debug) = defined($opts{'D'}); my($verbose) = defined($opts{'v'}); my($wikihost) = $opts{'d'}; die "$progname: no wikihost specified.\n" if (!defined ($wikihost)); my($wikipath) = $opts{'w'}; $wikipath = 'wiki/index.php' if (!defined($wikipath)); my($username) = $opts{'u'}; my($password) = $opts{'p'}; my($protect) = $opts{'P'}; my($message) = $opts{'m'}; $message = 'No description.' if (!defined($message)); foreach (@ARGV) { do_upload($_); }; exit 0; sub assert_success { my($b, $content_re, $expectation, $suggestion) = @_; my $uri = $b->uri; $uri = "(unknown)" if (!defined($uri)); if (defined($content_re)) { if ($b->response->content !~ /$content_re/) { croak "$progname: not $expectation---$suggestion\n"; }; }; croak "$progname: error on " . $uri . ": " . $b->response()->status_line . "\n" if (! $b->success()); } sub do_upload { my($file) = @_; my $b = WWW::Mechanize->new(); # log in print "$progname: going to log in.\n" if ($verbose); my($url) = "http://$wikihost/$wikipath/Special:Userlogin"; $b->get($url); assert_success($b, 'Create an account or log in', 'on login page', 'did you give the right wiki server name and directory?'); print "$progname: logging in.\n" if ($verbose); $b->form_name('userlogin') or die "$progname: cannot find userlogin form on login page.\n"; $b->field('wpName', $username); $b->field('wpPassword', $password); $b->submit(); assert_success($b, '<title>Login successful', 'past login page', 'did you give the right username and password?'); # go to upload page print "$progname: going to upload.\n" if ($verbose); $url = "http://$wikihost/$wikipath/Special:Upload"; $b->get($url); assert_success($b, '<title>Upload file', 'on upload page', 'probably internal problem'); print "$progname: uploading.\n" if ($verbose); # assert that form 1 is upload! in wikipedia 1.5 it's not named :-( $b->form_number(1) or die "$progname: cannot find first form (upload!) on upload page.\n"; # $b->form_name('upload') or die "$progname: cannot find upload form on upload page.\n"; $b->field('wpUploadFile', $file); my $dest = $file; $dest =~ s@^.*/([^/]+)$@$1@; $dest = ucfirst($dest); $b->field('wpDestFile', $dest); $b->field('wpUploadDescription', $message) if (defined($message)); print " $file to $dest (message $message).\n" if ($verbose); $b->click('wpUpload'); assert_success($b); if ($b->response->content =~ /class=\'error\'[^>]*>([^<]+)</m) { die "$progname: upload failed with error: $1.\n"; }; if ($b->response->content =~ /<ul.*\'warning\'[^>]*>(.*)<\/ul>/m) { print "$progname: warning overridden\n\t$1\n" if ($verbose); $b->form_number(1) or die "$progname: cannot find warning override.\n"; $b->click('wpUpload'); assert_success($b); }; assert_success($b, '<title>Image:', 'to image page', 'upload failed'); # protect # http://wiki.isi.edu/csci551/index.php?title=Image:Clark88a.pdf&action=protect if ($protect) { print "$progname: going to protect.\n" if ($verbose); $url = "http://$wikihost/$wikipath?title=Image:$dest&action=protect"; $b->get($url); assert_success($b, '<title>Confirm protection', 'to confirm protection page', 'internal problem'); print "$progname: protecting.\n" if ($verbose); $b->field('wpReasonProtect', 'uploader request'); $b->submit(); assert_success($b, '<title>Image', 'back to image page', 'internal problem'); }; print "$progname: done.\n" if ($verbose); }