wiki.pl: Port some fixes from upstream
[Orgmuse.git] / wikiappend
blob3a3c95fe0377c40b948d4f301f239cc0c8e2a7fa
1 #!/usr/bin/perl -w
3 # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
4 # Copyright (C) 2006 Alexandre (adulau) Dulaunoy
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.
19 # 59 Temple Place, Suite 330
20 # Boston, MA 02111-1307 USA
22 use strict;
23 use Getopt::Std;
24 use LWP::UserAgent;
26 my $usage = "$0 TARGET PAGENAME [USERNAME] [PASSWORD]\n"
27 . "Where TARGET is the base URL for the wiki.\n"
28 . "PAGENAME is the name of the page to be modified.\n"
29 . "USERNAME is the username to use for the edit.\n"
30 . "PASSWORD is the password to use if required.\n"
31 . "Example:\n"
32 . "echo this will be appended | $0 http://localhost/cgi-bin/wiki.pl \"My Cool Page\" MyName TheEditPassWord\n\n";
34 sub UrlEncode {
35 my $str = shift;
36 return '' unless $str;
37 my @letters = split(//, $str);
38 my @safe = ('a' .. 'z', 'A' .. 'Z', '0' .. '9', '-', '_', '.', '!',
39 '~', '*', "'", '(', ')', '#');
40 foreach my $letter (@letters) {
41 my $pattern = quotemeta($letter);
42 if (not grep(/$pattern/, @safe)) {
43 $letter = sprintf("%%%02x", ord($letter));
46 return join('', @letters);
49 sub GetRaw {
50 my ($uri) = @_;
51 my $ua = LWP::UserAgent->new;
52 my $response = $ua->get($uri);
53 return $response->content if $response->is_success;
56 sub PostRaw {
57 my ($uri, $id, $data, $user, $pwd) = @_;
58 my $ua = LWP::UserAgent->new;
59 my $response = $ua->post($uri, {title=>$id, text=>$data, raw=>1,
60 username=>$user, pwd=>$pwd});
61 warn "POST $id failed.\n" unless $response->is_success;
64 sub append {
65 my ($target, $page, $user, $pwd) = @_;
66 $page =~ s/ /_/g;
67 $page = UrlEncode ($page);
68 my $data = GetRaw("$target?action=browse;id=$page;raw=1");
69 $data .= <STDIN>;
70 PostRaw($target, $page, $data, $user, $pwd);
73 sub main {
74 my ($target, $page, $user, $pwd) = @ARGV;
75 die $usage unless $target;
76 die $usage unless $page;
77 append($target, $page, $user, $pwd);
80 main();