mediawiki-extensions-Relate.../resources/ext.relatedArticles.readMore.gateway/RelatedPagesGateway.js
jdlrobson 9126c7d0ef Make number of RelatedArticles configurable
Wikivoyage pages have more than 3 related articles as they make use
of the {{#related:}}  magic word.

After speaking with Nirzar we should allow this project to show more than 3.
This change allows this while keeping the existing behaviour on other wikis and
will pave the way for removing a bunch of code from this extension.

Additional changes:
* Cleanup skinStyles definitions
* Limit cards to 30% maximum width and give margin top to account for
situations where the number of cards are multiple of 3
** In Minerva hardcode the max-width to pixels.
* Margins are switched from hardcoded 10px to percentage based. Yes
this changes the right margin slightly but is more maintable and visually
the same.

Bug: T164765
Change-Id: I41119de3228c2df799f740d4bd00082101c21b97
2017-07-06 11:17:47 -07:00

118 lines
3.7 KiB
JavaScript

( function ( $, mw ) {
// FIXME: Move into separate file as this module becomes larger.
mw.relatedPages = {};
/**
* @class RelatedPagesGateway
* @param {mw.Api} api
* @param {string} currentPage the page that the editorCuratedPages relate to
* @param {Array} 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
*/
function RelatedPagesGateway(
api,
currentPage,
editorCuratedPages,
useCirrusSearch,
onlyUseCirrusSearch
) {
this.api = api;
this.currentPage = currentPage;
this.useCirrusSearch = useCirrusSearch;
if ( onlyUseCirrusSearch ) {
editorCuratedPages = [];
}
this.editorCuratedPages = editorCuratedPages || [];
}
OO.initClass( RelatedPagesGateway );
/**
* @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 Wikidata description, if any
*
* @method
* @param {number} limit of pages to get. Should be between 1-20.
* @return {jQuery.Promise}
*/
RelatedPagesGateway.prototype.getForCurrentPage = function ( limit ) {
var apiLimit,
parameters = {
action: 'query',
formatversion: 2,
prop: 'pageimages|pageterms',
piprop: 'thumbnail',
pithumbsize: 160, // FIXME: Revert to 80 once pithumbmode is implemented
wbptterms: 'description'
},
// Enforce limit
relatedPages = this.editorCuratedPages.slice( 0, limit );
if ( relatedPages.length ) {
parameters.pilimit = relatedPages.length;
parameters[ 'continue' ] = ''; // jscs:ignore requireDotNotation
parameters.titles = relatedPages;
} else if ( this.useCirrusSearch ) {
apiLimit = limit;
parameters.pilimit = apiLimit;
parameters.generator = 'search';
parameters.gsrsearch = 'morelike:' + this.currentPage;
parameters.gsrnamespace = '0';
parameters.gsrlimit = apiLimit;
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 );
};
mw.relatedPages.RelatedPagesGateway = RelatedPagesGateway;
}( jQuery, mediaWiki ) );