Fz-29 has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/372082 )

Change subject: Add validation for hierarchy input
......................................................................

Add validation for hierarchy input

Change-Id: I24fe67be0469dd628ddfd3d68960285bf1b6fe4d
---
M CargoFieldDescription.php
M CargoUtils.php
2 files changed, 33 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cargo 
refs/changes/82/372082/1

diff --git a/CargoFieldDescription.php b/CargoFieldDescription.php
index 6c78859..9fff7d0 100644
--- a/CargoFieldDescription.php
+++ b/CargoFieldDescription.php
@@ -71,6 +71,7 @@
                                                // hardcode it to a semicolon.
                                                if( 
$fieldDescription->mIsHierarchy == true ) {
                                                        // $paramValue contains 
"*" hierarchy structure
+                                                       
CargoUtils::validateHierarchyStructure( trim( $paramValue ) );
                                                        
$fieldDescription->mHierarchyStructure = trim( $paramValue );
                                                        // now make the allowed 
values param similar to the syntax
                                                        // used by other fields
@@ -78,7 +79,7 @@
                                                        $allowedValuesArray = 
array();
                                                        foreach ( 
$hierarchyNodesArray as $node ) {
                                                                // Remove 
prefix of multiple "*"
-                                                               
$allowedValuesArray[] = preg_replace( '/^[*]*/', '', $node );
+                                                               
$allowedValuesArray[] = preg_replace( '/^[*]* ?/', '', $node );
                                                        }
                                                } else {
                                                        $delimiter = ',';
diff --git a/CargoUtils.php b/CargoUtils.php
index a1686be..c72ce24 100644
--- a/CargoUtils.php
+++ b/CargoUtils.php
@@ -942,4 +942,35 @@
                }
        }
 
+       public static function validateHierarchyStructure( $hierarchyStructure 
) {
+               $hierarchyNodesArray = explode( "\n", $hierarchyStructure );
+               $matches = array();
+               preg_match( '/^([*]*)[^*]*/i', $hierarchyNodesArray[0], 
$matches );
+               if ( strlen( $matches[1] ) != 1 ) {
+                       throw new MWException( "Error: First entry of hierarchy 
values should start with exact one '*', the entry \"" .
+                               $hierarchyNodesArray[0] . "\" has " . strlen( 
$matches[1] ) . " '*'" );
+               }
+               $level = 0;
+               foreach( $hierarchyNodesArray as $node ) {
+                       if ( !preg_match( '/^([*]*)( *)(.*)/i', $node, $matches 
) ) {
+                               throw new MWException( "Error: The \"" . $node 
. "\" entry of hierarchy values does not follow syntax. " .
+                                       "The entry should be of the form : * 
entry" );
+                       }
+                       if ( strlen( $matches[1] ) < 1 ) {
+                               throw new MWException( "Error: Each entry of 
hierarchy values should start with atleast one '*', the entry \"" .
+                                       $node . "\" has " . strlen( $matches[1] 
) . " '*'" );
+                       }
+                       if ( strlen( $matches[1] ) - $level > 1 ) {
+                               throw new MWException( "Error: Level or count 
of '*' in hierarchy values should be increased only by count of 1, the entry 
\"" .
+                                       $node . "\" should have " . ( $level + 
1 ) . " or less '*'" );
+                       }
+                       $level = strlen( $matches[1] );
+                       if ( strlen( $matches[3] ) == 0 ) {
+                               throw new MWException( "Error: The entry of 
hierarchy values cannot be empty." );
+                       }
+                       if ( strlen( $matches[2] ) > 1 ) {
+                               throw new MWException( "Error: Single or no 
space should follow after '*'. \"" . $node . "\" needs correction." );
+                       }
+               }
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I24fe67be0469dd628ddfd3d68960285bf1b6fe4d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Cargo
Gerrit-Branch: master
Gerrit-Owner: Fz-29 <f29ah...@gmail.com>

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

Reply via email to