jenkins-bot has submitted this change and it was merged.

Change subject: Add Root#paramOrder to spec and implemention thereof
......................................................................


Add Root#paramOrder to spec and implemention thereof

* Detail the new property in the spec.
* The property is optional (as all new properties have to be
  for backwards compatibility).
* Validate value if present, and for normalisation fill it
  with inferred order from the JSON parser. This means the API
  output will always contain #paramOrder, however the strict
  specification for it is optional and users must still account
  for absence of this property.

Bug: 53608
Change-Id: I7bcd7c9146f5ae75c4bad22b0a9cd4400f196c8c
---
M TemplateData.i18n.php
M TemplateDataBlob.php
M spec.templatedata.json
M tests/TemplateDataBlobTest.php
4 files changed, 207 insertions(+), 1 deletion(-)

Approvals:
  Bartosz Dziewoński: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/TemplateData.i18n.php b/TemplateData.i18n.php
index 0baf2ab..70cdbf0 100644
--- a/TemplateData.i18n.php
+++ b/TemplateData.i18n.php
@@ -24,6 +24,7 @@
        'templatedata-doc-param-desc-empty' => 'no description',
 
        // Error message for edit page
+       'templatedata-invalid-duplicate-value' => 'Property "$1" ("$3") is a 
duplicate of "$2".',
        'templatedata-invalid-parse' => 'Syntax error in JSON.',
        'templatedata-invalid-type' => 'Property "$1" is expected to be of type 
"$2".',
        'templatedata-invalid-missing' => 'Required property "$1" not found.',
@@ -70,6 +71,10 @@
 {{Identical|Required}}',
        'templatedata-doc-param-desc-empty' => 'Displayed when a template 
parameter has no description (should not be a full sentence, used in a table 
cell).
 {{Identical|No description}}',
+       'templatedata-invalid-duplicate-value' => 'Displayed when an array that 
must only contain unique values contains a duplicate.
+* $1 - name of property containing the duplicate
+* $2 - name of property with first occurrence of value
+* $3 - the value being duplicated',
        'templatedata-invalid-parse' => 'Error message when there is a syntax 
error in JSON.',
        'templatedata-invalid-type' => 'Error message when a property is of the 
wrong type.
 * $1 - name of property. e.g. "params.1.required"
diff --git a/TemplateDataBlob.php b/TemplateDataBlob.php
index 6ddcdcb..681881b 100644
--- a/TemplateDataBlob.php
+++ b/TemplateDataBlob.php
@@ -72,6 +72,7 @@
                static $rootKeys = array(
                        'description',
                        'params',
+                       'paramOrder',
                        'sets',
                );
 
@@ -132,6 +133,7 @@
                // 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 ) );
+               $paramNames = array();
 
                foreach ( $data->params as $paramName => $paramObj ) {
                        if ( !is_object( $paramObj ) ) {
@@ -252,6 +254,8 @@
                        } else {
                                $paramObj->type = 'unknown';
                        }
+
+                       $paramNames[] = $paramName;
                }
 
                // Param.inherits
@@ -279,6 +283,43 @@
                        }
                }
 
+               // Root.paramOrder
+               if ( isset( $data->paramOrder ) ) {
+                       if ( !is_array( $data->paramOrder ) ) {
+                               return Status::newFatal( 
'templatedata-invalid-type', 'paramOrder', 'array' );
+                       }
+
+                       if ( !count( $data->paramOrder ) ) {
+                               return Status::newFatal( 
'templatedata-invalid-empty-array', "paramOrder" );
+                       }
+
+                       if ( count( $data->paramOrder ) < count( $paramNames ) 
) {
+                               $i = count( $data->paramOrder );
+                               return Status::newFatal( 
'templatedata-invalid-missing', "paramOrder[$i]" );
+                       }
+
+                       // Validate each of the values corresponds to a 
parameter and that there are no
+                       // duplicates
+                       $seen = array();
+                       foreach ( $data->paramOrder as $i => $param ) {
+                               if ( !isset( $data->params->$param ) ) {
+                                       return Status::newFatal( 
'templatedata-invalid-value', "paramOrder[$i]" );
+                               }
+                               if ( isset( $seen[$param] ) ) {
+                                       return Status::newFatal(
+                                               
'templatedata-invalid-duplicate-value',
+                                               "paramOrder[$i]",
+                                               "paramOrder[{$seen[$param]}]",
+                                               $param
+                                       );
+                               }
+                               $seen[$param] = $i;
+                       }
+
+               } elseif ( count( $paramNames ) ) {
+                       $data->paramOrder = $paramNames;
+               }
+
                // Root.sets
                if ( isset( $data->sets ) ) {
                        if ( !is_array( $data->sets ) ) {
@@ -290,7 +331,7 @@
 
                foreach ( $data->sets as $setNr => $setObj ) {
                        if ( !is_object( $setObj ) ) {
-                               return Status::newFatal( 
'templatedata-invalid-value', "paramOrder[$i]" );
+                               return Status::newFatal( 
'templatedata-invalid-value', "sets.{$setNr}" );
                        }
 
                        if ( !isset( $setObj->label ) ) {
diff --git a/spec.templatedata.json b/spec.templatedata.json
index 3a59bf7..83b1eda 100644
--- a/spec.templatedata.json
+++ b/spec.templatedata.json
@@ -9,6 +9,8 @@
        @property {null|InterfaceText} [description]
        @property {Object} params Contains all parameters.
         Keyed by parameter name, contains #Param objects.
+       @property {Array} [paramOrder] The logical order in which parameters 
should be
+        displayed. The array contains each parameter key exactly once.
        @property {Array} sets List of groups of parameters that should be used
         together. A parameter can be in multiple sets. Not every parameter has 
to be
         in a set. The array contains #Set objects.
diff --git a/tests/TemplateDataBlobTest.php b/tests/TemplateDataBlobTest.php
index 513bc95..581afe2 100644
--- a/tests/TemplateDataBlobTest.php
+++ b/tests/TemplateDataBlobTest.php
@@ -93,6 +93,7 @@
                                                        "type": "unknown"
                                                }
                                        },
+                                       "paramOrder": ["foo"],
                                        "sets": []
                                }
                                ',
@@ -133,6 +134,7 @@
                                                        "type": "unknown"
                                                }
                                        },
+                                       "paramOrder": ["nickname"],
                                        "sets": []
                                }
                                ',
@@ -158,6 +160,7 @@
                                                        "type": "unknown"
                                                }
                                        },
+                                       "paramOrder": ["nickname"],
                                        "sets": []
                                }
                                ',
@@ -207,6 +210,7 @@
                                                        "type": "unknown"
                                                }
                                        },
+                                       "paramOrder": ["1d", "2d"],
                                        "sets": []
                                }
                                ',
@@ -307,6 +311,7 @@
                                                        "type": "unknown"
                                                }
                                        },
+                                       "paramOrder": ["foo", "bar", "quux"],
                                        "sets": [
                                                {
                                                        "label": {
@@ -520,6 +525,7 @@
                                                        "type": "unknown"
                                                }
                                        },
+                                       "paramOrder": ["foo"],
                                        "sets": []
                                }
                                ',
@@ -551,6 +557,7 @@
                                                        "type": "unknown"
                                                }
                                        },
+                                       "paramOrder": ["foo"],
                                        "sets": []
                                }
                                ',
@@ -586,6 +593,7 @@
                                                        "type": "unknown"
                                                }
                                        },
+                                       "paramOrder": ["foo"],
                                        "sets": [
                                                {
                                                        "label": "Spanish",
@@ -633,4 +641,154 @@
                        $case['msg']
                );
        }
+
+       public static function provideParamOrder() {
+               $cases = array(
+                       array(
+                               'input' => '{
+                                       "params": {
+                                               "foo": {},
+                                               "bar": {},
+                                               "baz": {}
+                                       }
+                               }
+                               ',
+                               'output' => '{
+                                       "description": null,
+                                       "params": {
+                                               "foo": {
+                                                       "label": null,
+                                                       "required": false,
+                                                       "description": null,
+                                                       "deprecated": false,
+                                                       "aliases": [],
+                                                       "default": "",
+                                                       "type": "unknown"
+                                               },
+                                               "bar": {
+                                                       "label": null,
+                                                       "required": false,
+                                                       "description": null,
+                                                       "deprecated": false,
+                                                       "aliases": [],
+                                                       "default": "",
+                                                       "type": "unknown"
+                                               },
+                                               "baz": {
+                                                       "label": null,
+                                                       "required": false,
+                                                       "description": null,
+                                                       "deprecated": false,
+                                                       "aliases": [],
+                                                       "default": "",
+                                                       "type": "unknown"
+                                               }
+                                       },
+                                       "paramOrder": ["foo", "bar", "baz"],
+                                       "sets": []
+                               }
+                               ',
+                               'msg' => 'Normalisation adds paramOrder'
+                       ),
+                       array(
+                               'input' => '{
+                                       "params": {
+                                               "foo": {},
+                                               "bar": {},
+                                               "baz": {}
+                                       },
+                                       "paramOrder": ["baz", "foo", "bar"]
+                               }
+                               ',
+                               'output' => '{
+                                       "description": null,
+                                       "params": {
+                                               "foo": {
+                                                       "label": null,
+                                                       "required": false,
+                                                       "description": null,
+                                                       "deprecated": false,
+                                                       "aliases": [],
+                                                       "default": "",
+                                                       "type": "unknown"
+                                               },
+                                               "bar": {
+                                                       "label": null,
+                                                       "required": false,
+                                                       "description": null,
+                                                       "deprecated": false,
+                                                       "aliases": [],
+                                                       "default": "",
+                                                       "type": "unknown"
+                                               },
+                                               "baz": {
+                                                       "label": null,
+                                                       "required": false,
+                                                       "description": null,
+                                                       "deprecated": false,
+                                                       "aliases": [],
+                                                       "default": "",
+                                                       "type": "unknown"
+                                               }
+                                       },
+                                       "paramOrder": ["baz", "foo", "bar"],
+                                       "sets": []
+                               }
+                               ',
+                               'msg' => 'Custom paramOrder'
+                       ),
+                       array(
+                               'input' => '{
+                                       "params": {
+                                               "foo": {},
+                                               "bar": {},
+                                               "baz": {}
+                                       },
+                                       "paramOrder": ["foo", "bar"]
+                               }
+                               ',
+                               'status' => 'Required property 
"paramOrder&#91;2&#93;" not found.',
+                               'msg' => 'Incomplete paramOrder'
+                       ),
+                       array(
+                               'input' => '{
+                                       "params": {
+                                               "foo": {},
+                                               "bar": {},
+                                               "baz": {}
+                                       },
+                                       "paramOrder": ["foo", "bar", "baz", 
"quux"]
+                               }
+                               ',
+                               'status' => 'Invalid value for property 
"paramOrder&#91;3&#93;".',
+                               'msg' => 'Unknown params in paramOrder'
+                       ),
+                       array(
+                               'input' => '{
+                                       "params": {
+                                               "foo": {},
+                                               "bar": {},
+                                               "baz": {}
+                                       },
+                                       "paramOrder": ["foo", "bar", "baz", 
"bar"]
+                               }
+                               ',
+                               'status' => 'Property "paramOrder&#91;3&#93;" 
("bar") is a duplicate of ' .
+                                       '"paramOrder&#91;1&#93;".',
+                               'msg' => 'Duplicate params in paramOrder'
+                       ),
+               );
+               $calls = array();
+               foreach ( $cases as $case ) {
+                       $calls[] = array( $case );
+               }
+               return $calls;
+       }
+
+       /**
+        * @dataProvider provideParamOrder
+        */
+       public function testParamOrder( Array $case ) {
+               $this->assertTemplateData( $case );
+       }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/89867
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I7bcd7c9146f5ae75c4bad22b0a9cd4400f196c8c
Gerrit-PatchSet: 14
Gerrit-Project: mediawiki/extensions/TemplateData
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
Gerrit-Reviewer: Bartosz Dziewoński <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Trevor Parscal <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to