6 use Digest
::MD5 qw
/md5_hex/;
14 $Usage_str = "Usage: $0 [--user <wiki user>] [--password <wiki password>] [--summary <summary text>] [--minor] --article <article name> <mediawiki_url>\n";
17 Getopt
::Long
::Configure
(qw
/no_ignore_case/);
19 'u|user=s' => \
$mw_login{'username'},
20 'p|password=s' => \
$mw_login{'password'},
21 's|summary=s' => \
$Summary,
22 'a|article=s' => \
$Article,
23 'm|minor' => \
$isMinor,
30 $Article =~ s/^./\u$&/g;
31 $Article_decoded = Encode
::decode_utf8
($Article);
33 $Url = $ARGV[0] or die $Usage_str;
34 #$Url =~ /^(?'schema'[a-z0-9_+-]+):\/\/(((?'user'[^:@]+)(:(?'password'[^@]*))?)@)?(?'host'[^\/]+)(?'path'\/.*?)(?'article'[^\/=?&]*)$/i;
35 $Url =~ /^(?'schema'[a-z0-9_+-]+):\/\
/(((?'user'[^:@]+)(:(?'password'[^@]*))?)@)?(?'hostport'[^\/]+)(?
'path'\
/.*)?$/i;
38 $mw_schema = lc $+{'schema'};
39 $mw_hostport = $+{'hostport'};
40 $mw_path = $+{'path'} || "/";
41 $basic_username = $+{'user'};
42 $basic_password = $+{'password'};
45 if(defined $+{'user'} or defined $+{'password'}) {
46 $ua = LWP
::UserAgent
->new();
47 $http_response = $ua->get($mw_schema."://".$mw_hostport.$mw_path.($mw_path =~ /\/$/ ? "" : "/")."api
.php
");
48 $auth_header = $http_response->header('WWW-Authenticate');
51 if($auth_header =~ /Realm="(.*)"/i) { $realm = $1; }
52 elsif($auth_header =~ /Realm=([^\s;]+)/i) { $realm = $1; }
54 ($host, $port) = $mw_hostport =~ /^([^:]+)(?::(.*))?$/;
55 if(not defined $port) {
56 if($mw_schema eq "https
") {
64 %{$mw_login{'basic_auth'}} = (
65 netloc => "$host:$port",
67 uname => $basic_username,
68 pass => $basic_password,
72 # MediaWiki usernames are capital
73 $mw_login{'username'} =~ s/^./\u$&/;
76 # Create a Perl wikipedia object, and have it automatically log in and configure itself
77 $bot = MediaWiki::Bot->new(
79 protocol => $mw_schema,
82 login_data => \%mw_login,
83 # Turn debugging on, to see what the bot is doing
88 die "Could
not connect to MediaWiki
.\n" unless $bot;
91 print STDERR "Enter new text
for '$Article'!\n" if -t 0;
95 $wikitext =~ s/\n*$/\n\n/;
97 $wikitext =~ s/\n/\r\n/g;
98 $wikitext = Encode::decode_utf8($wikitext);
102 print STDERR "Posting to MediaWiki
...\n";
104 if($wikitext ne '') {
105 # Note: This does not warn of edit conflicts, it just overwrites existing text.
106 $result = $bot->edit(
108 page => $Article_decoded,
110 md5 => md5_hex(Encode::encode_utf8($wikitext)),
116 $result = $result->{'edit'};
117 printf "%s\t%s\n", $_, Encode::encode_utf8($result->{$_}) for keys %$result;
130 wikibot - Update Wikimedia (Wikipedia) article