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)
CACHE_LIFETIME = 300;
/**
* MediaWiki API gateway factory
*
* @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 {mw.ext.constants } config
* @returns {ext.popups.Gateway}
* @param {String} title
* @return {jQuery.Promise}
*/
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,
/**
* Fetch page data from the API
*
* @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
// so that text does not overflow from the card.
explaintext: true,
// There is an added geometric limit on .mwe-popups-extract
// so that text does not overflow from the card.
explaintext: true,
piprop: 'thumbnail',
pithumbsize: config.THUMBNAIL_SIZE,
rvprop: 'timestamp',
inprop: 'url',
titles: title,
smaxage: CACHE_LIFETIME,
maxage: CACHE_LIFETIME,
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
};
piprop: 'thumbnail',
pithumbsize: config.THUMBNAIL_SIZE,
rvprop: 'timestamp',
inprop: 'url',
titles: title,
smaxage: CACHE_LIFETIME,
maxage: CACHE_LIFETIME,
uselang: 'content'
}, {
headers: {
'X-Analytics': 'preview=1'
}
} );
}
/**
* 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
* @throws {Error} Throw an error if page data cannot be extracted,
* i.e. if the response is empty,
* @returns {Object}
* @param {String} title
* @returns {jQuery.Promise<ext.popups.PreviewModel>}
*/
function extractPageFromResponse( data ) {
if (
data.query &&
data.query.pages &&
data.query.pages.length
) {
return data.query.pages[ 0 ];
}
throw new Error( 'API response `query.pages` is empty.' );
function getPageSummary( title ) {
return fetch( title )
.then( extractPageFromResponse )
.then( convertPageToModel );
}
/**
* Transform the MediaWiki API response to a preview model
*
* @param {Object} page
* @returns {ext.popups.PreviewModel}
*/
function convertPageToModel( page ) {
return mw.popups.preview.createModel(
page.title,
page.canonicalurl,
page.pagelanguagehtmlcode,
page.pagelanguagedir,
page.extract,
page.thumbnail
);
return {
fetch: fetch,
extractPageFromResponse: extractPageFromResponse,
convertPageToModel: convertPageToModel,
getPageSummary: getPageSummary
};
}
/**
* Extract page data from the MediaWiki API response
*
* @param {Object} data API response data
* @throws {Error} Throw an error if page data cannot be extracted,
* 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;