Henning Snater has uploaded a new change for review. https://gerrit.wikimedia.org/r/158599
Change subject: Revert RepoApi to explicitly query for token ...................................................................... Revert RepoApi to explicitly query for token Instead of using the current wiki's token, RepoApi queries the actual Repo instance for an edit token again for being able to use an RepoApi instance on the client. Change-Id: Id45adfb73fccbea162510ae95e36389bdbe7496f --- M lib/resources/wikibase.RepoApi/wikibase.RepoApi.js 1 file changed, 69 insertions(+), 4 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/99/158599/1 diff --git a/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js b/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js index 3b64720..9924b1b 100644 --- a/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js +++ b/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js @@ -14,8 +14,17 @@ * @since 0.4 (since 0.3 as wb.Api without support for client usage) */ wb.RepoApi = function wbRepoApi() { - var wbRepo = mw.config.get( 'wbRepo' ); - this._repoApiEndpoint = wbRepo.url + wbRepo.scriptPath + '/api.php'; + var localApiEndpoint = mw.config.get( 'wgServer' ) + + mw.config.get( 'wgScriptPath' ) + + '/api.php'; + + this._repoConfig = mw.config.get( 'wbRepo' ); + this._repoApiEndpoint = this._repoConfig.url + this._repoConfig.scriptPath + '/api.php'; + + if( localApiEndpoint === this._repoApiEndpoint ) { + // The current wiki *is* the repo so we can just use user.tokens to get the edit token + this._repoEditToken = mw.user.tokens.get( 'editToken' ); + } }; $.extend( wb.RepoApi.prototype, { @@ -23,6 +32,16 @@ * @type {string} */ _repoApiEndpoint: null, + + /** + * @type {Object} + */ + _repoConfig: null, + + /** + * @type {string} + */ + _repoEditToken: null, /** * mediaWiki.Api object for internal usage. By having this initialized in the prototype, we can @@ -519,7 +538,9 @@ * @throws {Error} If a parameter is not specified properly */ post: function( params ) { - var options = {}; + var self = this, + options = {}, + deferred = $.Deferred(); this._extendRepoCallParams( params, options ); // Unconditionally set the bot parameter to match the UI behaviour of core @@ -531,7 +552,51 @@ } } ); - return this._api.postWithToken( 'edit', params, options ); + this._getRepoEditToken() + .done( function( token ) { + params.token = token; + + self._api.post( params, options ) + .done( deferred.resolve ) + .fail( deferred.reject ); + } ) + .fail( deferred.reject ); + + return deferred.promise(); + }, + + /** + * Retrieves an edit token. + * + * @return {jQuery.Promise} + * Resolved parameters: + * - {string} + * Rejected parameters: + * - {string} + * - {*} + */ + _getRepoEditToken: function() { + var self = this, + deferred = $.Deferred(); + + if( this._repoEditToken ) { + return deferred.resolve( this._repoEditToken ).promise(); + } + + this.get( { + action: 'query', + intoken: 'edit', + titles: 'Main page', + prop: 'info', + indexpageids: 1 + } ) + .done( function( data ) { + self._repoEditToken = data.query.pages[data.query.pageids[0]].edittoken; + deferred.resolve( self._repoEditToken ); + } ) + .fail( deferred.reject ); + + return deferred.promise(); }, /** -- To view, visit https://gerrit.wikimedia.org/r/158599 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id45adfb73fccbea162510ae95e36389bdbe7496f Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Henning Snater <henning.sna...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits