2014-12-16 01:06:05 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MediaWiki Initialization ApiResponseCache class.
|
|
|
|
*
|
2018-01-03 00:54:47 +00:00
|
|
|
* @copyright 2011-2018 VisualEditor Team and others; see AUTHORS.txt
|
2014-12-16 01:06:05 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
2015-04-30 16:03:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaWiki API batch queue.
|
|
|
|
*
|
|
|
|
* Used to queue up lists of items centrally to get information about in batches
|
|
|
|
* of requests.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.EventEmitter
|
|
|
|
* @constructor
|
2018-05-05 14:13:15 +00:00
|
|
|
* @param {mw.Api} [api] API object to use. Defaults to new mw.Api()
|
2015-04-30 16:03:30 +00:00
|
|
|
*/
|
2018-05-05 14:13:15 +00:00
|
|
|
ve.init.mw.ApiResponseCache = function VeInitMwApiResponseCache( api ) {
|
2015-04-30 16:03:30 +00:00
|
|
|
// Mixin constructor
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
2018-05-05 14:13:15 +00:00
|
|
|
this.api = api || new mw.Api();
|
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
// Keys are titles, values are deferreds
|
|
|
|
this.deferreds = {};
|
|
|
|
|
|
|
|
// Keys are page names, values are link data objects
|
|
|
|
// This is kept for synchronous retrieval of cached values via #getCached
|
|
|
|
this.cacheValues = {};
|
|
|
|
|
|
|
|
// Array of page titles queued to be looked up
|
|
|
|
this.queue = [];
|
|
|
|
|
|
|
|
this.schedule = ve.debounce( this.processQueue.bind( this ), 0 );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.mixinClass( ve.init.mw.ApiResponseCache, OO.EventEmitter );
|
|
|
|
|
|
|
|
/* Static methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process each page in the response of an API request
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @param {Object} page The page object
|
|
|
|
* @return {Object|undefined} Any relevant info that we want to cache and return.
|
|
|
|
*/
|
|
|
|
ve.init.mw.ApiResponseCache.static.processPage = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize the title of the response
|
|
|
|
*
|
|
|
|
* @param {string} title Title
|
|
|
|
* @return {string} Normalized title
|
|
|
|
*/
|
|
|
|
ve.init.mw.ApiResponseCache.static.normalizeTitle = function ( title ) {
|
|
|
|
var titleObj = mw.Title.newFromText( title );
|
|
|
|
if ( !titleObj ) {
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
return titleObj.getPrefixedText();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up data about a title. If the data about this title is already in the cache, this
|
|
|
|
* returns an already-resolved promise. Otherwise, it returns a pending promise and schedules
|
|
|
|
* an request to retrieve the data.
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2015-04-30 16:03:30 +00:00
|
|
|
* @param {string} title Title
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {jQuery.Promise} Promise that will be resolved with the data once it's available
|
2015-04-30 16:03:30 +00:00
|
|
|
*/
|
|
|
|
ve.init.mw.ApiResponseCache.prototype.get = function ( title ) {
|
|
|
|
if ( typeof title !== 'string' ) {
|
|
|
|
// Don't bother letting things like undefined or null make it all the way through,
|
|
|
|
// just reject them here. Otherwise they'll cause problems or exceptions at random
|
|
|
|
// other points in this file.
|
|
|
|
return $.Deferred().reject().promise();
|
|
|
|
}
|
|
|
|
title = this.constructor.static.normalizeTitle( title );
|
|
|
|
if ( !Object.prototype.hasOwnProperty.call( this.deferreds, title ) ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
this.deferreds[ title ] = $.Deferred();
|
2015-04-30 16:03:30 +00:00
|
|
|
this.queue.push( title );
|
|
|
|
this.schedule();
|
|
|
|
}
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.deferreds[ title ].promise();
|
2015-04-30 16:03:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up data about a page in the cache. If the data about this page is already in the cache,
|
|
|
|
* this returns that data. Otherwise, it returns undefined.
|
|
|
|
*
|
|
|
|
* @param {string} name Normalized page title
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object|undefined} Cache data for this name.
|
2015-04-30 16:03:30 +00:00
|
|
|
*/
|
|
|
|
ve.init.mw.ApiResponseCache.prototype.getCached = function ( name ) {
|
|
|
|
if ( Object.prototype.hasOwnProperty.call( this.cacheValues, name ) ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.cacheValues[ name ];
|
2015-04-30 16:03:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fired when a new entry is added to the cache.
|
|
|
|
*
|
|
|
|
* @event add
|
|
|
|
* @param {Object} entries Cache entries that were added. Object mapping names to data objects.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-07-28 22:02:32 +00:00
|
|
|
* Add entries to the cache. Does not overwrite already-set entries.
|
2015-04-30 16:03:30 +00:00
|
|
|
*
|
|
|
|
* @param {Object} entries Object keyed by page title, with the values being data objects
|
|
|
|
* @fires add
|
|
|
|
*/
|
|
|
|
ve.init.mw.ApiResponseCache.prototype.set = function ( entries ) {
|
2015-07-24 15:17:35 +00:00
|
|
|
var name;
|
2015-04-30 16:03:30 +00:00
|
|
|
for ( name in entries ) {
|
2015-07-28 22:02:32 +00:00
|
|
|
if ( !Object.prototype.hasOwnProperty.call( this.deferreds, name ) ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
this.deferreds[ name ] = $.Deferred();
|
2015-07-28 22:02:32 +00:00
|
|
|
}
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( this.deferreds[ name ].state() === 'pending' ) {
|
|
|
|
this.deferreds[ name ].resolve( entries[ name ] );
|
|
|
|
this.cacheValues[ name ] = entries[ name ];
|
2015-07-28 22:02:32 +00:00
|
|
|
}
|
2015-04-30 16:03:30 +00:00
|
|
|
}
|
|
|
|
this.emit( 'add', Object.keys( entries ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an API request promise to deal with a list of titles
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @method
|
|
|
|
* @param subqueue
|
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
|
|
|
ve.init.mw.ApiResponseCache.prototype.getRequestPromise = null;
|
2014-12-16 01:06:05 +00:00
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
/**
|
|
|
|
* Perform any scheduled API requests.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @fires add
|
|
|
|
*/
|
|
|
|
ve.init.mw.ApiResponseCache.prototype.processQueue = function () {
|
|
|
|
var subqueue, queue,
|
|
|
|
cache = this;
|
|
|
|
|
|
|
|
function rejectSubqueue( rejectQueue ) {
|
|
|
|
var i, len;
|
|
|
|
for ( i = 0, len = rejectQueue.length; i < len; i++ ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
cache.deferreds[ rejectQueue[ i ] ].reject();
|
2015-04-30 16:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processResult( data ) {
|
2018-05-23 10:48:01 +00:00
|
|
|
var i, pageid, page, processedPage, redirects,
|
2015-04-30 16:03:30 +00:00
|
|
|
pages = ( data.query && data.query.pages ) || data.pages,
|
|
|
|
processed = {};
|
|
|
|
|
2018-06-04 08:19:27 +00:00
|
|
|
redirects = ( data.query && data.query.redirects ) || [];
|
2015-04-30 16:03:30 +00:00
|
|
|
if ( pages ) {
|
|
|
|
for ( pageid in pages ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
page = pages[ pageid ];
|
2015-04-30 16:03:30 +00:00
|
|
|
processedPage = cache.constructor.static.processPage( page );
|
|
|
|
if ( processedPage !== undefined ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
processed[ page.title ] = processedPage;
|
2014-12-16 01:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-23 10:48:01 +00:00
|
|
|
for ( i = 0; i < redirects.length; i++ ) {
|
|
|
|
// Locate the title in redirects, if any.
|
|
|
|
if ( redirects[ i ].to === page.title ) {
|
|
|
|
processed[ redirects[ i ].from ] = processedPage;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-04-30 16:03:30 +00:00
|
|
|
cache.set( processed );
|
2014-12-16 01:06:05 +00:00
|
|
|
}
|
2015-04-30 16:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
queue = this.queue;
|
|
|
|
this.queue = [];
|
|
|
|
while ( queue.length ) {
|
|
|
|
subqueue = queue.splice( 0, 50 ).map( this.constructor.static.normalizeTitle );
|
|
|
|
this.getRequestPromise( subqueue )
|
|
|
|
.then( processResult )
|
|
|
|
|
|
|
|
// Reject everything in subqueue; this will only reject the ones
|
|
|
|
// that weren't already resolved above, because .reject() on an
|
|
|
|
// already resolved Deferred is a no-op.
|
|
|
|
.then( rejectSubqueue.bind( null, subqueue ) );
|
|
|
|
}
|
|
|
|
};
|