Power Users: Automating ProjectForum tasks.
A couple of weeks ago we talked about how to manipulate ProjectForum's underlying database files, and suggested that in almost all cases, there is a better way to do it. Today we'll talk about that better way.
We sometimes get asked if ProjectForum has an "API" (application programming interface), allowing other programs to do things like add users, post content to pages, and so on. In fact, there is, and it is the same API that you use whenever you use ProjectForum yourself - good old HTML and HTTP.
Want to get ProjectForum to do some repetitive task, but you don't want to sit there and click-click-click through all the forms to do it? You can write a script, in languages like Perl, PHP, Python, Tcl, Ruby, or any other language that lets you easily make web calls in it. Let that script simulate all that clicking around and filling in forms on your behalf.
This is a powerful technique; remember that everything you can do with ProjectForum can be done through the web, so that means you can write a program to do all those things for you. In fact, we use this technique to do a huge amount of completely automated testing every day on ProjectForum, making sure that as we add new features that they work, and we don't break any old features in the process. The amount of testing code we have is actually on par with the amount of code for the application itself.
Here is a short and simple example (albeit without error checking), written in Perl, demonstrating how a program can log in to a running ProjectForum server, and add a new user to a group. It does this by first logging in as the site administrator (so it has access to everything on the site), and then visits the group's accounts page, sees what users are already there, and adds a new user to the end of the list.
To run the example, save it to a file (e.g. addaccount.pl), edit the first few lines to provide information about your site, and then run the script.
my $siteurl = "http://127.0.0.1:3455/"; # base URL of the site
my $siteadminpass = "abc123" ; # site administration password
my $urlprefix = "1"; ; # URL prefix of the group we're adding the account to
my $newusername = "Bill"; ; # name of new user to add
my $newuserpass = "billpassword"; ; # password for new user
use HTML::Form;
use LWP;
my $ua = LWP::UserAgent->new;
$ua->cookie_jar( {} );
# signin as admin, which will set the cfadmintoken cookie
my $response = $ua->get($siteurl . "admin/adminsignin.html");
my $form = HTML::Form->parse($response->decoded_content, $response->base);
$form->value("adminpasswd", $siteadminpass);
$ua->request($form->click());
# now append the new account to the accounts screen
$response = $ua->get($siteurl . $urlprefix . "/admin/accounts.html");
$form = HTML::Form->parse($response->decoded_content, $response->base);
my $newaccount = "\n" . $newusername . ";" . $newuserpass;
$form->value("accountlist", $form->value("accountlist") . $newaccount );
$ua->request($form->click());
We'll talk more about this technique next week...

Very cool.
Posted by: JasonV | September 20, 2007 at 07:22 AM