http://www.mediawiki.org/wiki/Special:Code/MediaWiki/73766

Revision: 73766
Author:   jeroendedauw
Date:     2010-09-26 04:50:36 +0000 (Sun, 26 Sep 2010)

Log Message:
-----------
Changes for 0.4 - added support for parameter list manipulation during 
validation

Modified Paths:
--------------
    trunk/extensions/Validator/Validator.php
    trunk/extensions/Validator/includes/Parameter.php
    trunk/extensions/Validator/includes/Validator.php

Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php    2010-09-26 01:09:50 UTC (rev 
73765)
+++ trunk/extensions/Validator/Validator.php    2010-09-26 04:50:36 UTC (rev 
73766)
@@ -24,7 +24,7 @@
        die( 'Not an entry point.' );
 }
 
-define( 'Validator_VERSION', '0.4 alpha-4' );
+define( 'Validator_VERSION', '0.4 alpha-5' );
 
 // Constants indicating the strictness of the parameter validation.
 define( 'Validator_ERRORS_NONE', 0 );

Modified: trunk/extensions/Validator/includes/Parameter.php
===================================================================
--- trunk/extensions/Validator/includes/Parameter.php   2010-09-26 01:09:50 UTC 
(rev 73765)
+++ trunk/extensions/Validator/includes/Parameter.php   2010-09-26 04:50:36 UTC 
(rev 73766)
@@ -499,6 +499,18 @@
        }
        
        /**
+        * Returns a list of dependencies the parameter has, in the form of 
+        * other parameter names.
+        * 
+        * @since 0.4
+        * 
+        * @return array
+        */             
+       public function getDependencies() {
+               return $this->dependencies;
+       }
+       
+       /**
         * Returns the original use-provided name.
         * 
         * @since 0.4
@@ -679,4 +691,16 @@
                return in_array( $alias, $this->getAliases() );
        }
        
+       /**
+        * Sets the default parameter value. Null indicates no default,
+        * and therefore makes the parameter required.
+        * 
+        * @since 0.4
+        * 
+        * @param mixed $default
+        */
+       public function setDefault( $default ) {
+               $this->default = $default;
+       }
+       
 }
\ No newline at end of file

Modified: trunk/extensions/Validator/includes/Validator.php
===================================================================
--- trunk/extensions/Validator/includes/Validator.php   2010-09-26 01:09:50 UTC 
(rev 73765)
+++ trunk/extensions/Validator/includes/Validator.php   2010-09-26 04:50:36 UTC 
(rev 73766)
@@ -24,6 +24,17 @@
        protected $parameters;
        
        /**
+        * Asscoaitive array containing parameter names (keys) and their 
user-provided data (values).
+        * This list is needed because adittional parameter definitions can be 
added to the $parameters
+        * feild during validation, so we can't determine in advance if a 
parameter is unknown.
+        * 
+        * @since 0.4
+        * 
+        * @var array
+        */
+       protected $rawParameters = array();
+       
+       /**
         * List of ValidationError.
         * 
         * @since 0.4
@@ -64,8 +75,6 @@
         * @param boolean $toLower Indicates if the parameter values should be 
put to lower case. Defaults to true.
         */
        public function setFunctionParams( array $rawParams, array 
$parameterInfo, array $defaultParams = array(), $toLower = true ) {
-               $this->cleanParameterInfo( $parameterInfo );
-               
                $parameters = array();
 
                $nr = 0;
@@ -136,36 +145,9 @@
                // Loop through all the user provided parameters, and 
destinguise between those that are allowed and those that are not.
                foreach ( $parameters as $paramName => $paramData ) {
                        $paramName = trim( strtolower( $paramName ) );
+                       $paramValue = is_array( $paramData ) ? 
$paramData['original-value'] : trim( $paramData );
                        
-                       // Attempt to get the main parameter name (takes care 
of aliases).
-                       $mainName = self::getMainParamName( $paramName );
-
-                       // If the parameter is found in the list of allowed 
ones, add it to the $mParameters array.
-                       if ( $mainName ) {
-                               // If the valueis an array, this means it has 
been procesed in parseAndSetParams already.
-                               // If it is not, setParameters was called 
directly with an array of string parameter values.
-                               if ( is_array( $paramData ) ) {
-                                       
$this->parameters[$mainName]->setUserValue( $paramName, 
$paramData['original-value'] ); 
-                               }
-                               else {
-                                       if ( is_string( $paramData ) ) {
-                                               $paramData = trim( $paramData );
-                                       }
-                                       
-                                       
$this->parameters[$mainName]->setUserValue( $paramName, $paramData ); 
-                               }
-                       
-                       }
-                       else { // If the parameter is not found in the list of 
allowed ones, add an item to the $this->mErrors array.
-                               $this->registerNewError(
-                                       wfMsgExt(
-                                               
'validator_error_unknown_argument',
-                                               'parsemag',
-                                               $paramName
-                                       ),
-                                       'unknown'               
-                               );              
-                       }               
+                       $this->rawParameters[$paramName] = $paramValue;
                }
        }
        
@@ -202,72 +184,148 @@
        }
        
        /**
-        * Ensures all elements of the array are Parameter objects.
+        * Ensures all elements of the array are Parameter objects,
+        * and that the array keys match the main parameter name.
         * 
         * @since 0.4
         * 
         * @param array $paramInfo
         */
        protected function cleanParameterInfo( array &$paramInfo ) {
-               foreach ( $paramInfo as $key => &$parameter ) {
-                       $parameter = $parameter instanceof Parameter ? 
$parameter : Parameter::newFromArray( $key, $parameter );
+               $cleanedList = array();
+               
+               foreach ( $paramInfo as $key => $parameter ) {
+                       if ( $parameter instanceof Parameter ) {
+                               $cleanedList[$parameter->getName()] = 
$parameter;
+                       }
+                       else {
+                               $cleanedList[$key] = Parameter::newFromArray( 
$key, $parameter );
+                       }
                }
+               
+               $paramInfo = $cleanedList;
        }       
        
        /**
-        * Returns the main parameter name for a given parameter or alias, or 
false
-        * when it is not recognized as main parameter or alias.
-        *
-        * @param string $paramName
-        *
-        * @return string or false
+        * Validates and formats all the parameters (but aborts when a fatal 
error occurs).
+        * 
+        * @since 0.4
         */
-       protected function getMainParamName( $paramName ) {
-               $result = false;
+       public function validateParameters() {
+               // Start processing at the first parameter.
+               $keys = array_keys( $this->parameters );
+               $this->doParamProcessing( $keys[0] );
+               
+               // Loop over the remaining raw parameters.
+               // These are unrecognized parameters, as they where not used by 
any parameter definition.
+               foreach ( $this->rawParameters as $paramName => $paramValue ) {
+                       // TODO: error: unknown param
+               }
+       }
+       
+       /**
+        * Does the actual parameter processing. 
+        * 
+        * @since 0.4
+        * 
+        * @param string $firstParamName
+        * @param boolean $incFirst
+        */
+       protected function doParamProcessing( $firstParamName, $incFirst = true 
) {
+               $orderedParameters = $this->getParamsToProcess( 
$firstParamName, $incFirst );
 
-               if ( array_key_exists( $paramName, $this->parameters ) ) {
-                       $result = $paramName;
-               }
-               else {
-                       foreach ( $this->parameters as $name => $parameter ) {
-                               if ( $parameter->hasAlias( $paramName ) ) {
-                                       $result = $name;
+               foreach ( $orderedParameters as $paramName ) {
+                       $parameter = $this->parameters[$paramName];
+                       
+                       $setUservalue = $this->attemptToSetUserValue( 
$parameter );
+                       
+                       if ( !$setUservalue && $parameter->isRequired() ) {
+                               // TODO: FATAL
+                       }
+                       else {
+                               $validationSucceeded = $parameter->validate();
+                               
+                               if ( !$validationSucceeded ) {
+                                       foreach ( $parameter->getErrors() as 
$error ) {
+                                               $this->registerError( $error );
+                                       }
+                               }
+                               
+                               $paramCount = count( $this->parameters );
+                               
+                               $parameter->format( $this->parameters );        
+
+                               // This means additional parameters where added 
while formatting the param value.
+                               // To correctly handle dependencies, this 
function is called again for the remaining
+                               // parameters, and after execution, the current 
loop will be aborted.
+                               if ( $paramCount !== count( $this->parameters ) 
) {
+                                       $this->doParamProcessing( 
$parameter->getName(), false );
                                        break;
                                }
                        }
-               }
-
-               return $result;
+               }               
        }
        
        /**
-        * Validates and formats all the parameters (but aborts when a fatal 
error occurs).
+        * Gets an ordered list of parameters to process.
         * 
         * @since 0.4
+        * 
+        * @param string $firstParamName
+        * @param boolean $incFirst
+        * 
+        * @return array
         */
-       public function validateParameters() {
+       protected function getParamsToProcess( $firstParamName, $incFirst ) {
                $dependencyList = array();
                
+               $reachedStart = false;
+               
                foreach ( $this->parameters as $paramName => $parameter ) {
-                       $dependencyList[$paramName] = $parameter->dependencies;
+                       $reachedStart = $reachedStart || $paramName == 
$firstParamName;
+                       
+                       if ( $reachedStart ) {
+                               // If $incFirst is false, the first parameter 
should be ignored.
+                               if ( $incFirst ) {
+                                       $dependencyList[$paramName] = 
$parameter->getDependencies();
+                               }
+                               else {
+                                       $incFirst = true;
+                               }
+                       }
                }
                
                $sorter = new TopologicalSort( $dependencyList, true );
-               $orderedParameters = $sorter->doSort();
-
-               foreach ( $orderedParameters as $paramName ) {
-                       $parameter = $this->parameters[$paramName];
-                       
-                       $validationSucceeded = $parameter->validate();
-                       
-                       if ( !$validationSucceeded ) {
-                               foreach ( $parameter->getErrors() as $error ) {
-                                       $this->registerError( $error );
+               
+               return $sorter->doSort();               
+       }
+       
+       /**
+        * Tries to find a matching user provided value and, when found, 
assingns it
+        * to the parameter, and removes it from the raw values. Returns a 
boolean
+        * indicating if there was any user value set or not.
+        * 
+        * @since 0.4
+        * 
+        * @return boolean
+        */
+       protected function attemptToSetUserValue( Parameter $parameter ) {
+               if ( array_key_exists( $parameter->getName(), 
$this->rawParameters ) ) {
+                       $parameter->setUserValue( $parameter->getName(), 
$this->rawParameters[$parameter->getName()] );
+                       unset( $this->rawParameters[$parameter->getName()] );
+                       return true;
+               }
+               else {
+                       foreach ( $parameter->getAliases() as $alias ) {
+                               if ( array_key_exists( $alias, 
$this->rawParameters ) ) {
+                                       $parameter->setUserValue( $alias, 
$this->rawParameters[$alias] );
+                                       unset( $this->rawParameters[$alias] );
+                                       return true;
                                }
                        }
-                       
-                       $parameter->format( $this->parameters );
                }
+               
+               return false;
        }
        
        /**



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

Reply via email to