mediawiki-extensions-Relate.../resources/ext.relatedArticles.readMore.bootstrap/RelatedPagesGateway.js
Jon Robson e5431a1c0b Limit RelatedArticles feature to ES6 browsers
We currently require support for IntersectionObserver.
which is supported on Edge >= 15 (15 has partial support),
Firefox >55, Chrome >58, Safari 12.1, Opera >=38,
iOS Safari >=12.2, Android 100

Full ES6 is supported in Edge >=15, Firefox >=54, Chrome >=51,
Safari >=10, Opera >=38, iOS Safari >=10, so such a change
would only drop support for Edge 15 and Firefox 54.

CSS.escape is guaranteed in all these browsers according to
caniuse, with the only discrepancy being the Edge browser (versions
16-18) so it is also suggested we remove support for those browsers.

Firefox 54 accounts for 0.0026% of page views
Edge 15-18 accounts for 0.069% of page views

Bug: T306355
Change-Id: Id2987e3456607b610c38da9ee157a026d1d00ada
2022-04-27 15:45:42 +00:00

128 lines
3.8 KiB
JavaScript

/**
* @class RelatedPagesGateway
* @param {mw.Api} api
* @param {string} currentPage the page that the editorCuratedPages relate to
* @param {Array|null} editorCuratedPages a list of pages curated by editors for the current page
* @param {boolean} useCirrusSearch whether to hit the API when no editor-curated pages are available
* @param {boolean} [onlyUseCirrusSearch=false] whether to ignore the list of editor-curated pages
* @param {boolean|string} [descriptionSource=false] source to get the page description from
*/
function RelatedPagesGateway(
api,
currentPage,
editorCuratedPages,
useCirrusSearch,
onlyUseCirrusSearch,
descriptionSource
) {
this.api = api;
this.currentPage = currentPage;
this.useCirrusSearch = useCirrusSearch;
this.descriptionSource = descriptionSource;
if ( onlyUseCirrusSearch ) {
editorCuratedPages = [];
}
this.editorCuratedPages = editorCuratedPages || [];
}
/**
* @ignore
* @param {Object} result
* @return {Array}
*/
function getPages( result ) {
return result && result.query && result.query.pages ? result.query.pages : [];
}
/**
* Gets the related pages for the current page.
*
* If there are related pages assigned to this page using the `related`
* parser function, then they are returned.
*
* If there aren't any related pages assigned to the page, then the
* CirrusSearch extension's {@link https://www.mediawiki.org/wiki/Help:CirrusSearch#morelike: "morelike:" feature}
* is used. If the CirrusSearch extension isn't installed, then the API
* call will fail gracefully and no related pages will be returned.
* Thus the dependency on the CirrusSearch extension is soft.
*
* Related pages will have the following information:
*
* * The ID of the page corresponding to the title
* * The thumbnail, if any
* * The page description, if any
*
* @method
* @param {number} limit of pages to get. Should be between 1-20.
* @return {jQuery.Promise}
*/
RelatedPagesGateway.prototype.getForCurrentPage = function ( limit ) {
const parameters = {
action: 'query',
formatversion: 2,
origin: '*',
prop: 'pageimages',
piprop: 'thumbnail',
pithumbsize: 160 // FIXME: Revert to 80 once pithumbmode is implemented
},
// Enforce limit
relatedPages = this.editorCuratedPages.slice( 0, limit );
switch ( this.descriptionSource ) {
case 'wikidata':
parameters.prop += '|description';
break;
case 'textextracts':
parameters.prop += '|extracts';
parameters.exsentences = '1';
parameters.exintro = '1';
parameters.explaintext = '1';
break;
case 'pagedescription':
parameters.prop += '|pageprops';
parameters.ppprop = 'description';
break;
}
if ( relatedPages.length ) {
parameters.pilimit = relatedPages.length;
parameters.continue = '';
parameters.titles = relatedPages;
} else if ( this.useCirrusSearch ) {
parameters.pilimit = limit;
parameters.generator = 'search';
parameters.gsrsearch = 'morelike:' + this.currentPage;
parameters.gsrnamespace = '0';
parameters.gsrlimit = limit;
parameters.gsrqiprofile = 'classic_noboostlinks';
// Currently, if you're logged in, then the API uses your language by default ard so responses
// are always private i.e. they shouldn't be cached in a shared cache and can be cached by the
// browser.
//
// By make the API use the language of the content rather than that of the user, the API
// reponse is made public, i.e. they can be cached in a shared cache.
//
// See T97096 for more detail and discussion.
parameters.uselang = 'content';
// Instruct shared caches that the response will become stale in 24 hours.
parameters.smaxage = 86400;
// Instruct the browser that the response will become stale in 24 hours.
parameters.maxage = 86400;
} else {
return $.Deferred().resolve( [] );
}
return this.api.get( parameters )
.then( getPages );
};
module.exports = RelatedPagesGateway;