2015-11-03 23:39:48 +00:00
|
|
|
( function ( $ ) {
|
|
|
|
|
|
|
|
// 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 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2015-11-14 00:03:39 +00:00
|
|
|
* @param {number} limit of pages to get
|
2015-11-03 23:39:48 +00:00
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
|
|
|
RelatedPagesGateway.prototype.getForCurrentPage = function ( limit ) {
|
|
|
|
var parameters = {
|
|
|
|
action: 'query',
|
|
|
|
formatversion: 2,
|
|
|
|
prop: 'pageimages|pageterms',
|
|
|
|
piprop: 'thumbnail',
|
|
|
|
pithumbsize: 80,
|
|
|
|
wbptterms: 'description'
|
|
|
|
},
|
2015-11-14 00:03:39 +00:00
|
|
|
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 ) {
|
|
|
|
parameters.pilimit = limit;
|
|
|
|
|
|
|
|
parameters.generator = 'search';
|
|
|
|
parameters.gsrsearch = 'morelike:' + this.currentPage;
|
|
|
|
parameters.gsrnamespace = '0';
|
|
|
|
parameters.gsrlimit = limit;
|
|
|
|
} else {
|
|
|
|
return $.Deferred().resolve( [] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.api.get( parameters )
|
|
|
|
.then( getPages )
|
|
|
|
.then( processPages );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
function getPages( result ) {
|
|
|
|
return result && result.query && result.query.pages ? result.query.pages : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
function processPages( pages ) {
|
|
|
|
return $.map( pages, function ( page ) {
|
|
|
|
var result = {
|
|
|
|
id: page.pageid,
|
|
|
|
isMissing: !page.pageid,
|
|
|
|
title: page.title,
|
|
|
|
thumbnail: page.thumbnail,
|
|
|
|
wikidataDescription: undefined
|
|
|
|
};
|
|
|
|
|
|
|
|
if (
|
|
|
|
page.terms &&
|
|
|
|
page.terms.description &&
|
|
|
|
page.terms.description.length > 0
|
|
|
|
) {
|
|
|
|
result.wikidataDescription = page.terms.description[ 0 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2015-11-14 00:03:39 +00:00
|
|
|
mw.relatedPages.RelatedPagesGateway = RelatedPagesGateway;
|
2015-11-03 23:39:48 +00:00
|
|
|
}( jQuery ) );
|