Uploading to MediaWiki

(This stuff below will probably page-wrap badly. Sorry.)

As part of my quick coding of a solution to port from moinmoin to
MediaWiki, I needed to put together some code to upload the converted
content. The code I've written (sample below) is for uploading the
converted markup, and it works fine. Perhaps at the weekend I'll address
the issue of uploading attachments, but meanwhile if anyone has some
advice on that task, I'd welcome it.

Otherwise I'll just hack along in the rare moments of downtime (e.g.
coffee breaks) and see where it takes me.

Meanwhile just to report that I've enhanced the mm/mw translator to deal
with typical mm table markup (including r/c-spans, alignments and
colours). I've also improved the nested list processing and a few other
things. I'll stop this tweaking when I'm happy enough that the code will
process the DDWG wiki. If people want more functionality after that task
is complete, I'll have to think about it. :)

---Rotan.




use LWP::UserAgent;
use HTTP::Request::Common;
use strict;

my $contentType = "";
my $serverindexurl = 'http://wiki.example.com/wiki/index.php';
my $username = 'someusername';
my $password = 'secretpassword';
my $pagetitle = 'Test_Page'; # Must be valid WM page title, no spaces.
my $comment = 'This is a second scripted upload test';
my $newcontent =
 "This is a typical Wiki page.\n\n" .
 "This page was updated via an upload that performed these two steps:\n"
.
 "# Log in\n" .
 "# Upload\n";
my $ua = LWP::UserAgent->new(
  agent => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)' ,
  'cookie_jar' => {file => "wpcookies.txt", autosave => 1}
);
if (!LogInToServer($username,$password)) {
  die "Failed to log in to MediaWiki server";
}
if (!UploadToServer($pagetitle,$newcontent,$comment)) {
  die "Failed to upload content to MediaWiki server";
}
print "Finished";
exit;

sub LogInToServer { # Params: username,password
 	my %params = ();
	$params{'wpName'} = shift;
	$params{'wpPassword'} = shift;
	$params{'wpLoginattempt'} = 'Log in';
	my $response = $ua->request(
      POST "$serverindexurl?title=Special:Userlogin&action=submitlogin"
,
        Content_Type => 'application/x-www-form-urlencoded' ,
        Content => [ %params ]
	);
	my $successful = 0;
	foreach (keys %{$response->{'_headers'}}) {
        if ($_ =~ /^set-cookie$/i) { # server attempting to set a cookie
           my $a = $response->{'_headers'}->{$_};
           if ($a =~ /^ARRAY(.+)$/) {
            foreach (@{$a}) {
              if (/UserID=\d+\;/i) {
                $successful = 1; # Success!
                last;
              }
            }
          }
        }
	}
	return $successful;
}

sub UploadToServer { # Params: title,content,comment
  my $pagetitle = shift;
  my $pagecontent = shift;
  my $comment = shift;
  my $response = $ua->request(GET
"$serverindexurl?title=$pagetitle&action=edit");
  my @lines = split /\n/, $response->content();
  my $token = '';
  my $edittime = '';
  foreach (@lines) {
    if (/wpEditToken/) {
      s/type=.?hidden.? *value="(.+)" *name/$1/i;
      $token = $1;
    }
    if (/wpEdittime/) {
      s/type=.?hidden.? *value="(.+)" *name/$1/i;
      $edittime = $1 || '';
    }
  }
  my %params = ();
  $params{'wpTextbox1' } = $pagecontent;
  $params{'wpEdittime' } = $edittime;
  $params{'wpSave'     } = 'Save page';
  $params{'wpSection'  } = '';
  $params{'wpSummary'  } = $comment;
  $params{'wpEditToken'} = $token;
  $params{'title' }      = $pagetitle;
  $params{'action' }     = 'submit';
  $response = $ua->request(
    POST "$serverindexurl?title=${pagetitle}&action=submit",
    Content_Type => 'application/x-www-form-urlencoded',
    Content => [ %params ]
  );
  my $response_location = $response->{'_headers'}->{'location'} || '';
  if ($response_location =~ /[\/=]$pagetitle/i) {
    return 1; # Success
  }
  return 0; # Failure
}

Received on Thursday, 25 October 2007 16:25:58 UTC