2023-05-18 17:57:43 +00:00
|
|
|
const config = require( '../config.json' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build URL used for fetch request
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @return {string} url
|
|
|
|
*/
|
|
|
|
function getUrl( input ) {
|
|
|
|
const endpoint = config.wgScriptPath + '/api.php?format=json',
|
|
|
|
maxResults = config.wgCitizenMaxSearchResults,
|
2023-05-18 17:58:40 +00:00
|
|
|
askQueryTemplate = config.wgCitizenSearchSmwAskApiQueryTemplate;
|
2023-05-18 17:57:43 +00:00
|
|
|
|
|
|
|
let askQuery = '';
|
|
|
|
|
|
|
|
if ( input.includes( ':' ) ) {
|
2023-05-18 17:58:40 +00:00
|
|
|
let namespace = input.split( ':' )[ 0 ];
|
2023-07-07 22:00:14 +00:00
|
|
|
if ( namespace === 'Category' ) {
|
|
|
|
namespace = ':' + namespace;
|
|
|
|
}
|
2023-05-18 17:58:40 +00:00
|
|
|
input = input.split( ':' )[ 1 ];
|
2023-05-18 17:57:43 +00:00
|
|
|
askQuery += '[[' + namespace + ':+]]';
|
|
|
|
}
|
2023-05-18 17:58:40 +00:00
|
|
|
|
2023-07-07 22:00:14 +00:00
|
|
|
/* eslint-disable-next-line es-x/no-string-prototype-replaceall */
|
2023-05-18 17:57:43 +00:00
|
|
|
askQuery += askQueryTemplate.replaceAll( '${input}', input );
|
|
|
|
askQuery += '|limit=' + maxResults;
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
action: 'ask',
|
2023-05-18 17:58:40 +00:00
|
|
|
query: encodeURIComponent( askQuery )
|
2023-05-18 17:57:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 ) {
|
|
|
|
const userLang = mw.config.get( 'wgUserLanguage' );
|
|
|
|
|
|
|
|
const getDisplayTitle = ( item ) => {
|
2023-05-18 17:58:40 +00:00
|
|
|
if ( item.printouts.displaytitle && item.printouts.displaytitle.length &&
|
|
|
|
item.printouts.displaytitle[ 0 ][ 'Language code' ] && item.printouts.displaytitle[ 0 ].Text.item.length ) {
|
2023-05-18 17:57:43 +00:00
|
|
|
// multi-lang string preference: user lang => English => first result
|
2023-05-18 17:58:40 +00:00
|
|
|
let textEN = '';
|
|
|
|
let textResult = '';
|
2023-05-18 17:57:43 +00:00
|
|
|
for ( const text of item.printouts.displaytitle ) {
|
2023-07-07 22:00:14 +00:00
|
|
|
if ( text[ 'Language code' ].item[ 0 ] === userLang ) {
|
|
|
|
textResult = text.Text.item[ 0 ];
|
|
|
|
}
|
|
|
|
if ( text[ 'Language code' ].item[ 0 ] === 'en' ) {
|
|
|
|
textEN = text.Text.item[ 0 ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( textResult === '' ) {
|
|
|
|
textResult = textEN;
|
|
|
|
}
|
|
|
|
if ( textResult === '' ) {
|
|
|
|
textResult = item.printouts.displaytitle[ 0 ].Text.item[ 0 ];
|
2023-05-18 17:57:43 +00:00
|
|
|
}
|
|
|
|
return textResult;
|
|
|
|
} else if ( item.printouts.displaytitle && item.printouts.displaytitle.length ) {
|
2023-05-18 17:58:40 +00:00
|
|
|
return item.printouts.displaytitle[ 0 ];
|
|
|
|
} else if ( item.displaytitle && item.displaytitle !== '' ) {
|
2023-05-18 17:57:43 +00:00
|
|
|
return item.displaytitle;
|
2023-05-18 17:58:40 +00:00
|
|
|
} else { return item.fulltext; }
|
2023-05-18 17:57:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const getDescription = ( item ) => {
|
2023-05-18 17:58:40 +00:00
|
|
|
if ( item.printouts.desc && item.printouts.desc.length &&
|
|
|
|
item.printouts.desc[ 0 ][ 'Language code' ] && item.printouts.desc[ 0 ].Text.item.length ) {
|
2023-05-18 17:57:43 +00:00
|
|
|
// multi-lang string preference: user lang => English => first result
|
2023-05-18 17:58:40 +00:00
|
|
|
let textEN = '';
|
|
|
|
let textResult = '';
|
2023-05-18 17:57:43 +00:00
|
|
|
for ( const text of item.printouts.desc ) {
|
2023-07-07 22:00:14 +00:00
|
|
|
if ( text[ 'Language code' ].item[ 0 ] === userLang ) {
|
|
|
|
textResult = text.Text.item[ 0 ];
|
|
|
|
}
|
|
|
|
if ( text[ 'Language code' ].item[ 0 ] === 'en' ) {
|
|
|
|
textEN = text.Text.item[ 0 ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( textResult === '' ) {
|
|
|
|
textResult = textEN;
|
|
|
|
}
|
|
|
|
if ( textResult === '' ) {
|
|
|
|
textResult = item.printouts.desc[ 0 ].Text.item[ 0 ];
|
2023-05-18 17:57:43 +00:00
|
|
|
}
|
|
|
|
return textResult;
|
|
|
|
} else if ( item.printouts.desc && item.printouts.desc.length ) {
|
2023-05-18 17:58:40 +00:00
|
|
|
return item.printouts.desc[ 0 ];
|
|
|
|
} else { return ''; }
|
2023-05-18 17:57:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const getThumbnail = ( item ) => {
|
|
|
|
if ( item.printouts.thumbnail && item.printouts.thumbnail.length ) {
|
2023-07-07 22:00:14 +00:00
|
|
|
const imgTitle = item.printouts.thumbnail[ 0 ].fulltext;
|
|
|
|
return config.wgScriptPath + '/index.php?title=Special:Redirect/file/' + imgTitle + '&width=200&height=200';
|
2023-05-18 17:58:40 +00:00
|
|
|
} else { return undefined; }
|
2023-05-18 17:57:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const results = [];
|
|
|
|
|
|
|
|
data = Object.values( data.query.results );
|
|
|
|
|
|
|
|
for ( let i = 0; i < data.length; i++ ) {
|
|
|
|
results[ i ] = {
|
|
|
|
id: i,
|
|
|
|
key: data[ i ].fulltext,
|
|
|
|
title: getDisplayTitle( data[ i ] ),
|
|
|
|
desc: getDescription( data[ i ] ),
|
2023-05-18 17:58:40 +00:00
|
|
|
thumbnail: getThumbnail( data[ i ] )
|
2023-05-18 17:57:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getUrl: getUrl,
|
|
|
|
convertDataToResults: convertDataToResults
|
|
|
|
};
|