Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/dm/models/ve.dm.MWTemplateSpecModel.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWTemplateSpecModel class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * See https://www.mediawiki.org/wiki/Extension:TemplateData#Set_object
10 *
11 * @typedef {Object} Set
12 * @memberof ve.dm.MWTemplateSpecModel
13 * @property {string|Object.<string, string>} label A brief name for the parameter set.
14 * @property {string[]} params One or more names of parameters to include in the set.
15 */
16
17 /**
18 * Object literal returned by the TemplataData API. Expected to be in formatversion=2,
19 * guaranteed via ve.init.mw.Target#getContentApi.
20 *
21 * @class ve.dm.MWTemplatePageMetadata
22 * @private
23 */
24 /**
25 * @property {string|boolean} [missing] Either "1" or true
26 * @property {string|boolean} [notemplatedata] Either "1" or true when there is no user-provided
27 * documentation `params` are auto-detected in this case.
28 * @property {string} title Template page name including the "Template:" namespace
29 * @property {string|Object.<string,string>} [description] Template description
30 * @property {Object.<string,ve.dm.MWTemplateParamDescription>} [params] Parameters by param name
31 * @property {string[]} [paramOrder] Preferred parameter order as documented via TemplateData. If
32 * given, the TemplateData API makes sure this contains the same parameters as `params`.
33 * @property {ve.dm.MWTemplateSpecModel.Set[]} [sets] List of parameter
34 * sets, i.e. parameters that belong together (whatever that means, this feature is underspecified
35 * and unused)
36 * @property {Object.<string,Object.<string,string|string[]|string[][]>>} [maps] Source to target
37 * parameter mappings for consumers like Citoid or gadgets
38 */
39
40 /**
41 * Object literal
42 *
43 * @class ve.dm.MWTemplateParamDescription
44 * @private
45 */
46 /**
47 * @property {string|Object.<string,string>} [label]
48 * @property {string|Object.<string,string>} [description]
49 * @property {string[]} [suggestedvalues]
50 * @property {string} [default]
51 * @property {string|Object.<string,string>} [example]
52 * @property {string} [autovalue]
53 * @property {string} [type]
54 * @property {string[]} [aliases]
55 * @property {boolean} [required]
56 * @property {boolean} [suggested]
57 * @property {boolean|string} [deprecated]
58 */
59
60 /**
61 * Holds a mixture of:
62 *
63 * - A copy of a template's specification as it is documented via TemplateData.
64 * - Undocumented parameters that appear in a template invocation, {@link #fillFromTemplate}.
65 * - Documented aliases are also considered valid, known parameter names. Use
66 * {@link #isParameterAlias} to differentiate between the two.
67 *
68 * Therefore, this is not the original specification but an accessor to the documentation for an
69 * individual template invocation. It's possibly different for every invocation.
70 *
71 * Meant to be in a 1:1 relationship to {@link ve.dm.MWTemplateModel}.
72 *
73 * The actual, unmodified specification can be found in the {@link #templateData} property and
74 * the local `specCache` in {@link ve.dm.MWTransclusionModel}.
75 *
76 * See <https://github.com/wikimedia/mediawiki-extensions-TemplateData/blob/master/Specification.md>
77 * for the latest version of the TemplateData specification.
78 *
79 * @class
80 *
81 * @constructor
82 * @param {ve.dm.MWTemplateModel} template
83 */
84 ve.dm.MWTemplateSpecModel = function VeDmMWTemplateSpecModel( template ) {
85 this.template = template;
86 /**
87 * @property {Object.<string,boolean>} seenParameterNames Keeps track of any parameter from any
88 * source and in which order they have been seen first. Includes parameters that have been removed
89 * during the lifetime of this object, i.e. {@see fillFromTemplate} doesn't remove parameters that
90 * have been seen before. The order is typically but not necessarily the original order in which
91 * the parameters appear in the template. Aliases are resolved and don't appear on their original
92 * position any more.
93 */
94 this.seenParameterNames = {};
95 /**
96 * @property {Object} templateData Documentation as provided by the TemplateData API
97 */
98 this.templateData = { notemplatedata: true, params: {} };
99 /**
100 * @property {Object.<string,string>} aliases Maps aliases to primary parameter names
101 */
102 this.aliases = {};
103
104 // Initialization
105 this.fillFromTemplate();
106 };
107
108 OO.initClass( ve.dm.MWTemplateSpecModel );
109
110 /* Static methods */
111
112 /**
113 * @private
114 * @param {string|Object.<string,string>|null} stringOrObject
115 * @param {string} [languageCode]
116 * @return {string|null|undefined}
117 */
118 ve.dm.MWTemplateSpecModel.static.getLocalValue = function ( stringOrObject, languageCode ) {
119 return stringOrObject && typeof stringOrObject === 'object' ?
120 OO.ui.getLocalValue( stringOrObject, languageCode ) :
121 stringOrObject;
122 };
123
124 /* Methods */
125
126 /**
127 * Template spec data is available from the TemplateData extension's API.
128 *
129 * @param {ve.dm.MWTemplatePageMetadata} data
130 */
131 ve.dm.MWTemplateSpecModel.prototype.setTemplateData = function ( data ) {
132 if ( !data || !ve.isPlainObject( data ) ) {
133 return;
134 }
135
136 this.templateData = data;
137 // This is currently not optional in the TemplateData API but might be in the future
138 if ( !this.templateData.params ) {
139 this.templateData.params = {};
140 }
141 // Incomplete server validation makes this possible, but the empty string is reserved for
142 // {@see ve.ui.MWAddParameterPage}.
143 delete this.templateData.params[ '' ];
144
145 let resolveAliases = false;
146
147 for ( const primaryName in this.templateData.params ) {
148 this.seenParameterNames[ primaryName ] = true;
149
150 const aliases = this.getParameterAliases( primaryName );
151 for ( let i = 0; i < aliases.length; i++ ) {
152 const alias = aliases[ i ];
153 this.aliases[ alias ] = primaryName;
154 if ( alias in this.seenParameterNames ) {
155 resolveAliases = true;
156 }
157 }
158 }
159
160 if ( resolveAliases ) {
161 const primaryNames = {};
162 for ( const name in this.seenParameterNames ) {
163 primaryNames[ this.getPrimaryParameterName( name ) ] = true;
164 }
165 this.seenParameterNames = primaryNames;
166 }
167 };
168
169 /**
170 * Adds all (possibly undocumented) parameters from the linked template to the list of known
171 * parameters, {@see getKnownParameterNames}. This should be called every time a parameter is added
172 * to the template.
173 */
174 ve.dm.MWTemplateSpecModel.prototype.fillFromTemplate = function () {
175 for ( const name in this.template.getParameters() ) {
176 // Ignore placeholder parameters with no name
177 if ( name && !this.isKnownParameterOrAlias( name ) ) {
178 // There is no information other than the names of the parameters, that they exist, and
179 // in which order
180 this.seenParameterNames[ name ] = true;
181 }
182 }
183 };
184
185 /**
186 * @return {string} Normalized template name without the "Template:" namespace prefix, if possible.
187 * Otherwise the unnormalized template name as used in the wikitext. Might even be a string like
188 * `{{example}}` when a template name is dynamically generated.
189 */
190 ve.dm.MWTemplateSpecModel.prototype.getLabel = function () {
191 let title = this.template.getTemplateDataQueryTitle();
192 if ( title ) {
193 try {
194 // Normalize and remove namespace prefix if in the Template: namespace
195 title = new mw.Title( title )
196 .getRelativeText( mw.config.get( 'wgNamespaceIds' ).template );
197 } catch ( e ) { }
198 }
199 return title || this.template.getTarget().wt;
200 };
201
202 /**
203 * @param {string} [languageCode]
204 * @return {string|null} Template description or null if not available
205 */
206 ve.dm.MWTemplateSpecModel.prototype.getDescription = function ( languageCode ) {
207 return this.constructor.static.getLocalValue( this.templateData.description || null, languageCode );
208 };
209
210 /**
211 * True it the template does have any user-provided documentation. Note that undocumented templates
212 * can still have auto-detected `params` and a `paramOrder`, while documented templates might not
213 * have `params`. Use `{@see getDocumentedParameterOrder()}.length` to differentiate.
214 *
215 * @return {boolean}
216 */
217 ve.dm.MWTemplateSpecModel.prototype.isDocumented = function () {
218 return !this.templateData.notemplatedata;
219 };
220
221 /**
222 * Preferred order of parameters via TemplateData, without aliases or undocumented parameters. Empty
223 * if the template is not documented. Otherwise the explicit `paramOrder` if given, or the order of
224 * parameters as they appear in TemplateData. Returns a copy, i.e. it's safe to manipulate the
225 * array.
226 *
227 * @return {string[]} Preferred order of parameters via TemplateData, if given
228 */
229 ve.dm.MWTemplateSpecModel.prototype.getDocumentedParameterOrder = function () {
230 return Array.isArray( this.templateData.paramOrder ) ?
231 this.templateData.paramOrder.filter( ( name ) => name ) :
232 Object.keys( this.templateData.params );
233 };
234
235 /**
236 * The returned array is a copy, i.e. it's safe to manipulate.
237 *
238 * @return {string[]}
239 */
240 ve.dm.MWTemplateSpecModel.prototype.getUndocumentedParameterNames = function () {
241 const documentedParameters = this.templateData.params;
242
243 return this.getKnownParameterNames().filter( ( name ) => !( name in documentedParameters ) );
244 };
245
246 /**
247 * Same as {@see getKnownParameterNames}, but in a canonical order that's always the same, unrelated
248 * to how the parameters appear in the wikitext. Primary parameter names documented via TemplateData
249 * are first, in their documented order. Undocumented parameters are sorted with numeric names
250 * first, followed by alphabetically sorted names.
251 *
252 * The returned array is a copy, i.e. it's safe to manipulate.
253 *
254 * @return {string[]}
255 */
256 ve.dm.MWTemplateSpecModel.prototype.getCanonicalParameterOrder = function () {
257 const undocumentedParameters = this.getUndocumentedParameterNames();
258
259 undocumentedParameters.sort( ( a, b ) => {
260 if ( isNaN( a ) ) {
261 // If a and b are string, order alphabetically, otherwise numbers before strings
262 return isNaN( b ) ? a.localeCompare( b ) : 1;
263 } else {
264 // If a and b are numeric, order incrementally, otherwise numbers before strings
265 return !isNaN( b ) ? a - b : -1;
266 }
267 } );
268
269 return this.getDocumentedParameterOrder().concat( undocumentedParameters );
270 };
271
272 /**
273 * Check if a parameter name or alias was seen before. This includes parameters and aliases
274 * documented via TemplateData as well as undocumented parameters, e.g. from the original template
275 * invocation. When undocumented parameters are removed from the linked {@see ve.dm.MWTemplateModel}
276 * they are still known and will still be offered via {@see getKnownParameterNames} for the lifetime
277 * of this object.
278 *
279 * @param {string} name Parameter name or alias
280 * @return {boolean}
281 */
282 ve.dm.MWTemplateSpecModel.prototype.isKnownParameterOrAlias = function ( name ) {
283 return name in this.seenParameterNames || name in this.aliases;
284 };
285
286 /**
287 * @param {string} name Parameter name or alias
288 * @return {boolean}
289 */
290 ve.dm.MWTemplateSpecModel.prototype.isParameterAlias = function ( name ) {
291 return name in this.aliases;
292 };
293
294 /**
295 * @param {string} name Parameter name or alias
296 * @return {boolean}
297 */
298 ve.dm.MWTemplateSpecModel.prototype.isParameterDocumented = function ( name ) {
299 return name in this.templateData.params || name in this.aliases;
300 };
301
302 /**
303 * @param {string} name Parameter name or alias
304 * @param {string} [languageCode]
305 * @return {string} Descriptive label of the parameter, if given. Otherwise the alias or parameter
306 * name as is.
307 */
308 ve.dm.MWTemplateSpecModel.prototype.getParameterLabel = function ( name, languageCode ) {
309 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
310 return this.constructor.static.getLocalValue( param && param.label || name, languageCode );
311 };
312
313 /**
314 * @param {string} name Parameter name or alias
315 * @param {string} [languageCode]
316 * @return {string|null}
317 */
318 ve.dm.MWTemplateSpecModel.prototype.getParameterDescription = function ( name, languageCode ) {
319 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
320 return this.constructor.static.getLocalValue( param && param.description || null, languageCode );
321 };
322
323 /**
324 * @param {string} name Parameter name or alias
325 * @return {string[]}
326 */
327 ve.dm.MWTemplateSpecModel.prototype.getParameterSuggestedValues = function ( name ) {
328 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
329 return param && param.suggestedvalues || [];
330 };
331
332 /**
333 * The default value will be placed in the input field when the parameter is added. The user can
334 * edit or even remove it.
335 *
336 * @param {string} name Parameter name or alias
337 * @return {string} e.g. "{{PAGENAME}}"
338 */
339 ve.dm.MWTemplateSpecModel.prototype.getParameterDefaultValue = function ( name ) {
340 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
341 return param && param.default || '';
342 };
343
344 /**
345 * @param {string} name Parameter name or alias
346 * @param {string} [languageCode]
347 * @return {string|null}
348 */
349 ve.dm.MWTemplateSpecModel.prototype.getParameterExampleValue = function ( name, languageCode ) {
350 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
351 return this.constructor.static.getLocalValue( param && param.example || null, languageCode );
352 };
353
354 /**
355 * The auto-value will be used by the template in case the user doesn't provide a value. In
356 * VisualEditor this is only for documentation and should not appear in a serialization.
357 *
358 * @param {string} name Parameter name or alias
359 * @return {string}
360 */
361 ve.dm.MWTemplateSpecModel.prototype.getParameterAutoValue = function ( name ) {
362 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
363 return param && param.autovalue || '';
364 };
365
366 /**
367 * @param {string} name Parameter name or alias
368 * @return {string} e.g. "string"
369 */
370 ve.dm.MWTemplateSpecModel.prototype.getParameterType = function ( name ) {
371 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
372 return param && param.type || 'string';
373 };
374
375 /**
376 * Warning, this does not return a copy. Don't manipulate the returned array.
377 *
378 * @param {string} name Parameter name or alias
379 * @return {string[]} Alternate parameter names
380 */
381 ve.dm.MWTemplateSpecModel.prototype.getParameterAliases = function ( name ) {
382 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
383 return param && param.aliases || [];
384 };
385
386 /**
387 * Get the parameter name, resolving an alias.
388 *
389 * If a parameter is not an alias of another, the output will be the same as the input.
390 *
391 * @param {string} name Parameter name or alias
392 * @return {string}
393 */
394 ve.dm.MWTemplateSpecModel.prototype.getPrimaryParameterName = function ( name ) {
395 return this.aliases[ name ] || name;
396 };
397
398 /**
399 * @param {string} name Parameter name or alias
400 * @return {boolean}
401 */
402 ve.dm.MWTemplateSpecModel.prototype.isParameterRequired = function ( name ) {
403 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
404 return !!( param && param.required );
405 };
406
407 /**
408 * @param {string} name Parameter name or alias
409 * @return {boolean}
410 */
411 ve.dm.MWTemplateSpecModel.prototype.isParameterSuggested = function ( name ) {
412 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
413 return !!( param && param.suggested );
414 };
415
416 /**
417 * @param {string} name Parameter name or alias
418 * @return {boolean}
419 */
420 ve.dm.MWTemplateSpecModel.prototype.isParameterDeprecated = function ( name ) {
421 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
422 return !!( param && ( param.deprecated || typeof param.deprecated === 'string' ) );
423 };
424
425 /**
426 * @param {string} name Parameter name or alias
427 * @return {string} Explaining of why parameter is deprecated, empty if parameter is either not
428 * deprecated or no description has been specified
429 */
430 ve.dm.MWTemplateSpecModel.prototype.getParameterDeprecationDescription = function ( name ) {
431 const param = this.templateData.params[ this.getPrimaryParameterName( name ) ];
432 return param && typeof param.deprecated === 'string' ? param.deprecated : '';
433 };
434
435 /**
436 * Get all known primary parameter names, without aliases, in their original order as they became
437 * known (usually but not necessarily the order in which they appear in the template). This still
438 * includes undocumented parameters that have been part of the template at some point during the
439 * lifetime of this object, but have been removed from the linked {@see ve.dm.MWTemplateModel} in
440 * the meantime.
441 *
442 * The returned array is a copy, i.e. it's safe to manipulate.
443 *
444 * @return {string[]} Primary parameter names
445 */
446 ve.dm.MWTemplateSpecModel.prototype.getKnownParameterNames = function () {
447 return Object.keys( this.seenParameterNames );
448 };
449
450 /**
451 * @return {ve.dm.MWTemplateSpecModel.Set[]}
452 */
453 ve.dm.MWTemplateSpecModel.prototype.getParameterSets = function () {
454 return this.templateData.sets || [];
455 };
456
457 /**
458 * See https://www.mediawiki.org/wiki/Extension:TemplateData#Maps_object
459 *
460 * @return {Object.<string,Object>}
461 */
462 ve.dm.MWTemplateSpecModel.prototype.getMaps = function () {
463 return this.templateData.maps || {};
464 };