Page MenuHomePhabricator

Error while POST text to create a page
Closed, InvalidPublic

Description

Author: lebo.beland

Description:
I have a problem when trying to create a page with a php script on a Wikimedia site (same problem on fr.wikipedia.org and fr.wikinews.org, didn't try on other Wikimedia site). I'm sending the variable $_POST['text'] to the server but when 'text' is a bit long, I got an error. The error that I recieve is :
Request: POST http://fr.wikipedia.org/w/api.php, from 207.134.216.200 via sq35.wikimedia.org (squid/2.7.STABLE6) to ()
Error: ERR_INVALID_REQ, errno [No Error] at Mon, 04 Jan 2010 03:01:56 GMT

For example :
If I put $text = "<center> {| border=\"1px\" width=\"75%\" class=\"wikitable\" align=\"center\" cellspacing=\"3px\" !colspan=12| Résultats du " . $date . " |- style=\"border:1px solid #889999; background-color:#F0F0F0;\" ! Équipe gagnante Équipe perdante Pointage final |}"; everything works fine.
But, if I put $text = "{{date|{{subst:#ifeq:{{subst:#time:j}}|1|1er|{{subst:#time:j}}}} {{subst:#time:F Y}}}} Le " . $date . ", " . $nbParties . " parties se déroulaient dans la [[w:Ligue de hockey junior majeur du Québec|Ligue de hockey junior majeur du Québec]] (LHJMQ). <br/> <br/> <center> {| border=\"1px\" width=\"75%\" class=\"wikitable\" align=\"center\" cellspacing=\"3px\" !colspan=12| Résultats du " . $date . " |- style=\"border:1px solid #889999; background-color:#F0F0F0;\" ! Équipe gagnante Équipe perdante Pointage final |}"; I got the error message.

Amqui


Version: unspecified
Severity: enhancement
OS: Windows XP
Platform: PC
URL: http://fr.wikinews.org

Details

Reference
bz22005

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 21 2014, 10:51 PM
bzimport set Reference to bz22005.

I just tried this on enwikinews and it seemed to work fine - http://en.wikinews.org/wiki/User:Bawolff/test . Check what the HTTP status code is. sometimes thats a more descriptive error message.

lebo.beland wrote:

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.<br />\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.<br />\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.<br />\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.<br />\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.<br />\n";
        return false;
    }
    if ($newtext == "") {
        //the new content is void, nothing changes
        echo "Error: you were about to blank the page of " . $name . ".<br />\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.<br />\n";
            return false;
        }
        $post .= "&bot=yes";
    }
    if ($minor) {
        $post .= "&minor=yes";
    } else {
        $post .= "&notminor=yes";
    }
    $response = $this->postAPI($wiki, 'api.php', $post);
    if ($response["edit"]["result"] == "Success") {
        echo "Successfully edited " . $response["edit"]["title"] . ".<br />\n";
        sleep($epm);
        return true;
    } elseif (preg_match('/^Waiting for (.*) seconds lagged/', $result)) {
        echo "Error: max lag hit, not posted<br />\n";
        return false;
    } elseif (isset($response["error"])) {
  echo "Error - [".$response["error"]["code"]."] ". $response["error"]["info"] ."<br />\n";
          return false;
    } else {
        echo "Error - " . $response["edit"]["result"] . "&nbsp;<br />\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');
    curl_setopt($ch, CURLOPT_URL, ($wiki . $url . "&maxlag=" . $this->max_lag . "&format=$format"));
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        return curl_error($ch);
    }
    curl_close($ch);
    return unserialize($response);
}
private function postAPI($wiki, $url, $postdata = "") {
    $wiki = $this->wiki($wiki);
    $ch = curl_init();
    $url = $wiki . $url;
    if ($postdata !== "") {
        $postdata .= "&";
    }
    $postdata .= "format=php&maxlag=" . $this->max_lag;
    //echo "Final postdata: $postdata<br />\n";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded;charset=UTF-8'));
    curl_setopt($ch, CURLOPT_HEADER, false);
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        return curl_error($ch);
    }
    curl_close($ch);
    echo $response;
   return unserialize($response);
}

The error message is in the response from the server that I got in the postAPI function in $response.

Amqui

overlordq wrote:

Old change[1] in squid behavior, if your client continues to send data after receiving a HTTP 417 status code, this will result.

Add 'Expect:' to clear the header in your curl options.

1 - http://en.wikipedia.org/wiki/Wikipedia:Bot_owners%27_noticeboard/Archive_4#Squid_Changes