From 8d5f61e3440b0d4b8c19ad4efb5e740ca5b73bb0 Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Wed, 10 Nov 2004 18:38:34 +0000 Subject: [PATCH] Added by Stephan Vican --- wikiappend | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 wikiappend diff --git a/wikiappend b/wikiappend new file mode 100755 index 00000000..45b8b308 --- /dev/null +++ b/wikiappend @@ -0,0 +1,75 @@ +#!/usr/bin/perl -w +# +# Copyright (C) 2004 Alex Schroeder +# +# 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 .= ; + PostRaw($target, $page, $data); +} + +sub main { + my ($target, $page) = @ARGV; + die $usage unless $target; + die $usage unless $page; + append($target, $page); +} + +main();