Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/init/apiresponsecache/ve.init.mw.LinkCache.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki Initialization LinkCache class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Caches information about titles.
10 *
11 * @class
12 * @extends ve.init.mw.ApiResponseCache
13 * @constructor
14 * @param {mw.Api} [api]
15 */
16 ve.init.mw.LinkCache = function VeInitMwLinkCache() {
17 // Parent constructor
18 ve.init.mw.LinkCache.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.init.mw.LinkCache, ve.init.mw.ApiResponseCache );
24
25 /* Static methods */
26
27 /**
28 * Get the icon name to use for a particular link type
29 *
30 * @param {Object} linkData Link data
31 * @return {string} Icon name
32 */
33 ve.init.mw.LinkCache.static.getIconForLink = function ( linkData ) {
34 if ( linkData.missing ) {
35 return 'articleNotFound';
36 }
37 if ( linkData.redirect ) {
38 return 'articleRedirect';
39 }
40 if ( linkData.disambiguation ) {
41 return 'articleDisambiguation';
42 }
43 return 'article';
44 };
45
46 /**
47 * @inheritdoc
48 */
49 ve.init.mw.LinkCache.static.processPage = function ( page ) {
50 return {
51 missing: page.missing !== undefined,
52 known: page.known !== undefined,
53 redirect: page.redirect !== undefined,
54 disambiguation: ve.getProp( page, 'pageprops', 'disambiguation' ) !== undefined,
55 hidden: ve.getProp( page, 'pageprops', 'hiddencat' ) !== undefined,
56 imageUrl: ve.getProp( page, 'thumbnail', 'source' ),
57 description: page.description
58 };
59 };
60
61 /* Methods */
62
63 /**
64 * Requests information about the title, then adds classes to the provided element as appropriate.
65 *
66 * @param {string} title
67 * @param {jQuery} $element Element to style
68 * @param {boolean} [hasFragment=false] Whether the link goes to a fragment
69 */
70 ve.init.mw.LinkCache.prototype.styleElement = function ( title, $element, hasFragment ) {
71 const cachedMissingData = this.getCached( '_missing/' + title );
72
73 let promise;
74 // Use the synchronous missing link cache data if it exists
75 if ( cachedMissingData ) {
76 promise = ve.createDeferred().resolve( cachedMissingData ).promise();
77 } else {
78 promise = this.get( title );
79 }
80
81 promise.done( ( data ) => {
82 if ( data.missing && !data.known ) {
83 $element.addClass( 'new' );
84 } else {
85 // Provided by core MediaWiki, styled like a <strong> element by default.
86 if ( !hasFragment && this.constructor.static.normalizeTitle( title ) === this.constructor.static.normalizeTitle( mw.config.get( 'wgRelevantPageName' ) ) ) {
87 $element.addClass( 'mw-selflink' );
88 }
89 // Provided by core MediaWiki, no styles by default.
90 if ( data.redirect ) {
91 $element.addClass( 'mw-redirect' );
92 }
93 // Provided by the Disambiguator extension, no styles by default.
94 if ( data.disambiguation ) {
95 $element.addClass( 'mw-disambig' );
96 }
97 }
98 } );
99 };
100
101 /**
102 * Given a chunk of Parsoid HTML, applies style transformations.
103 *
104 * Previously this was used for applying red-link styles, but that
105 * has since been upstreamed to Parsoid.
106 *
107 * TODO: Evaluate if this method should be renamed/removed as it
108 * now has nothing to do with the link cache.
109 *
110 * @param {jQuery} $elements Elements to style
111 * @param {HTMLDocument} doc Base document to use for normalisation
112 */
113 ve.init.mw.LinkCache.prototype.styleParsoidElements = function ( $elements ) {
114 if ( ve.dm.MWLanguageVariantNode ) {
115 // Render the user's preferred variant in language converter markup
116 $elements.each( ( i, element ) => {
117 ve.dm.MWLanguageVariantNode.static.processVariants( element );
118 } );
119 }
120 };
121
122 /**
123 * Enable or disable automatic assumption of existence.
124 *
125 * While enabled, any get() for a title that's not already in the cache will return
126 * { missing: false } and write that to the cache.
127 *
128 * @param {boolean} assume Assume all uncached titles exist
129 */
130 ve.init.mw.LinkCache.prototype.setAssumeExistence = function ( assume ) {
131 this.assumeExistence = !!assume;
132 };
133
134 /**
135 * Set link missing data
136 *
137 * Stored separately from the full link data cache
138 *
139 * @param {Object} entries Object keyed by page title, with the values being data objects
140 */
141 ve.init.mw.LinkCache.prototype.setMissing = function ( entries ) {
142 const missingEntries = {};
143 for ( const name in entries ) {
144 missingEntries[ '_missing/' + name ] = entries[ name ];
145 }
146 this.set( missingEntries );
147 };
148
149 /**
150 * @inheritdoc
151 */
152 ve.init.mw.LinkCache.prototype.get = function ( title ) {
153 const data = {};
154 if ( this.assumeExistence ) {
155 data[ this.constructor.static.normalizeTitle( title ) ] = { missing: false };
156 this.setMissing( data );
157 }
158
159 // Parent method
160 return ve.init.mw.LinkCache.super.prototype.get.call( this, title );
161 };
162
163 /**
164 * @inheritdoc
165 */
166 ve.init.mw.LinkCache.prototype.getRequestPromise = function ( subqueue ) {
167 return this.api.get( {
168 action: 'query',
169 prop: 'info|pageprops|pageimages|description',
170 pithumbsize: 80,
171 pilimit: subqueue.length,
172 ppprop: 'disambiguation|hiddencat',
173 titles: subqueue,
174 continue: ''
175 } );
176 };