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

Revision: 74062
Author:   jeroendedauw
Date:     2010-10-01 10:19:32 +0000 (Fri, 01 Oct 2010)

Log Message:
-----------
Changes for 0.4 - work on error handling

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

Modified: trunk/extensions/Validator/includes/ItemParameterCriterion.php
===================================================================
--- trunk/extensions/Validator/includes/ItemParameterCriterion.php      
2010-10-01 09:23:41 UTC (rev 74061)
+++ trunk/extensions/Validator/includes/ItemParameterCriterion.php      
2010-10-01 10:19:32 UTC (rev 74062)
@@ -87,15 +87,32 @@
                        }
                        
                        if ( $result->hasInvalidItems() ) {
+                               $allInvalid = count( $result->getInvalidItems() 
) == count( $parameter->value );
+                               
+                               // If the parameter is required and all items 
are invalid, it's fatal.
+                               // Else it's high for required, and normal for 
non-required parameters.
+                               if ( $parameter->isRequired() ) {
+                                       $severity = $allInvalid ? 
ValidationError::SEVERITY_FATAL : ValidationError::SEVERITY_HIGH;
+                               }
+                               else {
+                                       $severity = $allInvalid ? 
ValidationError::SEVERITY_NORMAL : ValidationError::SEVERITY_LOW;
+                               }
+                                       
                                $result->addError(
-                                       new ValidationError( 
$this->getListErrorMessage( $parameter, $result->getInvalidItems() ) )          
                   
+                                       new ValidationError(
+                                               $this->getListErrorMessage( 
$parameter, $result->getInvalidItems() ),
+                                               $severity
+                                       )
                                );
                        }
                }
                else {
                        if ( !$this->doValidation( $parameter->value ) ) {
                                $result->addError(
-                                       new ValidationError( 
$this->getItemErrorMessage( $parameter ) )
+                                       new ValidationError(
+                                               $this->getItemErrorMessage( 
$parameter ),
+                                               $parameter->isRequired() ? 
ValidationError::SEVERITY_FATAL : ValidationError::SEVERITY_NORMAL
+                                       )
                                );
                        }
                }

Modified: trunk/extensions/Validator/includes/Parameter.php
===================================================================
--- trunk/extensions/Validator/includes/Parameter.php   2010-10-01 09:23:41 UTC 
(rev 74061)
+++ trunk/extensions/Validator/includes/Parameter.php   2010-10-01 10:19:32 UTC 
(rev 74062)
@@ -433,10 +433,9 @@
        
        /**
         * Validates the parameter value.
+        * Also sets the value to the default when it's not set or invalid, 
assuming there is a default.
         * 
         * @since 0.4
-        * 
-        * @return boolean Indicates if there was any validation error
         */     
        protected function doValidation() {
                if ( $this->setCount == 0 ) {
@@ -445,42 +444,35 @@
                                throw new Exception( 'Attempted to validate a 
required parameter without first setting a value.' );
                        }
                        else {
-                               $success = true;
                                $this->setToDefault();
                        }
                }
                else {
-                       $success = $this->validateCriteria();
+                       $this->validateCriteria();
+                       
+                       if ( count( $this->errors ) > 0 && 
!$this->hasFatalError() ) {
+                               $this->setToDefault();
+                       }
                }
-
-               return $success;
        }
        
        /**
         * Validates the provided value against all criteria.
         * 
         * @since 0.4
-        * 
-        * @return boolean Indicates if there was any validation error
         */
        protected function validateCriteria() {
-               $success = true;
-
                foreach ( $this->getCriteria() as $criterion ) {
                        $validationResult = $criterion->validate( $this );
                        
                        if ( !$validationResult->isValid() ) {
-                               $success = false;
-                               
                                $this->handleValidationError( $validationResult 
);
                                
-                               if ( !self::$accumulateParameterErrors ) {
-                                       break;
+                               if ( !self::$accumulateParameterErrors || 
$this->hasFatalError() ) {
+                                       break; 
                                }
                        }
                }
-
-               return $success;
        }
        
        /**
@@ -753,4 +745,19 @@
                $this->applyManipulationsToDefault = $doOrDoNot;
        }
        
+       /**
+        * Returns false when there are no fatal errors or an ValidationError 
when one is found.
+        * 
+        * @return mixed false or ValidationError
+        */
+       public function hasFatalError() {
+               foreach ( $this->errors as $error ) {
+                       if ( $error->isFatal() ) {
+                               return true;
+                       }
+               }
+               
+               return false;
+       }               
+       
 }
\ No newline at end of file

Modified: trunk/extensions/Validator/includes/Validator.php
===================================================================
--- trunk/extensions/Validator/includes/Validator.php   2010-10-01 09:23:41 UTC 
(rev 74061)
+++ trunk/extensions/Validator/includes/Validator.php   2010-10-01 10:19:32 UTC 
(rev 74062)
@@ -250,17 +250,20 @@
                                $this->registerNewError(
                                        wfMsgExt( 
'validator_error_required_missing', 'parsemag', $paramName ),
                                        array(),
-                                       ValidationError::SEVERITY_CRITICAL
+                                       ValidationError::SEVERITY_FATAL
                                );
                                break;
                        }
                        else {
-                               $validationSucceeded = $parameter->validate();
+                               $parameter->validate();                 
                                
-                               if ( !$validationSucceeded ) {
-                                       foreach ( $parameter->getErrors() as 
$error ) {
-                                               $this->registerError( $error );
-                                       }
+                               foreach ( $parameter->getErrors() as $error ) {
+                                       $this->registerError( $error );
+                               }                               
+                               
+                               if ( $parameter->hasFatalError() ) {
+                                       // If there was a fatal error, and the 
parameter is required, stop processing. 
+                                       break;
                                }
                                
                                $initialSet = $this->parameters;
@@ -293,7 +296,7 @@
                                if ( !array_key_exists( $paramName, 
$initialParamSet ) ) {
                                        $this->paramsTohandle[] = $paramName;
                                }
-                       }                       
+                       }       
                }
                
                $dependencies = array();
@@ -390,8 +393,6 @@
         * @return mixed false or ValidationError
         */
        public function hasFatalError() {
-               global $egErrorLevel;
-               
                foreach ( $this->errors as $error ) {
                        if ( $error->isFatal() ) {
                                return $error;



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

Reply via email to