From 5693594bd1b218a2155b72783944df925ccd98a1 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Wed, 5 Oct 2022 17:38:38 -0700 Subject: [PATCH] Remove $wgVectorSearchHost, replace with $wgVectorSearchApiUrl This allows the URL to the other wiki's rest.php to be configured exactly, rather than assuming that it has the same wgScriptPath as the current wiki. This is necessary to make this feature work on PatchDemo, where wgScriptPath looks like '/123abc456/w'. $wgVectorSearchHost is removed, since nothing uses it except PatchDemo (where it's broken) and development setups. Bug: T319494 Change-Id: Ife042f4f683d366a31a642723746d4aa80774c03 --- includes/Hooks.php | 2 +- resources/VectorResourceLoaderVirtualConfig.d.ts | 2 +- resources/skins.vector.js/searchLoader.js | 6 +++--- resources/skins.vector.search/App.vue | 6 ++++-- .../skins.vector.search/restSearchClient.js | 6 +++--- skin.json | 4 ++-- tests/jest/restSearchClient.test.js | 10 +++++----- tests/phpunit/integration/VectorHooksTest.php | 16 ++++++++-------- 8 files changed, 27 insertions(+), 25 deletions(-) diff --git a/includes/Hooks.php b/includes/Hooks.php index ab5890af..e81c3923 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -98,7 +98,7 @@ class Hooks implements Config $config ) { return [ - 'wgVectorSearchHost' => $config->get( 'VectorSearchHost' ), + 'wgVectorSearchApiUrl' => $config->get( 'VectorSearchApiUrl' ), 'wgVectorWebABTestEnrollment' => self::getActiveABTest( $config ), ]; } diff --git a/resources/VectorResourceLoaderVirtualConfig.d.ts b/resources/VectorResourceLoaderVirtualConfig.d.ts index 33b7bc3c..7c3570f9 100644 --- a/resources/VectorResourceLoaderVirtualConfig.d.ts +++ b/resources/VectorResourceLoaderVirtualConfig.d.ts @@ -1,4 +1,4 @@ /** See Vector\Hooks::getVectorResourceLoaderConfig */ interface VectorResourceLoaderVirtualConfig { - wgVectorSearchHost: string; + wgVectorSearchApiUrl: string; } diff --git a/resources/skins.vector.js/searchLoader.js b/resources/skins.vector.js/searchLoader.js index 78392b35..e5c4d8cb 100644 --- a/resources/skins.vector.js/searchLoader.js +++ b/resources/skins.vector.js/searchLoader.js @@ -141,9 +141,9 @@ function initSearchLoader( document ) { var searchBoxes = document.querySelectorAll( '.vector-search-box' ), isWikidata = mw.config.get( 'wgWikiID' ) === 'wikidatawiki'; - // Allow developers to defined $wgVectorSearchHost in LocalSettings to target different APIs - if ( config.wgVectorSearchHost ) { - mw.config.set( 'wgVectorSearchHost', config.wgVectorSearchHost ); + // Allow developers to defined $wgVectorSearchApiUrl in LocalSettings to target different APIs + if ( config.wgVectorSearchApiUrl ) { + mw.config.set( 'wgVectorSearchApiUrl', config.wgVectorSearchApiUrl ); } if ( !searchBoxes.length ) { diff --git a/resources/skins.vector.search/App.vue b/resources/skins.vector.search/App.vue index 3bffb5c5..fa21ace6 100644 --- a/resources/skins.vector.search/App.vue +++ b/resources/skins.vector.search/App.vue @@ -155,7 +155,9 @@ module.exports = exports = defineComponent( { * @param {string} value */ onInput: function ( value ) { - const domain = mw.config.get( 'wgVectorSearchHost', location.host ), + const searchApiUrl = mw.config.get( 'wgVectorSearchApiUrl', + mw.config.get( 'wgScriptPath' ) + '/rest.php' + ), query = value.trim(); if ( query === '' ) { @@ -166,7 +168,7 @@ module.exports = exports = defineComponent( { instrumentation.listeners.onFetchStart(); - restClient.fetchByTitle( query, domain, 10, this.showDescription ).fetch + restClient.fetchByTitle( query, searchApiUrl, 10, this.showDescription ).fetch .then( ( data ) => { this.suggestions = data.results; this.searchFooterUrl = urlGenerator.generateUrl( query ); diff --git a/resources/skins.vector.search/restSearchClient.js b/resources/skins.vector.search/restSearchClient.js index ce5067b4..6e5e03b0 100644 --- a/resources/skins.vector.search/restSearchClient.js +++ b/resources/skins.vector.search/restSearchClient.js @@ -66,7 +66,7 @@ function adaptApiResponse( config, query, restResponse, showDescription ) { /** * @callback fetchByTitle * @param {string} query The search term. - * @param {string} domain The base URL for the wiki without protocol. Example: 'sr.wikipedia.org'. + * @param {string} searchApiUrl The URL to rest.php * @param {number} [limit] Maximum number of results. * @return {AbortableSearchFetch} */ @@ -85,9 +85,9 @@ function restSearchClient( config ) { /** * @type {fetchByTitle} */ - fetchByTitle: ( q, domain, limit = 10, showDescription = true ) => { + fetchByTitle: ( q, searchApiUrl, limit = 10, showDescription = true ) => { const params = { q, limit }; - const url = '//' + domain + config.get( 'wgScriptPath' ) + '/rest.php/v1/search/title?' + $.param( params ); + const url = searchApiUrl + '/v1/search/title?' + $.param( params ); const result = fetchJson( url, { headers: { accept: 'application/json' diff --git a/skin.json b/skin.json index 751b31a5..3cdbba9b 100644 --- a/skin.json +++ b/skin.json @@ -473,9 +473,9 @@ "value": [], "description": "Temporary configuration flag for allowing users to preview the new Vector skin." }, - "VectorSearchHost": { + "VectorSearchApiUrl": { "value": "", - "description": "Override default search API. Can be used with $wgDisableTextSearch and $wgSearchForwardUrl to mimic user experience on production." + "description": "To override the default search API, set this to the URL to rest.php on another wiki. Can be used with $wgDisableTextSearch and $wgSearchForwardUrl to mimic user experience on production." }, "VectorUseIconWatch": { "value": true diff --git a/tests/jest/restSearchClient.test.js b/tests/jest/restSearchClient.test.js index 1e5dba19..295e72dc 100644 --- a/tests/jest/restSearchClient.test.js +++ b/tests/jest/restSearchClient.test.js @@ -66,7 +66,7 @@ describe( 'restApiSearchClient', () => { const searchResult = await restSearchClient( configMock ).fetchByTitle( 'media', - 'en.wikipedia.org', + 'https://en.wikipedia.org/w/rest.php', 2 ).fetch; @@ -87,7 +87,7 @@ describe( 'restApiSearchClient', () => { if ( mockedRequests ) { expect( fetchMock ).toHaveBeenCalledTimes( 1 ); expect( fetchMock ).toHaveBeenCalledWith( - '//en.wikipedia.org/w/rest.php/v1/search/title?q=media&limit=2', + 'https://en.wikipedia.org/w/rest.php/v1/search/title?q=media&limit=2', { headers: { accept: 'application/json' }, signal: controller.signal } ); } @@ -99,7 +99,7 @@ describe( 'restApiSearchClient', () => { const searchResult = await restSearchClient( configMock ).fetchByTitle( 'thereIsNothingLikeThis', - 'en.wikipedia.org' + 'https://en.wikipedia.org/w/rest.php' ).fetch; /* eslint-disable-next-line compat/compat */ @@ -111,7 +111,7 @@ describe( 'restApiSearchClient', () => { if ( mockedRequests ) { expect( fetchMock ).toHaveBeenCalledTimes( 1 ); expect( fetchMock ).toHaveBeenCalledWith( - '//en.wikipedia.org/w/rest.php/v1/search/title?q=thereIsNothingLikeThis&limit=10', + 'https://en.wikipedia.org/w/rest.php/v1/search/title?q=thereIsNothingLikeThis&limit=10', { headers: { accept: 'application/json' }, signal: controller.signal } ); } @@ -123,7 +123,7 @@ describe( 'restApiSearchClient', () => { await expect( restSearchClient( configMock ).fetchByTitle( 'anything', - 'en.wikipedia.org' + 'https://en.wikipedia.org/w/rest.php' ).fetch ).rejects.toThrow( 'failed' ); } ); } diff --git a/tests/phpunit/integration/VectorHooksTest.php b/tests/phpunit/integration/VectorHooksTest.php index f4cd9334..1485f7e2 100644 --- a/tests/phpunit/integration/VectorHooksTest.php +++ b/tests/phpunit/integration/VectorHooksTest.php @@ -56,10 +56,10 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase { [ [ 'VectorWebABTestEnrollment' => [], - 'VectorSearchHost' => 'en.wikipedia.org' + 'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php' ], [ - 'wgVectorSearchHost' => 'en.wikipedia.org', + 'wgVectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php', 'wgVectorWebABTestEnrollment' => [], ] ], @@ -83,10 +83,10 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase { ], ], ], - 'VectorSearchHost' => 'en.wikipedia.org' + 'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php' ], [ - 'wgVectorSearchHost' => 'en.wikipedia.org', + 'wgVectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php', 'wgVectorWebABTestEnrollment' => [ 'name' => 'vector.sticky_header', 'enabled' => true, @@ -115,7 +115,7 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase { # Bad experiment (no buckets) [ [ - 'VectorSearchHost' => 'en.wikipedia.org', + 'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php', 'VectorWebABTestEnrollment' => [ 'name' => 'vector.sticky_header', 'enabled' => true, @@ -125,7 +125,7 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase { # Bad experiment (no unsampled bucket) [ [ - 'VectorSearchHost' => 'en.wikipedia.org', + 'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php', 'VectorWebABTestEnrollment' => [ 'name' => 'vector.sticky_header', 'enabled' => true, @@ -140,7 +140,7 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase { # Bad experiment (wrong format) [ [ - 'VectorSearchHost' => 'en.wikipedia.org', + 'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php', 'VectorWebABTestEnrollment' => [ 'name' => 'vector.sticky_header', 'enabled' => true, @@ -153,7 +153,7 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase { # Bad experiment (samplingRate defined as string) [ [ - 'VectorSearchHost' => 'en.wikipedia.org', + 'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php', 'VectorWebABTestEnrollment' => [ 'name' => 'vector.sticky_header', 'enabled' => true,