2014-03-11 00:46:26 +00:00
|
|
|
|
/*!
|
|
|
|
|
* VisualEditor MediaWiki Initialization LinkCache class.
|
|
|
|
|
*
|
2017-01-03 16:58:33 +00:00
|
|
|
|
* @copyright 2011-2017 VisualEditor Team and others; 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
|
|
|
|
|
*/
|
|
|
|
|
ve.init.mw.LinkCache = function VeInitMwLinkCache() {
|
|
|
|
|
ve.init.mw.LinkCache.super.call( this );
|
2014-03-11 00:46:26 +00:00
|
|
|
|
|
2015-04-30 16:03:30 +00:00
|
|
|
|
// Keys are page names, values are link data objects
|
|
|
|
|
// This is kept for synchronous retrieval of cached values via #getCached
|
|
|
|
|
this.cacheValues = {};
|
|
|
|
|
};
|
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 ) {
|
|
|
|
|
return 'page-not-found';
|
|
|
|
|
}
|
|
|
|
|
if ( linkData.redirect ) {
|
|
|
|
|
return 'page-redirect';
|
|
|
|
|
}
|
|
|
|
|
if ( linkData.disambiguation ) {
|
|
|
|
|
return 'page-disambiguation';
|
|
|
|
|
}
|
|
|
|
|
return 'page-existing';
|
|
|
|
|
};
|
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,
|
|
|
|
|
imageUrl: ve.getProp( page, 'thumbnail', 'source' ),
|
|
|
|
|
description: ve.getProp( page, 'terms', '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
|
|
|
|
|
*/
|
|
|
|
|
ve.init.mw.LinkCache.prototype.styleElement = function ( title, $element ) {
|
2015-07-24 15:17:35 +00:00
|
|
|
|
var promise,
|
2017-03-30 10:56:13 +00:00
|
|
|
|
cache = this,
|
2015-07-24 15:17:35 +00:00
|
|
|
|
cachedMissingData = this.getCached( '_missing/' + title );
|
|
|
|
|
|
|
|
|
|
// Use the synchronous missing link cache data if it exists
|
|
|
|
|
if ( cachedMissingData ) {
|
|
|
|
|
promise = $.Deferred().resolve( cachedMissingData ).promise();
|
|
|
|
|
} else {
|
|
|
|
|
promise = this.get( title );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
promise.done( function ( 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.
|
2017-03-30 10:56:13 +00:00
|
|
|
|
if ( cache.constructor.static.normalizeTitle( title ) === cache.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
|
|
|
|
/**
|
|
|
|
|
* Given a chunk of Parsoid HTML, requests information about each link's title, then adds classes
|
|
|
|
|
* to each such element as appropriate.
|
|
|
|
|
*
|
|
|
|
|
* TODO: Most/all of this code should be done upstream, either by Parsoid itself or by an
|
|
|
|
|
* intermediary service – see T64803 and others.
|
|
|
|
|
*
|
|
|
|
|
* @chainable
|
|
|
|
|
* @param {jQuery} $element Elements to style
|
|
|
|
|
* @param {HTMLDocument} doc Base document to use for normalisation
|
|
|
|
|
* @return {jQuery} The elements, now styled
|
|
|
|
|
*/
|
|
|
|
|
ve.init.mw.LinkCache.prototype.styleParsoidElements = function ( $elements, doc ) {
|
|
|
|
|
// TODO: Remove when fixed upstream in Parsoid (T58756)
|
|
|
|
|
$elements
|
|
|
|
|
.find( 'a[rel="mw:ExtLink"]' ).addBack( 'a[rel="mw:ExtLink"]' )
|
|
|
|
|
.addClass( 'external' );
|
|
|
|
|
|
|
|
|
|
// TODO: Remove when moved upstream into Parsoid or another service (T64803)
|
|
|
|
|
// If the element isn't attached, doc will be null, so we don't know how to normalise titles
|
|
|
|
|
if ( doc ) {
|
|
|
|
|
$elements
|
|
|
|
|
.find( 'a[rel="mw:WikiLink"]' ).addBack( 'a[rel="mw:WikiLink"]' )
|
|
|
|
|
.each( function () {
|
|
|
|
|
var title,
|
|
|
|
|
href = this.href || mw.config.get( 'wgArticlePath' );
|
|
|
|
|
|
|
|
|
|
title = ve.init.platform.linkCache.constructor.static.normalizeTitle(
|
|
|
|
|
ve.dm.MWInternalLinkAnnotation.static.getTargetDataFromHref( href, doc ).title
|
|
|
|
|
);
|
|
|
|
|
ve.init.platform.linkCache.styleElement( title, $( this ) );
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $elements;
|
|
|
|
|
};
|
|
|
|
|
|
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 ) {
|
|
|
|
|
var name, missingEntries = {};
|
|
|
|
|
for ( 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 ) {
|
|
|
|
|
return new mw.Api().get( {
|
|
|
|
|
action: 'query',
|
|
|
|
|
prop: 'info|pageprops|pageimages|pageterms',
|
|
|
|
|
pithumbsize: 80,
|
2015-08-17 16:40:02 +00:00
|
|
|
|
pilimit: subqueue.length,
|
2015-04-30 16:03:30 +00:00
|
|
|
|
wbptterms: 'description',
|
|
|
|
|
ppprop: 'disambiguation',
|
2016-09-30 16:07:10 +00:00
|
|
|
|
titles: subqueue,
|
2016-10-28 00:22:30 +00:00
|
|
|
|
'continue': ''
|
2015-04-30 16:03:30 +00:00
|
|
|
|
} );
|
|
|
|
|
};
|