2013-06-11 19:16:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWTemplateSpecModel class.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2013-06-11 19:16:04 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-06-03 16:25:15 +00:00
|
|
|
* Holds a mixture of:
|
|
|
|
* - A copy of a template's specification as it is documented via TemplateData.
|
|
|
|
* - Undocumented parameters that appear in a template invocation, {@see fillFromTemplate}.
|
2021-06-25 14:46:59 +00:00
|
|
|
* - Documented aliases are also considered valid, known parameter names. Use
|
|
|
|
* {@see isParameterAlias} to differentiate between the two.
|
2021-06-03 16:25:15 +00:00
|
|
|
* Therefore this is not the original specification but an accessor to the documentation for an
|
|
|
|
* individual template invocation. It's possible different for every invocation.
|
|
|
|
*
|
|
|
|
* Meant to be in a 1:1 relationship to {@see ve.dm.MWTemplateModel}.
|
|
|
|
*
|
|
|
|
* The actual, unmodified specification can be found in {@see specCache} in
|
|
|
|
* {@see ve.dm.MWTransclusionModel}.
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
2020-12-10 15:47:48 +00:00
|
|
|
* See https://github.com/wikimedia/mediawiki-extensions-TemplateData/blob/master/Specification.md
|
2013-06-19 20:00:11 +00:00
|
|
|
* for the latest version of the TemplateData specification.
|
|
|
|
*
|
2013-06-11 19:16:04 +00:00
|
|
|
* @class
|
|
|
|
*
|
|
|
|
* @constructor
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {ve.dm.MWTemplateModel} template
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel = function VeDmMWTemplateSpecModel( template ) {
|
|
|
|
// Properties
|
|
|
|
this.template = template;
|
|
|
|
this.params = {};
|
2021-06-24 14:24:04 +00:00
|
|
|
this.aliases = {};
|
2021-06-23 14:31:07 +00:00
|
|
|
this.canonicalOrder = [];
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
// Initialization
|
2018-11-21 12:33:32 +00:00
|
|
|
this.fillFromTemplate();
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2018-09-23 16:36:05 +00:00
|
|
|
OO.initClass( ve.dm.MWTemplateSpecModel );
|
|
|
|
|
|
|
|
/* Static methods */
|
|
|
|
|
2021-06-24 12:44:10 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {string|Object.<string,string>} stringOrObject
|
2021-06-24 15:44:07 +00:00
|
|
|
* @param {string} [languageCode]
|
2021-06-24 12:44:10 +00:00
|
|
|
* @return {string|undefined}
|
|
|
|
*/
|
2021-06-24 15:44:07 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.static.getLocalValue = function ( stringOrObject, languageCode ) {
|
2018-09-23 16:36:05 +00:00
|
|
|
return stringOrObject && typeof stringOrObject === 'object' ?
|
2021-06-24 15:44:07 +00:00
|
|
|
OO.ui.getLocalValue( stringOrObject, languageCode ) :
|
2018-09-23 16:36:05 +00:00
|
|
|
stringOrObject;
|
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extend with template spec data.
|
|
|
|
*
|
|
|
|
* Template spec data is available from the TemplateData extension's API. Extension is passive so
|
|
|
|
* any filled in values are not overwritten unless new values are available. This prevents changes
|
|
|
|
* in the API or fill methods from causing issues.
|
|
|
|
*
|
|
|
|
* @param {Object} data Template spec data
|
|
|
|
* @param {string} [data.description] Template description
|
2014-01-14 20:15:29 +00:00
|
|
|
* @param {string[]} [data.paramOrder] Canonically ordered parameter names
|
2013-06-11 19:16:04 +00:00
|
|
|
* @param {Object} [data.params] Template param specs keyed by param name
|
2016-10-28 19:02:36 +00:00
|
|
|
* @param {Array} [data.sets] Lists of param sets
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.extend = function ( data ) {
|
|
|
|
if ( data.description !== null ) {
|
|
|
|
this.description = data.description;
|
|
|
|
}
|
2018-09-23 16:36:05 +00:00
|
|
|
if ( data.params ) {
|
2021-06-23 14:31:07 +00:00
|
|
|
this.canonicalOrder = Array.isArray( data.paramOrder ) ?
|
|
|
|
data.paramOrder :
|
|
|
|
Object.keys( data.params );
|
|
|
|
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var key in data.params ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( !this.params[ key ] ) {
|
2021-06-24 14:24:04 +00:00
|
|
|
this.params[ key ] = {};
|
2013-07-10 19:04:11 +00:00
|
|
|
}
|
2021-06-21 08:10:27 +00:00
|
|
|
var param = this.params[ key ];
|
2013-07-10 19:04:11 +00:00
|
|
|
// Extend existing spec
|
2015-08-19 17:33:02 +00:00
|
|
|
ve.extendObject( true, this.params[ key ], data.params[ key ] );
|
2013-06-28 21:38:40 +00:00
|
|
|
// Add aliased references
|
2021-06-24 13:43:17 +00:00
|
|
|
if ( param.aliases ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var i = 0; i < param.aliases.length; i++ ) {
|
2021-06-24 14:24:04 +00:00
|
|
|
var alias = param.aliases[ i ];
|
|
|
|
this.aliases[ alias ] = key;
|
|
|
|
// Remove duplicate specification when it turns out the template used an alias
|
|
|
|
delete this.params[ alias ];
|
2013-06-24 00:25:52 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-28 21:38:40 +00:00
|
|
|
this.sets = data.sets;
|
2015-06-02 01:00:48 +00:00
|
|
|
if ( data.maps ) {
|
|
|
|
this.maps = data.maps;
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-12-09 16:47:13 +00:00
|
|
|
* Filling is passive, so existing information is never overwritten. The spec should be re-filled
|
2013-06-11 19:16:04 +00:00
|
|
|
* after a parameter is added to ensure it's still complete, and this is safe because existing data
|
|
|
|
* is never overwritten.
|
|
|
|
*/
|
2018-11-21 12:33:32 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.fillFromTemplate = function () {
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var key in this.template.getParameters() ) {
|
2021-06-24 14:24:04 +00:00
|
|
|
// Ignore placeholder parameters with no name
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( key && !this.params[ key ] ) {
|
2021-06-24 14:24:04 +00:00
|
|
|
// There is no information other than the names of the parameters, that they exist, and
|
|
|
|
// in which order
|
|
|
|
this.params[ key ] = {};
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string} Template label
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getLabel = function () {
|
2021-06-21 08:10:27 +00:00
|
|
|
var title = this.template.getTitle(),
|
2013-06-18 19:42:10 +00:00
|
|
|
target = this.template.getTarget();
|
|
|
|
|
|
|
|
if ( title ) {
|
|
|
|
try {
|
2013-06-19 00:04:16 +00:00
|
|
|
// Normalize and remove namespace prefix if in the Template: namespace
|
2021-06-21 08:10:27 +00:00
|
|
|
title = new mw.Title( title )
|
|
|
|
.getRelativeText( mw.config.get( 'wgNamespaceIds' ).template );
|
2013-06-18 19:42:10 +00:00
|
|
|
} catch ( e ) { }
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
return title || target.wt;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-06-24 15:44:07 +00:00
|
|
|
* @param {string} [languageCode]
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string|null} Template description or null if not available
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2021-06-24 15:44:07 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getDescription = function ( languageCode ) {
|
|
|
|
return this.constructor.static.getLocalValue( this.description || null, languageCode );
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2014-01-14 20:15:29 +00:00
|
|
|
/**
|
|
|
|
* Get parameter order.
|
|
|
|
*
|
2021-06-23 14:31:07 +00:00
|
|
|
* @return {string[]} Canonically ordered parameter names: the explicit
|
|
|
|
* `paramOrder` if given, otherwise the order of parameters as they appear in
|
|
|
|
* TemplateData.
|
2014-01-14 20:15:29 +00:00
|
|
|
*/
|
2021-06-23 14:31:07 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getCanonicalParameterOrder = function () {
|
|
|
|
return this.canonicalOrder.slice();
|
2014-01-14 20:15:29 +00:00
|
|
|
};
|
|
|
|
|
2013-07-10 19:04:11 +00:00
|
|
|
/**
|
2021-06-24 12:53:39 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
|
|
|
* @return {boolean}
|
2013-07-10 19:04:11 +00:00
|
|
|
*/
|
2021-06-24 12:53:39 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isKnownParameterOrAlias = function ( name ) {
|
2021-06-24 14:24:04 +00:00
|
|
|
return name in this.params || name in this.aliases;
|
2013-07-10 19:04:11 +00:00
|
|
|
};
|
|
|
|
|
2013-07-11 00:33:21 +00:00
|
|
|
/**
|
|
|
|
* Check if a parameter name is an alias.
|
|
|
|
*
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-06-24 15:41:23 +00:00
|
|
|
* @return {boolean}
|
2013-07-11 00:33:21 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterAlias = function ( name ) {
|
2021-06-24 14:24:04 +00:00
|
|
|
return name in this.aliases;
|
2013-07-11 00:33:21 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-06-24 15:44:07 +00:00
|
|
|
* @param {string} [languageCode]
|
2021-06-24 15:41:23 +00:00
|
|
|
* @return {string}
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2021-06-24 15:44:07 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterLabel = function ( name, languageCode ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
2021-06-24 15:44:07 +00:00
|
|
|
return this.constructor.static.getLocalValue( param && param.label || name, languageCode );
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-06-24 15:44:07 +00:00
|
|
|
* @param {string} [languageCode]
|
2021-06-24 15:41:23 +00:00
|
|
|
* @return {string|null}
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2021-06-24 15:44:07 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterDescription = function ( name, languageCode ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
2021-06-24 15:44:07 +00:00
|
|
|
return this.constructor.static.getLocalValue( param && param.description || null, languageCode );
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2021-03-11 16:12:45 +00:00
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-06-24 15:41:23 +00:00
|
|
|
* @return {string[]}
|
2021-03-11 16:12:45 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterSuggestedValues = function ( name ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
|
|
|
return param && param.suggestedvalues || [];
|
2021-03-11 16:12:45 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-06-24 15:41:23 +00:00
|
|
|
* @return {string}
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterDefaultValue = function ( name ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
2021-06-24 13:43:17 +00:00
|
|
|
return param && param.default || '';
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2015-03-04 23:31:05 +00:00
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-06-24 15:44:07 +00:00
|
|
|
* @param {string} [languageCode]
|
2021-06-24 13:43:17 +00:00
|
|
|
* @return {string|null}
|
2015-03-04 23:31:05 +00:00
|
|
|
*/
|
2021-06-24 15:44:07 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterExampleValue = function ( name, languageCode ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
2021-06-24 15:44:07 +00:00
|
|
|
return this.constructor.static.getLocalValue( param && param.example || null, languageCode );
|
2015-03-04 23:31:05 +00:00
|
|
|
};
|
|
|
|
|
2014-07-15 07:08:09 +00:00
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-06-24 15:41:23 +00:00
|
|
|
* @return {string}
|
2014-07-15 07:08:09 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterAutoValue = function ( name ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
2021-06-24 13:43:17 +00:00
|
|
|
return param && param.autovalue || '';
|
2014-07-15 07:08:09 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-06-24 15:41:23 +00:00
|
|
|
* @return {string} e.g. "string"
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterType = function ( name ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
|
|
|
return param && param.type || 'string';
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string[]} Alternate parameter names
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterAliases = function ( name ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
|
|
|
return param && param.aliases || [];
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2013-06-28 21:38:40 +00:00
|
|
|
/**
|
2013-07-11 00:33:21 +00:00
|
|
|
* Get the parameter name, resolving an alias.
|
2013-06-28 21:38:40 +00:00
|
|
|
*
|
2013-07-11 00:33:21 +00:00
|
|
|
* If a parameter is not an alias of another, the output will be the same as the input.
|
2013-06-28 21:38:40 +00:00
|
|
|
*
|
2021-06-24 12:48:29 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
|
|
|
* @return {string}
|
2013-06-28 21:38:40 +00:00
|
|
|
*/
|
2021-06-24 12:48:29 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getPrimaryParameterName = function ( name ) {
|
2021-06-24 14:24:04 +00:00
|
|
|
return this.aliases[ name ] || name;
|
2013-06-28 21:38:40 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-06-24 15:41:23 +00:00
|
|
|
* @return {boolean}
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterRequired = function ( name ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
|
|
|
return !!( param && param.required );
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2014-04-28 19:10:31 +00:00
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-06-24 15:41:23 +00:00
|
|
|
* @return {boolean}
|
2014-04-28 19:10:31 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterSuggested = function ( name ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
|
|
|
return !!( param && param.suggested );
|
2014-04-28 19:10:31 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-06-24 15:41:23 +00:00
|
|
|
* @return {boolean}
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterDeprecated = function ( name ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
|
|
|
return !!( param && ( param.deprecated || typeof param.deprecated === 'string' ) );
|
2013-06-19 20:00:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string} Explaining of why parameter is deprecated, empty if parameter is either not
|
2014-04-25 23:50:21 +00:00
|
|
|
* deprecated or no description has been specified
|
2013-06-19 20:00:11 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterDeprecationDescription = function ( name ) {
|
2021-06-24 15:38:24 +00:00
|
|
|
var param = this.params[ this.getPrimaryParameterName( name ) ];
|
|
|
|
return param && typeof param.deprecated === 'string' ? param.deprecated : '';
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-06-24 14:24:04 +00:00
|
|
|
* Get all primary parameter names, without aliases. Warning:
|
|
|
|
* - This is not necessarily in the original order as the parameters appeared in the template.
|
|
|
|
* - When a parameter is known to be an alias, the alias is resolved.
|
|
|
|
* - This includes parameters that appear in the template but aren't documented via TemplateData.
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
2021-06-24 14:24:04 +00:00
|
|
|
* @return {string[]}
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterNames = function () {
|
2021-06-24 14:24:04 +00:00
|
|
|
return Object.keys( this.params );
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object[]} Lists of parameter set descriptors
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterSets = function () {
|
2021-06-24 13:04:15 +00:00
|
|
|
return this.sets || [];
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
2014-10-21 10:45:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get map describing relationship between another content type and the parameters.
|
|
|
|
*
|
|
|
|
* @return {Object} Object with application property maps to parameters keyed to application name.
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getMaps = function () {
|
2021-06-24 13:04:15 +00:00
|
|
|
return this.maps || {};
|
2014-10-21 10:45:03 +00:00
|
|
|
};
|