2022-01-24 22:46:47 +00:00
|
|
|
/* global FetchEndEvent, SuggestionClickEvent, SearchSubmitEvent */
|
2020-11-24 22:16:03 +00:00
|
|
|
/**
|
|
|
|
* The value of the `inputLocation` property of any and all SearchSatisfaction events sent by the
|
|
|
|
* corresponding instrumentation.
|
|
|
|
*
|
|
|
|
* @see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/skins/Vector/+/refs/heads/master/includes/Constants.php
|
|
|
|
*/
|
2022-01-31 21:43:32 +00:00
|
|
|
const INPUT_LOCATION_MOVED = 'header-moved',
|
2020-12-01 22:41:44 +00:00
|
|
|
wgScript = mw.config.get( 'wgScript' ),
|
|
|
|
// T251544: Collect search performance metrics to compare Vue search with
|
|
|
|
// mediawiki.searchSuggest performance. Marks and Measures will only be
|
|
|
|
// recorded on the Vector skin and only if browser supported.
|
|
|
|
shouldTestSearchPerformance = !!( window.performance &&
|
2022-01-24 22:46:47 +00:00
|
|
|
// @ts-ignore
|
2020-12-01 22:41:44 +00:00
|
|
|
window.requestAnimationFrame &&
|
2021-05-05 20:37:35 +00:00
|
|
|
/* eslint-disable compat/compat */
|
2022-01-24 22:46:47 +00:00
|
|
|
// @ts-ignore
|
2020-12-01 22:41:44 +00:00
|
|
|
performance.mark &&
|
2022-01-24 22:46:47 +00:00
|
|
|
// @ts-ignore
|
2020-12-01 22:41:44 +00:00
|
|
|
performance.measure &&
|
2022-01-24 22:46:47 +00:00
|
|
|
// @ts-ignore
|
2020-12-01 22:41:44 +00:00
|
|
|
performance.getEntriesByName &&
|
|
|
|
performance.clearMarks ),
|
2021-05-05 20:37:35 +00:00
|
|
|
/* eslint-enable compat/compat */
|
2020-12-01 22:41:44 +00:00
|
|
|
loadStartMark = 'mwVectorVueSearchLoadStart',
|
|
|
|
queryMark = 'mwVectorVueSearchQuery',
|
|
|
|
renderMark = 'mwVectorVueSearchRender',
|
|
|
|
queryToRenderMeasure = 'mwVectorVueSearchQueryToRender',
|
|
|
|
loadStartToFirstRenderMeasure = 'mwVectorVueSearchLoadStartToFirstRender';
|
|
|
|
|
|
|
|
function onFetchStart() {
|
|
|
|
if ( !shouldTestSearchPerformance ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear past marks that are no longer relevant. This likely means that the
|
|
|
|
// search request failed or was cancelled. Whatever the reason, the mark
|
|
|
|
// is no longer needed since we are only interested in collecting the time
|
|
|
|
// from query to render.
|
|
|
|
if ( performance.getEntriesByName( queryMark ).length ) {
|
|
|
|
performance.clearMarks( queryMark );
|
|
|
|
}
|
|
|
|
|
2021-05-05 20:37:35 +00:00
|
|
|
/* eslint-disable-next-line compat/compat */
|
2020-12-01 22:41:44 +00:00
|
|
|
performance.mark( queryMark );
|
|
|
|
}
|
2020-11-24 22:16:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {FetchEndEvent} event
|
|
|
|
*/
|
|
|
|
function onFetchEnd( event ) {
|
|
|
|
mw.track( 'mediawiki.searchSuggest', {
|
|
|
|
action: 'impression-results',
|
|
|
|
numberOfResults: event.numberOfResults,
|
|
|
|
// resultSetType: '',
|
|
|
|
// searchId: '',
|
|
|
|
query: event.query,
|
|
|
|
inputLocation: INPUT_LOCATION_MOVED
|
|
|
|
} );
|
2020-12-01 22:41:44 +00:00
|
|
|
|
|
|
|
if ( shouldTestSearchPerformance ) {
|
|
|
|
// Schedule the mark after the search results have rendered and are
|
|
|
|
// visible to the user. Two rAF's are needed for this since rAF will
|
|
|
|
// execute before the rendering steps happen (e.g. layout and paint). A
|
|
|
|
// nested rAF will execute after these rendering steps have completed
|
|
|
|
// and ensure the search results are visible to the user.
|
|
|
|
requestAnimationFrame( function () {
|
|
|
|
requestAnimationFrame( function () {
|
|
|
|
if ( !performance.getEntriesByName( queryMark ).length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
performance.mark( renderMark );
|
|
|
|
performance.measure( queryToRenderMeasure, queryMark, renderMark );
|
|
|
|
|
|
|
|
// Measure from the start of the lazy load to the first render if we
|
|
|
|
// haven't already captured that info.
|
|
|
|
if ( performance.getEntriesByName( loadStartMark ).length &&
|
|
|
|
!performance.getEntriesByName( loadStartToFirstRenderMeasure ).length ) {
|
|
|
|
performance.measure( loadStartToFirstRenderMeasure, loadStartMark, renderMark );
|
|
|
|
}
|
|
|
|
|
|
|
|
// The measures are the most meaningful info so we remove the marks
|
|
|
|
// after we have the measure.
|
|
|
|
performance.clearMarks( queryMark );
|
|
|
|
performance.clearMarks( renderMark );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
2020-11-24 22:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-24 22:46:47 +00:00
|
|
|
* @param {SuggestionClickEvent|SearchSubmitEvent} event
|
2020-11-24 22:16:03 +00:00
|
|
|
*/
|
|
|
|
function onSuggestionClick( event ) {
|
|
|
|
mw.track( 'mediawiki.searchSuggest', {
|
|
|
|
action: 'click-result',
|
|
|
|
numberOfResults: event.numberOfResults,
|
|
|
|
index: event.index
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the value of the `wprov` parameter to be used in the URL of a search result and the
|
|
|
|
* `wprov` hidden input.
|
|
|
|
*
|
|
|
|
* See https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/WikimediaEvents/+/refs/heads/master/modules/ext.wikimediaEvents/searchSatisfaction.js
|
|
|
|
* and also the top of that file for additional detail about the shape of the parameter.
|
|
|
|
*
|
|
|
|
* @param {number} index
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function getWprovFromResultIndex( index ) {
|
|
|
|
|
|
|
|
// If the user hasn't highlighted an autocomplete result.
|
|
|
|
if ( index === -1 ) {
|
|
|
|
return 'acrw1';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'acrw1' + index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} SearchResultPartial
|
|
|
|
* @property {string} title
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} GenerateUrlMeta
|
|
|
|
* @property {number} index
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-02-01 20:52:16 +00:00
|
|
|
* Used by the Vue-enhanced search component to generate URLs for the search results. Adds a
|
2020-11-24 22:16:03 +00:00
|
|
|
* `wprov` paramater to the URL to satisfy the SearchSatisfaction instrumentation.
|
|
|
|
*
|
|
|
|
* @see getWprovFromResultIndex
|
|
|
|
*
|
|
|
|
* @param {SearchResultPartial|string} suggestion
|
|
|
|
* @param {GenerateUrlMeta} meta
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function generateUrl( suggestion, meta ) {
|
2022-01-31 21:43:32 +00:00
|
|
|
const result = new mw.Uri( wgScript );
|
2020-11-24 22:16:03 +00:00
|
|
|
|
|
|
|
if ( typeof suggestion !== 'string' ) {
|
|
|
|
suggestion = suggestion.title;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.query.title = 'Special:Search';
|
|
|
|
result.query.suggestion = suggestion;
|
|
|
|
result.query.wprov = getWprovFromResultIndex( meta.index );
|
|
|
|
|
|
|
|
return result.toString();
|
|
|
|
}
|
2022-02-01 20:52:16 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} Instrumentation
|
|
|
|
* @property {Object} listeners
|
|
|
|
* @property {Function} getWprovFromResultIndex
|
|
|
|
* @property {Function} generateUrl
|
|
|
|
*/
|
2020-11-24 22:16:03 +00:00
|
|
|
|
2022-02-01 20:52:16 +00:00
|
|
|
/**
|
|
|
|
* @type {Instrumentation}
|
|
|
|
*/
|
2020-11-24 22:16:03 +00:00
|
|
|
module.exports = {
|
|
|
|
listeners: {
|
2022-01-31 21:43:32 +00:00
|
|
|
onFetchStart,
|
|
|
|
onFetchEnd,
|
|
|
|
onSuggestionClick,
|
2020-11-24 22:16:03 +00:00
|
|
|
|
|
|
|
// As of writing (2020/12/08), both the "click-result" and "submit-form" kind of
|
|
|
|
// mediawiki.searchSuggestion events result in a "click" SearchSatisfaction event being
|
|
|
|
// logged [0]. However, when processing the "submit-form" kind of mediawiki.searchSuggestion
|
|
|
|
// event, the SearchSatisfaction instrument will modify the DOM, adding a hidden input
|
|
|
|
// element, in order to set the appropriate provenance parameter (see [1] for additional
|
|
|
|
// detail).
|
|
|
|
//
|
|
|
|
// In this implementation of the mediawiki.searchSuggestion protocol, we don't want to
|
|
|
|
// trigger the above behavior as we're using Vue.js, which doesn't expect the DOM to be
|
|
|
|
// modified underneath it.
|
|
|
|
//
|
|
|
|
// [0] https://gerrit.wikimedia.org/g/mediawiki/extensions/WikimediaEvents/+/df97aa9c9407507e8c48827666beeab492fd56a8/modules/ext.wikimediaEvents/searchSatisfaction.js#735
|
|
|
|
// [1] https://phabricator.wikimedia.org/T257698#6416826
|
|
|
|
onSubmit: onSuggestionClick
|
|
|
|
},
|
2022-01-31 21:43:32 +00:00
|
|
|
getWprovFromResultIndex,
|
|
|
|
generateUrl
|
2020-11-24 22:16:03 +00:00
|
|
|
};
|