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

Revision: 72280
Author:   jeroendedauw
Date:     2010-09-03 12:37:32 +0000 (Fri, 03 Sep 2010)

Log Message:
-----------
Changes for 0.4 - work on error registration in Validator class

Modified Paths:
--------------
    trunk/extensions/Validator/Validator.i18n.php
    trunk/extensions/Validator/Validator.php
    trunk/extensions/Validator/includes/ParserHook.php
    trunk/extensions/Validator/includes/ValidationManager.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-09-03 12:06:47 UTC 
(rev 72279)
+++ trunk/extensions/Validator/Validator.i18n.php       2010-09-03 12:37:32 UTC 
(rev 72280)
@@ -23,7 +23,7 @@
        // General errors
        '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 
($2) with $3', 
+       'validator-error-override-argument' => 'Tried to override parameter $1 
(value: $2) with value "$3"', 
 
        // Criteria errors for single values
        'validator_error_empty_argument' => 'Parameter $1 can not have an empty 
value.',

Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php    2010-09-03 12:06:47 UTC (rev 
72279)
+++ trunk/extensions/Validator/Validator.php    2010-09-03 12:37:32 UTC (rev 
72280)
@@ -24,7 +24,7 @@
        die( 'Not an entry point.' );
 }
 
-define( 'Validator_VERSION', '0.4 alpha-2' );
+define( 'Validator_VERSION', '0.4 alpha-3' );
 
 // Constants indicating the strictness of the parameter validation.
 define( 'Validator_ERRORS_NONE', 0 );
@@ -45,6 +45,12 @@
        'descriptionmsg' => 'validator-desc',
 );
 
+// This function has been deprecated in 1.16, but needed for earlier versions.
+// It's present in 1.16 as a stub, but lets check if it exists in case it gets 
removed at some point.
+if ( function_exists( 'wfLoadExtensionMessages' ) ) {
+       wfLoadExtensionMessages( 'Validator' );
+}
+
 // Autoload the general classes.
 $incDir = dirname( __FILE__ ) . '/includes/';
 $wgAutoloadClasses['Validator']                        = $incDir . 
'Validator.php';

Modified: trunk/extensions/Validator/includes/ParserHook.php
===================================================================
--- trunk/extensions/Validator/includes/ParserHook.php  2010-09-03 12:06:47 UTC 
(rev 72279)
+++ trunk/extensions/Validator/includes/ParserHook.php  2010-09-03 12:37:32 UTC 
(rev 72280)
@@ -13,6 +13,10 @@
  */
 abstract class ParserHook {
        
+       protected $validator;
+       
+       protected $parser;
+       
        /**
         * Gets the name of the parser hook.
         * 
@@ -34,6 +38,15 @@
        protected abstract function render( array $parameters );        
        
        /**
+        * Constructor.
+        * 
+        * @since 0.4
+        */
+       public function __construct() {
+               $this->validator = new Validator( $this->getName() );
+       }
+       
+       /**
         * Function to hook up the coordinate rendering functions to the parser.
         * 
         * @since 0.4
@@ -78,6 +91,8 @@
         * @return string
         */
        public function renderTag( $input, array $args, Parser $parser, PPFrame 
$frame ) {
+               $this->parser = $parser;
+               
                $defaultParam = array_shift( $this->getDefaultParameters() );
                
                // If there is a first default parameter, set the tag contents 
as it's value.
@@ -101,8 +116,7 @@
        public function renderFunction() {
                $args = func_get_args();
                
-               // No need for the parser...
-               array_shift( $args );   
+               $this->parser = array_shift( $args );   
        
                return array( $this->validateAndRender( $args, false ) );
        }
@@ -119,28 +133,26 @@
         */
        public function validateAndRender( array $arguments, $parsed ) {
                global $egValidatorErrorLevel;
-
-               $validator = new Validator();           
                
                if ( $parsed ) {
-                       $validator->setParameters( $arguments, 
$this->getParameterInfo() );
+                       $this->validator->setParameters( $arguments, 
$this->getParameterInfo() );
                }
                else {
-                       $validator->parseAndSetParams( $arguments, 
$this->getParameterInfo(), $this->getDefaultParameters() );
+                       $this->validator->parseAndSetParams( $arguments, 
$this->getParameterInfo(), $this->getDefaultParameters() );
                }
                
-               $validator->validateAndFormatParameters();
+               $this->validator->validateAndFormatParameters();
                
-               if ( $validator->hasErrors() && $egValidatorErrorLevel < 
Validator_ERRORS_STRICT ) {
-                       $validator->correctInvalidParams();
+               if ( $this->validator->hasErrors() && $egValidatorErrorLevel < 
Validator_ERRORS_STRICT ) {
+                       $this->validator->correctInvalidParams();
                }
                
-               if ( $validator->hasFatalError() ) {
+               if ( $this->validator->hasFatalError() ) {
                        // TODO
                        $output = 'Demo: fatal error';
                }
                else {
-                       $output = $this->render( $validator->getValidParams( 
false ) );
+                       $output = $this->render( 
$this->validator->getValidParams( false ) );
                }
                
                return $output;

Modified: trunk/extensions/Validator/includes/ValidationManager.php
===================================================================
--- trunk/extensions/Validator/includes/ValidationManager.php   2010-09-03 
12:06:47 UTC (rev 72279)
+++ trunk/extensions/Validator/includes/ValidationManager.php   2010-09-03 
12:37:32 UTC (rev 72280)
@@ -3,6 +3,8 @@
 /**
  * Class for parameter handling.
  *
+ * @deprecated
+ *
  * @file ValidationManager.php
  * @ingroup Validator
  *

Modified: trunk/extensions/Validator/includes/Validator.php
===================================================================
--- trunk/extensions/Validator/includes/Validator.php   2010-09-03 12:06:47 UTC 
(rev 72279)
+++ trunk/extensions/Validator/includes/Validator.php   2010-09-03 12:37:32 UTC 
(rev 72280)
@@ -129,27 +129,62 @@
        protected $errors = array();
 
        /**
-        * Constructor.
         * 
+        * 
         * @since 0.4
+        * 
+        * @var string
         */
-       public function __construct() {
-               // TODO
-       }
+       protected $element;
        
        /**
+        * Constructor.
         * 
+        * @param srting $element
         * 
         * @since 0.4
-        * 
-        * @return string
         */
-       protected function getElement() {
-               return '';
-               // TODO
-       } 
+       public function __construct( $element = '' ) {
+               $this->element = $element;
+       }
        
        /**
+        * Adds a new criteria type and the validation function that should 
validate values of this type.
+        * You can use this function to override existing criteria type 
handlers.
+        *
+        * @param string $criteriaName The name of the cirteria.
+        * @param array $functionName The functions location. If it's a global 
function, only the name,
+        * if it's in a class, first the class name, then the method name.
+        */
+       public static function addValidationFunction( $criteriaName, array 
$functionName ) {
+               self::$mValidationFunctions[$criteriaName] = $functionName;
+       }
+       
+       /**
+        * Adds a new list criteria type and the validation function that 
should validate values of this type.
+        * You can use this function to override existing criteria type 
handlers.
+        *
+        * @param string $criteriaName The name of the list cirteria.
+        * @param array $functionName The functions location. If it's a global 
function, only the name,
+        * if it's in a class, first the class name, then the method name.
+        */
+       public static function addListValidationFunction( $criteriaName, array 
$functionName ) {
+               self::$mListValidationFunctions[strtolower( $criteriaName )] = 
$functionName;
+       }
+       
+       /**
+        * Adds a new output format and the formatting function that should 
validate values of this type.
+        * You can use this function to override existing criteria type 
handlers.
+        *
+        * @param string $formatName The name of the format.
+        * @param array $functionName The functions location. If it's a global 
function, only the name,
+        * if it's in a class, first the class name, then the method name.
+        */
+       public static function addOutputFormat( $formatName, array 
$functionName ) {
+               self::$mOutputFormats[strtolower( $formatName )] = 
$functionName;
+       }       
+       
+       /**
         * Registers an error.
         * 
         * @param string $message
@@ -160,7 +195,7 @@
                $error = new ValidatorError(
                        $message,
                        $severity,
-                       $this->getElement(),
+                       $this->element,
                        (array)$tags
                );
                
@@ -392,7 +427,7 @@
                                                        $paramName
                                                ),
                                                'missing'               
-                                       );                                      
        
+                                       );
                                }
                                else {
                                        // Set the default value (or default 
'default value' if none is provided), and ensure the type is correct.
@@ -815,43 +850,117 @@
         * @return boolean
         */
        public function hasFatalError() {
-               // TODO
-               return false;
+               $has = false;
+               
+               foreach ( $this->errors as $error ) {
+                       if ( $error->severity >= 
ValidatorError::SEVERITY_CRITICAL ) {
+                               $has = true;
+                               break;
+                       }
+               }
+               
+               return $has;
        }       
-
-       /**
-        * Adds a new criteria type and the validation function that should 
validate values of this type.
-        * You can use this function to override existing criteria type 
handlers.
-        *
-        * @param string $criteriaName The name of the cirteria.
-        * @param array $functionName The functions location. If it's a global 
function, only the name,
-        * if it's in a class, first the class name, then the method name.
-        */
-       public static function addValidationFunction( $criteriaName, array 
$functionName ) {
-               self::$mValidationFunctions[$criteriaName] = $functionName;
-       }
        
        /**
-        * Adds a new list criteria type and the validation function that 
should validate values of this type.
-        * You can use this function to override existing criteria type 
handlers.
-        *
-        * @param string $criteriaName The name of the list cirteria.
-        * @param array $functionName The functions location. If it's a global 
function, only the name,
-        * if it's in a class, first the class name, then the method name.
+        * Returns an error message for a criteria validation that failed.
+        * 
+        * @since 0.4
+        * 
+        * @param string $criteria
+        * @param string $paramName
+        * @param string $paramValue
+        * @param array $args
+        * @param boolean $isList
+        * @param array $invalidItems
+        * 
+        * @return string
         */
-       public static function addListValidationFunction( $criteriaName, array 
$functionName ) {
-               self::$mListValidationFunctions[strtolower( $criteriaName )] = 
$functionName;
+       protected function getCriteriaErrorMessage( $criteria, $paramName, 
$paramValue, array $args = array(), $isList = false, array $invalidItems = 
array() ) {
+               global $wgLang, $egValidatorErrorLevel;
+               
+               if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW && 
$this->validator->hasErrors() ) {
+                       $rawErrors = $this->validator->getErrors();
+                       
+                       $errorList = '<b>' . wfMsgExt( 
'validator_error_parameters', 'parsemag', count( $rawErrors ) ) . '</b><br 
/><i>';
+
+                       $errors = array();
+                       
+                       foreach ( $rawErrors as $error ) {
+                               $error['name'] = '<b>' . Sanitizer::escapeId( 
$error['name'] ) . '</b>';
+                               
+                               if ( $error['type'] == 'unknown' ) {
+                                       $errors[] = wfMsgExt( 
'validator_error_unknown_argument', array( 'parsemag' ), $error['name'] );
+                               }
+                               elseif ( $error['type'] == 'missing' ) {
+                                       $errors[] = wfMsgExt( 
'validator_error_required_missing', array( 'parsemag' ), $error['name'] );
+                               }
+                               elseif ( array_key_exists( 'list', $error ) && 
$error['list'] ) {
+                                       switch( $error['type'] ) {
+                                               case 'not_empty' :
+                                                       $msg = wfMsgExt( 
'validator_list_error_empty_argument', array( 'parsemag' ), $error['name'] );
+                                                       break;
+                                               case 'in_range' :
+                                                       $msg = wfMsgExt( 
'validator_list_error_invalid_range', array( 'parsemag' ), $error['name'], 
'<b>' . $error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' );
+                                                       break;
+                                               case 'is_numeric' :
+                                                       $msg = wfMsgExt( 
'validator_list_error_must_be_number', array( 'parsemag' ), $error['name'] );
+                                                       break;
+                                               case 'is_integer' :
+                                                       $msg = wfMsgExt( 
'validator_list_error_must_be_integer', array( 'parsemag' ), $error['name'] );
+                                                       break;
+                                               case 'in_array' :
+                                                       $itemsText = 
$wgLang->listToText( $error['args'] );
+                                                       $msg = wfMsgExt( 
'validator_error_accepts_only', array( 'parsemag' ), $error['name'], 
$itemsText, count( $error['args'] ) );
+                                                       break;
+                                               case 'invalid' : default :
+                                                       $msg = wfMsgExt( 
'validator_list_error_invalid_argument', array( 'parsemag' ), $error['name'] );
+                                                       break;
+                                       }
+
+                                       if ( array_key_exists( 'invalid-items', 
$error ) ) {
+                                               $omitted = array();
+                                               foreach ( 
$error['invalid-items'] as $item ) $omitted[] = Sanitizer::escapeId( $item );
+                                               $msg .= ' ' . wfMsgExt( 
'validator_list_omitted', array( 'parsemag' ),
+                                                       $wgLang->listToText( 
$omitted ), count( $omitted ) );
+                                       }
+
+                                       $errors[] = $msg;
+                               }
+                               else {
+                                       switch( $error['type'] ) {
+                                               case 'not_empty' :
+                                                       $errors[] = wfMsgExt( 
'validator_error_empty_argument', array( 'parsemag' ), $error['name'] );
+                                                       break;
+                                               case 'in_range' :
+                                                       $errors[] = wfMsgExt( 
'validator_error_invalid_range', array( 'parsemag' ), $error['name'], '<b>' . 
$error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' );
+                                                       break;
+                                               case 'is_numeric' :
+                                                       $errors[] = wfMsgExt( 
'validator_error_must_be_number', array( 'parsemag' ), $error['name'] );
+                                                       break;
+                                               case 'is_integer' :
+                                                       $errors[] = wfMsgExt( 
'validator_error_must_be_integer', array( 'parsemag' ), $error['name'] );
+                                                       break;
+                                               case 'in_array' :
+                                                       $itemsText = 
$wgLang->listToText( $error['args'] );
+                                                       $errors[] = wfMsgExt( 
'validator_error_accepts_only', array( 'parsemag' ), $error['name'], 
$itemsText, count( $error['args'] ) );
+                                                       break;
+                                               case 'invalid' : default :
+                                                       $errors[] = wfMsgExt( 
'validator_error_invalid_argument', array( 'parsemag' ), '<b>' . 
htmlspecialchars( $error['value'] ) . '</b>', $error['name'] );
+                                                       break;
+                                       }
+                               }
+                       }
+
+                       return $errorList . implode( $errors, '<br />' ) . 
'</i><br />';
+               }
+               elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN && 
$this->validator->hasErrors() ) {
+                       return '<b>' . wfMsgExt( 
'validator_warning_parameters', array( 'parsemag' ), count( 
$this->validator->getErrors() ) ) . '</b>';
+               }
+               else {
+                       return '';
+               }
+               
        }
        
-       /**
-        * Adds a new output format and the formatting function that should 
validate values of this type.
-        * You can use this function to override existing criteria type 
handlers.
-        *
-        * @param string $formatName The name of the format.
-        * @param array $functionName The functions location. If it's a global 
function, only the name,
-        * if it's in a class, first the class name, then the method name.
-        */
-       public static function addOutputFormat( $formatName, array 
$functionName ) {
-               self::$mOutputFormats[strtolower( $formatName )] = 
$functionName;
-       }
 }
\ No newline at end of file

Modified: 
trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php
===================================================================
--- trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php    
2010-09-03 12:06:47 UTC (rev 72279)
+++ trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php    
2010-09-03 12:37:32 UTC (rev 72280)
@@ -105,8 +105,7 @@
                $errorList = ValidatorErrorHandler::getErrorList( 
self::$severityMap[$parameters['minseverity']] );
                
                if ( $errorList ) {
-                       // TODO: render wikitext
-                       return $errorList;
+                       return $this->parser->recursiveTagParse( $errorList );
                }
                else {
                        return '';



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

Reply via email to