2 * Additional mw.Api methods to assist with API calls related to editing wiki pages.
5 ( function( $, mw
, undefined ) {
7 // Cache token so we don't have to keep fetching new ones for every single request.
8 var cachedToken
= null;
10 $.extend( mw
.Api
.prototype, {
13 * Post to API with edit token. If we have no token, get one and try to post.
14 * If we have a cached token try using that, and if it fails, blank out the
15 * cached token and start over.
17 * @param params {Object} API parameters
18 * @param ok {Function} callback for success
19 * @param err {Function} [optional] error callback
22 postWithEditToken: function( params
, ok
, err
) {
23 var api
= this, useTokenToPost
, getTokenIfBad
;
24 if ( cachedToken
=== null ) {
25 // We don't have a valid cached token, so get a fresh one and try posting.
26 // We do not trap any 'badtoken' or 'notoken' errors, because we don't want
27 // an infinite loop. If this fresh token is bad, something else is very wrong.
28 useTokenToPost = function( token
) {
30 api
.post( params
, ok
, err
);
32 return api
.getEditToken( useTokenToPost
, err
);
34 // We do have a token, but it might be expired. So if it is 'bad' then
35 // start over with a new token.
36 params
.token
= cachedToken
;
37 getTokenIfBad = function( code
, result
) {
38 if ( code
=== 'badtoken' ) {
39 cachedToken
= null; // force a new token
40 api
.postWithEditToken( params
, ok
, err
);
45 return api
.post( params
, { ok
: ok
, err
: getTokenIfBad
});
50 * Api helper to grab an edit token
52 * token callback has signature ( String token )
53 * error callback has signature ( String code, Object results, XmlHttpRequest xhr, Exception exception )
54 * Note that xhr and exception are only available for 'http_*' errors
55 * code may be any http_* error code (see mw.Api), or 'token_missing'
57 * @param tokenCallback {Function} received token callback
58 * @param err {Function} error callback
61 getEditToken: function( tokenCallback
, err
) {
65 // we need some kind of dummy page to get a token from. This will return a response
66 // complaining that the page is missing, but we should also get an edit token
67 titles
: 'DummyPageForEditToken'
69 ok = function( data
) {
71 $.each( data
.query
.pages
, function( i
, page
) {
72 if ( page
.edittoken
) {
73 token
= page
.edittoken
;
77 if ( token
!== undefined ) {
79 tokenCallback( token
);
81 err( 'token-missing', data
);
87 // Due to the API assuming we're logged out if we pass the callback-parameter,
88 // we have to disable jQuery's callback system, and instead parse JSON string,
89 // by setting 'jsonp' to false.
93 return this.get( parameters
, ajaxOptions
);
97 * Create a new section of the page.
98 * @param title {mw.Title|String} target page
99 * @param header {String}
100 * @param message {String} wikitext message
101 * @param ok {Function} success handler
102 * @param err {Function} error handler
105 newSection: function( title
, header
, message
, ok
, err
) {
110 title
: title
.toString(),
114 return this.postWithEditToken( params
, ok
, err
);
119 } )( jQuery
, mediaWiki
);