-
Notifications
You must be signed in to change notification settings - Fork 1
/
wikibot
executable file
·133 lines (96 loc) · 2.93 KB
/
wikibot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env perl
use Data::Dumper;
use MediaWiki::Bot;
use Getopt::Long;
use Digest::MD5 qw/md5_hex/;
use Encode;
use LWP::UserAgent;
%mw_login = ();
%http_login = ();
$isMinor = 0;
$Usage_str = "Usage: $0 [--user <wiki user>] [--password <wiki password>] [--summary <summary text>] [--minor] --article <article name> <mediawiki_url>\n";
Getopt::Long::Configure(qw/no_ignore_case/);
GetOptions(
'u|user=s' => \$mw_login{'username'},
'p|password=s' => \$mw_login{'password'},
's|summary=s' => \$Summary,
'a|article=s' => \$Article,
'm|minor' => \$isMinor,
) or
die $Usage_str;
$Article =~ s/ /_/g;
$Article =~ s/^./\u$&/g;
$Article_decoded = Encode::decode_utf8($Article);
$Url = $ARGV[0] or die $Usage_str;
#$Url =~ /^(?'schema'[a-z0-9_+-]+):\/\/(((?'user'[^:@]+)(:(?'password'[^@]*))?)@)?(?'host'[^\/]+)(?'path'\/.*?)(?'article'[^\/=?&]*)$/i;
$Url =~ /^(?'schema'[a-z0-9_+-]+):\/\/(((?'user'[^:@]+)(:(?'password'[^@]*))?)@)?(?'hostport'[^\/]+)(?'path'\/.*)?$/i;
$mw_schema = lc $+{'schema'};
$mw_hostport = $+{'hostport'};
$mw_path = $+{'path'} || "/";
$basic_username = $+{'user'};
$basic_password = $+{'password'};
if(defined $+{'user'} or defined $+{'password'}) {
$ua = LWP::UserAgent->new();
$http_response = $ua->get($mw_schema."://".$mw_hostport.$mw_path.($mw_path =~ /\/$/ ? "" : "/")."api.php");
$auth_header = $http_response->header('WWW-Authenticate');
$realm = '';
if($auth_header =~ /Realm="(.*)"/i) { $realm = $1; }
elsif($auth_header =~ /Realm=([^\s;]+)/i) { $realm = $1; }
($host, $port) = $mw_hostport =~ /^([^:]+)(?::(.*))?$/;
if(not defined $port) {
if($mw_schema eq "https") {
$port = 443;
}
else {
$port = 80;
}
}
%{$mw_login{'basic_auth'}} = (
netloc => "$host:$port",
realm => $realm,
uname => $basic_username,
pass => $basic_password,
);
}
# MediaWiki usernames are capital
$mw_login{'username'} =~ s/^./\u$&/;
# Create a Perl wikipedia object, and have it automatically log in and configure itself
$bot = MediaWiki::Bot->new(
{
protocol => $mw_schema,
host => $mw_hostport,
path => $mw_path,
login_data => \%mw_login,
# Turn debugging on, to see what the bot is doing
debug => 1,
}
);
die "Could not connect to MediaWiki.\n" unless $bot;
print STDERR "Enter new text for '$Article'!\n" if -t 0;
$/ = undef;
$wikitext = <STDIN>;
$wikitext =~ s/\n*$/\n\n/;
$wikitext =~ s/\r//g;
$wikitext =~ s/\n/\r\n/g;
$wikitext = Encode::decode_utf8($wikitext);
print STDERR "Posting to MediaWiki ...\n";
if($wikitext ne '') {
# Note: This does not warn of edit conflicts, it just overwrites existing text.
$result = $bot->edit(
{
page => $Article_decoded,
text => $wikitext,
md5 => md5_hex(Encode::encode_utf8($wikitext)),
summary => $Summary,
minor => $isMinor,
}
);
$result = $result->{'edit'};
printf "%s\t%s\n", $_, Encode::encode_utf8($result->{$_}) for keys %$result;
}
$bot->logout();
__END__
=pod
=head1 NAME
wikibot - Update Wikimedia (Wikipedia) article
=cut