mw.widgets.TitleWidget: Don't allow interwiki links by default
authorEd Sanders <esanders@wikimedia.org>
Wed, 8 May 2019 18:27:41 +0000 (13:27 -0500)
committerEd Sanders <esanders@wikimedia.org>
Wed, 8 May 2019 18:27:41 +0000 (13:27 -0500)
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

resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js

index 6f4c72c..22bac08 100644 (file)
@@ -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;
        };
 
        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',
                }
 
                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 );
        };