File: ve.init.mw.LinkCache.js

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (176 lines) | stat: -rw-r--r-- 4,887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*!
 * VisualEditor MediaWiki Initialization LinkCache class.
 *
 * @copyright See AUTHORS.txt
 * @license The MIT License (MIT); see LICENSE.txt
 */

/**
 * Caches information about titles.
 *
 * @class
 * @extends ve.init.mw.ApiResponseCache
 * @constructor
 * @param {mw.Api} [api]
 */
ve.init.mw.LinkCache = function VeInitMwLinkCache() {
	// Parent constructor
	ve.init.mw.LinkCache.super.apply( this, arguments );
};

/* Inheritance */

OO.inheritClass( ve.init.mw.LinkCache, ve.init.mw.ApiResponseCache );

/* Static methods */

/**
 * 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 'articleNotFound';
	}
	if ( linkData.redirect ) {
		return 'articleRedirect';
	}
	if ( linkData.disambiguation ) {
		return 'articleDisambiguation';
	}
	return 'article';
};

/**
 * @inheritdoc
 */
ve.init.mw.LinkCache.static.processPage = function ( page ) {
	return {
		missing: page.missing !== undefined,
		known: page.known !== undefined,
		redirect: page.redirect !== undefined,
		disambiguation: ve.getProp( page, 'pageprops', 'disambiguation' ) !== undefined,
		hidden: ve.getProp( page, 'pageprops', 'hiddencat' ) !== undefined,
		imageUrl: ve.getProp( page, 'thumbnail', 'source' ),
		description: page.description
	};
};

/* Methods */

/**
 * Requests information about the title, then adds classes to the provided element as appropriate.
 *
 * @param {string} title
 * @param {jQuery} $element Element to style
 * @param {boolean} [hasFragment=false] Whether the link goes to a fragment
 */
ve.init.mw.LinkCache.prototype.styleElement = function ( title, $element, hasFragment ) {
	const cachedMissingData = this.getCached( '_missing/' + title );

	let promise;
	// Use the synchronous missing link cache data if it exists
	if ( cachedMissingData ) {
		promise = ve.createDeferred().resolve( cachedMissingData ).promise();
	} else {
		promise = this.get( title );
	}

	promise.done( ( data ) => {
		if ( data.missing && !data.known ) {
			$element.addClass( 'new' );
		} else {
			// Provided by core MediaWiki, styled like a <strong> element by default.
			if ( !hasFragment && this.constructor.static.normalizeTitle( title ) === this.constructor.static.normalizeTitle( mw.config.get( 'wgRelevantPageName' ) ) ) {
				$element.addClass( 'mw-selflink' );
			}
			// Provided by core MediaWiki, no styles by default.
			if ( data.redirect ) {
				$element.addClass( 'mw-redirect' );
			}
			// Provided by the Disambiguator extension, no styles by default.
			if ( data.disambiguation ) {
				$element.addClass( 'mw-disambig' );
			}
		}
	} );
};

/**
 * Given a chunk of Parsoid HTML, applies style transformations.
 *
 * 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.
 *
 * @param {jQuery} $elements Elements to style
 * @param {HTMLDocument} doc Base document to use for normalisation
 */
ve.init.mw.LinkCache.prototype.styleParsoidElements = function ( $elements ) {
	if ( ve.dm.MWLanguageVariantNode ) {
		// Render the user's preferred variant in language converter markup
		$elements.each( ( i, element ) => {
			ve.dm.MWLanguageVariantNode.static.processVariants( element );
		} );
	}
};

/**
 * 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;
};

/**
 * 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 ) {
	const missingEntries = {};
	for ( const name in entries ) {
		missingEntries[ '_missing/' + name ] = entries[ name ];
	}
	this.set( missingEntries );
};

/**
 * @inheritdoc
 */
ve.init.mw.LinkCache.prototype.get = function ( title ) {
	const data = {};
	if ( this.assumeExistence ) {
		data[ this.constructor.static.normalizeTitle( title ) ] = { missing: false };
		this.setMissing( data );
	}

	// Parent method
	return ve.init.mw.LinkCache.super.prototype.get.call( this, title );
};

/**
 * @inheritdoc
 */
ve.init.mw.LinkCache.prototype.getRequestPromise = function ( subqueue ) {
	return this.api.get( {
		action: 'query',
		prop: 'info|pageprops|pageimages|description',
		pithumbsize: 80,
		pilimit: subqueue.length,
		ppprop: 'disambiguation|hiddencat',
		titles: subqueue,
		continue: ''
	} );
};