2013-06-11 19:16:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWTemplateModel 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaWiki template model.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.dm.MWTransclusionPartModel
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWTransclusionModel} transclusion Transclusion
|
|
|
|
* @param {Object} target Template target
|
|
|
|
* @param {string} target.wt Original wikitext of target
|
|
|
|
* @param {string} [target.href] Hypertext reference to target
|
|
|
|
*/
|
2013-12-02 20:10:55 +00:00
|
|
|
ve.dm.MWTemplateModel = function VeDmMWTemplateModel( transclusion, target ) {
|
2013-06-11 19:16:04 +00:00
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.dm.MWTemplateModel.super.call( this, transclusion );
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.target = target;
|
2013-10-09 21:20:51 +00:00
|
|
|
|
|
|
|
// TODO: Either here or in uses of this constructor we need to validate the title
|
2020-03-20 17:08:24 +00:00
|
|
|
this.title = target.href ? mw.libs.ve.normalizeParsoidResourceName( target.href ) : null;
|
2013-06-11 19:16:04 +00:00
|
|
|
this.sequence = null;
|
|
|
|
this.params = {};
|
|
|
|
this.spec = new ve.dm.MWTemplateSpecModel( this );
|
2013-07-12 00:23:33 +00:00
|
|
|
this.originalData = null;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.dm.MWTemplateModel, ve.dm.MWTransclusionPartModel );
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event add
|
2014-02-24 21:49:48 +00:00
|
|
|
* @param {ve.dm.MWParameterModel} param Added param
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event remove
|
2014-02-24 21:49:48 +00:00
|
|
|
* @param {ve.dm.MWParameterModel} param Removed param
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
|
2013-07-12 00:23:33 +00:00
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create from data.
|
|
|
|
*
|
|
|
|
* Data is in the format provided by Parsoid.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWTransclusionModel} transclusion Transclusion template is in
|
|
|
|
* @param {Object} data Template data
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {ve.dm.MWTemplateModel} New template model
|
2013-07-12 00:23:33 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.newFromData = function ( transclusion, data ) {
|
|
|
|
var key,
|
2015-01-11 17:38:36 +00:00
|
|
|
template = new ve.dm.MWTemplateModel( transclusion, data.target );
|
2013-07-12 00:23:33 +00:00
|
|
|
|
|
|
|
for ( key in data.params ) {
|
|
|
|
template.addParameter(
|
2015-08-19 17:33:02 +00:00
|
|
|
new ve.dm.MWParameterModel( template, key, data.params[ key ].wt )
|
2013-07-12 00:23:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
template.setOriginalData( data );
|
|
|
|
|
|
|
|
return template;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create from name.
|
|
|
|
*
|
|
|
|
* Name is equivalent to what would be entered between double brackets, defaulting to the Template
|
|
|
|
* namespace, using a leading colon to access other namespaces.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWTransclusionModel} transclusion Transclusion template is in
|
2014-10-30 16:24:04 +00:00
|
|
|
* @param {string|mw.Title} name Template name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {ve.dm.MWTemplateModel|null} New template model
|
2013-07-12 00:23:33 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.newFromName = function ( transclusion, name ) {
|
2014-11-10 20:03:10 +00:00
|
|
|
var href, title,
|
|
|
|
templateNs = mw.config.get( 'wgNamespaceIds' ).template;
|
2014-10-30 16:24:04 +00:00
|
|
|
if ( name instanceof mw.Title ) {
|
|
|
|
title = name;
|
2014-11-10 20:03:10 +00:00
|
|
|
name = title.getRelativeText( templateNs );
|
2014-10-30 16:24:04 +00:00
|
|
|
} else {
|
2014-11-10 20:03:10 +00:00
|
|
|
title = mw.Title.newFromText( name, templateNs );
|
2014-10-30 16:24:04 +00:00
|
|
|
}
|
|
|
|
if ( title !== null ) {
|
|
|
|
href = title.getPrefixedText();
|
2015-01-11 17:38:36 +00:00
|
|
|
return new ve.dm.MWTemplateModel( transclusion, { href: href, wt: name } );
|
2014-10-30 16:24:04 +00:00
|
|
|
}
|
2013-07-12 00:23:33 +00:00
|
|
|
|
2014-10-30 16:24:04 +00:00
|
|
|
return null;
|
2013-07-12 00:23:33 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get template target.
|
|
|
|
*
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object} Template target
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getTarget = function () {
|
|
|
|
return this.target;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get template title.
|
|
|
|
*
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string|null} Template title, if available
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getTitle = function () {
|
|
|
|
return this.title;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get template specification.
|
|
|
|
*
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {ve.dm.MWTemplateSpecModel} Template specification
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getSpec = function () {
|
|
|
|
return this.spec;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all params.
|
|
|
|
*
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object.<string,ve.dm.MWParameterModel>} Parameters keyed by name
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getParameters = function () {
|
|
|
|
return this.params;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a parameter.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {ve.dm.MWParameterModel} Parameter
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getParameter = function ( name ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.params[ name ];
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2013-07-10 19:04:11 +00:00
|
|
|
/**
|
|
|
|
* Check if a parameter exists.
|
|
|
|
*
|
|
|
|
* @param {string} name Parameter name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {boolean} Parameter exists
|
2013-07-10 19:04:11 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.hasParameter = function ( name ) {
|
2013-07-11 00:33:21 +00:00
|
|
|
var i, len, primaryName, names;
|
2013-07-10 19:04:11 +00:00
|
|
|
|
|
|
|
// Check if name (which may be an alias) is present in the template
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( this.params[ name ] ) {
|
2013-07-10 19:04:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the name is known at all
|
|
|
|
if ( this.spec.isParameterKnown( name ) ) {
|
2013-07-11 00:33:21 +00:00
|
|
|
primaryName = this.spec.getParameterName( name );
|
|
|
|
// Check for primary name (may be the same as name)
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( this.params[ primaryName ] ) {
|
2013-07-10 19:04:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Check for other aliases (may include name)
|
2013-07-11 00:33:21 +00:00
|
|
|
names = this.spec.getParameterAliases( primaryName );
|
2013-07-10 19:04:11 +00:00
|
|
|
for ( i = 0, len = names.length; i < len; i++ ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( this.params[ names[ i ] ] ) {
|
2013-07-10 19:04:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* Get ordered list of parameter names.
|
|
|
|
*
|
2015-12-09 16:47:13 +00:00
|
|
|
* Numeric names, whether strings or real numbers, are placed at the beginning, followed by
|
2013-06-11 19:16:04 +00:00
|
|
|
* alphabetically sorted names.
|
|
|
|
*
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {string[]} List of parameter names
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getParameterNames = function () {
|
2014-01-14 20:15:29 +00:00
|
|
|
var i, len, index, paramOrder, paramNames;
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
if ( !this.sequence ) {
|
2014-01-14 20:15:29 +00:00
|
|
|
paramOrder = this.spec.getParameterOrder();
|
2015-03-10 12:50:42 +00:00
|
|
|
paramNames = Object.keys( this.params );
|
2014-01-14 20:15:29 +00:00
|
|
|
|
|
|
|
this.sequence = [];
|
|
|
|
// Known parameters first
|
|
|
|
for ( i = 0, len = paramOrder.length; i < len; i++ ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
index = paramNames.indexOf( paramOrder[ i ] );
|
2014-01-14 20:15:29 +00:00
|
|
|
if ( index !== -1 ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
this.sequence.push( paramOrder[ i ] );
|
2014-01-14 20:15:29 +00:00
|
|
|
paramNames.splice( index, 1 );
|
|
|
|
}
|
|
|
|
}
|
2014-02-27 23:58:37 +00:00
|
|
|
// Unknown parameters in alpha-numeric order second, empty string at the very end
|
2014-01-14 20:15:29 +00:00
|
|
|
paramNames.sort( function ( a, b ) {
|
2015-08-19 18:05:01 +00:00
|
|
|
var aIsNaN = isNaN( a ),
|
|
|
|
bIsNaN = isNaN( b );
|
|
|
|
|
2014-02-27 23:58:37 +00:00
|
|
|
if ( a === '' ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ( b === '' ) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-06-11 19:16:04 +00:00
|
|
|
if ( aIsNaN && bIsNaN ) {
|
|
|
|
// Two strings
|
|
|
|
return a < b ? -1 : a === b ? 0 : 1;
|
|
|
|
}
|
|
|
|
if ( aIsNaN ) {
|
|
|
|
// A is a string
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ( bIsNaN ) {
|
|
|
|
// B is a string
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Two numbers
|
|
|
|
return a - b;
|
|
|
|
} );
|
2014-10-15 19:15:52 +00:00
|
|
|
ve.batchPush( this.sequence, paramNames );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
return this.sequence;
|
|
|
|
};
|
|
|
|
|
2014-03-01 00:39:02 +00:00
|
|
|
/**
|
|
|
|
* Get parameter from its ID.
|
|
|
|
*
|
|
|
|
* @param {string} id Parameter ID
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {ve.dm.MWParameterModel|null} Parameter with matching ID, null if no parameters match
|
2014-03-01 00:39:02 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getParameterFromId = function ( id ) {
|
|
|
|
var name;
|
|
|
|
|
|
|
|
for ( name in this.params ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( this.params[ name ].getId() === id ) {
|
|
|
|
return this.params[ name ];
|
2014-03-01 00:39:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* Add a parameter to template.
|
|
|
|
*
|
2014-02-24 21:49:48 +00:00
|
|
|
* @param {ve.dm.MWParameterModel} param Parameter to add
|
2013-10-22 17:54:59 +00:00
|
|
|
* @fires add
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2013-06-29 02:37:42 +00:00
|
|
|
ve.dm.MWTemplateModel.prototype.addParameter = function ( param ) {
|
|
|
|
var name = param.getName();
|
2013-06-11 19:16:04 +00:00
|
|
|
this.sequence = null;
|
2015-08-19 17:33:02 +00:00
|
|
|
this.params[ name ] = param;
|
2018-11-21 12:33:32 +00:00
|
|
|
this.spec.fillFromTemplate();
|
2014-08-22 20:50:48 +00:00
|
|
|
param.connect( this, { change: [ 'emit', 'change' ] } );
|
2013-06-11 19:16:04 +00:00
|
|
|
this.emit( 'add', param );
|
2013-12-09 19:05:57 +00:00
|
|
|
this.emit( 'change' );
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove parameter from template.
|
|
|
|
*
|
2014-02-24 21:49:48 +00:00
|
|
|
* @param {ve.dm.MWParameterModel} param Parameter to remove
|
2013-10-22 17:54:59 +00:00
|
|
|
* @fires remove
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2013-06-29 02:37:42 +00:00
|
|
|
ve.dm.MWTemplateModel.prototype.removeParameter = function ( param ) {
|
2013-06-11 19:16:04 +00:00
|
|
|
if ( param ) {
|
|
|
|
this.sequence = null;
|
2015-08-19 17:33:02 +00:00
|
|
|
delete this.params[ param.getName() ];
|
2013-12-09 19:05:57 +00:00
|
|
|
param.disconnect( this );
|
2013-06-11 19:16:04 +00:00
|
|
|
this.emit( 'remove', param );
|
2013-12-09 19:05:57 +00:00
|
|
|
this.emit( 'change' );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
};
|
2013-07-12 00:23:33 +00:00
|
|
|
|
|
|
|
/**
|
2016-05-13 19:43:47 +00:00
|
|
|
* @inheritdoc
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
2014-04-28 19:10:31 +00:00
|
|
|
ve.dm.MWTemplateModel.prototype.addPromptedParameters = function () {
|
2016-09-30 00:57:23 +00:00
|
|
|
var i, len, name, j, aliases, aliasLen, foundAlias,
|
2016-06-30 15:46:38 +00:00
|
|
|
addedCount = 0,
|
2013-12-02 20:10:55 +00:00
|
|
|
spec = this.getSpec(),
|
|
|
|
names = spec.getParameterNames();
|
|
|
|
|
|
|
|
for ( i = 0, len = names.length; i < len; i++ ) {
|
2016-06-30 15:46:38 +00:00
|
|
|
name = names[ i ];
|
2016-09-30 00:57:23 +00:00
|
|
|
aliases = spec.getParameterAliases( name );
|
|
|
|
foundAlias = false;
|
|
|
|
for ( j = 0, aliasLen = aliases.length; j < aliasLen; j++ ) {
|
|
|
|
if ( Object.prototype.hasOwnProperty.call( this.params, aliases[ j ] ) ) {
|
|
|
|
foundAlias = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-28 19:10:31 +00:00
|
|
|
if (
|
2016-09-30 00:57:23 +00:00
|
|
|
!foundAlias &&
|
2016-06-30 15:46:38 +00:00
|
|
|
!this.params[ name ] &&
|
|
|
|
(
|
|
|
|
spec.isParameterRequired( name ) ||
|
|
|
|
spec.isParameterSuggested( name )
|
|
|
|
)
|
|
|
|
) {
|
2015-08-19 17:33:02 +00:00
|
|
|
this.addParameter( new ve.dm.MWParameterModel( this, names[ i ] ) );
|
2014-07-21 21:06:54 +00:00
|
|
|
addedCount++;
|
2013-12-02 20:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-21 21:06:54 +00:00
|
|
|
|
|
|
|
return addedCount;
|
2013-12-02 20:10:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set original data, to be used as a base for serialization.
|
2016-10-28 19:02:36 +00:00
|
|
|
*
|
|
|
|
* @param {Object} data Original data
|
2013-07-12 00:23:33 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.setOriginalData = function ( data ) {
|
|
|
|
this.originalData = data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.serialize = function () {
|
2014-11-10 20:00:34 +00:00
|
|
|
var name, origName,
|
|
|
|
origData = this.originalData || {},
|
|
|
|
origParams = origData.params || {},
|
2014-10-30 18:57:53 +00:00
|
|
|
template = { target: this.getTarget(), params: {} },
|
2013-07-12 00:23:33 +00:00
|
|
|
params = this.getParameters();
|
|
|
|
|
|
|
|
for ( name in params ) {
|
2014-03-20 05:27:59 +00:00
|
|
|
if ( name === '' ) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-16 18:43:00 +00:00
|
|
|
|
|
|
|
// Don't add empty parameters if they were not present before the edit (T101075)
|
|
|
|
if ( params[ name ].getValue() === '' && !Object.prototype.hasOwnProperty.call( origParams, name ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-19 17:33:02 +00:00
|
|
|
origName = params[ name ].getOriginalName();
|
|
|
|
template.params[ origName ] = ve.extendObject(
|
2014-11-10 20:00:34 +00:00
|
|
|
{},
|
2015-08-19 17:33:02 +00:00
|
|
|
origParams[ origName ],
|
|
|
|
{ wt: params[ name ].getValue() }
|
2014-11-10 20:00:34 +00:00
|
|
|
);
|
|
|
|
|
2013-07-12 00:23:33 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 20:00:34 +00:00
|
|
|
// Performs a non-deep extend, so this won't reintroduce
|
2018-08-09 14:10:53 +00:00
|
|
|
// deleted parameters (T75134)
|
2014-11-10 20:00:34 +00:00
|
|
|
template = ve.extendObject( {}, origData, template );
|
2014-08-22 20:50:48 +00:00
|
|
|
return { template: template };
|
2013-07-12 00:23:33 +00:00
|
|
|
};
|
2013-12-09 19:05:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getWikitext = function () {
|
|
|
|
var param,
|
|
|
|
wikitext = this.getTarget().wt,
|
|
|
|
params = this.getParameters();
|
|
|
|
|
|
|
|
for ( param in params ) {
|
2014-03-20 05:27:59 +00:00
|
|
|
if ( param === '' ) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-12-09 19:05:57 +00:00
|
|
|
wikitext += '|' + param + '=' +
|
2015-08-19 17:33:02 +00:00
|
|
|
ve.dm.MWTransclusionNode.static.escapeParameter( params[ param ].getValue() );
|
2013-12-09 19:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return '{{' + wikitext + '}}';
|
|
|
|
};
|