3

I'm trying to make a simple extension that moves a page on my mediawiki site, but whether using curl or FauxRequest, I always get bad token response. Tried urlencoding, not encoding, escaping, without +/, etc, doesn't matter.

Code looks like this currently using FauxRequest, with param1/2/3 coming from the parser function I'm creating.

global $wgRequest;
$token = $token = $wgUser->editToken();

$params = new FauxRequest( 
    array(
        'action'    => 'move',
        'from'      => $param1,
        'to'        => $param2,
        'format'    => 'php',
        'reason'    => $param3,
        'token'     => $token)
);
$api = new ApiMain( $params, true);
$api->execute();
$data = & $api->getResultData();


$output = "moved $param1 to $param2 - $token";

also tried the below code using curl instead, which results in bad token as well

global $wgUser;
$token = $wgUser->editToken();
$url = 'http://www.website.com/api.php?';
    $myvars = 'action=move&format=xml&from=' . "$param1" . '&to=' . "$param2" . '&reason=' . "$param3" . '&token=' . urlencode($token);

    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt( $ch, CURLOPT_HEADER, 0);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

    $response = curl_exec( $ch );


$output = "moved $param1 to $param2 - $myvars - $response";

Am I missing something in the code, or could I have a setting wrong somewhere?

Any help would be hugely appreciated!

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Justin M
  • 103
  • 1
  • 8

2 Answers2

0

The actual error might be something else. Did you try doing it as specified in https://www.mediawiki.org/wiki/API:Calling_internally

nischayn22
  • 447
  • 3
  • 14
  • Yeah, I started with the code on that page, but got the understanding from reading the page that I needed to use FauxRequest. Either way I could never get it to work. – Justin M Apr 27 '14 at 18:52
  • Using FauxRequest for write operations without passing request context causes bug 34838. – nischayn22 Apr 28 '14 at 07:19
0

I was recommended to not use either method for moving a page in an extension, and was instructed to use the following code instead, which works great. Hope this helps someone.

$oldTitle = Title::newFromText( $param1 );
$newTitle = Title::newFromText( $param2 );
// Error checking here
$oldTitle->moveTo( $newTitle, true, $param3, true );
Justin M
  • 103
  • 1
  • 8