Hygiene: Remove unnecessary IIFE in gateway/mediawiki.js

Change-Id: If6d76c2915b13f9871dea3b7f33cb74ec5906566
This commit is contained in:
joakin 2017-02-14 21:16:55 +01:00
parent 0260325bb9
commit f1e6e2bfa1
3 changed files with 89 additions and 92 deletions

Binary file not shown.

Binary file not shown.

View file

@ -1,109 +1,106 @@
( function ( mw ) { var EXTRACT_LENGTH = 525,
// Public and private cache lifetime (5 minutes)
CACHE_LIFETIME = 300,
createModel = require( '../preview/model' ).createModel;
var EXTRACT_LENGTH = 525, /**
// Public and private cache lifetime (5 minutes) * MediaWiki API gateway factory
CACHE_LIFETIME = 300; *
* @param {mw.Api} api
* @param {mw.ext.constants} config
* @returns {ext.popups.Gateway}
*/
function createMediaWikiApiGateway( api, config ) {
/** /**
* MediaWiki API gateway factory * Fetch page data from the API
* *
* @param {mw.Api} api * @param {String} title
* @param {mw.ext.constants } config * @return {jQuery.Promise}
* @returns {ext.popups.Gateway}
*/ */
function createMediaWikiApiGateway( api, config ) { function fetch( title ) {
return api.get( {
action: 'query',
prop: 'info|extracts|pageimages|revisions|info',
formatversion: 2,
redirects: true,
exintro: true,
exchars: EXTRACT_LENGTH,
/** // There is an added geometric limit on .mwe-popups-extract
* Fetch page data from the API // so that text does not overflow from the card.
* explaintext: true,
* @param {String} title
* @return {jQuery.Promise}
*/
function fetch( title ) {
return api.get( {
action: 'query',
prop: 'info|extracts|pageimages|revisions|info',
formatversion: 2,
redirects: true,
exintro: true,
exchars: EXTRACT_LENGTH,
// There is an added geometric limit on .mwe-popups-extract piprop: 'thumbnail',
// so that text does not overflow from the card. pithumbsize: config.THUMBNAIL_SIZE,
explaintext: true, rvprop: 'timestamp',
inprop: 'url',
piprop: 'thumbnail', titles: title,
pithumbsize: config.THUMBNAIL_SIZE, smaxage: CACHE_LIFETIME,
rvprop: 'timestamp', maxage: CACHE_LIFETIME,
inprop: 'url', uselang: 'content'
titles: title, }, {
smaxage: CACHE_LIFETIME, headers: {
maxage: CACHE_LIFETIME, 'X-Analytics': 'preview=1'
uselang: 'content' }
}, { } );
headers: {
'X-Analytics': 'preview=1'
}
} );
}
/**
* Get the page summary from the api and transform the data
*
* @param {String} title
* @returns {jQuery.Promise<ext.popups.PreviewModel>}
*/
function getPageSummary( title ) {
return fetch( title )
.then( extractPageFromResponse )
.then( convertPageToModel );
}
return {
fetch: fetch,
extractPageFromResponse: extractPageFromResponse,
convertPageToModel: convertPageToModel,
getPageSummary: getPageSummary
};
} }
/** /**
* Extract page data from the MediaWiki API response * Get the page summary from the api and transform the data
* *
* @param {Object} data API response data * @param {String} title
* @throws {Error} Throw an error if page data cannot be extracted, * @returns {jQuery.Promise<ext.popups.PreviewModel>}
* i.e. if the response is empty,
* @returns {Object}
*/ */
function extractPageFromResponse( data ) { function getPageSummary( title ) {
if ( return fetch( title )
data.query && .then( extractPageFromResponse )
data.query.pages && .then( convertPageToModel );
data.query.pages.length
) {
return data.query.pages[ 0 ];
}
throw new Error( 'API response `query.pages` is empty.' );
} }
/** return {
* Transform the MediaWiki API response to a preview model fetch: fetch,
* extractPageFromResponse: extractPageFromResponse,
* @param {Object} page convertPageToModel: convertPageToModel,
* @returns {ext.popups.PreviewModel} getPageSummary: getPageSummary
*/ };
function convertPageToModel( page ) { }
return mw.popups.preview.createModel(
page.title, /**
page.canonicalurl, * Extract page data from the MediaWiki API response
page.pagelanguagehtmlcode, *
page.pagelanguagedir, * @param {Object} data API response data
page.extract, * @throws {Error} Throw an error if page data cannot be extracted,
page.thumbnail * i.e. if the response is empty,
); * @returns {Object}
*/
function extractPageFromResponse( data ) {
if (
data.query &&
data.query.pages &&
data.query.pages.length
) {
return data.query.pages[ 0 ];
} }
module.exports = createMediaWikiApiGateway; throw new Error( 'API response `query.pages` is empty.' );
}
}( mediaWiki ) ); /**
* Transform the MediaWiki API response to a preview model
*
* @param {Object} page
* @returns {ext.popups.PreviewModel}
*/
function convertPageToModel( page ) {
return createModel(
page.title,
page.canonicalurl,
page.pagelanguagehtmlcode,
page.pagelanguagedir,
page.extract,
page.thumbnail
);
}
module.exports = createMediaWikiApiGateway;