search: Don’t pass searchApiUrl into fetchByTitle()

Not all implementations will need this (e.g. the Wikibase search is
based on the Action API, not the REST API), so simplify App and move
this bit of complexity into the default search client instead.

As far as I can tell from MediaWiki code search [1] and Global Search
[2], nothing apart from Wikibase uses wgVectorSearchClient yet, so we
should be able to make this breaking change now.

[1]: https://codesearch.wmcloud.org/search/?q=wgVectorSearchClient
[2]: https://global-search.toolforge.org/?q=wgVectorSearchClient&namespaces=2%2C4%2C8&title=%28Gadgets-definition%7C.*%5C.js%29

Change-Id: I0d52e407c12b3fbf80cd36ed66c67da4cba9acbd
This commit is contained in:
Lucas Werkmeister 2022-11-25 14:52:19 +01:00
parent 135f0c246e
commit 090500276d
3 changed files with 12 additions and 12 deletions

View file

@ -162,10 +162,7 @@ module.exports = exports = defineComponent( {
* @param {string} value
*/
onInput: function ( value ) {
const searchApiUrl = mw.config.get( 'wgVectorSearchApiUrl',
mw.config.get( 'wgScriptPath' ) + '/rest.php'
),
query = value.trim();
const query = value.trim();
this.currentSearchQuery = query;
@ -177,7 +174,7 @@ module.exports = exports = defineComponent( {
instrumentation.listeners.onFetchStart();
restClient.fetchByTitle( query, searchApiUrl, 10, this.showDescription ).fetch
restClient.fetchByTitle( query, 10, this.showDescription ).fetch
.then( ( data ) => {
// Only use these results if they're still relevant
// If currentSearchQuery !== query, these results are for a previous search

View file

@ -66,8 +66,8 @@ function adaptApiResponse( config, query, restResponse, showDescription ) {
/**
* @callback fetchByTitle
* @param {string} query The search term.
* @param {string} searchApiUrl The URL to rest.php
* @param {number} [limit] Maximum number of results.
* @param {boolean} [showDescription] Whether descriptions should be added to the results.
* @return {AbortableSearchFetch}
*/
@ -85,7 +85,10 @@ function restSearchClient( config ) {
/**
* @type {fetchByTitle}
*/
fetchByTitle: ( q, searchApiUrl, limit = 10, showDescription = true ) => {
fetchByTitle: ( q, limit = 10, showDescription = true ) => {
const searchApiUrl = config.get( 'wgVectorSearchApiUrl',
config.get( 'wgScriptPath' ) + '/rest.php'
);
const params = { q, limit };
const url = searchApiUrl + '/v1/search/title?' + $.param( params );
const result = fetchJson( url, {

View file

@ -11,6 +11,9 @@ const configMock = {
if ( key === 'wgScript' ) {
return '/w/index.php';
}
if ( key === 'wgVectorSearchApiUrl' ) {
return 'https://en.wikipedia.org/w/rest.php';
}
return fallback;
} ),
set: jest.fn()
@ -66,7 +69,6 @@ describe( 'restApiSearchClient', () => {
const searchResult = await restSearchClient( configMock ).fetchByTitle(
'media',
'https://en.wikipedia.org/w/rest.php',
2
).fetch;
@ -98,8 +100,7 @@ describe( 'restApiSearchClient', () => {
fetchMock.mockOnce( JSON.stringify( restResponse ) );
const searchResult = await restSearchClient( configMock ).fetchByTitle(
'thereIsNothingLikeThis',
'https://en.wikipedia.org/w/rest.php'
'thereIsNothingLikeThis'
).fetch;
/* eslint-disable-next-line compat/compat */
@ -122,8 +123,7 @@ describe( 'restApiSearchClient', () => {
fetchMock.mockRejectOnce( new Error( 'failed' ) );
await expect( restSearchClient( configMock ).fetchByTitle(
'anything',
'https://en.wikipedia.org/w/rest.php'
'anything'
).fetch ).rejects.toThrow( 'failed' );
} );
}