From a4e29de89f857469f70b003446be6ae5d8332c47 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Wed, 8 May 2019 13:27:41 -0500 Subject: [PATCH] mw.widgets.TitleWidget: Don't allow interwiki links by default Technically this is a breaking change, but the only user of this widget that wants to support interwikis in the VE link inspector, and that is currently broken anyway due to a Parsoid bug. Bug: T222831 Change-Id: I3b8fca39a668e657b66a7836d4ba59183eceb4b4 --- .../mw.widgets.TitleWidget.js | 61 ++++++++++++------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js b/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js index 6f4c72c501..22bac085a7 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js +++ b/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js @@ -24,6 +24,7 @@ * @cfg {boolean} [showImages] Show page images * @cfg {boolean} [showDescriptions] Show page descriptions * @cfg {boolean} [showMissing=true] Show missing pages + * @cfg {boolean} [showInterwikis=false] Show pages with a valid interwiki prefix * @cfg {boolean} [addQueryInput=true] Add exact user's input query to results * @cfg {boolean} [excludeCurrentPage] Exclude the current page from suggestions * @cfg {boolean} [excludeDynamicNamespaces] Exclude pages whose namespace is negative @@ -50,6 +51,7 @@ this.showImages = !!config.showImages; this.showDescriptions = !!config.showDescriptions; this.showMissing = config.showMissing !== false; + this.showInterwikis = !!config.showInterwikis; this.addQueryInput = config.addQueryInput !== false; this.excludeCurrentPage = !!config.excludeCurrentPage; this.excludeDynamicNamespaces = !!config.excludeDynamicNamespaces; @@ -103,9 +105,16 @@ }; mw.widgets.TitleWidget.prototype.getInterwikiPrefixesPromise = function () { - var api = this.getApi(), - cache = this.constructor.static.interwikiPrefixesPromiseCache, - key = api.defaults.ajax.url; + var api, cache, key; + + if ( !this.showInterwikis ) { + return $.Deferred().resolve( [] ).promise(); + } + + api = this.getApi(); + cache = this.constructor.static.interwikiPrefixesPromiseCache; + key = api.defaults.ajax.url; + if ( !Object.prototype.hasOwnProperty.call( cache, key ) ) { cache[ key ] = api.get( { action: 'query', @@ -147,27 +156,33 @@ } return this.getInterwikiPrefixesPromise().then( function ( interwikiPrefixes ) { - var interwiki = query.substring( 0, query.indexOf( ':' ) ); - if ( - interwiki && interwiki !== '' && - interwikiPrefixes.indexOf( interwiki ) !== -1 - ) { - return $.Deferred().resolve( { query: { - pages: [ { - title: query - } ] - } } ).promise( promiseAbortObject ); - } else { - req = api.get( widget.getApiParams( query ) ); - promiseAbortObject.abort = req.abort.bind( req ); // TODO ew - return req.then( function ( ret ) { - if ( widget.showMissing && ret.query === undefined ) { - ret = api.get( { action: 'query', titles: query } ); - promiseAbortObject.abort = ret.abort.bind( ret ); - } - return ret; - } ); + var interwiki; + // Optimization: check we have any prefixes. + if ( interwikiPrefixes.length ) { + interwiki = query.substring( 0, query.indexOf( ':' ) ); + if ( + interwiki && interwiki !== '' && + interwikiPrefixes.indexOf( interwiki ) !== -1 + ) { + // Interwiki prefix is valid: return the original query as a valid title + // NB: This doesn't check if the title actually exists on the other wiki + return $.Deferred().resolve( { query: { + pages: [ { + title: query + } ] + } } ).promise( promiseAbortObject ); + } } + // Not a interwiki: do an API lookup of the query + req = api.get( widget.getApiParams( query ) ); + promiseAbortObject.abort = req.abort.bind( req ); // TODO ew + return req.then( function ( ret ) { + if ( widget.showMissing && ret.query === undefined ) { + ret = api.get( { action: 'query', titles: query } ); + promiseAbortObject.abort = ret.abort.bind( ret ); + } + return ret; + } ); } ).promise( promiseAbortObject ); }; -- 2.20.1