Wiki Paster

Aus Ethersex_Wiki
Wechseln zu: Navigation, Suche
#! /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',
    fields => { wpTextbox1 => "<source lang=\"$filetype\">$content
",
   	wpMinoredit => $minor },

) or die; </source>