Jdlrobson has uploaded a new change for review. https://gerrit.wikimedia.org/r/237491
Change subject: WIP: Hygiene: SearchApi -> SearchApiGateway ...................................................................... WIP: Hygiene: SearchApi -> SearchApiGateway TODO: * Gather compatibility Change-Id: I4d20f940d54c3ee1cfbaf33237e798f2d0646107 --- M includes/Resources.php R resources/mobile.categories.overlays/CategoryApiGateway.js R resources/mobile.search.api/SearchApiGateway.js D resources/mobile.search.beta.api/SearchApi.js A resources/mobile.search.beta.api/SearchApiGateway.js M resources/mobile.search/SearchOverlay.js M resources/mobile.search/init.js R tests/qunit/mobile.search.beta.api/test_SearchApiGateway.js M tests/qunit/mobile.search/test_SearchApi.js 9 files changed, 109 insertions(+), 96 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend refs/changes/91/237491/1 diff --git a/includes/Resources.php b/includes/Resources.php index ec774b5..dc8a817 100644 --- a/includes/Resources.php +++ b/includes/Resources.php @@ -756,7 +756,7 @@ 'mediawiki.Title', ), 'scripts' => array( - 'resources/mobile.search.api/SearchApi.js', + 'resources/mobile.search.api/SearchApiGateway.js', ), ), @@ -774,7 +774,7 @@ 'mobile.search.api', ), 'scripts' => array( - 'resources/mobile.search.beta.api/SearchApi.js', + 'resources/mobile.search.beta.api/SearchApiGateway.js', ), ), @@ -909,7 +909,7 @@ 'oojs-ui', ), 'scripts' => array( - 'resources/mobile.categories.overlays/CategoryApi.js', + 'resources/mobile.categories.overlays/CategoryApiGateway.js', 'resources/mobile.categories.overlays/CategoryLookupInputWidget.js', 'resources/mobile.categories.overlays/CategoryOverlay.js', 'resources/mobile.categories.overlays/CategoryAddOverlay.js', diff --git a/resources/mobile.categories.overlays/CategoryApi.js b/resources/mobile.categories.overlays/CategoryApiGateway.js similarity index 70% rename from resources/mobile.categories.overlays/CategoryApi.js rename to resources/mobile.categories.overlays/CategoryApiGateway.js index 425526c..bc7c017 100644 --- a/resources/mobile.categories.overlays/CategoryApi.js +++ b/resources/mobile.categories.overlays/CategoryApiGateway.js @@ -1,14 +1,17 @@ -( function ( M ) { +( function ( M, $ ) { - var CategoryApi, - SearchApi = M.require( 'modules/search/SearchApi' ); + var prototype, + SearchApiGateway = M.require( 'mobile.search.api/SearchApiGateway' ); /** * Api for CategoryOverlay - * @class CategoryApi - * @extends SearchApi + * @class CategoryApiGateway + * @extends SearchApiGatewayApi */ - CategoryApi = SearchApi.extend( { + function CategoryApiGateway() { + SearchApiGateway.call( this, arguments ); + } + prototype = { /** * @inheritdoc */ @@ -55,8 +58,11 @@ cllimit: 50 // FIXME: Replace with InfiniteScroll } ); } - } ); + }; + OO.inheritClass( CategoryApiGateway, SearchApiGateway ); + $.extend( CategoryApiGateway.prototype, prototype ); - M.define( 'modules/categories/CategoryApi', CategoryApi ); + M.define( 'mobile.categories.overlays/CategoryApiGateway' ) + .deprecate( 'modules/categories/CategoryApi' ); }( mw.mobileFrontend, jQuery ) ); diff --git a/resources/mobile.search.api/SearchApi.js b/resources/mobile.search.api/SearchApiGateway.js similarity index 91% rename from resources/mobile.search.api/SearchApi.js rename to resources/mobile.search.api/SearchApiGateway.js index 813787a..9fa9e4d 100644 --- a/resources/mobile.search.api/SearchApi.js +++ b/resources/mobile.search.api/SearchApiGateway.js @@ -1,25 +1,16 @@ -/** - * API for search - * @extends Api - * @class SearchApi - */ ( function ( M, $ ) { - - var SearchApi, - Page = M.require( 'Page' ), - Api = M.require( 'api' ).Api; + var Page = M.require( 'Page' ); /** - * @class SearchApi - * @extends Api + * @class SearchApiGateway + * @uses mw.Api */ - SearchApi = Api.extend( { - /** @inheritdoc */ - initialize: function () { - Api.prototype.initialize.apply( this, arguments ); - this.searchCache = {}; - }, + function SearchApiGateway( options ) { + this.api = options.api; + this.searchCache = {}; + } + SearchApiGateway.prototype = { /** * The namespace to search in. * @type {Number} @@ -177,7 +168,7 @@ self = this; if ( !this.isCached( query ) ) { - request = this.get( this.getApiData( query ) ) + request = this.api.get( this.getApiData( query ) ) .done( function ( data ) { // resolve the Deferred object result.resolve( { @@ -212,8 +203,9 @@ isCached: function ( query ) { return Boolean( this.searchCache[ query ] ); } - } ); + }; - M.define( 'modules/search/SearchApi', SearchApi ); + M.define( 'mobile.search.api/SearchApiGateway', SearchApiGateway ) + .deprecate( 'modules/search/SearchApi' ); }( mw.mobileFrontend, jQuery ) ); diff --git a/resources/mobile.search.beta.api/SearchApi.js b/resources/mobile.search.beta.api/SearchApi.js deleted file mode 100644 index 587c5b6..0000000 --- a/resources/mobile.search.beta.api/SearchApi.js +++ /dev/null @@ -1,42 +0,0 @@ -( function ( M ) { - var SearchApi = M.require( 'modules/search/SearchApi' ), - SearchApiBeta; - - /** - * The Api renders pages with Wikidata descriptions - * @class SearchApiBeta - * @extends SearchApi - * @inheritdoc - */ - SearchApiBeta = SearchApi.extend( { - /** - * In addition to the base data, we need to get Wikidata description for the page too - * @inheritdoc - */ - getApiData: function ( query ) { - var data = SearchApi.prototype.getApiData.call( this, query ); - data.prop = data.prop + '|pageterms'; - data.wbptterms = 'description'; - return data; - }, - - /** - * Add wikidataDescription (if it exists) to the page data - * @inheritdoc - * @returns {Object} data needed to create a {Page} - * @private - */ - _getPageData: function ( query, info ) { - var data = SearchApi.prototype._getPageData.call( this, query, info ), - terms = info.terms; - - if ( terms && terms.description && terms.description.length ) { - data.wikidataDescription = terms.description[0]; - } - return data; - } - } ); - - M.define( 'modules/search.beta/SearchApi', SearchApiBeta ); - -}( mw.mobileFrontend ) ); diff --git a/resources/mobile.search.beta.api/SearchApiGateway.js b/resources/mobile.search.beta.api/SearchApiGateway.js new file mode 100644 index 0000000..94b835f --- /dev/null +++ b/resources/mobile.search.beta.api/SearchApiGateway.js @@ -0,0 +1,48 @@ +( function ( M, $ ) { + var prototype, + SearchApiGateway = M.require( 'mobile.search.api/SearchApiGateway' ); + + /** + * The Api renders pages with Wikidata descriptions + * @class SearchApiGatewayBeta + * @extends SearchApiGateway + * @inheritdoc + */ + function SearchApiGatewayBeta() { + SearchApiGateway.call( this, arguments ); + } + OO.inheritClass( SearchApiGatewayBeta, SearchApiGateway ); + prototype = { + /** + * In addition to the base data, we need to get Wikidata description for the page too + * @inheritdoc + */ + getApiData: function ( query ) { + var data = SearchApiGateway.prototype.getApiData.call( this, query ); + data.prop = data.prop + '|pageterms'; + data.wbptterms = 'description'; + return data; + }, + + /** + * Add wikidataDescription (if it exists) to the page data + * @inheritdoc + * @returns {Object} data needed to create a {Page} + * @private + */ + _getPageData: function ( query, info ) { + var data = SearchApiGateway.prototype._getPageData.call( this, query, info ), + terms = info.terms; + + if ( terms && terms.description && terms.description.length ) { + data.wikidataDescription = terms.description[0]; + } + return data; + } + }; + $.extend( SearchApiGatewayBeta.prototype, prototype ); + + M.define( 'mobile.search.api.beta/SearchApiGateway', SearchApiGatewayBeta ) + .deprecate( 'modules/search.beta/SearchApi' ); + +}( mw.mobileFrontend, jQuery ) ); diff --git a/resources/mobile.search/SearchOverlay.js b/resources/mobile.search/SearchOverlay.js index f653dbf..1da4e7f 100644 --- a/resources/mobile.search/SearchOverlay.js +++ b/resources/mobile.search/SearchOverlay.js @@ -15,7 +15,7 @@ * Overlay displaying search results * @class SearchOverlay * @extends Overlay - * @uses SearchApi + * @uses SearchApiGateway * @uses Icon */ SearchOverlay = Overlay.extend( { @@ -28,6 +28,7 @@ /** * @inheritdoc * @cfg {Object} defaults Default options hash. + * @cfg {SearchApiGateway} defaults.gateway An API gateway to use to retrieve search results * @cfg {Object} defaults.clearIcon options for the button that clears the search text. * @cfg {Object} defaults.searchContentIcon options for the button that allows you to search within content * @cfg {String} defaults.searchTerm Search text. @@ -102,8 +103,7 @@ initialize: function ( options ) { var self = this; Overlay.prototype.initialize.call( this, options ); - // use the given api module or use the default SearchApi - this.api = options.api || new M.require( 'modules/search/SearchApi' ); + this.gateway = options.gateway; // FIXME: Remove when search registers route with overlay manager // we need this because of the focus/delay hack in search.js @@ -298,7 +298,7 @@ // it seems the input event can be fired when virtual keyboard is closed // (Chrome for Android) if ( query !== this.lastQuery ) { - this.api.abort(); + this.gateway.api.abort(); clearTimeout( this.timer ); self.$searchContent.hide(); self.$searchFeedback.hide(); @@ -316,7 +316,7 @@ */ M.emit( 'search-start' ); - self.api.search( query ).done( function ( data ) { + self.gateway.search( query ).done( function ( data ) { // check if we're getting the rights response in case of out of // order responses (need to get the current value of the input) if ( data.query === self.$input.val() ) { @@ -348,7 +348,7 @@ } ); } } ); - }, this.api.isCached( query ) ? 0 : SEARCH_DELAY ); + }, this.gateway.isCached( query ) ? 0 : SEARCH_DELAY ); } else { self.$( '.spinner' ).hide(); } diff --git a/resources/mobile.search/init.js b/resources/mobile.search/init.js index b1c87ab..bfdcf76 100644 --- a/resources/mobile.search/init.js +++ b/resources/mobile.search/init.js @@ -4,19 +4,21 @@ uiSchema = new SchemaMobileWebClickTracking( {}, 'MobileWebUIClickTracking' ), context = M.require( 'context' ), router = M.require( 'router' ), + // FIXME: use mw.Api when abort method is on Api + api = M.require( 'api' ), browser = M.require( 'browser' ), moduleConfig = { modules: [ 'mobile.search.api', 'mobile.search' ], - api: 'modules/search/SearchApi', + api: 'mobile.search.api/SearchApiGateway', overlay: 'modules/search/SearchOverlay' }, SearchOverlay, - SearchApi; + SearchApiGateway; if ( context.isBetaGroupMember() ) { moduleConfig = $.extend( moduleConfig, { modules: [ 'mobile.search.beta.api', 'mobile.search.beta' ], - api: 'modules/search.beta/SearchApi' + api: 'mobile.search.api.beta/SearchApiGateway' } ); } @@ -37,11 +39,13 @@ } ); mw.loader.using( moduleConfig.modules ).done( function () { - SearchApi = M.require( moduleConfig.api ); + SearchApiGateway = M.require( moduleConfig.api ); SearchOverlay = M.require( moduleConfig.overlay ); new SearchOverlay( { - api: new SearchApi(), + gateway: new SearchApiGateway( { + api: api + } ), searchTerm: searchTerm, placeholderMsg: placeholder } ).show(); diff --git a/tests/qunit/mobile.search.beta/test_SearchApi.js b/tests/qunit/mobile.search.beta.api/test_SearchApiGateway.js similarity index 82% rename from tests/qunit/mobile.search.beta/test_SearchApi.js rename to tests/qunit/mobile.search.beta.api/test_SearchApiGateway.js index 909f4ce..d645e3f 100644 --- a/tests/qunit/mobile.search.beta/test_SearchApi.js +++ b/tests/qunit/mobile.search.beta.api/test_SearchApiGateway.js @@ -1,8 +1,8 @@ ( function ( $, M ) { - var SearchApi = M.require( 'modules/search.beta/SearchApi' ); + var SearchApiGateway = M.require( 'mobile.search.api/SearchApiGateway' ); - QUnit.module( 'MobileFrontend SearchApi', { + QUnit.module( 'MobileFrontend SearchApiGateway', { setup: function () { var data = { query: { @@ -51,12 +51,14 @@ ] } }; - this.stub( SearchApi.prototype, 'get' ).returns( $.Deferred().resolve( data ) ); + this.sandbox.stub( mw.Api.prototype, 'get' ).returns( $.Deferred().resolve( data ) ); } } ); QUnit.asyncTest( 'Wikidata Description in search results', 3, function ( assert ) { - var searchApi = new SearchApi(); + var searchApi = new SearchApiGateway( { + api: new mw.Api() + } ); searchApi.search( 'brad' ).done( function ( resp ) { var results = resp.results; QUnit.start(); diff --git a/tests/qunit/mobile.search/test_SearchApi.js b/tests/qunit/mobile.search/test_SearchApi.js index a33b84e..6868efc 100644 --- a/tests/qunit/mobile.search/test_SearchApi.js +++ b/tests/qunit/mobile.search/test_SearchApi.js @@ -1,12 +1,13 @@ ( function ( $, M ) { - var SearchApi = M.require( 'modules/search/SearchApi' ), - api; + var SearchApiGateway = M.require( 'mobile.search.api/SearchApiGateway' ); QUnit.module( 'MobileFrontend: SearchApi', { setup: function () { - api = new SearchApi(); - this.sandbox.stub( SearchApi.prototype, 'get', function () { + this.gateway = new SearchApiGateway( { + api: new mw.Api() + } ); + this.sandbox.stub( this.gateway.api, 'get', function () { return $.Deferred().resolve( { warnings: { query: { @@ -70,7 +71,10 @@ } ); QUnit.test( '._highlightSearchTerm', 14, function ( assert ) { - var data = [ + var data, + gateway = this.gateway; + + data = [ [ 'Hello World', 'Hel', '<strong>Hel</strong>lo World' ], [ 'Hello kitty', 'el', 'Hello kitty' ], // not at start [ 'Hello worl', 'hel', '<strong>Hel</strong>lo worl' ], @@ -87,14 +91,13 @@ [ '<script>alert("FAIL")</script> should be safe', '<script>alert("FAIL"', '<strong><script>alert("FAIL"</strong>)</script> should be safe' ] ]; - $.each( data, function ( i, item ) { - assert.strictEqual( api._highlightSearchTerm( item[ 0 ], item[ 1 ] ), item[ 2 ], 'highlightSearchTerm test ' + i ); + assert.strictEqual( gateway._highlightSearchTerm( item[ 0 ], item[ 1 ] ), item[ 2 ], 'highlightSearchTerm test ' + i ); } ); } ); QUnit.test( 'show redirect targets', 6, function ( assert ) { - api.search( 'barack' ).done( function ( response ) { + this.gateway.search( 'barack' ).done( function ( response ) { assert.strictEqual( response.query, 'barack' ); assert.strictEqual( response.results.length, 2 ); assert.strictEqual( response.results[ 0 ].displayTitle, 'Claude Monet' ); -- To view, visit https://gerrit.wikimedia.org/r/237491 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I4d20f940d54c3ee1cfbaf33237e798f2d0646107 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MobileFrontend Gerrit-Branch: master Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits