Adrian Lang has uploaded a new change for review. https://gerrit.wikimedia.org/r/145281
Change subject: Inject config into RepoApi into AbstractedRepoApi ...................................................................... Inject config into RepoApi into AbstractedRepoApi Change-Id: Ibae5bfca6f5d3b72257a5a97ecaf19b01ed42a15 --- M client/resources/wikibase.client.linkitem.init.js M lib/WikibaseLib.hooks.php M lib/resources/Resources.php M lib/resources/wikibase.RepoApi/wikibase.RepoApi.js M lib/tests/qunit/wikibase.RepoApi/wikibase.RepoApi.tests.js M repo/resources/Resources.php M repo/resources/wikibase.ui.entityViewInit.js 7 files changed, 54 insertions(+), 42 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/81/145281/1 diff --git a/client/resources/wikibase.client.linkitem.init.js b/client/resources/wikibase.client.linkitem.init.js index 9e2a8e0..b7b2a70 100644 --- a/client/resources/wikibase.client.linkitem.init.js +++ b/client/resources/wikibase.client.linkitem.init.js @@ -19,7 +19,9 @@ mw.loader.using( [ 'jquery.wikibase.linkitem', + 'mediawiki.api', 'mediawiki.Title', + 'user.tokens', 'mw.config.values.wbRepo', 'wikibase.RepoApi', ], @@ -29,10 +31,28 @@ $spinner.remove(); var repoConfig = mw.config.get( 'wbRepo' ); + var repoScriptUrl = repoConfig.url + repoConfig.scriptPath; + var localScriptUrl = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ); + var repoIsLocal = repoScriptUrl === localScriptUrl; + var originForCors = null; + + if( !repoIsLocal ) { + originForCors = mw.config.get( 'wgServer' ); + if ( originForCors.indexOf( '//' ) === 0 ) { + // The origin parameter musn't be protocol relative + originForCors = document.location.protocol + originForCors; + } + } + $linkItemLink .show() .linkitem( { - repoApi: new wb.RepoApi(), + repoApi: new wb.RepoApi( + new mw.Api(), + repoScriptUrl + '/api.php', + repoIsLocal && mw.user.tokens.get( 'editToken' ), + originForCors + ), pageTitle: ( new mw.Title( mw.config.get( 'wgTitle' ), mw.config.get( 'wgNamespaceNumber' ) diff --git a/lib/WikibaseLib.hooks.php b/lib/WikibaseLib.hooks.php index fda088c..47a2080 100644 --- a/lib/WikibaseLib.hooks.php +++ b/lib/WikibaseLib.hooks.php @@ -118,6 +118,7 @@ 'tests/qunit/jquery.wikibase/toolbar/toolbareditgroup.tests.js', ), 'dependencies' => array( + 'mediawiki.api', 'wikibase.tests.qunit.testrunner', 'wikibase', 'wikibase.AbstractedRepoApi', diff --git a/lib/resources/Resources.php b/lib/resources/Resources.php index d35b2a4..3d24b59 100644 --- a/lib/resources/Resources.php +++ b/lib/resources/Resources.php @@ -157,10 +157,7 @@ ), 'dependencies' => array( 'json', - 'user.tokens', - 'mediawiki.api', 'mediawiki', - 'mw.config.values.wbRepo', 'wikibase', ) ), diff --git a/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js b/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js index f729e7f..6613f3e 100644 --- a/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js +++ b/lib/resources/wikibase.RepoApi/wikibase.RepoApi.js @@ -18,31 +18,28 @@ * * @constructor * @since 0.4 (since 0.3 as wb.Api without support for client usage) + * + * @param {mw.Api} api + * @param {string} apiEndpoint + * @param {string} [editToken] + * @param {string} [corsOrigin] CORS origin parameter; Won't perform a CORS request if not set. */ -wb.RepoApi = function wbRepoApi() { - 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' ); - } +wb.RepoApi = function wbRepoApi( api, apiEndpoint, editToken, corsOrigin ) { + this._api = api; + this._corsOrigin = corsOrigin; + this._repoApiEndpoint = apiEndpoint; + this._repoEditToken = editToken; }; $.extend( wb.RepoApi.prototype, { - - _repoApiEndpoint: null, - _repoConfig: null, - _repoEditToken: null, - /** - * mediaWiki.Api object for internal usage. By having this initialized in the prototype, we can - * share one instance for all instances of the wikibase API. * @type mw.Api */ - _api: new mw.Api(), + _api: null, + + _corsOrigin: null, + _repoApiEndpoint: null, + _repoEditToken: null, /** * Creates a new entity with the given type and data. @@ -479,12 +476,9 @@ * @param {object} options AJAX options */ _extendRepoCallParams: function( params, options ) { - var localServerRaw = mw.config.get( 'wgServer' ).replace( /.*\/\//, '' ), - repoServerRaw = mw.config.get( 'wbRepo' ).url.replace( /.*\/\//, '' ).replace( /\/.*/, '' ); - options.url = this._repoApiEndpoint; - if ( localServerRaw === repoServerRaw ) { + if ( !this._corsOrigin ) { // We don't need/ want CORS when on the same domain return; } @@ -494,13 +488,7 @@ }; options.crossDomain = true; - var currentServer = mw.config.get( 'wgServer' ); - if ( currentServer.indexOf( '//' ) === 0 ) { - // The origin parameter musn't be protocol relative - currentServer = document.location.protocol + currentServer; - } - - params.origin = currentServer; + params.origin = this._corsOrigin; }, /** diff --git a/lib/tests/qunit/wikibase.RepoApi/wikibase.RepoApi.tests.js b/lib/tests/qunit/wikibase.RepoApi/wikibase.RepoApi.tests.js index 067a6de..7d31433 100644 --- a/lib/tests/qunit/wikibase.RepoApi/wikibase.RepoApi.tests.js +++ b/lib/tests/qunit/wikibase.RepoApi/wikibase.RepoApi.tests.js @@ -6,14 +6,14 @@ * @author H. Snater < mediaw...@snater.com > */ -( function( wb, $, QUnit, undefined ) { +( function( mw, wb, $, QUnit, undefined ) { 'use strict'; /** * wb.RepoApi object * @var {Object} */ - var api = new wb.RepoApi(); + var api = new wb.RepoApi( new mw.Api() ); /** * wb.AbstractedRepoApi object @@ -553,4 +553,4 @@ } ); -}( wikibase, jQuery, QUnit ) ); +}( mediaWiki, wikibase, jQuery, QUnit ) ); diff --git a/repo/resources/Resources.php b/repo/resources/Resources.php index 5c63310..8e6c9b4 100644 --- a/repo/resources/Resources.php +++ b/repo/resources/Resources.php @@ -26,12 +26,14 @@ 'dependencies' => array( 'mediawiki.api', 'mediawiki.user', + 'mw.config.value.wbRepo', 'wikibase.ui.PropertyEditTool', 'jquery.wikibase.entityview', 'jquery.wikibase.toolbarcontroller', 'jquery.wikibase.wbtooltip', 'jquery.cookie', 'jquery.wikibase.claimgrouplabelscroll', + 'user.tokens', 'wikibase.AbstractedRepoApi', 'wikibase.dataTypes', 'wikibase.experts', diff --git a/repo/resources/wikibase.ui.entityViewInit.js b/repo/resources/wikibase.ui.entityViewInit.js index 4c5df77..26eccd4 100644 --- a/repo/resources/wikibase.ui.entityViewInit.js +++ b/repo/resources/wikibase.ui.entityViewInit.js @@ -32,7 +32,13 @@ // adjustments are necessary. mw.hook( 'wikibase.domready' ).fire(); - var repoApi = new wb.RepoApi(); + var mwApi = new mw.Api(); + var repoConfig = mw.config.get( 'wbRepo' ); + var repoApi = new wb.RepoApi( + mwApi, + repoConfig.url + repoConfig.scriptPath + '/api.php', + mw.user.tokens.get( 'editToken' ) + ); // add an edit tool for the main label. This will be integrated into the heading nicely: var $firstHeading = $( '.wb-firstHeading' ); @@ -261,13 +267,12 @@ if ( mw.user.isAnon() ) { $.cookie( cookieKey, copyRightVersion, { 'expires': 365 * 3, 'path': '/' } ); } else { - var api = new mw.Api(); - api.get( { + mwApi.get( { 'action': 'tokens', 'type': 'options' }, function( data ) { if ( data.tokens && data.tokens.optionstoken ) { - api.post( { + mwApi.post( { 'action': 'options', 'token': data.tokens.optionstoken, 'optionname': optionsKey, @@ -308,9 +313,8 @@ // Note: The exposed function fails for empty jQuery collections if ( $link.length ) { updateWatchLink( $link, 'watch', 'loading' ); - var api = new mw.Api(); var pageid = mw.config.get( 'wgArticleId' ); - api.get( { + mwApi.get( { 'action': 'query', 'prop': 'info', 'inprop': 'watched', -- To view, visit https://gerrit.wikimedia.org/r/145281 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibae5bfca6f5d3b72257a5a97ecaf19b01ed42a15 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Adrian Lang <adrian.l...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits