2013-06-11 19:16:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWTemplateSpecModel class.
|
|
|
|
*
|
2016-01-03 22:56:59 +00:00
|
|
|
* @copyright 2011-2016 VisualEditor Team and others; see AUTHORS.txt
|
2013-06-11 19:16:04 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaWiki template specification.
|
|
|
|
*
|
2013-06-19 20:00:11 +00:00
|
|
|
* See https://raw.github.com/wikimedia/mediawiki-extensions-TemplateData/master/spec.templatedata.json
|
|
|
|
* for the latest version of the TemplateData specification.
|
|
|
|
*
|
2013-06-11 19:16:04 +00:00
|
|
|
* @class
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWTemplateModel} template Template
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel = function VeDmMWTemplateSpecModel( template ) {
|
|
|
|
// Properties
|
|
|
|
this.template = template;
|
|
|
|
this.description = null;
|
|
|
|
this.params = {};
|
2014-01-14 20:15:29 +00:00
|
|
|
this.paramOrder = [];
|
2013-06-11 19:16:04 +00:00
|
|
|
this.sets = [];
|
2014-10-21 10:45:03 +00:00
|
|
|
this.maps = {};
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.fill();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
* @param {string[][]} [data.sets] Lists of param sets
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.extend = function ( data ) {
|
2013-07-10 19:04:11 +00:00
|
|
|
var key, param, i, len;
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
if ( data.description !== null ) {
|
|
|
|
this.description = data.description;
|
|
|
|
}
|
2014-09-24 00:58:09 +00:00
|
|
|
if ( Array.isArray( data.paramOrder ) ) {
|
2014-01-14 20:15:29 +00:00
|
|
|
this.paramOrder = data.paramOrder.slice();
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
if ( ve.isPlainObject( data.params ) ) {
|
|
|
|
for ( key in data.params ) {
|
2013-07-10 19:04:11 +00:00
|
|
|
// Pre-fill spec
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( !this.params[ key ] ) {
|
|
|
|
this.params[ key ] = this.getDefaultParameterSpec( key );
|
2013-07-10 19:04:11 +00:00
|
|
|
}
|
2015-08-19 17:33:02 +00:00
|
|
|
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
|
2013-07-10 19:04:11 +00:00
|
|
|
if ( param.aliases.length ) {
|
|
|
|
for ( i = 0, len = param.aliases.length; i < len; i++ ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
this.params[ param.aliases[ i ] ] = param;
|
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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill from template.
|
|
|
|
*
|
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.
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.fill = function () {
|
|
|
|
var key;
|
|
|
|
|
|
|
|
for ( key in this.template.getParameters() ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( key && !this.params[ key ] ) {
|
|
|
|
this.params[ key ] = this.getDefaultParameterSpec( key );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default spec for a parameter.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object} Parameter spec
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getDefaultParameterSpec = function ( name ) {
|
|
|
|
return {
|
2014-08-22 20:50:48 +00:00
|
|
|
label: name,
|
|
|
|
description: null,
|
|
|
|
default: '',
|
|
|
|
type: 'string',
|
|
|
|
aliases: [],
|
|
|
|
name: name,
|
|
|
|
required: false,
|
|
|
|
suggested: false,
|
|
|
|
deprecated: false
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get template label.
|
|
|
|
*
|
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 () {
|
2013-06-19 01:33:58 +00:00
|
|
|
var titleObj,
|
|
|
|
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
|
|
|
|
titleObj = new mw.Title( title );
|
2014-09-22 17:48:42 +00:00
|
|
|
title = titleObj.getRelativeText( 10 );
|
2013-06-18 19:42:10 +00:00
|
|
|
} catch ( e ) { }
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
return title || target.wt;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get template description.
|
|
|
|
*
|
2013-11-13 00:11:30 +00:00
|
|
|
* @param {string} [lang] Language to get description in
|
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
|
|
|
*/
|
2013-11-13 00:11:30 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getDescription = function ( lang ) {
|
|
|
|
var value = this.description;
|
|
|
|
return ve.isPlainObject( value ) ? OO.ui.getLocalValue( value, lang ) : value;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2014-01-14 20:15:29 +00:00
|
|
|
/**
|
|
|
|
* Get parameter order.
|
|
|
|
*
|
|
|
|
* @method
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string[]} Canonically ordered parameter names
|
2014-01-14 20:15:29 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterOrder = function () {
|
|
|
|
return this.paramOrder.slice();
|
|
|
|
};
|
|
|
|
|
2013-07-10 19:04:11 +00:00
|
|
|
/**
|
2013-07-11 00:33:21 +00:00
|
|
|
* Check if a parameter name is known.
|
|
|
|
*
|
|
|
|
* Could be a primary name or alias.
|
2013-07-10 19:04:11 +00:00
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {boolean} Parameter name is known
|
2013-07-10 19:04:11 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterKnown = function ( name ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.params[ name ] !== undefined;
|
2013-07-10 19:04:11 +00:00
|
|
|
};
|
|
|
|
|
2013-07-11 00:33:21 +00:00
|
|
|
/**
|
|
|
|
* Check if a parameter name is an alias.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {boolean} Parameter name is an alias
|
2013-07-11 00:33:21 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterAlias = function ( name ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.params[ name ] !== undefined && this.params[ name ].name !== name;
|
2013-07-11 00:33:21 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* Get a parameter label.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2013-11-13 00:11:30 +00:00
|
|
|
* @param {string} [lang] Language to get label in
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string} Parameter label
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2013-11-13 00:11:30 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterLabel = function ( name, lang ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
var value = this.params[ name ].label || name;
|
2013-11-13 00:11:30 +00:00
|
|
|
return ve.isPlainObject( value ) ? OO.ui.getLocalValue( value, lang ) : value;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a parameter description.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2013-11-13 00:11:30 +00:00
|
|
|
* @param {string} [lang] Language to get description
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string|null} Parameter description
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2013-11-13 00:11:30 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterDescription = function ( name, lang ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
var value = this.params[ name ].description;
|
2013-11-13 00:11:30 +00:00
|
|
|
return ve.isPlainObject( value ) ? OO.ui.getLocalValue( value, lang ) : value;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a parameter value.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string} Default parameter value
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterDefaultValue = function ( name ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.params[ name ].default;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2015-03-04 23:31:05 +00:00
|
|
|
/**
|
|
|
|
* Get a parameter example value.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
|
|
|
* @param {string} [lang] Language to get description
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string} Example parameter value
|
2015-03-04 23:31:05 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterExampleValue = function ( name, lang ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
var value = this.params[ name ].example;
|
2015-03-04 23:31:05 +00:00
|
|
|
return ve.isPlainObject( value ) ? OO.ui.getLocalValue( value, lang ) : value;
|
|
|
|
};
|
|
|
|
|
2014-07-15 07:08:09 +00:00
|
|
|
/**
|
|
|
|
* Get a parameter auto value.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string} Auto-value for the parameter
|
2014-07-15 07:08:09 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterAutoValue = function ( name ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.params[ name ].autovalue;
|
2014-07-15 07:08:09 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* Get a parameter type.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string} Parameter type
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterType = function ( name ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.params[ name ].type;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parameter aliases.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
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 ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.params[ name ].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
|
|
|
*
|
2013-07-11 00:33:21 +00:00
|
|
|
* @param {string} name Parameter alias
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string} Parameter name
|
2013-06-28 21:38:40 +00:00
|
|
|
*/
|
2013-07-11 00:33:21 +00:00
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterName = function ( name ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.params[ name ].name;
|
2013-06-28 21:38:40 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* Check if parameter is required.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {boolean} Parameter is required
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterRequired = function ( name ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return !!this.params[ name ].required;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2014-04-28 19:10:31 +00:00
|
|
|
/**
|
2015-12-09 16:47:13 +00:00
|
|
|
* Check if parameter is suggested.
|
2014-04-28 19:10:31 +00:00
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {boolean} Parameter is suggested
|
2014-04-28 19:10:31 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterSuggested = function ( name ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return !!this.params[ name ].suggested;
|
2014-04-28 19:10:31 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* Check if parameter is deprecated.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {boolean} Parameter is deprecated
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterDeprecated = function ( name ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.params[ name ].deprecated !== false;
|
2013-06-19 20:00:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parameter deprecation description.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
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 ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return typeof this.params[ name ].deprecated === 'string' ?
|
|
|
|
this.params[ name ].deprecated : '';
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-07-11 00:33:21 +00:00
|
|
|
* Get all primary parameter names.
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string[]} Parameter names
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterNames = function () {
|
2013-07-11 00:33:21 +00:00
|
|
|
var name,
|
|
|
|
names = [];
|
|
|
|
|
|
|
|
for ( name in this.params ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( this.params[ name ].name === name ) {
|
2013-07-11 00:33:21 +00:00
|
|
|
names.push( name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return names;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parameter sets.
|
|
|
|
*
|
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 () {
|
|
|
|
return this.sets;
|
|
|
|
};
|
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 () {
|
|
|
|
return this.maps;
|
|
|
|
};
|