Created
June 30, 2012 17:28
-
-
Save neingeist/3024733 to your computer and use it in GitHub Desktop.
Upload a file to a recent MediaWiki using the API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Upload a file to a recent MediaWiki using the API. | |
use strict; | |
use MediaWiki::API 0.39; | |
my $wikiuser = $ARGV[0]; | |
my $wikipass = $ARGV[1]; | |
my $file = $ARGV[2]; | |
my $comment = $ARGV[3]; | |
# usage | |
if (!defined($comment)) { | |
print "Usage: $0 <wikiuser> <wikipass> <file> <comment>\n"; | |
exit; | |
} | |
# log in to the wiki | |
my $mw = MediaWiki::API->new(); | |
$mw->{config}->{api_url} = 'https://example.com/wiki/api.php'; | |
$mw->login( { lgname => $wikiuser, lgpassword => $wikipass } ) | |
|| die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; | |
# upload file | |
open FILE, $file or die $!; | |
binmode FILE; | |
my ($buffer, $data); | |
while ( read(FILE, $buffer, 65536) ) { | |
$data .= $buffer; | |
} | |
close(FILE); | |
$mw->upload( { title => $file, | |
summary => $comment, | |
data => $data } ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment