Wiki Paster

Aus Ethersex_Wiki
Wechseln zu: Navigation, Suche

Manchmal will man einfach ein Stückchen Quellcode im Wiki veröffentlichen, weil er zwar praktisch ist, umgekehrt aber so kurz, dass es kein Projekt ist, was man als solches veröffentlichen will.

Hier ein kleines Perl-Skript, das ein Codestück hier in's Wiki klopft. Aber Achtung, im Moment wird die Seite mit dem Inhalt überschrieben, nicht zum Beispiel der erste Source-Tag bearbeitet.

#! /usr/bin/perl -w
use strict;
use LWP;
use WWW::Mechanize;
use POSIX qw/localtime/;

###############################################################################
# User Configuration

my $site = "http://brokenpipe.de/index.php";
my $login_site = "$site/Spezial:Anmelden";
my $wp_username = "Stella";
my $wp_password = "keineahnung";

###############################################################################

my $minor = "off";
if(scalar(@ARGV) && $ARGV[0] eq '--minor') {
  $minor = "on";
  shift @ARGV
}

if(scalar(@ARGV) != 2) {
  print STDERR "Usage: wiki-paste [--minor] PAGENAME FILETYPE\n\n";
  exit 1;
}

my ($pagename, $filetype) = @ARGV;
my $editurl = "$site?title=$pagename&action=edit";

my $content;
while (<STDIN>) { $content .= $_ }

# Use Log::TraceMessages if installed.
BEGIN {
    eval { require Log::TraceMessages };
    if ($@) {
	print "Log::TraceMessages not available\n";
        *t = sub {};
        *d = sub { '' };
    }
    else {
	print "Using Log::TraceMessages\n";
	$Log::TraceMessages::On = 1;
        *t = \&Log::TraceMessages::t;
        *d = \&Log::TraceMessages::d;
    }
}

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->env_proxy;

my $mech = WWW::Mechanize->new();
$mech->env_proxy;

# we'll be presented a 301-Redirect after issuing POST request.
# WWW::Mechanize won't do what we'd expect, so just remove 'POST'
# from the redirectable requests list ...
@{ $mech->requests_redirectable } = ( "GET", "HEAD" );

$mech->get($login_site) or die;

t "trying to log in";
$mech->submit_form (
    form_name => 'userlogin',
    fields => { wpName => $wp_username, wpPassword => $wp_password },
) or die;

t "HTTP-Code returned on Login-POST: " . $mech->status();

t "Getting Edit-Page of $pagename ...";
$mech->get($editurl) or die;

die "HTTP-request failed, stop"
  unless ($mech->success ());

die "seems like we failed to log in, there's no edit textbox"
  unless ($mech->content() =~ m/<textarea/);

t "Submitting content now...";
$mech->submit_form (
    form_name => 'editform',
    # hmmm, we've got to hide the closing source-tag from the wiki itself *g*
    fields => { wpTextbox1 => "<source lang=\"$filetype\">$content<" . "/source>",
    	wpMinoredit => $minor },
) or die;