AzaToth has uploaded a new change for review.

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


Change subject: Add depends and conflicts parameters
......................................................................

Add depends and conflicts parameters

To solve the situation when templates are more complext, we need to have
a way to signal dependices and conflicts between the parameters.

Bug: 50407
Change-Id: If9fdb32e40b863c51d75805326937f9291d4f029
---
M TemplateData.i18n.php
M TemplateDataBlob.php
M resources/ext.templateData.css
M spec.templatedata.json
M tests/TemplateDataBlobTest.php
5 files changed, 198 insertions(+), 16 deletions(-)


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

diff --git a/TemplateData.i18n.php b/TemplateData.i18n.php
index 2ed46b9..005ef26 100644
--- a/TemplateData.i18n.php
+++ b/TemplateData.i18n.php
@@ -16,6 +16,8 @@
        'templatedata-doc-param-type' => 'Type',
        'templatedata-doc-param-default' => 'Default',
        'templatedata-doc-param-status' => 'Status',
+       'templatedata-doc-param-depends' => 'Depends',
+       'templatedata-doc-param-conflicts' => 'Conflicts',
 
        // Error message for edit page
        'templatedata-invalid-parse' => 'Syntax error in JSON.',
@@ -23,6 +25,7 @@
        'templatedata-invalid-missing' => 'Required property "$1" not found.',
        'templatedata-invalid-unknown' => 'Unexpected property "$1".',
        'templatedata-invalid-value' => 'Invalid value for property "$1".',
+       'templatedata-invalid-self-reference' => 'Property "$1" is 
self-referencing.',
 );
 
 /** Message documentation (Message documentation)
diff --git a/TemplateDataBlob.php b/TemplateDataBlob.php
index 4a4a53e..fb0c5be 100644
--- a/TemplateDataBlob.php
+++ b/TemplateDataBlob.php
@@ -61,7 +61,9 @@
                        'aliases',
                        'default',
                        'inherits',
-                       'type',
+            'type',
+            'depends',
+            'conflicts',
                );
                static $types = array(
                        'unknown',
@@ -229,6 +231,64 @@
                        } else {
                                $paramObj->type = 'unknown';
                        }
+
+            // Param.depends
+            if ( isset( $paramObj->depends ) ) {
+              if ( !is_array( $paramObj->depends ) ) {
+                return Status::newFatal(
+                  'templatedata-invalid-type',
+                  "params.{$paramName}.depends",
+                  'array'
+                );
+              }
+
+              foreach ( $paramObj->depends as $dep ) {
+                if ( !isset( $data->params-> { $dep } ) ) {
+                  return Status::newFatal(
+                    'templatedata-invalid-missing',
+                    "params.{$dep}"
+                  );
+                }
+
+                if ( $dep == $paramName ) {
+                  return Status::newFatal(
+                    'templatedata-invalid-self-reference',
+                    "params.{$dep}"
+                  );
+                }
+              }
+            } else {
+              $paramObj->depends = array();
+            }
+
+            // Param.conflicts
+            if ( isset( $paramObj->conflicts ) ) {
+              if ( !is_array( $paramObj->conflicts ) ) {
+                return Status::newFatal(
+                  'templatedata-invalid-type',
+                  "params.{$paramName}.conflicts",
+                  'array'
+                );
+              }
+
+              foreach ( $paramObj->conflicts as $dep ) {
+                if ( !isset( $data->params-> { $dep } ) ) {
+                  return Status::newFatal(
+                    'templatedata-invalid-missing',
+                    "params.{$dep}"
+                  );
+                }
+
+                if ( $dep == $paramName ) {
+                  return Status::newFatal(
+                    'templatedata-invalid-self-reference',
+                    "params.{$dep}"
+                  );
+                }
+              }
+            } else {
+              $paramObj->conflicts = array();
+            }
                }
 
                // Param.inherits
@@ -354,6 +414,8 @@
                        . Html::element( 'th', array(), $context->msg( 
'templatedata-doc-param-type' ) )
                        . Html::element( 'th', array(), $context->msg( 
'templatedata-doc-param-default' ) )
                        . Html::element( 'th', array(), $context->msg( 
'templatedata-doc-param-status' ) )
+                       . Html::element( 'th', array(), $context->msg( 
'templatedata-doc-param-depends' ) )
+                       . Html::element( 'th', array(), $context->msg( 
'templatedata-doc-param-conflicts' ) )
                        . '</tr></thead>'
                        . '<tbody>';
 
@@ -368,7 +430,7 @@
                                                'class' => 
'mw-templatedata-doc-param-alias'
                                        ), $alias );
                                }
-                       }
+            }
 
                        $html .= '<tr>'
                        // Label
@@ -416,6 +478,34 @@
                                        $paramObj->required ? 'required' : 
'optional'
                                )
                        )
+            // Depends
+            . Html::Element( 'td', array(),
+              implode(
+                "",
+                array_map(
+                  function( $entry ) {
+                    return Html::element( 'tt', array(
+                      'class' => 'mw-templatedata-doc-param-depends'
+                    ), $entry );
+                  } ,
+                    $paramObj->depends
+                  )
+                )
+              )
+            // Conflicts
+            . Html::Element( 'td', array(),
+              implode(
+                "",
+                array_map(
+                  function( $entry ) {
+                    return Html::element( 'tt', array(
+                      'class' => 'mw-templatedata-doc-param-conflicts'
+                    ), $entry );
+                  } ,
+                    $paramObj->conflicts
+                  )
+                )
+              )
                        . '</tr>';
                }
                $html .= '</tbody></table>'
diff --git a/resources/ext.templateData.css b/resources/ext.templateData.css
index b83595a..6796749 100644
--- a/resources/ext.templateData.css
+++ b/resources/ext.templateData.css
@@ -3,7 +3,9 @@
        font-weight: normal;
 }
 
-.mw-templatedata-doc-param-alias {
+.mw-templatedata-doc-param-alias,
+.mw-templatedata-doc-param-depends,
+.mw-templatedata-doc-param-conflicts {
        color: #777;
        display: block;
        margin-left: 1em;
diff --git a/spec.templatedata.json b/spec.templatedata.json
index 26e8cf3..acf21be 100644
--- a/spec.templatedata.json
+++ b/spec.templatedata.json
@@ -29,6 +29,9 @@
        @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 {Array} [depends] Other parameters which are required if this 
parameter is used.
+       @property {Array} [conflicts] Other parameters which should not be used 
when this parameter is used.
+
 
 @structure {Object} Set
        @property {InterfaceText} label Label of this set.
@@ -81,23 +84,30 @@
                        "description": {
                                "en": "Timestamp of when the comment was 
posted, in YYYY-MM-DD format."
                        },
-                       "aliases": ["2"]
+            "aliases": ["2"],
+            "conflicts": ["year", "month", "day"]
                },
                "year": {
                        "label": "Year",
-                       "type": "number"
+            "type": "number",
+            "conflicts": [ "date" ]
                },
                "month": {
                        "label": "Month",
-                       "inherits": "year"
+                       "inherits": "year",
+            "conflicts": [ "date" ]
                },
                "day": {
                        "label": "Day",
-                       "inherits": "year"
+                       "inherits": "year",
+            "conflicts": [ "date" ]
                },
                "comment": {
                        "required": false
-               }
+        },
+        "comment2": {
+          "depends": [ "comment" ]
+        }
        },
        "sets": [
                {
diff --git a/tests/TemplateDataBlobTest.php b/tests/TemplateDataBlobTest.php
index 35a4c85..153139b 100644
--- a/tests/TemplateDataBlobTest.php
+++ b/tests/TemplateDataBlobTest.php
@@ -70,7 +70,9 @@
                                                        "required": false,
                                                        "deprecated": false,
                                                        "aliases": [],
-                                                       "type": "unknown"
+                            "type": "unknown",
+                            "depends": [],
+                            "conflicts": []
                                                }
                                        },
                                        "sets": []
@@ -110,7 +112,9 @@
                                                        "aliases": [
                                                                "1"
                                                        ],
-                                                       "type": "unknown"
+                                                       "type": "unknown",
+                            "depends": [],
+                            "conflicts": []
                                                }
                                        },
                                        "sets": []
@@ -135,7 +139,9 @@
                                                        "aliases": [
                                                                "1"
                                                        ],
-                                                       "type": "unknown"
+                                                       "type": "unknown",
+                            "depends": [],
+                            "conflicts": []
                                                }
                                        },
                                        "sets": []
@@ -173,7 +179,9 @@
                                                        "default": "example",
                                                        "deprecated": false,
                                                        "aliases": [],
-                                                       "type": "unknown"
+                                                       "type": "unknown",
+                            "depends": [],
+                            "conflicts": []
                                                },
                                                "2d": {
                                                        "label": null,
@@ -184,7 +192,9 @@
                                                        "default": "overridden",
                                                        "deprecated": false,
                                                        "aliases": [],
-                                                       "type": "unknown"
+                                                       "type": "unknown",
+                            "depends": [],
+                            "conflicts": []
                                                }
                                        },
                                        "sets": []
@@ -266,7 +276,9 @@
                                                        "deprecated": false,
                                                        "aliases": [],
                                                        "default": "",
-                                                       "type": "unknown"
+                                                       "type": "unknown",
+                            "depends": [],
+                            "conflicts": []
                                                },
                                                "bar": {
                                                        "label": null,
@@ -275,7 +287,9 @@
                                                        "deprecated": false,
                                                        "aliases": [],
                                                        "default": "",
-                                                       "type": "unknown"
+                                                       "type": "unknown",
+                            "depends": [],
+                            "conflicts": []
                                                },
                                                "quux": {
                                                        "label": null,
@@ -284,7 +298,9 @@
                                                        "deprecated": false,
                                                        "aliases": [],
                                                        "default": "",
-                                                       "type": "unknown"
+                                                       "type": "unknown",
+                            "depends": [],
+                            "conflicts": []
                                                }
                                        },
                                        "sets": [
@@ -304,6 +320,67 @@
                                }',
                                'status' => true
                        ),
+            array(
+              "input" => '{
+                "params": {
+                  "foo": {
+                    "depends": "bar"
+                  }
+                }
+              }',
+              "status" => 'Property "params.foo.depends" is expected to be of 
type "array".'
+            ),
+            array(
+              "input" => '{
+                "params": {
+                  "foo": {
+                    "depends": ["foo"]
+                  }
+                }
+              }',
+              "status" => 'Property "params.foo" is self-referencing.'
+            ),
+             array(
+              "input" => '{
+                "params": {
+                  "foo": {
+                    "depends": ["bar"]
+                  }
+                }
+              }',
+              "status" => 'Required property "params.bar" not found.'
+            ),
+
+            array(
+              "input" => '{
+                "params": {
+                  "foo": {
+                    "conflicts": "bar"
+                  }
+                }
+              }',
+              "status" => 'Property "params.foo.conflicts" is expected to be 
of type "array".'
+            ),
+            array(
+              "input" => '{
+                "params": {
+                  "foo": {
+                    "conflicts": ["foo"]
+                  }
+                }
+              }',
+              "status" => 'Property "params.foo" is self-referencing.'
+            ),
+             array(
+              "input" => '{
+                "params": {
+                  "foo": {
+                    "conflicts": ["bar"]
+                  }
+                }
+              }',
+              "status" => 'Required property "params.bar" not found.'
+            ),
                );
                $calls = array();
                foreach ( $cases as $case ) {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If9fdb32e40b863c51d75805326937f9291d4f029
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/TemplateData
Gerrit-Branch: master
Gerrit-Owner: AzaToth <azat...@gmail.com>

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

Reply via email to