add kvpairs2td
[hband-tools.git] / user-tools / wikibot
blob6689eefcdaed52ceac727dc471c852462a6af98f
1 #!/usr/bin/env perl
3 use Data::Dumper;
4 use MediaWiki::Bot;
5 use Getopt::Long;
6 use Digest::MD5 qw/md5_hex/;
7 use Encode;
8 use LWP::UserAgent;
11 %mw_login = ();
12 %http_login = ();
13 $isMinor = 0;
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/);
18 GetOptions(
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,
24 ) or
25 die $Usage_str;
29 $Article =~ s/ /_/g;
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');
50 $realm = '';
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") {
57 $port = 443;
59 else {
60 $port = 80;
64 %{$mw_login{'basic_auth'}} = (
65 netloc => "$host:$port",
66 realm => $realm,
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,
80 host => $mw_hostport,
81 path => $mw_path,
82 login_data => \%mw_login,
83 # Turn debugging on, to see what the bot is doing
84 debug => 1,
88 die "Could not connect to MediaWiki.\n" unless $bot;
91 print STDERR "Enter new text for '$Article'!\n" if -t 0;
93 $/ = undef;
94 $wikitext = <STDIN>;
95 $wikitext =~ s/\n*$/\n\n/;
96 $wikitext =~ s/\r//g;
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,
109 text => $wikitext,
110 md5 => md5_hex(Encode::encode_utf8($wikitext)),
111 summary => $Summary,
112 minor => $isMinor,
116 $result = $result->{'edit'};
117 printf "%s\t%s\n", $_, Encode::encode_utf8($result->{$_}) for keys %$result;
121 $bot->logout();
124 __END__
126 =pod
128 =head1 NAME
130 wikibot - Update Wikimedia (Wikipedia) article
132 =cut