Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / resources / mediawiki.api / mediawiki.api.edit.js
blob2cd1b9295a63d97f1509e1889cedf83a6fb10211
1 /**
2  * @class mw.Api.plugin.edit
3  */
4 ( function ( mw, $ ) {
6         // Cache token so we don't have to keep fetching new ones for every single request.
7         var cachedToken = null;
9         $.extend( mw.Api.prototype, {
11                 /**
12                  * Post to API with edit token. If we have no token, get one and try to post.
13                  * If we have a cached token try using that, and if it fails, blank out the
14                  * cached token and start over.
15                  *
16                  * @param {Object} params API parameters
17                  * @param {Function} [ok] Success callback (deprecated)
18                  * @param {Function} [err] Error callback (deprecated)
19                  * @return {jQuery.Promise} See #post
20                  */
21                 postWithEditToken: function ( params, ok, err ) {
22                         var useTokenToPost, getTokenIfBad,
23                                 api = this;
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 ) {
29                                         params.token = token;
30                                         api.post( params, ok, err );
31                                 };
32                                 return api.getEditToken( useTokenToPost, err );
33                         } else {
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                                                 // force a new token, clear any old one
40                                                 cachedToken = null;
41                                                 api.postWithEditToken( params, ok, err );
42                                         } else {
43                                                 err( code, result );
44                                         }
45                                 };
46                                 return api.post( params, { ok : ok, err : getTokenIfBad });
47                         }
48                 },
50                 /**
51                  * Api helper to grab an edit token.
52                  *
53                  * @param {Function} [ok] Success callback
54                  * @param {Function} [err] Error callback
55                  * @return {jQuery.Promise}
56                  * @return {Function} return.done
57                  * @return {string} return.done.token Received token.
58                  */
59                 getEditToken: function ( ok, err ) {
60                         var d = $.Deferred(),
61                                 apiPromise;
62                         // Backwards compatibility (< MW 1.20)
63                         d.done( ok );
64                         d.fail( err );
66                         apiPromise = this.get( {
67                                         action: 'tokens',
68                                         type: 'edit'
69                                 }, {
70                                         // Due to the API assuming we're logged out if we pass the callback-parameter,
71                                         // we have to disable jQuery's callback system, and instead parse JSON string,
72                                         // by setting 'jsonp' to false.
73                                         // TODO: This concern seems genuine but no other module has it. Is it still
74                                         // needed and/or should we pass this by default?
75                                         jsonp: false
76                                 } )
77                                 .done( function ( data ) {
78                                         var token;
79                                         // If token type is not available for this user,
80                                         // key 'edittoken' is missing or can contain Boolean false
81                                         if ( data.tokens && data.tokens.edittoken ) {
82                                                 token = data.tokens.edittoken;
83                                                 cachedToken = token;
84                                                 d.resolve( token );
85                                         } else {
86                                                 d.reject( 'token-missing', data );
87                                         }
88                                 })
89                                 .fail( d.reject );
91                         return d.promise( { abort: apiPromise.abort } );
92                 },
94                 /**
95                  * Create a new section of the page.
96                  * @see #postWithEditToken
97                  * @param {mw.Title|String} title Target page
98                  * @param {string} header
99                  * @param {string} message wikitext message
100                  * @param {Function} [ok] Success handler
101                  * @param {Function} [err] Error handler
102                  * @return {jQuery.Promise}
103                  */
104                 newSection: function ( title, header, message, ok, err ) {
105                         return this.postWithEditToken( {
106                                 action: 'edit',
107                                 section: 'new',
108                                 format: 'json',
109                                 title: title.toString(),
110                                 summary: header,
111                                 text: message
112                         }, ok, err );
113                 }
115          } );
117         /**
118          * @class mw.Api
119          * @mixins mw.Api.plugin.edit
120          */
122 }( mediaWiki, jQuery ) );