Merge "search: Add wprov to result URLs"

This commit is contained in:
jenkins-bot 2022-10-24 17:40:59 +00:00 committed by Gerrit Code Review
commit 5da5190a9d
5 changed files with 76 additions and 12 deletions

14
package-lock.json generated
View file

@ -17,7 +17,7 @@
"@wikimedia/codex": "0.1.0-alpha.10",
"@wikimedia/codex-icons": "0.1.0-alpha.10",
"@wikimedia/codex-search": "0.1.0-alpha.10",
"@wikimedia/mw-node-qunit": "6.4.0",
"@wikimedia/mw-node-qunit": "6.4.1",
"@wikimedia/types-wikimedia": "0.3.3",
"babel-loader": "8.0.6",
"commander": "9.1.0",
@ -4513,9 +4513,9 @@
}
},
"node_modules/@wikimedia/mw-node-qunit": {
"version": "6.4.0",
"resolved": "https://registry.npmjs.org/@wikimedia/mw-node-qunit/-/mw-node-qunit-6.4.0.tgz",
"integrity": "sha512-WrAPZGsvUTb0LYG1U4S1KP8FB1+zPIYpCG2IDkJ0AyLnjXxM/AsyLFQnG+RLG3aQN3LSfbSECGWyPP4ITQocXQ==",
"version": "6.4.1",
"resolved": "https://registry.npmjs.org/@wikimedia/mw-node-qunit/-/mw-node-qunit-6.4.1.tgz",
"integrity": "sha512-Qdku90cgO7l7anXnYoRghcaxLreqGJUxt/cxL7Rv3F6pugBHioh5dfJDv4MlEo2MD4iNzF52V7pJXRBXQ0aUmQ==",
"dev": true,
"dependencies": {
"eslint-config-wikimedia": "0.21.0",
@ -30619,9 +30619,9 @@
"requires": {}
},
"@wikimedia/mw-node-qunit": {
"version": "6.4.0",
"resolved": "https://registry.npmjs.org/@wikimedia/mw-node-qunit/-/mw-node-qunit-6.4.0.tgz",
"integrity": "sha512-WrAPZGsvUTb0LYG1U4S1KP8FB1+zPIYpCG2IDkJ0AyLnjXxM/AsyLFQnG+RLG3aQN3LSfbSECGWyPP4ITQocXQ==",
"version": "6.4.1",
"resolved": "https://registry.npmjs.org/@wikimedia/mw-node-qunit/-/mw-node-qunit-6.4.1.tgz",
"integrity": "sha512-Qdku90cgO7l7anXnYoRghcaxLreqGJUxt/cxL7Rv3F6pugBHioh5dfJDv4MlEo2MD4iNzF52V7pJXRBXQ0aUmQ==",
"dev": true,
"requires": {
"eslint-config-wikimedia": "0.21.0",

View file

@ -34,7 +34,7 @@
"@wikimedia/codex": "0.1.0-alpha.10",
"@wikimedia/codex-icons": "0.1.0-alpha.10",
"@wikimedia/codex-search": "0.1.0-alpha.10",
"@wikimedia/mw-node-qunit": "6.4.0",
"@wikimedia/mw-node-qunit": "6.4.1",
"@wikimedia/types-wikimedia": "0.3.3",
"babel-loader": "8.0.6",
"commander": "9.1.0",

View file

@ -170,7 +170,7 @@ module.exports = exports = defineComponent( {
restClient.fetchByTitle( query, searchApiUrl, 10, this.showDescription ).fetch
.then( ( data ) => {
this.suggestions = data.results;
this.suggestions = instrumentation.addWprovToSearchResultUrls( data.results );
this.searchFooterUrl = urlGenerator.generateUrl( query );
const event = {

View file

@ -123,6 +123,7 @@ function getWprovFromResultIndex( index ) {
/**
* @typedef {Object} SearchResultPartial
* @property {string} title
* @property {string} [url]
*/
/**
@ -153,11 +154,31 @@ function generateUrl( suggestion, meta ) {
return result.toString();
}
/**
* Return a new list of search results,
* with the `wprov` parameter added to each result's url (if any).
*
* @param {SearchResultPartial[]} results Not modified.
* @return {SearchResultPartial[]}
*/
function addWprovToSearchResultUrls( results ) {
return results.map( ( result, index ) => {
if ( result.url ) {
const uri = new mw.Uri( result.url );
uri.query.wprov = getWprovFromResultIndex( index );
result = Object.assign( {}, result, { url: uri.toString() } );
}
return result;
} );
}
/**
* @typedef {Object} Instrumentation
* @property {Object} listeners
* @property {Function} getWprovFromResultIndex
* @property {Function} generateUrl
* @property {Function} addWprovToSearchResultUrls
*/
/**
@ -185,5 +206,6 @@ module.exports = {
onSubmit: onSuggestionClick
},
getWprovFromResultIndex,
generateUrl
generateUrl,
addWprovToSearchResultUrls
};

View file

@ -17,8 +17,50 @@ describe( 'instrumentation', () => {
] )( 'should generate URL from %s', ( _name, suggestion ) => {
const meta = { index: 1 };
expect( instrumentation.generateUrl( suggestion, meta ) )
// mw-node-qunit provides a pretty weird mw.Uri.toString()...
.toBe( 'https://host?title=suggestion=wprov' );
.toBe( 'https://host/?title=Special%3ASearch&suggestion=title&wprov=acrw11' );
} );
} );
test( 'addWprovToSearchResultUrls', () => {
const url1 = 'https://host/?title=Special%3ASearch&search=Aa',
url2Base = 'https://host/?title=Special%3ASearch&search=Ab',
url3 = 'https://host/Ac';
const results = [
{
title: 'Aa',
url: url1
},
{
title: 'Ab',
url: `${url2Base}&wprov=xyz`
},
{
title: 'Ac',
url: url3
},
{
title: 'Ad'
}
];
expect( instrumentation.addWprovToSearchResultUrls( results ) )
.toStrictEqual( [
{
title: 'Aa',
url: `${url1}&wprov=acrw10`
},
{
title: 'Ab',
url: `${url2Base}&wprov=acrw11`
},
{
title: 'Ac',
url: `${url3}?wprov=acrw12`
},
{
title: 'Ad'
}
] );
expect( results[ 0 ].url ).toStrictEqual( url1 );
} );
} );