As promised, a patch that gets it working with newer versions of MW and
also adds a bit more debug and uses public methods, rather than private
variables from HTTP::Response.
--- CMS\MediaWiki.pm.old Thu Feb 21 08:29:38 2013
+++ CMS\MediaWiki.pm Thu Feb 21 08:29:38 2013
@@ -13,7 +13,7 @@
#######################################################################
use strict;
my $package = __PACKAGE__;
-our $VERSION = '0.8014';
+our $VERSION = '0.8015';
use LWP::UserAgent;
use HTTP::Request::Common;
@@ -37,7 +37,7 @@
$self->{'protocol'} = $params{'protocol'} || 'http'; # optional
$self->{'host' } = $params{'host'} || 'localhost';
$self->{'path' } = $params{'path'} || '';
- $self->{'debug' } = $params{'debug'} || 0; # 0, 1, 2
+ $self->{'debug' } = $params{'debug'} || 0; # 0, 1, 2, 3
$Var{'SERVER_SIG'} = '*Unknown*';
$Var{'EDIT_TIME_BEFORE'} = '*Unknown*';
@@ -76,6 +76,20 @@
my $login_url = "$args{'protocol'}://$args{'host'}$index_path?title=Special:Userlogin&action=submitlogin";
+ # (Pre-)fetch page for token...
+ my $pre_resp = $ua->request(GET $login_url);
+ my @lines = split /\n/, $pre_resp->content();
+ my $login_token = '';
+ foreach (@lines) {
+ #Debug "X $_";
+ if (/wpLoginToken/) {
+ s/type=.?hidden.? *name="wpLoginToken" *value="(.+)"/$1/i;
+ $login_token = $1;
+ $tags{'wpLoginToken'} = $login_token;
+ Debug "[login] Got wpLoginToken" if $self->{'debug'};
+ }
+ }
+
Debug "[login] POST $login_url\..." if $self->{'debug'};
my $resp = $ua->request(
@@ -85,17 +99,18 @@
);
my $login_okay = 0;
- foreach (keys %{$resp->{'_headers'}}) {
- Debug "(header) $_ = " . $resp->{'_headers'}->{$_} if $self->{'debug'} > 1;
+ foreach ($resp->header_field_names()) {
+ Debug "(header) $_ = " . $resp->header($_) if $self->{'debug'} > 1;
if ($_ =~ /^set-cookie$/i) {
- my $arr = $resp->{'_headers'}->{$_};
- if ($arr =~ /^ARRAY(.+)$/) {
+ my $arr = [$resp->header($_)];
+ if (ref($arr) eq 'ARRAY') {
foreach (@{$arr}) {
Debug "- (cookie) $_" if $self->{'debug'} > 1;
# wikiUserID or wikidbUserID
if ($_ =~ /UserID=\d+\;/i) {
# Success!
$login_okay = 1;
+ Debug "(cookie) Got UserID" if $self->{'debug'} > 1;
}
Debug "(cookie) $_" if $self->{'debug'} > 1;
}
@@ -105,9 +120,11 @@
}
}
if ($_ =~ /^server$/i) {
- $Var{'SERVER_SIG'} = $resp->{'_headers'}->{$_};
+ $Var{'SERVER_SIG'} = $resp->header($_);
}
}
+
+ Debug "(decoded content) =\n" . $resp->decoded_content() if $self->{'debug'} > 2;
return $login_okay ? 0 : 1;
}
@@ -133,9 +150,13 @@
Debug "Editing page '$args{'title'}' (section '$args{'section'}')..." if $self->{'debug'};
my $edit_section = length($args{'section'}) > 0 ? "\§ion=$args{'section'}" : '';
+
+ my $edit_url = "$args{'protocol'}://$WHOST/$WPATH/index.php?title=$args{'title'}&action=edit$edit_section";
+ Debug "[editPage] GET $edit_url" if $self->{'debug'};
+
# (Pre-)fetch page...
- my $resp = $ua->request(GET "$args{'protocol'}://$WHOST/$WPATH/index.php?title=$args{'title'}&action=edit$edit_section");
+ my $resp = $ua->request(GET $edit_url);
my @lines = split /\n/, $resp->content();
my $token = my $edit_time = '';
foreach (@lines) {
@@ -181,12 +202,15 @@
Content => [ %tags ]
);
- foreach (sort keys %{$resp->{'_headers'}}) {
- Debug "(header) $_ = " . $resp->{'_headers'}->{$_} if $self->{'debug'} > 1;
+ foreach (sort $resp->header_field_names()) {
+ Debug "(header) $_ = " . $resp->header($_) if $self->{'debug'} > 1;
}
- my $response_location = $resp->{'_headers'}->{'location'} || '';
+ my $response_location = $resp->header('Location') || '';
Debug "Response Location: $response_location" if $self->{'debug'};
Debug "Comparing with \"/$args{'title'}\"" if $self->{'debug'};
+
+ Debug "(decoded content) =\n" . $resp->decoded_content() if $self->{'debug'} > 2;
+
if ($response_location =~ /[\/=]$args{'title'}/i) {
Debug "Success!" if $self->{'debug'};
return 0;
@@ -230,9 +254,13 @@
Debug "Fetching page '$args{'title'}' (section '$args{'section'}')..." if $self->{'debug'};
my $edit_section = $args{'section'} ? "\§ion=$args{'section'}" : '';
- my $resp = $ua->request(GET "$args{'protocol'}://$WHOST/$WPATH/index.php?title=$args{'title'}&action=edit$edit_section");
+ my $edit_url = "$args{'protocol'}://$WHOST/$WPATH/index.php?title=$args{'title'}&action=edit$edit_section";
+ my $resp = $ua->request(GET $edit_url);
+ Debug "[getPage] From " . $edit_url if $self->{'debug'};
my @lines = split /\n/, $resp->content();
+ Debug "(decoded content) =\n" . $resp->decoded_content() if $self->{'debug'} > 2;
+
my @content = ();
my $saving = 0;
@@ -297,7 +325,7 @@
# protocol => 'https', # Optional, default is http
host => 'localhost', # Default: localhost
path => 'wiki' , # Can be empty on 3rd-level domain Wikis
- debug => 0 # Optional. 0=no debug msgs, 1=some msgs, 2=more msgs
+ debug => 0 # Optional. 0=no debug msgs, 1=some msgs, 2=more msgs, 3=all msgs
);
=head1 DESCRIPTION