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

Revision: 74060
Author:   jeroendedauw
Date:     2010-10-01 09:14:19 +0000 (Fri, 01 Oct 2010)

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

Modified Paths:
--------------
    trunk/extensions/Validator/Validator.i18n.php
    trunk/extensions/Validator/Validator.php
    trunk/extensions/Validator/Validator_Settings.php
    trunk/extensions/Validator/includes/ParserHook.php
    trunk/extensions/Validator/includes/ValidationError.php
    trunk/extensions/Validator/includes/Validator.php
    trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php

Modified: trunk/extensions/Validator/Validator.i18n.php
===================================================================
--- trunk/extensions/Validator/Validator.i18n.php       2010-10-01 07:01:47 UTC 
(rev 74059)
+++ trunk/extensions/Validator/Validator.i18n.php       2010-10-01 09:14:19 UTC 
(rev 74060)
@@ -17,11 +17,14 @@
 $messages['en'] = array(
        'validator-desc' => 'Provides generic parameter handling support for 
other extensions',
 
+       'validator-warning' => '<b>Warning</b>: $1',
+       'validator-error' => '<b>Error</b>: $1',
+       'validator-fatal-error' => '<b>Fatal error</b>: $1',
        'validator_error_parameters' => 'The following {{PLURAL:$1|error 
has|errors have}} been detected in your syntax:',
        'validator_warning_parameters' => 'There {{PLURAL:$1|is an error|are 
errors}} in your syntax.',
+       'validator-warning-adittional-errors' => '... and {{PLURAL:$1|one more 
issue|multiple more issues}}.',
 
        // General errors
-       'validator-error' => '<b>Error</b>: $1',
        'validator_error_unknown_argument' => '$1 is not a valid parameter.',
        'validator_error_required_missing' => 'The required parameter "$1" is 
not provided.',
        'validator-error-override-argument' => 'Tried to override parameter $1 
(value: $2) with value "$3"', 

Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php    2010-10-01 07:01:47 UTC (rev 
74059)
+++ trunk/extensions/Validator/Validator.php    2010-10-01 09:14:19 UTC (rev 
74060)
@@ -26,13 +26,6 @@
 
 define( 'Validator_VERSION', '0.4 alpha-6' );
 
-// Constants indicating the strictness of the parameter validation.
-define( 'Validator_ERRORS_NONE', 0 );
-define( 'Validator_ERRORS_LOG', 1 );
-define( 'Validator_ERRORS_WARN', 2 );
-define( 'Validator_ERRORS_SHOW', 3 );
-define( 'Validator_ERRORS_STRICT', 4 );
-
 // Register the internationalization file.
 $wgExtensionMessagesFiles['Validator'] = dirname( __FILE__ ) . 
'/Validator.i18n.php';
 

Modified: trunk/extensions/Validator/Validator_Settings.php
===================================================================
--- trunk/extensions/Validator/Validator_Settings.php   2010-10-01 07:01:47 UTC 
(rev 74059)
+++ trunk/extensions/Validator/Validator_Settings.php   2010-10-01 09:14:19 UTC 
(rev 74060)
@@ -21,14 +21,10 @@
 $wgHooks['ParserFirstCallInit'][] = 'ValidatorListErrors::staticInit';
 $wgHooks['LanguageGetMagic'][] = 'ValidatorListErrors::staticMagic';
 
-# Validator_ERRORS_NONE        : Validator will not show any errors, and make 
the best of the input it got.
-# Validator_ERRORS_WARN                : Validator will make the best of the 
input it got, but will show a warning that there are errors.
-# Validator_ERRORS_SHOW                : Validator will make the best of the 
input it got, but will show a list of all errors.
-# Validator_ERRORS_STRICT      : Validator will only show regular output when 
there are no errors, if there are, a list of them will be shown.
-$egErrorLevel = array(
-       ValidationError::SEVERITY_MINOR => Validator_ERRORS_LOG,
-       ValidationError::SEVERITY_LOW => Validator_ERRORS_LOG,
-       ValidationError::SEVERITY_NORMAL => Validator_ERRORS_WARN,
-       ValidationError::SEVERITY_HIGH => Validator_ERRORS_SHOW,
-       ValidationError::SEVERITY_CRITICAL => Validator_ERRORS_STRICT,
+// TODO: document
+$egErrorActions = array(
+       ValidationError::SEVERITY_MINOR => ValidationError::ACTION_LOG,
+       ValidationError::SEVERITY_LOW => ValidationError::ACTION_WARN,
+       ValidationError::SEVERITY_NORMAL => ValidationError::ACTION_SHOW,
+       ValidationError::SEVERITY_HIGH => ValidationError::ACTION_DEMAND,
 );
\ No newline at end of file

Modified: trunk/extensions/Validator/includes/ParserHook.php
===================================================================
--- trunk/extensions/Validator/includes/ParserHook.php  2010-10-01 07:01:47 UTC 
(rev 74059)
+++ trunk/extensions/Validator/includes/ParserHook.php  2010-10-01 09:14:19 UTC 
(rev 74060)
@@ -97,7 +97,7 @@
         * @param minxed $input string or null
         * @param array $args
         * @param Parser $parser
-        * @param PPFrame $frame Available from 1.16 - commented for bc for now
+        * @param PPFrame $frame Available from 1.16 - commented out for bc for 
now
         * 
         * @return string
         */
@@ -160,26 +160,85 @@
                
                if ( $fatalError === false ) {
                        $output = $this->render( 
$this->validator->getParameterValues() );
+                       $output = $this->renderErrors( $output );
                }
                else {
-                       $output = $this->renderError( $fatalError );            
+                       $output = $this->renderFatalError( $fatalError );       
        
                }
                
                return $output;
        }
        
        /**
+        * Returns the ValidationError objects for the errors and warnings that 
should be displayed.
+        * 
+        * @since 0.4
+        * 
+        * @return array of array of ValidationError
+        */
+       protected function getErrorsToDisplay() {
+               $errors = array();
+               $warnings = array();
+               
+               foreach ( $this->validator->getErrors() as $error ) {
+                       // Check if the severity of the error is high enough to 
display it.
+                       if ( $error->shouldShow() ) {
+                               $errors[] = $error;
+                       }
+                       else if ( $error->shouldWarn() ) {
+                               $warnings[] = $error;
+                       }
+               }
+               
+               return array( 'errors' => $errors, 'warnings' => $warnings );
+       }
+       
+       /**
         * Creates and returns the output when a fatal error prevent regular 
rendering.
         * 
         * @since 0.4
         * 
+        * @param ValidationError $error
+        * 
         * @return string
         */
-       protected function renderError( ValidationError $error ) {
+       protected function renderFatalError( ValidationError $error ) {
                return wfMsgExt( 'validator-error', 'parsemag', 
$error->getMessage() );
        }
        
        /**
+        * @since 0.4
+        * 
+        * @param string $output
+        * 
+        * @return string
+        */
+       protected function renderErrors( $output ) {
+               $displayStuff = $this->getErrorsToDisplay();
+               
+               if ( count( $displayStuff['errors'] ) > 0 ) {
+                       $output .= wfMsgExt( 'validator_error_parameters', 
'parsemag', count( $displayStuff['errors'] ) );
+                       
+                       foreach( $displayStuff['errors'] as $error ) {
+                               $output .= '<br />* ' . $error->getMessage();
+                       }
+                       
+                       if ( count( $displayStuff['warnings'] ) > 0 ) {
+                               $output .= '<br />* ' . wfMsgExt( 
'validator-warning-adittional-errors', 'parsemag', count( 
$displayStuff['warnings'] ) );
+                       }
+               }
+               else if ( count( $displayStuff['warnings'] ) > 0 ) {
+                       $output .= wfMsgExt(
+                               'validator-warning',
+                               'parsemag',
+                               wfMsgExt( 'validator_warning_parameters', 
'parsemag', count( $displayStuff['warnings'] ) )
+                       );
+               }
+               
+               return $output;
+       }
+       
+       /**
         * Returns an array containing the parameter info.
         * Override in deriving classes to add parameter info.
         * 

Modified: trunk/extensions/Validator/includes/ValidationError.php
===================================================================
--- trunk/extensions/Validator/includes/ValidationError.php     2010-10-01 
07:01:47 UTC (rev 74059)
+++ trunk/extensions/Validator/includes/ValidationError.php     2010-10-01 
09:14:19 UTC (rev 74060)
@@ -12,12 +12,18 @@
  */
 class ValidationError {
        
-       const SEVERITY_MINOR = 0;
-       const SEVERITY_LOW = 1;
-       const SEVERITY_NORMAL = 2;
-       const SEVERITY_HIGH = 3;
-       const SEVERITY_CRITICAL = 4;
+       const SEVERITY_MINOR = 0;       // Minor error. ie a decapriation notice
+       const SEVERITY_LOW = 1;         // Lower-then-normal severity. ie an 
unknown parameter
+       const SEVERITY_NORMAL = 2;      // Normal severiry. ie an invalid value 
provided
+       const SEVERITY_HIGH = 3;        // Higher-then-normal severity. ie an 
invalid value for a significant parameter
+       const SEVERITY_FATAL = 4;       // Fatal error. Either a missing or an 
invalid required parameter
 
+       const ACTION_IGNORE = 0;        // Ignore the error
+       const ACTION_LOG = 1;           // Log the error
+       const ACTION_WARN = 2;          // Warn that there is an error
+       const ACTION_SHOW = 3;          // Show the error
+       const ACTION_DEMAND = 4;        // Show the error and don't render 
output
+       
        public $message;
        public $severity;
        
@@ -65,4 +71,114 @@
                return $this->message;
        }
        
+       /**
+        * Returns the severity of the error.
+        * 
+        * @since 0.4
+        * 
+        * @return integer Element of the ValidationError::SEVERITY_ enum
+        */     
+       public function getSeverity() {
+               return $this->severity;
+       }
+       
+       /**
+        * Returns if the severity is equal to or bigger then the provided one.
+        * 
+        * @since 0.4
+        * 
+        * @return boolean
+        */
+       public function hasSeverity( $severity ) {
+               return $this->severity >= $severity;
+       }
+       
+       /**
+        * Returns the action associated with the errors severity.
+        * 
+        * @since 0.4
+        * 
+        * @return integer Element of the ValidationError::ACTION_ enum
+        */
+       public function getAction() {
+               global $egErrorActions;
+               
+               if ( $this->severity === self::SEVERITY_FATAL ) {
+                       // This action should not be configurable, as lowering 
it would break in the Validator class.
+                       return self::ACTION_DEMAND;
+               }
+               else if ( array_key_exists( $this->severity, $egErrorActions ) 
) {
+                       return $egErrorActions[$this->severity];
+               }
+               else {
+                       throw new Exception( "No action associated with error 
severity '$this->severity'" );
+               }
+       }       
+       
+       /**
+        * Returns if the action associated with the severity is equal to or 
bigger then the provided one.
+        * 
+        * @since 0.4
+        * 
+        * @return boolean
+        */
+       public function hasAction( $action ) {
+               return $this->getAction() >= $action;
+       }
+       
+       /**
+        * Returns if the error is fatal.
+        * 
+        * @since 0.4
+        * 
+        * @return boolean
+        */
+       public function isFatal() {
+               return $this->hasSeverity( self::SEVERITY_FATAL );
+       }
+       
+       /**
+        * Returns if the error should be logged.
+        * 
+        * @since 0.4
+        * 
+        * @return boolean
+        */
+       public function shouldLog() {
+               return $this->hasAction( self::ACTION_LOG );
+       }
+       
+       /**
+        * Returns if there should be a warning that errors are present.
+        * 
+        * @since 0.4
+        * 
+        * @return boolean
+        */
+       public function shouldWarn() {
+               return $this->hasAction( self::ACTION_WARN );
+       }
+       
+       /**
+        * Returns if the error message should be shown. 
+        * 
+        * @since 0.4
+        * 
+        * @return boolean
+        */
+       public function shouldShow() {
+               return $this->hasAction( self::ACTION_SHOW );
+       }       
+       
+       /**
+        * Returns if the error message should be shown, and the output not be 
rendered. 
+        * 
+        * @since 0.4
+        * 
+        * @return boolean
+        */
+       public function shouldDemand() {
+               return $this->hasAction( self::ACTION_DEMAND );
+       }
+       
 }
\ No newline at end of file

Modified: trunk/extensions/Validator/includes/Validator.php
===================================================================
--- trunk/extensions/Validator/includes/Validator.php   2010-10-01 07:01:47 UTC 
(rev 74059)
+++ trunk/extensions/Validator/includes/Validator.php   2010-10-01 09:14:19 UTC 
(rev 74060)
@@ -388,8 +388,10 @@
         * @return mixed false or ValidationError
         */
        public function hasFatalError() {
+               global $egErrorLevel;
+               
                foreach ( $this->errors as $error ) {
-                       if ( $error->severity >= 
ValidationError::SEVERITY_CRITICAL ) {
+                       if ( $error->isFatal() ) {
                                return $error;
                        }
                }

Modified: 
trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php
===================================================================
--- trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php    
2010-10-01 07:01:47 UTC (rev 74059)
+++ trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php    
2010-10-01 09:14:19 UTC (rev 74060)
@@ -25,7 +25,7 @@
                'low' => ValidationError::SEVERITY_LOW,
                'normal' => ValidationError::SEVERITY_NORMAL,
                'high' => ValidationError::SEVERITY_HIGH,
-               'critical' => ValidationError::SEVERITY_CRITICAL,
+               'fatal' => ValidationError::SEVERITY_FATAL
        );
        
        /**



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

Reply via email to