mediawiki-extensions-Visual.../modules/ve-mw/init/ve.init.mw.LinkCache.js
Roan Kattouw e367ecc948 Make API module's LinkCache data transmission format more efficient
We used to send data like { title: { missing: true|false } }
With this change, we send data like { missing: [titles], existing: true|[titles] }
where 'existing' is set to true (assume all non-missing titles exist)
for current revisions and to an array of existing titles for
old revisions.

This is because we always output this data for links in the current
revision, even when loading an old revision: in that case we rely
on the client to request the omitted information, so there we can't
assume that all pages we don't have information about exist.

Bug: T88259
Change-Id: I7b58b3f669cc78fd81b60859cf76928a9087066f
2015-02-03 11:30:40 -08:00

100 lines
2.7 KiB
JavaScript

/*!
* VisualEditor MediaWiki Initialization LinkCache class.
*
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
( function () {
/**
* Caches information about titles.
* @class
* @extends ve.init.mw.ApiResponseCache
* @constructor
*/
ve.init.mw.LinkCache = function VeInitMwLinkCache() {
ve.init.mw.LinkCache.super.call( this );
// Keys are page names, values are link data objects
// This is kept for synchronous retrieval of cached values via #getCached
this.cacheValues = {};
};
OO.inheritClass( ve.init.mw.LinkCache, ve.init.mw.ApiResponseCache );
// TODO unit tests
/**
* Requests information about the title, then adds classes to the provided element as appropriate.
*
* @param {string} title
* @param {jQuery} $element Element to style
*/
ve.init.mw.LinkCache.prototype.styleElement = function ( title, $element ) {
this.get( title ).done( function ( data ) {
if ( data.missing ) {
$element.addClass( 'new' );
} else {
// Provided by core MediaWiki, no styles by default.
if ( data.redirect ) {
$element.addClass( 'mw-redirect' );
}
// Should be provided by the Disambiguator extension, but no one has yet written a suitably
// performant patch to do it. It is instead implemented in JavaScript in on-wiki gadgets.
if ( data.disambiguation ) {
$element.addClass( 'mw-disambig' );
}
}
} );
};
/**
* Enable or disable automatic assumption of existence.
*
* While enabled, any get() for a title that's not already in the cache will return
* { missing: false } and write that to the cache.
*
* @param {boolean} assume Assume all uncached titles exist
*/
ve.init.mw.LinkCache.prototype.setAssumeExistence = function ( assume ) {
this.assumeExistence = !!assume;
};
/**
* @inheritdoc
*/
ve.init.mw.LinkCache.prototype.get = function ( title ) {
var data = {};
if ( this.assumeExistence ) {
data[this.normalizeTitle( title )] = { missing: false };
this.set( data );
}
// Parent method
return ve.init.mw.LinkCache.super.prototype.get.call( this, title );
};
/**
* @inheritdoc
*/
ve.init.mw.LinkCache.prototype.getRequestPromise = function ( subqueue ) {
return ve.init.target.constructor.static.apiRequest( {
action: 'query',
prop: 'info|pageprops',
ppprop: 'disambiguation',
titles: subqueue.join( '|' )
} );
};
/**
* @inheritdoc
*/
ve.init.mw.LinkCache.prototype.processPage = function ( page ) {
return {
missing: page.missing !== undefined,
redirect: page.redirect !== undefined,
// Disambiguator extension
disambiguation: page.pageprops && page.pageprops.disambiguation !== undefined
};
};
}() );