Here is a script I wrote to create wiki pages from wiki and other
sources using the api. Hopefully this helps you (or someone else out):
<?php
/*
* Wiki on Inside New Page from Post Creator - Jason Kuter 11/2007
*
* This script receives a post from anything, though currently expecting
wiki to be sending it. It then
* takes the post information and sends it to make a new wiki page via
the wiki api. Since the wiki api
* is so immature this script can only create new pages and relies on the
uer having logged into wiki
* previously so the wiki logon cookies reside in their session. This
script supports email as well and will
* send one when requested.
*
* The following post values are listened to:
*
* $_POST['user'] required to exist so we can edit as that user
* $_POST['summary'] optional to set wiki edit summary (will also be
email subject if send email is true)
* $_POST['category'] optional to set category of new page (nice for
collection)
* $_POST['title'] option if you want to set page title, otherwise unique
title created using user and date
* $_POST['to'] optional is you want to email someone the contents. You
could seperate by , if you want to mail more than one person
* $_POST['JobNo'] optional if you want to append wiki link into existing
bat job
*/
//Pull in pear classes
require_once "classes/mail/Mail.php";
require_once "classes/mime/mime.php";
//Snoopy Browser
require_once("classes/Snoopy.class.php");
//wiki api url
$apiURL =
"http://".$_SERVER['HTTP_HOST']."/phpapps/wiki/api.php";
//set wiki user
$wikiUser = isset($_POST['user']) ? trim($_POST['user']) : false;
//check we were posted a username and use it log in making sure its not
an IP
if ($wikiUser != false && !is_numeric($wikiUser{0})) {
//try and log into wiki
logintoWiki($wikiUser,$apiURL);
//set up some basic information needed to create a new page
$date = date(DATE_RFC822);
$batJob = !empty($_POST['JobNo']) ? trim($_POST['JobNo']) :
"";
$category = !empty($_POST['category']) ?
"[[Category:".trim($_POST['category'])."]]" : "";
$summary = !empty($_POST['summary']) ? trim($_POST['summary']) :
"$wikiUser created new page";
$title = !empty($_POST['title']) ? trim($_POST['title']) :
"$summary on $date for $wikiUser";
$sendEmail = !empty($_POST['email']) && !empty($_POST['to']) ?
true : false;
//set up body of wiki page
$formData = "{|class=\"wikitable sortable\"\n!Checklist
Item!!Value\n|-\n";
$ignore_fields = array("to", "email", "summary",
"title",
"category", "Submit_Checklist");
foreach ($_POST as $key=>$value) {
if (!in_array($key, $ignore_fields)) {
$formData .= "\n|$key||$value\n|-\n";
}
}
$formData .= "|}\n";
$formData .= "\n$category";
//create a new page
$pageCreated =
makeNewWikiPage($title,$apiURL,$summary,$formData);
//if successful send them there and do other requested actions
if ($pageCreated) {
//apend a bat job if asked
if ($batJob != "") {
$appendBatJobParams = array(
"user"=> $wikiUser,
"link"=> "http://" . $_SERVER['HTTP_HOST'].
"/wiki/" . $title,
"job"=> $batJob
);
//this may take a sec depending on how long it
takes the perl script on the other end to respond
$appendBatJob =
getSnooped("http://www-internal/includes/php/batjobcommenter.php",$appen
dBatJobParams);
}
//send email if asked
if (isset($_POST['to'])) {
$to = $_POST['to'];
$from = !empty($_POST['from']) ? $_POST['from']
: "inside(a)mathworks.com".com";
$body = "$title Wiki page was created properly";
$itmailed = mailit($to,$body,$from,$summary,"");
if ($itmailed !== true) {
die($itmailed);
}
}
header("Location: http://" . $_SERVER['HTTP_HOST'].
"/wiki/" . $title);
// handle the page not created error
} else {
echo "<h1 style=\"color:red\">Error: The page was not
created properly, please contact <a
href=\"mailto:inside@mathworks.com\">inside@mathworks.com</a>";
//send email if asked also send error email
if ($sendEmail) {
$to = $_POST['to'];
$from = !empty($_POST['from']) ? $_POST['from']
: "inside(a)mathworks.com".com";
$body = "There was an error with new wiki page
$title's creation via Wiki Api Page Creator";
$itmailed =
mailit($to,$body,$from,$summary,"inside(a)mathworks.com")com");
if ($itmailed !== true) {
die($itmailed);
}
} else {
$body = "There was an error with new wiki page
$title's creation via Wiki Api Page Creator";
$itmailed =
mailit("inside(a)mathworks.com",$body,$from,$summary,"")ummary,"");
if ($itmailed !== true) {
die($itmailed);
}
}
}
} else {
// no username to work on
echo "<h1 style=\"color:red\">Error: You need to log into wiki
before you can submit.</h1>\n";
echo
"<script>setTimeout(\"document.location.href='".$_SERVER['HTTP_REFERER']
."'\",2000);</script>";
}
//create new wiki page from submitted content
function makeNewWikiPage($title,$apiURL,$summary,$formData) {
//Parameters to send to api for edit token
$titleURLParams = array(
"action"=>"query",
"prop"=>"info",
"titles"=>$title,
"intoken"=>"edit",
"format"=>"php"
);
//get edit token response from api
$titleResponse =
unserialize(getSnooped($apiURL,$titleURLParams));
//flattens the deep array returned by the api
$titleResponseFlat = array_flatten($titleResponse);
//make sure we have a token
$editToken = !empty($titleResponseFlat['edittoken']) ?
$titleResponseFlat['edittoken'] : -1;
//If edit token is granted send content to new page
if ($editToken != -1) {
$newPageURLParams = array(
"action"=>"query",
"title"=>$title,
"edittoken"=>$editToken,
"format"=>"php",
"summary"=>$summary,
"content"=>$formData
);
$makeNewPageResponse =
getSnooped($apiURL,$newPageURLParams);
} else {
die("I am unable to get an edit token and can not
continue");
}
//as long as there isn't an error return true - this is stupid
because the wiki api is immature
if (strpos($makeNewPageResponse,'error') !== true) {
return true;
} else {
return false;
}
}
//This function attempts to log into wiki in order to grab cookies
function logintoWiki($wikiUser,$apiURL) {
//Parameters to send to api for login token
$loginURLParams = array(
"action"=>"login",
"lgname"=>$wikiUser,
"lgpassword"=>"",
"lgdomain"=>"ad.mathworks.com",
"format"=>"php"
);
//get login token response from api
$loginResponse =
unserialize(getSnooped($apiURL,$loginURLParams));
//flattens the deep array returned by the api
$loginResponseFlat = array_flatten($loginResponse);
//make sure we have a token
$loginToken = $loginResponseFlat['result'] == "Success" ? true :
false;
if ($loginToken == false) {
die('Invalid User Name Sent-You must wait 60 seconds
before trying again.');
} else {
return true;
}
}
//Snoopy function acts as a web browser to access api sending along
users cookies
function getSnooped($url,$vars){
$snoopy = new Snoopy;
//argg - gotta send sookies because logon tokens were removed
from api
$snoopy->cookies["inside_wikiToken"] =
$_COOKIE["inside_wikiToken"];
$snoopy->cookies["inside_wikiUserID"] =
$_COOKIE["inside_wikiUserID"];
$snoopy->cookies["inside_wikiUserName"] =
$_COOKIE["inside_wikiUserName"];
$snoopy->cookies["inside_wiki_session"] =
$_COOKIE["inside_wiki_session"];
$snoopy->submit($url,$vars);
$snoopy->setcookies();
return $snoopy->results;
}
//flattens the deep array returned by the api
function array_flatten($array, $preserve_keys = 1, &$newArray = Array())
{
foreach ($array as $key => $child) {
if (is_array($child)) {
$newArray =& array_flatten($child,
$preserve_keys, $newArray);
} elseif ($preserve_keys + is_string($key) > 1) {
$newArray[$key] = $child;
} else {
$newArray[] = $child;
}
}
return $newArray;
}
// Sends email and returns pear error message if no go
function mailit($to,$bodyhtml,$from,$subject,$cc) {
$crlf = "\n";
$headers = array("To" => $to,"From" =>
$from,"Subject" =>
$subject,"Cc" => $cc);
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($bodyhtml);
$body = $mime->get();
$headers = $mime->headers($headers);
// Create the mail object using the pear Mail::factory method
$mail_object =& Mail::factory("mail");
$itmailed = $mail_object->send($to, $headers, $body);
if (PEAR::isError($itmailed)) {
$itmailed = $send->getMessage();
return $itmailed;
} else {
return true;
}
}
?>
From: mediawiki-api-bounces(a)lists.wikimedia.org
[mailto:mediawiki-api-bounces@lists.wikimedia.org] On Behalf Of
marion.leclerc(a)orange-ftgroup.com
Sent: Friday, December 07, 2007 9:00 AM
To: MediaWiki API announcements & discussion
Subject: [Mediawiki-api] API - Possibility to create/edit pages
Roan,
As far as I have understood, the API should permit to create/edit pages
as described in the proposal found at
http://www.mediawiki.org/wiki/API:Edit_-_Create%26Edit_pages
In the last version of API developped, I did not found this
functionnality.
Is it planned to develop it ? If yes, when ?
Thanks for your help.
Marion Leclerc