Scripted Updates to Wiki Pages

josiahbryan
Joined: Sat 29 of Jan, 2005

Re: Scripted Updates to Wiki Pages

Posted:Tue 30 of Sep, 2008 (14:40 UTC)
Perl Script provided by James Thomas:

  1. !/usr/bin/perl

  1. Command to update or create a page on www.voip-info.org
  2. Usage:
  3. ./update_voip_info_page.pl --user=login_username --password=login_password --title="page name" --comment="comment text" --overwrite filename
  4. Examples:
  5. Create a new page, error if page already exists
  6. /update_voip_info_page.pl --user=asteriskwizard --password=zaptel123 --title="Asterisk Triva" --comment="initial page creation" page.txt
  7. Update page created with command above
  8. /update_voip_info_page.pl --user=asteriskwizard --password=zaptel123 --title="Asterisk Triva" --comment="fix spelling" --overwrite page.txt
  9. Create a new page, or overwrite an existing one if it already exists
  10. /update_voip_info_page.pl --user=asteriskwizard --password=zaptel123 --title="Asterisk Triva II" --comment="more triva" --overwrite page.txt
  11. Author: James H. Thompson jht@voip-info.org
  12. Copyright: 2008
  13. Date: 2008-09-29 23:12:11 -1000
  14. License: GPL V2 or GPL V3

use strict;
use Getopt::Long;
use WWW::Mechanize;

my $param_user;
my $param_password;
my $result;
my $param_title;
my $param_comment;
my $param_overwrite;

$result = GetOptions (
'user=s' => \$param_user,
'password=s' => \$param_password,
'title=s' => \$param_title,
'comment=s' => \$param_comment,
'overwrite' => \$param_overwrite,
);

die "Please specify --user" if !$param_user;
die "Please specify --password" if !$param_password;
die "Please specify --title" if !$param_title;

my $op;
my $page_content = join("",<>);
die "No page content on STDIN" if !length($page_content);


my $base_url = "http://www.voip-info.org";

  1. my $mech = WWW::Mechanize->new( autocheck => 1 );
my $mech = WWW::Mechanize->new();

  1. Retreive the Login form
$mech->get( "$base_url/users/login.php" );
my $result = $mech->content;
if ($result !~ m#/users/validate.php#) {
die "Failed to retreive login form";
}


  1. Submit the login form
$mech->submit_form(
form_number => 1,
fields => {
user => $param_user,
pass => $param_password,
},
);
die unless ($mech->success);
$result = $mech->content;
if ($result =~ m/Invalid username/ || $result !~ m#/users/logout.php#) {
die "Login Failed";
}
  1. print $result,"\n";

  1. Find the page
$mech->get( "$base_url/wiki/index.php?page=$param_title" );
  1. die unless ($mech->success);
$result = $mech->content;
if ($result !~ m#/wiki/edit.php#)
{
die "Get page creation/edit form failed";
}

$op = "update";
if ($result =~ m/Create the page:/ && $result =~ m/Oops\!/)
{
$op = "create";
}

if ($op eq 'update' && !$param_overwrite)
{
die "Page: '$param_title' already exists, specify --overwrite to replace";
}

  1. Get the page creation/edit form
$mech->follow_link( url_regex => qr/edit\.php/i );
die unless ($mech->success);

$result = $mech->content;
if ($result !~ m#/wiki/edit.php#)
{
die "Get page creation/edit form failed";
}

  1. print "Result: $result, Content: ".$mech->content;

my $page_id = $mech->value("page_id");
  1. print "PageID: $page_id\n";

my $fields =
{
edit_comment => $param_comment,
edit => $page_content,
};

$fields->{title} = $param_title if ! $page_id ;

  1. Submit the page creation form
$mech->submit_form(
form_number => 1,
fields => $fields,
button => "fSavePage",
);
die unless ($mech->success);

$result = $mech->content;

  1. check to see if page creation failed
if ($result =~ m/already exists. Please choose a different name./)
{
print STDERR "Page: '$param_title' Already Exists\n";
exit;
}

  1. print $result,"\n";
if ($op eq 'create')
{
print STDERR "Page '$param_title' created.\n";
}
else
{
print STDERR "Page '$param_title' updated.\n";
}
josiahbryan
Joined: Sat 29 of Jan, 2005

Scripted Updates to Wiki Pages

Posted:Mon 29 of Sep, 2008 (18:46 UTC)
Hey -

I'd like to write a script to automatically upload Asterisk Documentation to pages linked to http://www.voip-info.org/wiki/view/Asterisk+Documentation. However, I'm having a difficult time scripting the login to voip-info.org.


Any suggestions on how to connect and automatically update wiki pages?


Currently, I'm trying to first:

wget --debug --keep-session-cookies --save-cookies cookies.txt http://www.voip-info.org/users/login.php -O - | grep 'name="tk"'

To get the value of the 'tk' hidden field. Then I post the user/pass back:

wget --debug --keep-session-cookies --save-cookies cookies.txt --post-data 'user=jbryan@productiveconcepts.com&password=*****&rme=on&login=Log%20in%20to%20%voip-info.org&tk=e6c2fb8246bed562a69e' http://www.voip-info.org/users/login.php -O login.html

However, that doesn't *seem* to work (the login.html just gives the login form again.)

I try to test to see if it logged in by trying to access edit.php:

wget --load-cookies cookies.txt http://www.voip-info.org/wiki/edit.php -O edit.html ; links edit.html

However, edit.html says that "you must login first," which seems to indicate that the login request above didn't complete.

The cookies.txt file that is stored contains:

www.voip-info.org FALSE / FALSE 1225305455 bit-user-voipinfoorg m5gvpdoc3es6lscc035o4mein7
www.voip-info.org FALSE / FALSE 0 BWSESSION m5gvpdoc3es6lscc035o4mein7

Let me know if you have any thoughts or suggestions. Thanks!
-josiah