2021-05-17 20:34:14 +00:00
|
|
|
const config = require( '../config.json' ),
|
|
|
|
descriptionSource = config.wgCitizenSearchDescriptionSource;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build URL used for fetch request
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @return {string} url
|
|
|
|
*/
|
|
|
|
function getUrl( input ) {
|
|
|
|
const endpoint = config.wgScriptPath + '/api.php?format=json',
|
|
|
|
cacheExpiry = config.wgSearchSuggestCacheExpiry,
|
|
|
|
maxResults = config.wgCitizenMaxSearchResults,
|
|
|
|
query = {
|
|
|
|
action: 'query',
|
|
|
|
smaxage: cacheExpiry,
|
|
|
|
maxage: cacheExpiry,
|
|
|
|
generator: 'prefixsearch',
|
|
|
|
prop: 'pageprops|pageimages',
|
|
|
|
redirects: '',
|
|
|
|
ppprop: 'displaytitle',
|
|
|
|
piprop: 'thumbnail',
|
|
|
|
pithumbsize: 200,
|
|
|
|
pilimit: maxResults,
|
|
|
|
gpssearch: input,
|
|
|
|
gpsnamespace: 0,
|
|
|
|
gpslimit: maxResults
|
|
|
|
};
|
|
|
|
|
|
|
|
switch ( descriptionSource ) {
|
|
|
|
case 'wikidata':
|
|
|
|
query.prop += '|description';
|
|
|
|
break;
|
|
|
|
case 'textextracts':
|
|
|
|
query.prop += '|extracts';
|
|
|
|
query.exchars = '60';
|
|
|
|
query.exintro = '1';
|
|
|
|
query.exlimit = maxResults;
|
|
|
|
query.explaintext = '1';
|
|
|
|
break;
|
|
|
|
case 'pagedescription':
|
|
|
|
query.prop += '|pageprops';
|
|
|
|
query.ppprop = 'description';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let queryString = '';
|
|
|
|
for ( const property in query ) {
|
|
|
|
queryString += '&' + property + '=' + query[ property ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return endpoint + queryString;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map raw response to Results object
|
|
|
|
*
|
|
|
|
* @param {Object} data
|
|
|
|
* @return {Object} Results
|
|
|
|
*/
|
|
|
|
function convertDataToResults( data ) {
|
2022-06-21 20:20:43 +00:00
|
|
|
const getDisplayTitle = ( item ) => {
|
2021-05-17 20:34:14 +00:00
|
|
|
if ( item.pageprops && item.pageprops.displaytitle ) {
|
|
|
|
return item.pageprops.displaytitle;
|
|
|
|
} else {
|
|
|
|
return item.title;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDescription = ( item ) => {
|
|
|
|
switch ( descriptionSource ) {
|
|
|
|
case 'wikidata':
|
|
|
|
return item.description || '';
|
|
|
|
case 'textextracts':
|
|
|
|
return item.extract || '';
|
|
|
|
case 'pagedescription':
|
2021-12-16 03:20:34 +00:00
|
|
|
return item.pageprops.description.slice( 0, 60 ) + '...' || '';
|
2021-05-17 20:34:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const results = [];
|
|
|
|
|
2021-05-18 16:35:05 +00:00
|
|
|
if ( typeof data?.query?.pages === 'undefined' ) {
|
2021-05-18 16:34:08 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-05-17 20:34:14 +00:00
|
|
|
/* eslint-disable-next-line compat/compat, es/no-object-values */
|
|
|
|
data = Object.values( data.query.pages );
|
|
|
|
|
|
|
|
// Sort the data with the index property since it is not in order
|
|
|
|
data.sort( ( a, b ) => {
|
|
|
|
return a.index - b.index;
|
|
|
|
} );
|
|
|
|
|
|
|
|
for ( let i = 0; i < data.length; i++ ) {
|
|
|
|
results[ i ] = {
|
|
|
|
id: data[ i ].pageid,
|
2022-06-21 20:20:43 +00:00
|
|
|
key: data[ i ].title,
|
|
|
|
title: getDisplayTitle( data[ i ] ),
|
2021-05-17 20:34:14 +00:00
|
|
|
description: getDescription( data[ i ] )
|
|
|
|
};
|
|
|
|
if ( data[ i ].thumbnail && data[ i ].thumbnail.source ) {
|
|
|
|
results[ i ].thumbnail = data[ i ].thumbnail.source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getUrl: getUrl,
|
|
|
|
convertDataToResults: convertDataToResults
|
|
|
|
};
|