Problem with action=edit using PHP

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with action=edit using PHP

Marcus Ramsden

Hi,

 

I am getting an issue using the edit action of the MediaWiki API. Whenever I send an edit request I am getting an error response stating badtoken. The question I have is which token is this referring to. Is this referring to my cookie token which I have instructed curl to store or is it referring to the edit token.

 

The login operation is given the following parameters using a POST request;

action=login&lgname=<bot username>&lgpassword=<bot password>&format=php

 

To obtain the token I am using the following parameters in a GET request ;

action=query&intoken=edit&titles=IBWikiBotTest&prop=info|revisions&format=php.

 

The following parameters are then used as part of a POST request in the attempt to create a new page;

action=edit&title=IBWikiBotTest&summary=Page created by the IBWikiBot.&text=This is some test content added by the IBWikiBot.&md5=e21e79d8ee8ee6b5038c9bac090decf1&bot&recreate&token=0e72e13613a9408646cae5aa1a630d0a+\\&format=php

 

The unserialized response from the API is:

Array

(

    [error] => Array

        (

            [code] => badtoken

            [info] => Invalid token

        )

)

 

Any insight on this would be greatly appreciated.

 

Marcus Ramsden

 

---

Marcus Ramsden

Technical Project Assistant

Learning Societies Laboratory, Electronics and Computer Science

University of Southampton

3069, Building 32, Highfield Campus

Southampton SO17 1BJ

www.southampton.ac.uk/ecs

 


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Problem with action=edit using PHP

Roan Kattouw
Marcus Ramsden schreef:

>
> Hi,
>
>  
>
> I am getting an issue using the edit action of the MediaWiki API.
> Whenever I send an edit request I am getting an error response stating
> badtoken. The question I have is which token is this referring to. Is
> this referring to my cookie token which I have instructed curl to
> store or is it referring to the edit token.
>
The badtoken error always refers to the edit token.

>
>  
>
> The login operation is given the following parameters using a POST
> request;
>
> action=login&lgname=<bot username>&lgpassword=<bot password>&format=php
>
>  
>
> To obtain the token I am using the following parameters in a GET request ;
>
> action=query&intoken=edit&titles=IBWikiBotTest&prop=info|revisions&format=php.
>
>
>  
>
> The following parameters are then used as part of a POST request in
> the attempt to create a new page;
>
> action=edit&title=IBWikiBotTest&summary=Page created by the
> IBWikiBot.&text=This is some test content added by the
> IBWikiBot.&md5=e21e79d8ee8ee6b5038c9bac090decf1&bot&recreate&token=0e72e13613a9408646cae5aa1a630d0a+\\&format=php
>
Try removing the second backslash (you don't need to escape backslashes
in URLs) and encoding the + as %2B (if you don't do that, it could end
up being converted to a space).

Roan Kattouw (Catrope)

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Problem with action=edit using PHP

Marcus Ramsden
In reply to this post by Marcus Ramsden
Hi Roan,

I have put the edit token through the php urlencode function which converts
the token to the following format, 771ceb39a33469b2a87873ef26b75b82%2B%5C.
This has stopped the invalid token error being sent. But now there is
another issue.
Rather than the response being the standard response the API is responding
only with a 1. As opposed to the appropriate PHP data structure.

Marcus

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Roan Kattouw
Sent: 30 September 2008 13:34
To: MediaWiki API announcements & discussion
Subject: Re: [Mediawiki-api] Problem with action=edit using PHP

Marcus Ramsden schreef:

>
> Hi,
>
>  
>
> I am getting an issue using the edit action of the MediaWiki API.
> Whenever I send an edit request I am getting an error response stating
> badtoken. The question I have is which token is this referring to. Is
> this referring to my cookie token which I have instructed curl to
> store or is it referring to the edit token.
>
The badtoken error always refers to the edit token.

>
>  
>
> The login operation is given the following parameters using a POST
> request;
>
> action=login&lgname=<bot username>&lgpassword=<bot password>&format=php
>
>  
>
> To obtain the token I am using the following parameters in a GET request ;
>
>
action=query&intoken=edit&titles=IBWikiBotTest&prop=info|revisions&format=ph
p.

>
>
>  
>
> The following parameters are then used as part of a POST request in
> the attempt to create a new page;
>
> action=edit&title=IBWikiBotTest&summary=Page created by the
> IBWikiBot.&text=This is some test content added by the
>
IBWikiBot.&md5=e21e79d8ee8ee6b5038c9bac090decf1&bot&recreate&token=0e72e1361
3a9408646cae5aa1a630d0a+\\&format=php
>
Try removing the second backslash (you don't need to escape backslashes
in URLs) and encoding the + as %2B (if you don't do that, it could end
up being converted to a space).

Roan Kattouw (Catrope)

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Problem with action=edit using PHP

Bryan Tong Minh
In reply to this post by Roan Kattouw
On Tue, Sep 30, 2008 at 2:34 PM, Roan Kattouw <[hidden email]> wrote:

>>
>> The following parameters are then used as part of a POST request in
>> the attempt to create a new page;
>>
>> action=edit&title=IBWikiBotTest&summary=Page created by the
>> IBWikiBot.&text=This is some test content added by the
>> IBWikiBot.&md5=e21e79d8ee8ee6b5038c9bac090decf1&bot&recreate&token=0e72e13613a9408646cae5aa1a630d0a+\\&format=php
>>
> Try removing the second backslash (you don't need to escape backslashes
> in URLs) and encoding the + as %2B (if you don't do that, it could end
> up being converted to a space).
>
> Roan Kattouw (Catrope)
>
> _______________________________________________
> Mediawiki-api mailing list
> [hidden email]
> https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
>

You should actually encode the +\ in proper url encoding. Those
characters exist on purpose in edittokens to detect software that
handles escaping badly.

Bryan

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Problem with action=edit using PHP

Roan Kattouw
In reply to this post by Marcus Ramsden
Marcus Ramsden schreef:
> Hi Roan,
>
> I have put the edit token through the php urlencode function which converts
> the token to the following format, 771ceb39a33469b2a87873ef26b75b82%2B%5C.
>  
Looks good. Of course the built-in urlencode() function is the way to
go, hadn't thought about that when I wrote my first message.
> This has stopped the invalid token error being sent.
That probably means it's correct.
>  But now there is
> another issue.
> Rather than the response being the standard response the API is responding
> only with a 1. As opposed to the appropriate PHP data structure.
That's very weird. Please do the following things:
* check whether the edit got through;
* try using a different format like XML or JSON and see whether they
return the right data;
* tell me which wiki you're running this bot against (if it's not
Wikipedia or another WMF wiki, also include the version you see in
Special:Version)

Roan Kattouw (Catrope)

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Problem with action=edit using PHP

Marcus Ramsden
In reply to this post by Marcus Ramsden
Right I think I know where this error is coming up now. I still tried the
approach in XML and unfortunately it was the same story with a 1 being
returned. Also my script throws out some output so here it is;

POST Data: action=login&lgname=***&lgpassword=***&format=xml
POST Result: <?xml version="1.0" encoding="utf-8"?><api><login
result="Success" lguserid="***" lgusername="***"
lgtoken="d6ef5e4f6baaa4a7b3468a1eac5160b3" cookieprefix="ibwiki_"
sessionid="***" /></api>
GET Data: action=query&intoken=edit&titles=BotTest&prop=info&format=xml
GET Result: <?xml version="1.0" encoding="utf-8"?><api><query><pages><page
ns="0" title="BotTest" missing=""
edittoken="771ceb39a33469b2a87873ef26b75b82+\" /></pages></query></api>
POST Data:
action=edit&title=BotTest&summary=Page+created+by+the+IBWikiBot.&text=This+i
s+some+test+content+added+by+the+IBWikiBot.&recreate=1&token=771ceb39a33469b
2a87873ef26b75b82%2B%5C&notminor=1&bot=1&format=xml
POST Result: 1

Just to clarify that the data lines show any parameters being passed while
the result lines show the XML responses.

Onto where the error is actually occurring. Hopefully I am not going too
off-topic on this list. I notice in my PHP error log that some of my
extension functions are dying with a fatal error when the edit request
occurs. I know that these functions all run successfully when a standard
request from a browser is made.

The specific error is that it appears that wiki global $wgTitle does not
have a value in it. Is there a way to check where the request originates
from, be it the browser or a request through the API. If I could ascertain
this then I would be able to instruct my extension to not register any
functions which rely on some of the wiki globals being set.

Marcus

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Roan Kattouw
Sent: 30 September 2008 15:00
To: MediaWiki API announcements & discussion
Subject: Re: [Mediawiki-api] Problem with action=edit using PHP

Marcus Ramsden schreef:
> Hi Roan,
>
> I have put the edit token through the php urlencode function which
converts
> the token to the following format, 771ceb39a33469b2a87873ef26b75b82%2B%5C.
>  
Looks good. Of course the built-in urlencode() function is the way to
go, hadn't thought about that when I wrote my first message.
> This has stopped the invalid token error being sent.
That probably means it's correct.
>  But now there is
> another issue.
> Rather than the response being the standard response the API is responding
> only with a 1. As opposed to the appropriate PHP data structure.
That's very weird. Please do the following things:
* check whether the edit got through;
* try using a different format like XML or JSON and see whether they
return the right data;
* tell me which wiki you're running this bot against (if it's not
Wikipedia or another WMF wiki, also include the version you see in
Special:Version)

Roan Kattouw (Catrope)

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Problem with action=edit using PHP

Bryan Tong Minh
On Tue, Sep 30, 2008 at 4:37 PM, Marcus Ramsden <[hidden email]> wrote:
> The specific error is that it appears that wiki global $wgTitle does not
> have a value in it. Is there a way to check where the request originates
> from, be it the browser or a request through the API. If I could ascertain
> this then I would be able to instruct my extension to not register any
> functions which rely on some of the wiki globals being set.
>

A terrible hack would be something like isset($GLOBALS['processor']))
but I think we should add something like define('API', 1) in api.php
so that you can check defined('API')

Bryan

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|

Re: Problem with action=edit using PHP

Roan Kattouw
Bryan Tong Minh schreef:

> On Tue, Sep 30, 2008 at 4:37 PM, Marcus Ramsden <[hidden email]> wrote:
>  
>> The specific error is that it appears that wiki global $wgTitle does not
>> have a value in it. Is there a way to check where the request originates
>> from, be it the browser or a request through the API. If I could ascertain
>> this then I would be able to instruct my extension to not register any
>> functions which rely on some of the wiki globals being set.
>>
>>    
>
> A terrible hack would be something like isset($GLOBALS['processor']))
> but I think we should add something like define('API', 1) in api.php
> so that you can check defined('API')
While we should have define('API', 1); , for now is_null($wgTitle) will
do. So if you add

global $wgTitle;
if(is_null($wgTitle))
        return true;

on top of all your hook functions that use $wgTitle, you should be fine.

Roan Kattouw (Catrope)

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api