[MediaWiki-commits] [Gerrit] mediawiki/core[master]: Allow API endpoint customization for mw.widgets.TitleWidget

2016-11-29 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: Allow API endpoint customization for mw.widgets.TitleWidget
..


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
---
M resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js
M resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js
2 files changed, 38 insertions(+), 14 deletions(-)

Approvals:
  Krinkle: Looks good to me, but someone else must approve
  Esanders: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js 
b/resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js
index 39bee7c..2ac75c5 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 e1e50ea..0e5e0c5 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' );
@@ -64,6 +56,10 @@
/* Setup */
 
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( 

[MediaWiki-commits] [Gerrit] mediawiki/core[master]: Allow API endpoint customization for mw.widgets.TitleWidget

2016-11-24 Thread Santhosh (Code Review)
Santhosh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/323502

Change subject: Allow API endpoint customization for mw.widgets.TitleWidget
..

Allow API endpoint customization for mw.widgets.TitleWidget

Currently the API is not customizable in subclasses and always
points to current wiki's api returned by mw.Api().

This patch allows subclasses to override the API so that a subclass
can use a different API endpoint for querying titles. For example,
searching for titles in a different language wikipedia in contexts
such as translation of pages.

Change-Id: I81811cdd1a0750a8335432eee8f971ab9e0b8ee7
---
M resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js
1 file changed, 24 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/02/323502/1

diff --git a/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js 
b/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js
index e1e50ea..cf2cd3c 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
 *
@@ -93,6 +83,18 @@
this.namespace = namespace;
};
 
+   mw.widgets.TitleWidget.prototype.getInterwikiPrefixesPromise = 
function() {
+   return this.getApi().get( {
+   action: 'query',
+   meta: 'siteinfo',
+   siprop: 'interwikimap'
+   } ).then( function ( data ) {
+   return $.map( data.query.interwikimap, function ( 
interwiki ) {
+   return interwiki.prefix;
+   } );
+   } );
+   };
+
/**
 * Get a promise which resolves with an API repsonse for suggested
 * links for the current query.
@@ -108,7 +110,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 +144,11 @@
params.prop.push( 'pageterms' );
params.wbptterms = 
'description';
}
-   req = new mw.Api().get( params );
+   req = widget.getApi().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 = 
widget.getApi().get( { action: 'query', titles: query } );

promiseAbortObject.abort = ret.abort.bind( ret );
}
return ret;
@@ -161,6 +163,15 @@
};
 
/**
+* Get the api for the title requests
+*
+* @return {mw.Api} MediaWiki API
+*/
+   mw.widgets.TitleWidget.prototype.getApi = function () {
+   return new mw.Api();
+   };
+
+   /**
 * Get option widgets from the server response
 *
 * @param {Object} data Query result

-- 
To view, visit https://gerrit.wikimedia.org/r/323502
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I81811cdd1a0750a8335432eee8f971ab9e0b8ee7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Santhosh 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits