wiki.pl: Port some fixes from upstream
[Orgmuse.git] / wikiput
blob2736ea07ec02c7571a867cc9a830562f5db69f25
1 #!/usr/bin/perl -w
3 # Copyright (C) 2004, 2005, 2006, 2008 Alex Schroeder <alex@gnu.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 require LWP;
19 use Getopt::Std;
21 our ($opt_v, $opt_w);
23 # We make our own specialization of LWP::UserAgent that asks for
24 # user/password if document is protected.
26 package RequestAgent;
27 @ISA = qw(LWP::UserAgent);
29 sub new {
30 my $self = LWP::UserAgent::new(@_);
31 $self;
34 sub get_basic_credentials {
35 my($self, $realm, $uri) = @_;
36 return split(':', $main::opt_w, 2);
40 my $usage = qq{$0 [-f] [-m] [-s SUMMARY]
41 \t[-u USERNAME] [-p PASSWORD] [-w USERNAME:PASSWORD]
42 \t[-q QUESTION] [-a ANSWER] [-z SECRET]
43 \tTARGET
45 Post the data on stdin on the wikipage described by wikipage.
47 TARGET is the URL for the wiki page.
49 Options:
51 -f Allow the posting of empty pages (default: no)
52 -m Whether this is a minor edit (default: no)
54 Options with arguments:
56 -s The summary for RecentChanges (default: none)
57 -u The username for RecentChanges (default: none)
58 -p The password to use for locked pages (default: none)
59 -w The username:password combo for basic authentication (default:none)
60 -q The question number to answer (default: 0, ie. the first question)
61 -a The answer to the question (default: none)
62 -z Alternatively, the secret key (default: question)
63 -v Verbose output for debugging (default: none)
65 The defaults are chosen such that if the QuestionAsker extension is
66 used and the secret key is unchanged, there is no need to provide
67 either secret key or password.
69 If the target wiki is protected by so-called "basic authentication" --
70 that is, if you need to provide a username and password before you can
71 even view the site -- then you can pass those along using the -w
72 option. Separate username and password using a colon.
75 sub post {
76 my ($uri, $id, $data, $minor, $summary, $username, $password,
77 $question, $answer, $secret) = @_;
78 my $ua = RequestAgent->new;
79 my %params = (title=>$id, text=>$data, raw=>1,
80 username=>$username, pwd=>$password,
81 summary=>$summary, question_num=>$question,
82 answer=>$answer, $secret=>1,
83 recent_edit=>$minor);
84 if ($opt_v) {
85 foreach my $key (keys %params) {
86 my $value = $params{$key} || '(none)';
87 $value = substr($value,0,50) . '...'
88 if $key eq 'text' and length($value) > 53;
89 warn "$key: $value\n";
92 my $response = $ua->post($uri, \%params);
93 my $status = $response->code . ' ' . $response->message;
94 warn "POST $id failed: $status.\n" unless $response->is_success;
97 sub main {
98 # $opt_v, $opt_w are global
99 our($opt_f, $opt_m, $opt_s, $opt_u, $opt_p, $opt_q, $opt_a, $opt_z);
100 getopts('fms:u:p:q:a:z:vw:');
101 my $target = shift @ARGV;
102 die $usage if not $target or @ARGV; # not enough or too many
103 die "Cannot determine page id from $target\n" unless $target =~ m!^(.*)[/?](.*?)$!;
104 my ($uri, $id) = ($1, $2);
105 warn "id $id" if $opt_v;
106 undef $/;
107 my $data = <STDIN>;
108 die "No content to post or use -f to force it\n" if not $data and not $opt_f;
109 warn length($data) . " bytes of data" if $opt_v;
110 post ($uri, $id, $data, $opt_m ? 'on' : '', $opt_s, $opt_u, $opt_p,
111 $opt_q, $opt_a, $opt_z||'question');
114 main();