2014-03-11 00:46:26 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MediaWiki Initialization LinkCache class.
|
|
|
|
*
|
2015-01-08 23:54:03 +00:00
|
|
|
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
|
2014-03-11 00:46:26 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
( function () {
|
2014-06-26 10:41:18 +00:00
|
|
|
/**
|
2014-12-16 01:06:05 +00:00
|
|
|
* Caches information about titles.
|
2014-06-26 10:41:18 +00:00
|
|
|
* @class
|
2014-12-16 01:06:05 +00:00
|
|
|
* @extends ve.init.mw.ApiResponseCache
|
2014-06-26 10:41:18 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
ve.init.mw.LinkCache = function VeInitMwLinkCache() {
|
2014-12-16 01:06:05 +00:00
|
|
|
ve.init.mw.LinkCache.super.call( this );
|
2014-09-24 01:40:51 +00:00
|
|
|
|
|
|
|
// Keys are page names, values are link data objects
|
|
|
|
// This is kept for synchronous retrieval of cached values via #getCached
|
|
|
|
this.cacheValues = {};
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
2014-03-11 00:46:26 +00:00
|
|
|
|
2014-12-16 01:06:05 +00:00
|
|
|
OO.inheritClass( ve.init.mw.LinkCache, ve.init.mw.ApiResponseCache );
|
2014-03-11 00:46:26 +00:00
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
// TODO unit tests
|
2014-03-11 00:46:26 +00:00
|
|
|
|
2014-10-03 01:27:55 +00:00
|
|
|
/**
|
|
|
|
* Requests information about the title, then adds classes to the provided element as appropriate.
|
|
|
|
*
|
2014-11-05 22:45:09 +00:00
|
|
|
* @param {string} title
|
2014-10-03 01:27:55 +00:00
|
|
|
* @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' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2015-02-03 04:30:00 +00:00
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
};
|
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
/**
|
2014-12-16 01:06:05 +00:00
|
|
|
* @inheritdoc
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
2014-12-16 01:06:05 +00:00
|
|
|
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( '|' )
|
|
|
|
} );
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
2014-03-11 00:46:26 +00:00
|
|
|
|
2014-06-26 10:41:18 +00:00
|
|
|
/**
|
2014-12-16 01:06:05 +00:00
|
|
|
* @inheritdoc
|
2014-06-26 10:41:18 +00:00
|
|
|
*/
|
2014-12-16 01:06:05 +00:00
|
|
|
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
|
|
|
|
};
|
2014-06-26 10:41:18 +00:00
|
|
|
};
|
2015-01-31 00:41:37 +00:00
|
|
|
}() );
|