Merge "mediawiki.api: Add #badToken for invalidating bad cached tokens"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 5 May 2015 19:01:13 +0000 (19:01 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 5 May 2015 19:01:13 +0000 (19:01 +0000)
1  2 
resources/src/mediawiki.api/mediawiki.api.js

@@@ -5,6 -5,7 +5,6 @@@
        // wondering, would it be simpler to make it easy to clone the api object,
        // change error handling, and use that instead?
        var defaultOptions = {
 -
                        // Query parameters for API requests
                        parameters: {
                                action: 'query',
@@@ -20,7 -21,6 +20,7 @@@
                                dataType: 'json'
                        }
                },
 +
                // Keyed by ajax url and symbolic name for the individual request
                promises = {};
  
                                        // Error handler
                                        function ( code ) {
                                                if ( code === 'badtoken' ) {
-                                                       // Clear from cache
-                                                       promises[ api.defaults.ajax.url ][ tokenType + 'Token' ] =
-                                                               params.token = undefined;
+                                                       api.badToken( tokenType );
                                                        // Try again, once
+                                                       params.token = undefined;
                                                        return api.getToken( tokenType, params.assert ).then( function ( token ) {
                                                                params.token = token;
                                                                return api.post( params, ajaxOptions );
  
                                d = apiPromise
                                        .then( function ( data ) {
-                                               // If token type is not available for this user,
-                                               // key '...token' is either missing or set to boolean false
                                                if ( data.tokens && data.tokens[type + 'token'] ) {
                                                        return data.tokens[type + 'token'];
                                                }
  
+                                               // If token type is not available for this user,
+                                               // key '...token' is either missing or set to boolean false
                                                return $.Deferred().reject( 'token-missing', data );
                                        }, function () {
                                                // Clear promise. Do not cache errors.
                                                delete promiseGroup[ type + 'Token' ];
                                                // Pass on to allow the caller to handle the error
                                                return this;
                                        } )
                        }
  
                        return d;
+               },
+               /**
+                * Indicate that the cached token for a certain action of the API is bad.
+                *
+                * Call this if you get a 'badtoken' error when using the token returned by #getToken.
+                * You may also want to use #postWithToken instead, which invalidates bad cached tokens
+                * automatically.
+                *
+                * @param {string} type Token type
+                * @since 1.26
+                */
+               badToken: function ( type ) {
+                       var promiseGroup = promises[ this.defaults.ajax.url ];
+                       if ( promiseGroup ) {
+                               delete promiseGroup[ type + 'Token' ];
+                       }
                }
        };