2014-03-11 00:46:26 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MediaWiki Initialization LinkCache class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2014-03-11 00:46:26 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
2014-09-24 01:40:51 +00:00
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
/**
|
|
|
|
* Caches information about titles.
|
2015-08-19 18:21:01 +00:00
|
|
|
*
|
2015-04-30 16:03:30 +00:00
|
|
|
* @class
|
|
|
|
* @extends ve.init.mw.ApiResponseCache
|
|
|
|
* @constructor
|
2018-05-05 14:13:15 +00:00
|
|
|
* @param {mw.Api} [api]
|
2015-04-30 16:03:30 +00:00
|
|
|
*/
|
|
|
|
ve.init.mw.LinkCache = function VeInitMwLinkCache() {
|
2018-05-05 14:13:15 +00:00
|
|
|
// Parent constructor
|
|
|
|
ve.init.mw.LinkCache.super.apply( this, arguments );
|
2015-04-30 16:03:30 +00:00
|
|
|
};
|
2015-04-28 15:26:42 +00:00
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
/* Inheritance */
|
2014-03-11 00:46:26 +00:00
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
OO.inheritClass( ve.init.mw.LinkCache, ve.init.mw.ApiResponseCache );
|
2015-04-28 15:26:42 +00:00
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
/* Static methods */
|
2015-04-28 15:26:42 +00:00
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
/**
|
|
|
|
* Get the icon name to use for a particular link type
|
|
|
|
*
|
|
|
|
* @param {Object} linkData Link data
|
|
|
|
* @return {string} Icon name
|
|
|
|
*/
|
|
|
|
ve.init.mw.LinkCache.static.getIconForLink = function ( linkData ) {
|
|
|
|
if ( linkData.missing ) {
|
2019-07-17 12:08:50 +00:00
|
|
|
return 'articleNotFound';
|
2015-04-30 16:03:30 +00:00
|
|
|
}
|
|
|
|
if ( linkData.redirect ) {
|
2019-07-17 12:08:50 +00:00
|
|
|
return 'articleRedirect';
|
2015-04-30 16:03:30 +00:00
|
|
|
}
|
|
|
|
if ( linkData.disambiguation ) {
|
2019-07-17 12:08:50 +00:00
|
|
|
return 'articleDisambiguation';
|
2015-04-30 16:03:30 +00:00
|
|
|
}
|
2019-07-17 12:08:50 +00:00
|
|
|
return 'article';
|
2015-04-30 16:03:30 +00:00
|
|
|
};
|
2014-03-11 00:46:26 +00:00
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.init.mw.LinkCache.static.processPage = function ( page ) {
|
|
|
|
return {
|
|
|
|
missing: page.missing !== undefined,
|
2016-10-11 21:47:53 +00:00
|
|
|
known: page.known !== undefined,
|
2015-04-30 16:03:30 +00:00
|
|
|
redirect: page.redirect !== undefined,
|
|
|
|
disambiguation: ve.getProp( page, 'pageprops', 'disambiguation' ) !== undefined,
|
2018-05-09 20:12:26 +00:00
|
|
|
hidden: ve.getProp( page, 'pageprops', 'hiddencat' ) !== undefined,
|
2015-04-30 16:03:30 +00:00
|
|
|
imageUrl: ve.getProp( page, 'thumbnail', 'source' ),
|
2018-05-07 14:59:02 +00:00
|
|
|
description: page.description
|
2014-10-03 01:27:55 +00:00
|
|
|
};
|
2015-04-30 16:03:30 +00:00
|
|
|
};
|
2014-10-03 01:27:55 +00:00
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
/* Methods */
|
2015-02-03 04:30:00 +00:00
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
/**
|
|
|
|
* Requests information about the title, then adds classes to the provided element as appropriate.
|
|
|
|
*
|
|
|
|
* @param {string} title
|
|
|
|
* @param {jQuery} $element Element to style
|
2022-02-14 15:18:57 +00:00
|
|
|
* @param {boolean} [hasFragment=false] Whether the link goes to a fragment
|
2015-04-30 16:03:30 +00:00
|
|
|
*/
|
2017-04-11 17:00:49 +00:00
|
|
|
ve.init.mw.LinkCache.prototype.styleElement = function ( title, $element, hasFragment ) {
|
2024-05-01 12:32:49 +00:00
|
|
|
var cachedMissingData = this.getCached( '_missing/' + title );
|
2015-07-24 15:17:35 +00:00
|
|
|
|
2021-10-13 12:57:45 +00:00
|
|
|
var promise;
|
2015-07-24 15:17:35 +00:00
|
|
|
// Use the synchronous missing link cache data if it exists
|
|
|
|
if ( cachedMissingData ) {
|
2019-11-02 05:06:28 +00:00
|
|
|
promise = ve.createDeferred().resolve( cachedMissingData ).promise();
|
2015-07-24 15:17:35 +00:00
|
|
|
} else {
|
|
|
|
promise = this.get( title );
|
|
|
|
}
|
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
promise.done( ( data ) => {
|
2016-10-11 21:47:53 +00:00
|
|
|
if ( data.missing && !data.known ) {
|
2015-04-30 16:03:30 +00:00
|
|
|
$element.addClass( 'new' );
|
|
|
|
} else {
|
2017-03-09 20:25:22 +00:00
|
|
|
// Provided by core MediaWiki, styled like a <strong> element by default.
|
2024-05-01 12:32:49 +00:00
|
|
|
if ( !hasFragment && this.constructor.static.normalizeTitle( title ) === this.constructor.static.normalizeTitle( mw.config.get( 'wgRelevantPageName' ) ) ) {
|
2017-03-09 20:25:22 +00:00
|
|
|
$element.addClass( 'mw-selflink' );
|
|
|
|
}
|
2015-04-30 16:03:30 +00:00
|
|
|
// Provided by core MediaWiki, no styles by default.
|
|
|
|
if ( data.redirect ) {
|
|
|
|
$element.addClass( 'mw-redirect' );
|
|
|
|
}
|
2015-09-24 17:20:12 +00:00
|
|
|
// Provided by the Disambiguator extension, no styles by default.
|
2015-04-30 16:03:30 +00:00
|
|
|
if ( data.disambiguation ) {
|
|
|
|
$element.addClass( 'mw-disambig' );
|
|
|
|
}
|
2015-02-03 04:30:00 +00:00
|
|
|
}
|
2015-04-30 16:03:30 +00:00
|
|
|
} );
|
|
|
|
};
|
2015-02-03 04:30:00 +00:00
|
|
|
|
2017-03-30 16:37:54 +00:00
|
|
|
/**
|
2019-02-22 17:49:11 +00:00
|
|
|
* Given a chunk of Parsoid HTML, applies style transformations.
|
2017-03-30 16:37:54 +00:00
|
|
|
*
|
2019-02-22 17:49:11 +00:00
|
|
|
* Previously this was used for applying red-link styles, but that
|
|
|
|
* has since been upstreamed to Parsoid.
|
|
|
|
*
|
|
|
|
* TODO: Evaluate if this method should be renamed/removed as it
|
|
|
|
* now has nothing to do with the link cache.
|
2017-03-30 16:37:54 +00:00
|
|
|
*
|
2019-04-16 15:17:29 +00:00
|
|
|
* @param {jQuery} $elements Elements to style
|
2017-03-30 16:37:54 +00:00
|
|
|
* @param {HTMLDocument} doc Base document to use for normalisation
|
|
|
|
*/
|
2019-02-22 17:49:11 +00:00
|
|
|
ve.init.mw.LinkCache.prototype.styleParsoidElements = function ( $elements ) {
|
2017-05-31 16:46:37 +00:00
|
|
|
if ( ve.dm.MWLanguageVariantNode ) {
|
|
|
|
// Render the user's preferred variant in language converter markup
|
2024-04-30 16:44:25 +00:00
|
|
|
$elements.each( ( i, element ) => {
|
2019-11-02 16:28:32 +00:00
|
|
|
ve.dm.MWLanguageVariantNode.static.processVariants( element );
|
|
|
|
} );
|
2017-05-31 16:46:37 +00:00
|
|
|
}
|
2017-03-30 16:37:54 +00:00
|
|
|
};
|
|
|
|
|
2015-04-30 16:03:30 +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;
|
|
|
|
};
|
2015-02-03 04:30:00 +00:00
|
|
|
|
2015-07-24 15:17:35 +00:00
|
|
|
/**
|
|
|
|
* Set link missing data
|
|
|
|
*
|
|
|
|
* Stored separately from the full link data cache
|
|
|
|
*
|
|
|
|
* @param {Object} entries Object keyed by page title, with the values being data objects
|
|
|
|
*/
|
|
|
|
ve.init.mw.LinkCache.prototype.setMissing = function ( entries ) {
|
2021-10-13 12:57:45 +00:00
|
|
|
var missingEntries = {};
|
|
|
|
for ( var name in entries ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
missingEntries[ '_missing/' + name ] = entries[ name ];
|
2015-07-24 15:17:35 +00:00
|
|
|
}
|
|
|
|
this.set( missingEntries );
|
|
|
|
};
|
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.init.mw.LinkCache.prototype.get = function ( title ) {
|
|
|
|
var data = {};
|
|
|
|
if ( this.assumeExistence ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
data[ this.constructor.static.normalizeTitle( title ) ] = { missing: false };
|
2015-07-24 15:17:35 +00:00
|
|
|
this.setMissing( data );
|
2015-04-30 16:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parent method
|
|
|
|
return ve.init.mw.LinkCache.super.prototype.get.call( this, title );
|
|
|
|
};
|
2014-03-11 00:46:26 +00:00
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.init.mw.LinkCache.prototype.getRequestPromise = function ( subqueue ) {
|
2018-05-05 14:13:15 +00:00
|
|
|
return this.api.get( {
|
2015-04-30 16:03:30 +00:00
|
|
|
action: 'query',
|
2018-05-07 14:59:02 +00:00
|
|
|
prop: 'info|pageprops|pageimages|description',
|
2015-04-30 16:03:30 +00:00
|
|
|
pithumbsize: 80,
|
2015-08-17 16:40:02 +00:00
|
|
|
pilimit: subqueue.length,
|
2018-05-09 20:12:26 +00:00
|
|
|
ppprop: 'disambiguation|hiddencat',
|
2016-09-30 16:07:10 +00:00
|
|
|
titles: subqueue,
|
2019-01-08 17:00:09 +00:00
|
|
|
continue: ''
|
2015-04-30 16:03:30 +00:00
|
|
|
} );
|
|
|
|
};
|