From 950b292d201eec24a052b6e0687866cbcb71dc95 Mon Sep 17 00:00:00 2001 From: Santhosh Thottingal Date: Fri, 25 Nov 2016 12:32:37 +0530 Subject: [PATCH] Allow API endpoint customization for mw.widgets.TitleWidget Currently the API is not customizable and always points to current wiki's api returned by `new mw.Api()`. This patch allows users to specify their own API object for querying titles e.g. searching for titles on a different language Wikipedia when translating pages. Change-Id: I81811cdd1a0750a8335432eee8f971ab9e0b8ee7 --- .../mw.widgets.SearchInputWidget.js | 2 +- .../mw.widgets.TitleWidget.js | 50 ++++++++++++++----- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js b/resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js index 39bee7c119..2ac75c59f2 100755 --- a/resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js +++ b/resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js @@ -78,7 +78,7 @@ * @inheritdoc mw.widgets.TitleWidget */ mw.widgets.SearchInputWidget.prototype.getSuggestionsPromise = function () { - var api = new mw.Api(), + var api = this.getApi(), promise, self = this; diff --git a/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js b/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js index e1e50ea17a..0e5e0c5ecf 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js +++ b/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js @@ -6,16 +6,6 @@ */ ( function ( $, mw ) { - var interwikiPrefixesPromise = new mw.Api().get( { - action: 'query', - meta: 'siteinfo', - siprop: 'interwikimap' - } ).then( function ( data ) { - return $.map( data.query.interwikimap, function ( interwiki ) { - return interwiki.prefix; - } ); - } ); - /** * Mixin for title widgets * @@ -36,6 +26,7 @@ * @cfg {boolean} [validateTitle=true] Whether the input must be a valid title (if set to true, * the widget will marks itself red for invalid inputs, including an empty query). * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument + * @cfg {mw.Api} [api] API object to use, creates a default mw.Api instance if not specified */ mw.widgets.TitleWidget = function MwWidgetsTitleWidget( config ) { // Config initialization @@ -56,6 +47,7 @@ this.excludeCurrentPage = !!config.excludeCurrentPage; this.validateTitle = config.validateTitle !== undefined ? config.validateTitle : true; this.cache = config.cache; + this.api = config.api || new mw.Api(); // Initialization this.$element.addClass( 'mw-widget-titleWidget' ); @@ -65,6 +57,10 @@ OO.initClass( mw.widgets.TitleWidget ); + /* Static properties */ + + mw.widgets.TitleWidget.static.interwikiPrefixesPromiseCache = {}; + /* Methods */ /** @@ -93,6 +89,24 @@ this.namespace = namespace; }; + mw.widgets.TitleWidget.prototype.getInterwikiPrefixesPromise = function () { + var api = this.getApi(), + cache = this.constructor.static.interwikiPrefixesPromiseCache, + key = api.defaults.ajax.url; + if ( !cache.hasOwnProperty( key ) ) { + cache[ key ] = api.get( { + action: 'query', + meta: 'siteinfo', + siprop: 'interwikimap' + } ).then( function ( data ) { + return $.map( data.query.interwikimap, function ( interwiki ) { + return interwiki.prefix; + } ); + } ); + } + return cache[ key ]; + }; + /** * Get a promise which resolves with an API repsonse for suggested * links for the current query. @@ -101,6 +115,7 @@ */ mw.widgets.TitleWidget.prototype.getSuggestionsPromise = function () { var req, + api = this.getApi(), query = this.getQueryValue(), widget = this, promiseAbortObject = { abort: function () { @@ -108,7 +123,7 @@ } }; if ( mw.Title.newFromText( query ) ) { - return interwikiPrefixesPromise.then( function ( interwikiPrefixes ) { + return this.getInterwikiPrefixesPromise().then( function ( interwikiPrefixes ) { var params, interwiki = query.substring( 0, query.indexOf( ':' ) ); if ( @@ -142,11 +157,11 @@ params.prop.push( 'pageterms' ); params.wbptterms = 'description'; } - req = new mw.Api().get( params ); + req = api.get( params ); promiseAbortObject.abort = req.abort.bind( req ); // TODO ew return req.then( function ( ret ) { if ( ret.query === undefined ) { - ret = new mw.Api().get( { action: 'query', titles: query } ); + ret = api.get( { action: 'query', titles: query } ); promiseAbortObject.abort = ret.abort.bind( ret ); } return ret; @@ -160,6 +175,15 @@ } }; + /** + * Get the API object for title requests + * + * @return {mw.Api} MediaWiki API + */ + mw.widgets.TitleWidget.prototype.getApi = function () { + return this.api; + }; + /** * Get option widgets from the server response * -- 2.20.1