mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TemplateData
synced 2024-11-11 16:59:25 +00:00
Merge "TemplateDataBlob: Convert 'clones' to 'inherits'"
This commit is contained in:
commit
34325173cc
|
@ -48,6 +48,21 @@ class TemplateDataBlob {
|
|||
private function parse() {
|
||||
$data = $this->data;
|
||||
|
||||
static $rootKeys = array(
|
||||
'params',
|
||||
'description',
|
||||
);
|
||||
static $paramKeys = array(
|
||||
// 'label',
|
||||
'required',
|
||||
'description',
|
||||
'deprecated',
|
||||
'aliases',
|
||||
'default',
|
||||
'inherits',
|
||||
// 'type',
|
||||
);
|
||||
|
||||
if ( $data === null ) {
|
||||
return Status::newFatal( 'templatedata-invalid-parse' );
|
||||
}
|
||||
|
@ -57,7 +72,7 @@ class TemplateDataBlob {
|
|||
}
|
||||
|
||||
foreach ( $data as $key => $value ) {
|
||||
if ( !in_array( $key, array( 'params', 'description' ) ) ) {
|
||||
if ( !in_array( $key, $rootKeys ) ) {
|
||||
return Status::newFatal( 'templatedata-invalid-unknown', $key );
|
||||
}
|
||||
}
|
||||
|
@ -81,36 +96,22 @@ class TemplateDataBlob {
|
|||
return Status::newFatal( 'templatedata-invalid-type', 'params', 'object' );
|
||||
}
|
||||
|
||||
// Deep clone
|
||||
// We need this to determine whether a property was originally set
|
||||
// to decide whether 'inherits' will add it or not.
|
||||
$unnormalizedParams = unserialize( serialize( $data->params ) );
|
||||
|
||||
foreach ( $data->params as $paramName => $paramObj ) {
|
||||
if ( !is_object( $paramObj ) ) {
|
||||
return Status::newFatal( 'templatedata-invalid-type', 'params.' . $paramName, 'object' );
|
||||
}
|
||||
|
||||
foreach ( $paramObj as $key => $value ) {
|
||||
if ( !in_array( $key, array(
|
||||
'required',
|
||||
'description',
|
||||
'deprecated',
|
||||
'aliases',
|
||||
'clones',
|
||||
'default',
|
||||
) ) ) {
|
||||
if ( !in_array( $key, $paramKeys ) ) {
|
||||
return Status::newFatal( 'templatedata-invalid-unknown', $key );
|
||||
}
|
||||
}
|
||||
|
||||
// Param.inherits
|
||||
// TODO: Implementation specifies we use inherit (target references origin), instead
|
||||
// of clone (origin lists targets).
|
||||
if ( isset( $paramObj->clones ) ) {
|
||||
if ( !is_array( $paramObj->clones ) ) {
|
||||
// TODO: Validate the array values.
|
||||
return Status::newFatal( 'templatedata-invalid-type', 'params.' . $paramName . '.clones', 'array' );
|
||||
}
|
||||
} else {
|
||||
$paramObj->clones = array();
|
||||
}
|
||||
|
||||
// TODO: Param.label
|
||||
|
||||
// Param.required
|
||||
|
@ -165,6 +166,26 @@ class TemplateDataBlob {
|
|||
// TODO: Param.type
|
||||
}
|
||||
|
||||
// Param.inherits
|
||||
// Done afterwards to avoid code duplication
|
||||
foreach ( $data->params as $paramName => $paramObj ) {
|
||||
if ( isset( $paramObj->inherits ) ) {
|
||||
if ( !isset( $data->params->{ $paramObj->inherits } ) ) {
|
||||
return Status::newFatal( 'templatedata-invalid-missing', "params.{$paramObj->inherits}" );
|
||||
}
|
||||
$parentParamObj = $data->params->{ $paramObj->inherits };
|
||||
foreach ( $parentParamObj as $key => $value ) {
|
||||
if ( !in_array( $key, $paramKeys ) ) {
|
||||
return Status::newFatal( 'templatedata-invalid-unknown', $key );
|
||||
}
|
||||
if ( !isset( $unnormalizedParams->$paramName->$key ) ) {
|
||||
$paramObj->$key = is_object( $parentParamObj->$key ) ? clone $parentParamObj->$key : $parentParamObj->$key;
|
||||
}
|
||||
}
|
||||
unset( $paramObj->inherits );
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Root.sets
|
||||
|
||||
return Status::newGood();
|
||||
|
|
|
@ -14,9 +14,6 @@
|
|||
Keyed by an internal id, contains #Set objects.
|
||||
|
||||
@structure {Object} Param
|
||||
@property {string} [inherits] Key to another object in `Root.params`.
|
||||
The current Param object will inherit from that one, with local properties
|
||||
overriding the inherited ones.
|
||||
@property {InterfaceText} [label] Defaults to key of object in `Root.params`.
|
||||
@property {boolean} [required=false]
|
||||
@property {InterfaceText} [description]
|
||||
|
@ -29,6 +26,9 @@
|
|||
own property marked "deprecated".
|
||||
@property {string} [default] The default value or description thereof.
|
||||
@property {Type} [type] The type of the expected parameter value.
|
||||
@property {string} [inherits] Key to another object in `Root.params`.
|
||||
The current Param object will inherit from that one, with local properties
|
||||
overriding the inherited ones.
|
||||
|
||||
@structure {Object} Set
|
||||
@property {InterfaceText} [label] Defaults to key of object in `Root.sets`.
|
||||
|
|
|
@ -53,8 +53,7 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
|||
"default": "",
|
||||
"required": false,
|
||||
"deprecated": false,
|
||||
"aliases": [],
|
||||
"clones": []
|
||||
"aliases": []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -92,8 +91,7 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
|||
"deprecated": false,
|
||||
"aliases": [
|
||||
"1"
|
||||
],
|
||||
"clones": []
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -116,14 +114,59 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
|
|||
"deprecated": false,
|
||||
"aliases": [
|
||||
"1"
|
||||
],
|
||||
"clones": []
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
',
|
||||
'Fully normalised json should be valid input and stay unchanged'
|
||||
)
|
||||
),
|
||||
array(
|
||||
'
|
||||
{
|
||||
"description": "Document the documenter.",
|
||||
"params": {
|
||||
"1d": {
|
||||
"description": "Description of the template parameter",
|
||||
"required": true,
|
||||
"default": "example"
|
||||
},
|
||||
"2d": {
|
||||
"inherits": "1d",
|
||||
"default": "overridden"
|
||||
}
|
||||
}
|
||||
}
|
||||
',
|
||||
'
|
||||
{
|
||||
"description": {
|
||||
"en": "Document the documenter."
|
||||
},
|
||||
"params": {
|
||||
"1d": {
|
||||
"description": {
|
||||
"en": "Description of the template parameter"
|
||||
},
|
||||
"required": true,
|
||||
"default": "example",
|
||||
"deprecated": false,
|
||||
"aliases": []
|
||||
},
|
||||
"2d": {
|
||||
"description": {
|
||||
"en": "Description of the template parameter"
|
||||
},
|
||||
"required": true,
|
||||
"default": "overridden",
|
||||
"deprecated": false,
|
||||
"aliases": []
|
||||
}
|
||||
}
|
||||
}
|
||||
',
|
||||
'The inherits property copies over properties from another parameter (preserving overides)'
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue