diff --git a/resources/dist/index.js b/resources/dist/index.js index 96d6f823b..c6767aef5 100644 Binary files a/resources/dist/index.js and b/resources/dist/index.js differ diff --git a/resources/dist/index.js.map b/resources/dist/index.js.map index 566242172..d323c95e3 100644 Binary files a/resources/dist/index.js.map and b/resources/dist/index.js.map differ diff --git a/src/gateway/mediawiki.js b/src/gateway/mediawiki.js index 4d52f69cc..8578a5dce 100644 --- a/src/gateway/mediawiki.js +++ b/src/gateway/mediawiki.js @@ -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} - */ - 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} */ - 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;