mediawiki-extensions-Relate.../resources/ext.relatedArticles.readMore/index.js
Erik Bernhardson ad1e1e448c Add a wprov parameter for measuring impact
We would like to know how many users are using this related articles
endpoint. To accomplish define a new wprov parameter (on wikitech)
and use it here. wprov is specifically defined to avoid caching
issues, this should be safe to attach to any page view to track
that page views provenance.

Bug: T358351
Change-Id: I58b450eb632a721a39b627cc34f30eb26e67be59
2024-04-10 11:44:56 -07:00

88 lines
2 KiB
JavaScript

// eslint-disable-next-line spaced-comment
/// <reference path="./codex.ts" />
const RelatedArticles = require( './RelatedArticles.js' );
const data = require( './data.json' );
const RelatedPagesGateway = require( './RelatedPagesGateway.js' );
const relatedPages = new RelatedPagesGateway(
new mw.Api( {
ajax: {
url: data.searchUrl
}
} ),
mw.config.get( 'wgPageName' ),
mw.config.get( 'wgRelatedArticles' ),
data.useCirrusSearch,
data.onlyUseCirrusSearch,
data.descriptionSource
);
const LIMIT = mw.config.get( 'wgRelatedArticlesCardLimit', 3 );
/**
* Generates suggestion objects from pages
*
* @param {MwApiPageObject[]} pages
* @return {Codex.ListTitleObject[]}
*/
function getCards( pages ) {
return pages.map( function ( page ) {
const result = {
id: page.title,
label: page.title,
url: mw.util.getUrl( page.title, {
wprov: 'rarw1'
} ),
thumbnail: page.thumbnail ? {
width: page.thumbnail.width,
height: page.thumbnail.height,
url: page.thumbnail.source
} : undefined,
description: ( page.description || page.extract ||
( page.pageprops ? page.pageprops.description : '' ) )
};
return result;
} );
}
/**
* @param {MwApiPageObject[]} pages
* @param {Element} el
* @param {string} heading
* @param {boolean} isContainerSmall
*/
function render( pages, el, heading, isContainerSmall ) {
el.innerHTML = RelatedArticles( {
isContainerSmall,
heading,
cards: getCards( pages )
} );
}
/**
* @param {HTMLElement} container to initialize into
*/
function init( container ) {
relatedPages.getForCurrentPage( LIMIT ).then( ( /** @type {MwApiPageObject[]} */ pages ) => {
if ( pages.length ) {
render(
pages,
container,
mw.msg( 'relatedarticles-read-more-heading' ),
// Take up multiple columns if possible
false
);
} else if ( container.parentNode ) {
container.parentNode.removeChild( container );
}
} );
}
module.exports = {
init,
render,
getCards,
test: {
relatedPages
}
};