2013-06-11 19:16:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWTemplateSpecModel class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2013-06-18 19:42:10 +00:00
|
|
|
/*global mw */
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* 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 = {};
|
|
|
|
this.sets = [];
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.fill();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the correct value from a message property.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @static
|
2013-06-19 20:00:11 +00:00
|
|
|
* @param {string|Object|null} val Messsage or object with messages keyed by language
|
2013-06-11 19:16:04 +00:00
|
|
|
* @param {Mixed} [fallback=null] Value to use if message is not available
|
|
|
|
* @param {string} [lang] Language to prefer, user interface language will be used by default
|
2013-06-17 10:50:24 +00:00
|
|
|
* @returns {string} Message text or fallback if not available
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.getMessage = function ( val, fallback, lang ) {
|
2013-08-20 19:59:54 +00:00
|
|
|
var i, len, langs = lang !== undefined ? [lang] : ve.init.platform.getUserLanguages();
|
2013-06-11 19:16:04 +00:00
|
|
|
if ( fallback === undefined ) {
|
|
|
|
fallback = null;
|
|
|
|
}
|
|
|
|
if ( ve.isPlainObject( val ) ) {
|
2013-08-20 19:59:54 +00:00
|
|
|
for ( i = 0, len = langs.length; i < len; i++ ) {
|
|
|
|
if ( val[langs[i]] ) {
|
|
|
|
return val[langs[i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fallback;
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
return typeof val === 'string' ? val : fallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Object} data Template spec data
|
|
|
|
* @param {string} [data.description] Template description
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
if ( ve.isPlainObject( data.params ) ) {
|
|
|
|
for ( key in data.params ) {
|
2013-07-10 19:04:11 +00:00
|
|
|
// Pre-fill spec
|
|
|
|
if ( !this.params[key] ) {
|
|
|
|
this.params[key] = this.getDefaultParameterSpec( key );
|
|
|
|
}
|
|
|
|
param = this.params[key];
|
|
|
|
// Extend existing spec
|
|
|
|
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++ ) {
|
|
|
|
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;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill from template.
|
|
|
|
*
|
|
|
|
* Filling is passive, so existing information is never overwitten. The spec should be re-filled
|
|
|
|
* after a parameter is added to ensure it's still complete, and this is safe because existing data
|
|
|
|
* is never overwritten.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.fill = function () {
|
|
|
|
var key;
|
|
|
|
|
|
|
|
for ( key in this.template.getParameters() ) {
|
|
|
|
if ( !this.params[key] ) {
|
|
|
|
this.params[key] = this.getDefaultParameterSpec( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default spec for a parameter.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Parameter name
|
|
|
|
* @returns {Object} Parameter spec
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getDefaultParameterSpec = function ( name ) {
|
|
|
|
return {
|
|
|
|
'label': { 'en': name },
|
|
|
|
'description': null,
|
|
|
|
'default': '',
|
|
|
|
'type': 'string',
|
|
|
|
'aliases': [],
|
2013-07-11 00:33:21 +00:00
|
|
|
'name': name,
|
2013-06-11 19:16:04 +00:00
|
|
|
'required': false,
|
|
|
|
'deprecated': false
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get template label.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {string} Template label
|
|
|
|
*/
|
|
|
|
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 );
|
2013-06-19 01:33:58 +00:00
|
|
|
if ( titleObj.getNamespaceId() === 10 ) {
|
|
|
|
// Template namespace, remove namespace prefix
|
|
|
|
title = titleObj.getNameText();
|
|
|
|
} else {
|
|
|
|
// Other namespace, already has a prefix
|
|
|
|
title = titleObj.getPrefixedText();
|
2013-06-19 00:04:16 +00:00
|
|
|
}
|
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.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {string|null} Template description or null if not available
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getDescription = function () {
|
|
|
|
return this.constructor.getMessage( this.description, null );
|
|
|
|
};
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Parameter name
|
2013-07-11 00:33:21 +00:00
|
|
|
* @returns {boolean} Parameter name is known
|
2013-07-10 19:04:11 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterKnown = function ( name ) {
|
|
|
|
return this.params[name] !== undefined;
|
|
|
|
};
|
|
|
|
|
2013-07-11 00:33:21 +00:00
|
|
|
/**
|
|
|
|
* Check if a parameter name is an alias.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Parameter name
|
|
|
|
* @returns {boolean} Parameter name is an alias
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterAlias = function ( name ) {
|
|
|
|
return this.params[name] !== undefined && this.params[name].name !== name;
|
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* Get a parameter label.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Parameter name
|
|
|
|
* @returns {string} Parameter label
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterLabel = function ( name ) {
|
|
|
|
return this.constructor.getMessage( this.params[name].label, name );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a parameter description.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Parameter name
|
2013-06-19 20:00:11 +00:00
|
|
|
* @returns {string|null} Parameter description
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterDescription = function ( name ) {
|
2013-06-19 20:00:11 +00:00
|
|
|
return this.constructor.getMessage( this.params[name].description );
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a parameter value.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Parameter name
|
|
|
|
* @returns {string} Default parameter value
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterDefaultValue = function ( name ) {
|
|
|
|
return this.params[name]['default'];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a parameter type.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Parameter name
|
|
|
|
* @returns {string} Parameter type
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterType = function ( name ) {
|
|
|
|
return this.params[name].type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parameter aliases.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Parameter name
|
|
|
|
* @returns {string[]} Alternate parameter names
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterAliases = function ( name ) {
|
|
|
|
return this.params[name].aliases;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @method
|
2013-07-11 00:33:21 +00:00
|
|
|
* @param {string} name Parameter alias
|
|
|
|
* @returns {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 ) {
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Parameter name
|
|
|
|
* @returns {boolean} Parameter is required
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterRequired = function ( name ) {
|
|
|
|
return !!this.params[name].required;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if parameter is deprecated.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Parameter name
|
|
|
|
* @returns {boolean} Parameter is deprecated
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.isParameterDeprecated = function ( name ) {
|
2013-06-19 20:00:11 +00:00
|
|
|
return this.params[name].deprecated !== false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parameter deprecation description.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {string} name Parameter name
|
|
|
|
* @returns {string} Explaining of why parameter is deprecated, empty if parameter is not deprecated
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterDeprecationDescription = function ( name ) {
|
|
|
|
return 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
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {string[]} Parameter names
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterNames = function () {
|
2013-07-11 00:33:21 +00:00
|
|
|
var name,
|
|
|
|
names = [];
|
|
|
|
|
|
|
|
for ( name in this.params ) {
|
|
|
|
if ( this.params[name].name === name ) {
|
|
|
|
names.push( name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return names;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get parameter sets.
|
|
|
|
*
|
|
|
|
* @method
|
2013-06-19 20:00:11 +00:00
|
|
|
* @returns {Object[]} Lists of parameter set descriptors
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateSpecModel.prototype.getParameterSets = function () {
|
|
|
|
return this.sets;
|
|
|
|
};
|