Krinkle has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/89867


Change subject: [WIP] Add Root#paramOrder
......................................................................

[WIP] Add Root#paramOrder

Change-Id: I7bcd7c9146f5ae75c4bad22b0a9cd4400f196c8c
---
M TemplateData.i18n.php
M TemplateDataBlob.php
M spec.templatedata.json
M tests/TemplateDataBlobTest.php
4 files changed, 85 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/TemplateData 
refs/changes/67/89867/1

diff --git a/TemplateData.i18n.php b/TemplateData.i18n.php
index 33fe1b5..c02ca77 100644
--- a/TemplateData.i18n.php
+++ b/TemplateData.i18n.php
@@ -27,6 +27,7 @@
        '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.',
+       'templatedata-invalid-empty-array' => 'Property "$1" must have at least 
one value in its array.',
        'templatedata-invalid-unknown' => 'Unexpected property "$1".',
        'templatedata-invalid-value' => 'Invalid value for property "$1".',
        'templatedata-invalid-length' => 'Data too large to save 
({{formatnum:$1}} {{PLURAL:$1|byte|bytes}}, {{PLURAL:$2|limit is}} 
{{formatnum:$2}})',
diff --git a/TemplateDataBlob.php b/TemplateDataBlob.php
index 0534011..f34661e 100644
--- a/TemplateDataBlob.php
+++ b/TemplateDataBlob.php
@@ -132,6 +132,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 +253,8 @@
                        } else {
                                $paramObj->type = 'unknown';
                        }
+
+                       $paramNames[] = $paramName;
                }
 
                // Param.inherits
@@ -279,6 +282,27 @@
                        }
                }
 
+               // 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" );
+                       }
+
+                       // Validate each of the values corresponds to a 
parameter
+                       foreach ( $data->paramOrder as $param ) {
+                               if ( !isset( $data->params->$param ) ) {
+                                       return Status::newFatal( 
'templatedata-invalid-value', "params.{$param}" );
+                               }
+                       }
+
+               } else {
+                       $data->paramOrder = $paramNames;
+               }
+
                // Root.sets
                if ( isset( $data->sets ) ) {
                        if ( !is_array( $data->sets ) ) {
@@ -290,7 +314,7 @@
 
                foreach ( $data->sets as $setNr => $setObj ) {
                        if ( !is_object( $setObj ) ) {
-                               return Status::newFatal( 
'templatedata-invalid-type', "sets.{$setNr}", 'object' );
+                               return Status::newFatal( 
'templatedata-invalid-value', "paramOrder[$i]" );
                        }
 
                        if ( !isset( $setObj->label ) ) {
@@ -320,9 +344,13 @@
                                return Status::newFatal( 
'templatedata-invalid-type', "sets.{$setNr}.params", 'array' );
                        }
 
-                       foreach ( $setObj->params as $param ) {
+                       if ( !count( $setObj->params ) ) {
+                               return Status::newFatal( 
'templatedata-invalid-empty-array', "sets.{$setNr}.params" );
+                       }
+
+                       foreach ( $setObj->params as $i => $param ) {
                                if ( !isset( $data->params->$param ) ) {
-                                       return Status::newFatal( 
'templatedata-invalid-missing', "params.{$param}" );
+                                       return Status::newFatal( 
'templatedata-invalid-value', "sets.{$setNr}.params[$i]" );
                                }
                        }
                }
diff --git a/spec.templatedata.json b/spec.templatedata.json
index 26e8cf3..3036923 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 wich parameters 
should be
+        displayed. The array contains one or more parameter keys.
        @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.
@@ -32,7 +34,7 @@
 
 @structure {Object} Set
        @property {InterfaceText} label Label of this set.
-       @property {Array} params A subset of the parameter's names that belong 
to this set.
+       @property {Array} params One or more parameter keys.
 
 @structure {string} Type
        One of the following:
diff --git a/tests/TemplateDataBlobTest.php b/tests/TemplateDataBlobTest.php
index 4648790..25bf05f 100644
--- a/tests/TemplateDataBlobTest.php
+++ b/tests/TemplateDataBlobTest.php
@@ -618,4 +618,54 @@
                        $case['msg']
                );
        }
+
+       public static function provideParamOrder() {
+               $cases = array(
+                       array(
+                               'input' => '{
+                                       "params": {
+                                               "foo": {},
+                                               "bar": {},
+                                               "baz": {}
+                                       }
+                               }
+                               ',
+                               'output' => '{
+                                       "description": null,
+                                       "params": {
+                                               "foo": {},
+                                               "bar": {},
+                                               "baz": {}
+                                       },
+                                       "paramOrder": ["foo", "bar", "baz"],
+                                       "sets": []
+                               }
+                               ',
+                               'lang' => 'de',
+                               'msg' => 'Simple description'
+                       ),
+               );
+               $calls = array();
+               foreach ( $cases as $case ) {
+                       $calls[] = array( $case );
+               }
+               return $calls;
+       }
+
+       /**
+        * @dataProvider provideParamOrder
+        */
+       public function testParamOrder( Array $case ) {
+               $t = TemplateDataBlob::newFromJSON( $case['input'] );
+               $status = $t->getStatus();
+
+               $this->assertTrue( $status->isGood(), 'Status is good: ' . 
$case['msg'] );
+
+               $actual = $t->getDataInLanguage( $case['lang'] );
+               $this->assertJsonStringEqualsJsonString(
+                       $case['output'],
+                       json_encode( $actual ),
+                       $case['msg']
+               );
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7bcd7c9146f5ae75c4bad22b0a9cd4400f196c8c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/TemplateData
Gerrit-Branch: master
Gerrit-Owner: Krinkle <krinklem...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to