2015-12-07 18:19:16 +00:00
|
|
|
( function ( $, mw ) {
|
2015-11-03 23:39:48 +00:00
|
|
|
|
|
|
|
// FIXME: Move into separate file as this module becomes larger.
|
2015-11-14 00:03:39 +00:00
|
|
|
mw.relatedPages = {};
|
2015-11-03 23:39:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class RelatedPagesGateway
|
|
|
|
* @param {mw.Api} api
|
2015-11-14 00:03:39 +00:00
|
|
|
* @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
|
2015-11-03 23:39:48 +00:00
|
|
|
*/
|
2015-11-11 15:30:19 +00:00
|
|
|
function RelatedPagesGateway(
|
|
|
|
api,
|
|
|
|
currentPage,
|
2015-11-14 00:03:39 +00:00
|
|
|
editorCuratedPages,
|
2015-11-11 15:30:19 +00:00
|
|
|
useCirrusSearch,
|
|
|
|
onlyUseCirrusSearch
|
|
|
|
) {
|
2015-11-03 23:39:48 +00:00
|
|
|
this.api = api;
|
|
|
|
this.currentPage = currentPage;
|
|
|
|
this.useCirrusSearch = useCirrusSearch;
|
2015-11-11 15:30:19 +00:00
|
|
|
|
|
|
|
if ( onlyUseCirrusSearch ) {
|
2015-11-14 00:03:39 +00:00
|
|
|
editorCuratedPages = [];
|
2015-11-11 15:30:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 00:03:39 +00:00
|
|
|
this.editorCuratedPages = editorCuratedPages || [];
|
2015-11-11 15:30:19 +00:00
|
|
|
|
2015-11-03 23:39:48 +00:00
|
|
|
}
|
|
|
|
OO.initClass( RelatedPagesGateway );
|
|
|
|
|
2017-04-07 20:21:12 +00:00
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @param {Object} result
|
|
|
|
* @return {Array}
|
|
|
|
*/
|
|
|
|
function getPages( result ) {
|
|
|
|
return result && result.query && result.query.pages ? result.query.pages : [];
|
|
|
|
}
|
|
|
|
|
2015-11-03 23:39:48 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2017-05-25 18:02:33 +00:00
|
|
|
* @param {number} limit of pages to get. Should be between 1-20.
|
2015-11-03 23:39:48 +00:00
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
|
|
|
RelatedPagesGateway.prototype.getForCurrentPage = function ( limit ) {
|
2017-07-14 23:15:35 +00:00
|
|
|
var parameters = {
|
2015-11-03 23:39:48 +00:00
|
|
|
action: 'query',
|
|
|
|
formatversion: 2,
|
|
|
|
prop: 'pageimages|pageterms',
|
|
|
|
piprop: 'thumbnail',
|
2016-04-08 20:13:47 +00:00
|
|
|
pithumbsize: 160, // FIXME: Revert to 80 once pithumbmode is implemented
|
2015-11-03 23:39:48 +00:00
|
|
|
wbptterms: 'description'
|
|
|
|
},
|
2017-05-25 18:02:33 +00:00
|
|
|
// Enforce limit
|
|
|
|
relatedPages = this.editorCuratedPages.slice( 0, limit );
|
2015-11-03 23:39:48 +00:00
|
|
|
|
2015-11-14 00:03:39 +00:00
|
|
|
if ( relatedPages.length ) {
|
|
|
|
parameters.pilimit = relatedPages.length;
|
2015-11-03 23:39:48 +00:00
|
|
|
parameters[ 'continue' ] = ''; // jscs:ignore requireDotNotation
|
|
|
|
|
2015-11-14 00:03:39 +00:00
|
|
|
parameters.titles = relatedPages;
|
2015-11-03 23:39:48 +00:00
|
|
|
} else if ( this.useCirrusSearch ) {
|
2017-07-14 23:15:35 +00:00
|
|
|
parameters.pilimit = limit;
|
2015-11-03 23:39:48 +00:00
|
|
|
|
|
|
|
parameters.generator = 'search';
|
|
|
|
parameters.gsrsearch = 'morelike:' + this.currentPage;
|
|
|
|
parameters.gsrnamespace = '0';
|
2017-07-14 23:15:35 +00:00
|
|
|
parameters.gsrlimit = limit;
|
2016-08-29 19:38:09 +00:00
|
|
|
parameters.gsrqiprofile = 'classic_noboostlinks';
|
2016-09-02 09:31:43 +00:00
|
|
|
|
|
|
|
// 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;
|
2015-11-03 23:39:48 +00:00
|
|
|
} else {
|
|
|
|
return $.Deferred().resolve( [] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.api.get( parameters )
|
2015-11-16 10:40:03 +00:00
|
|
|
.then( getPages );
|
2015-11-03 23:39:48 +00:00
|
|
|
};
|
|
|
|
|
2015-11-14 00:03:39 +00:00
|
|
|
mw.relatedPages.RelatedPagesGateway = RelatedPagesGateway;
|
2015-12-07 18:19:16 +00:00
|
|
|
}( jQuery, mediaWiki ) );
|