mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/RelatedArticles
synced 2024-12-19 03:20:44 +00:00
e4685f02be
You can't give it a fallback value. This is causing JS errors in production. Bug: T116758 Change-Id: Ia459a42250ca6e030025781005e40204f1ede58e
82 lines
1.8 KiB
JavaScript
82 lines
1.8 KiB
JavaScript
( function ( $ ) {
|
|
|
|
var relatedArticles = mw.config.get( 'wgRelatedArticles' ) || [],
|
|
config = mw.config.get( [ 'skin', 'wgNamespaceNumber', 'wgMFMode', 'wgIsMainPage' ] ),
|
|
module;
|
|
|
|
// Limit number of related articles to 4 (more of them increases likelihood of reader ignoring).
|
|
relatedArticles = relatedArticles.slice( 0, 4 );
|
|
|
|
/**
|
|
* Retrieves the data required to render a card.
|
|
*
|
|
* Given a title, the following additional information is retrieved
|
|
* from the API:
|
|
*
|
|
* * The ID of the page corresponding to the title
|
|
* * The thumbnail, if any
|
|
* * The Wikidata description, if any
|
|
*
|
|
* @private
|
|
*
|
|
* @param {string[]} titles
|
|
* @return {jQuery.Promise}
|
|
*/
|
|
function getData( titles ) {
|
|
var api = new mw.Api();
|
|
|
|
return api.get( {
|
|
action: 'query',
|
|
prop: 'pageimages|pageterms',
|
|
wbptterms: 'description',
|
|
pilimit: titles.length,
|
|
'continue': '',
|
|
|
|
titles: titles
|
|
} ).then( function ( data ) {
|
|
if ( !data.query || !data.query.pages ) {
|
|
return [];
|
|
}
|
|
|
|
return $.map( data.query.pages, function ( page ) {
|
|
var result = {
|
|
id: page.pageid,
|
|
title: page.title,
|
|
thumbnail: page.thumbnail,
|
|
description: undefined
|
|
};
|
|
|
|
if (
|
|
page.terms &&
|
|
page.terms.description &&
|
|
page.terms.description.length > 0
|
|
) {
|
|
result.description = page.terms.description[ 0 ];
|
|
}
|
|
|
|
return result;
|
|
} );
|
|
} );
|
|
}
|
|
|
|
if (
|
|
relatedArticles.length > 0 &&
|
|
config.wgNamespaceNumber === 0 &&
|
|
!config.wgIsMainPage &&
|
|
config.skin === 'minerva' &&
|
|
config.wgMFMode === 'beta'
|
|
) {
|
|
module = 'ext.relatedArticles.readMore.minerva';
|
|
|
|
$( function () {
|
|
$.when(
|
|
mw.loader.using( module ),
|
|
getData( relatedArticles )
|
|
).done( function ( _, data ) {
|
|
mw.track( 'ext.relatedArticles.init', { pages: data } );
|
|
} );
|
|
} );
|
|
}
|
|
|
|
}( jQuery ) );
|