Files
oddmuse/wikiappend
2004-11-10 18:38:34 +00:00

76 lines
2.2 KiB
Perl
Executable File

#!/usr/bin/perl -w
#
# Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.
# 59 Temple Place, Suite 330
# Boston, MA 02111-1307 USA
use strict;
use Getopt::Std;
use LWP::UserAgent;
my $usage = "$0 TARGET PAGENAME\n"
. "Where TARGET is the base URL for the wiki.\n"
. "PAGENAME is the name of the page to be modified.\n"
. "Example:\n"
. "echo this will be appended | $0 http://localhost/cgi-bin/wiki.pl \"My Cool Page\"\n\n";
sub UrlEncode {
my $str = shift;
return '' unless $str;
my @letters = split(//, $str);
my @safe = ('a' .. 'z', 'A' .. 'Z', '0' .. '9', '-', '_', '.', '!', '~', '*', "'", '(', ')', '#');
foreach my $letter (@letters) {
my $pattern = quotemeta($letter);
if (not grep(/$pattern/, @safe)) {
$letter = sprintf("%%%02x", ord($letter));
}
}
return join('', @letters);
}
sub GetRaw {
my ($uri) = @_;
my $ua = LWP::UserAgent->new;
my $response = $ua->get($uri);
return $response->content if $response->is_success;
}
sub PostRaw {
my ($uri, $id, $data) = @_;
my $ua = LWP::UserAgent->new;
my $response = $ua->post($uri, {title=>$id, text=>$data, raw=>1}); # fix for other wikis
warn "POST $id failed.\n" unless $response->is_success;
}
sub append {
my ($target, $page) = @_;
$page =~ s/ /_/g;
$page = UrlEncode ($page);
my $data = GetRaw("$target?action=browse;id=$page;raw=1"); # fix for other wikis
$data .= <STDIN>;
PostRaw($target, $page, $data);
}
sub main {
my ($target, $page) = @ARGV;
die $usage unless $target;
die $usage unless $page;
append($target, $page);
}
main();