https://bugzilla.wikimedia.org/show_bug.cgi?id=22005
--- Comment #2 from Amqui 2010-01-04 06:54:49 UTC ---
I tries on en.wikinews just to make sure and I got the same error.
Here's the functions that I use :
public function create_page($page, $text, $summary, $minor = false, $bot =
false, $wiki = "")//create a new page
{
$response = $this->callAPI($wiki,
"api.php?action=query&prop=info|revisions&intoken=edit&titles=" .
urlencode($page));
$this->editdetails = $response["query"]["pages"];
if (!isset($this->editdetails[-1])) {
echo "Page $page already exists. Call edit_page instead.\n";
return false;
}
if ($this->put_page($page, $text, $summary, $minor, $bot, $wiki)) {
return true;
} else {
echo "^^^ Error with put_page called from edit_page.\n";
return false;
}
}
public function edit_page($page, $text, $summary, $minor = false, $bot =
true, $wiki = "")//edit a page which already exists
{
$response = $this->callAPI($wiki,
"api.php?action=query&prop=info|revisions&intoken=edit&titles=" .
urlencode($page));
$this->editdetails = $response["query"]["pages"];
if (isset($this->editdetails[-1])) {
echo "Page $page does not already exist. Call create_page
instead.\n";
return false;
}
if ($this->put_page($page, $text, $summary, $minor, $bot, $wiki)) {
return true;
} else {
echo "^^^ Error with put_page called from edit_page.\n";
return false;
}
}
private function put_page($name, $newtext, $summary, $minor = false, $bot
= true, $wiki = "")//edit a page, regardless of whether it exists before or not
{
foreach ($this->editdetails as $key => $value) {
$token = urlencode($value["edittoken"]);
$sts = $value["starttimestamp"];
if (isset($this->editdetails[-1])) {
$ts = $sts;
$extra = "&createonly=yes";
} else {
$ts = $value["revisions"][0]["timestamp"];
$extra = "&nocreate=yes";
}
}
$newtext = urlencode($newtext);
$rawoldtext = $this->get_page($name, $wiki);
$oldtext = urlencode($rawoldtext);
$summary = urlencode($summary);
if ($newtext == $oldtext) {
//the new content is the same, nothing changes
echo "The new content for " . $name . " is exactly the same as
the current content, so the page wasn't edited.\n";
return false;
}
if ($newtext == "") {
//the new content is void, nothing changes
echo "Error: you were about to blank the page of " . $name .
".\n";
return false;
}
$post =
"title=$name&action=edit&basetimestamp=$ts&starttimestamp=$sts&token=$token&summary=$summary$extra&text=$newtext";
if ($bot) {
if (!$this->allowBots($rawoldtext)) {
echo "Bot edits, or those specifically from this bot, have
been blocked on this page.\n";
return false;
}
$post .= "&bot=yes";
}
if ($minor) {
$post .= "&minor=yes";
} else {
$post .= "¬minor=yes";
}
$response = $this->postAPI($wiki, 'api.php', $post);
if ($response["edit"]["result"] == "Success") {
echo "Successfully edited " . $response["edit"]["title"] . ".\n";
sleep($epm);
return true;
} elseif (preg_match('/^Waiting for (.*) seconds lagged/', $result))
{
echo "Error: max lag hit, not posted\n";
return false;
} elseif (isset($response["error"])) {
echo "Error - [".$response["error"]["code"]."] ".
$response["error"]["info"] ."\n";
return false;
} else {
echo "Error - " . $response["edit"]["result"] . " \n";
return false;
}
}
private function wiki($wiki)//manager wiki different from default wiki
{
if ($wiki == "") {
//if not declarated put default wiki
return $this->wiki;
} elseif (strpos($wiki, "://") == false) {
//if is a mediawiki project the user write only code language
return "http://"; . $wiki . ".wikipedia.org/w/";
}
//if it is a other wiki project
return $wiki;
}
private function callAPI($wiki, $url, $format = "php") {
$wiki = $this->wiki($wiki);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');