r72280 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72279‎ | r72280 | r72281 >
Date:12:37, 3 September 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.4 - work on error registration in Validator class
Modified paths:
  • /trunk/extensions/Validator/Validator.i18n.php (modified) (history)
  • /trunk/extensions/Validator/Validator.php (modified) (history)
  • /trunk/extensions/Validator/includes/ParserHook.php (modified) (history)
  • /trunk/extensions/Validator/includes/ValidationManager.php (modified) (history)
  • /trunk/extensions/Validator/includes/Validator.php (modified) (history)
  • /trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php
@@ -105,8 +105,7 @@
106106 $errorList = ValidatorErrorHandler::getErrorList( self::$severityMap[$parameters['minseverity']] );
107107
108108 if ( $errorList ) {
109 - // TODO: render wikitext
110 - return $errorList;
 109+ return $this->parser->recursiveTagParse( $errorList );
111110 }
112111 else {
113112 return '';
Index: trunk/extensions/Validator/includes/ParserHook.php
@@ -13,6 +13,10 @@
1414 */
1515 abstract class ParserHook {
1616
 17+ protected $validator;
 18+
 19+ protected $parser;
 20+
1721 /**
1822 * Gets the name of the parser hook.
1923 *
@@ -34,6 +38,15 @@
3539 protected abstract function render( array $parameters );
3640
3741 /**
 42+ * Constructor.
 43+ *
 44+ * @since 0.4
 45+ */
 46+ public function __construct() {
 47+ $this->validator = new Validator( $this->getName() );
 48+ }
 49+
 50+ /**
3851 * Function to hook up the coordinate rendering functions to the parser.
3952 *
4053 * @since 0.4
@@ -78,6 +91,8 @@
7992 * @return string
8093 */
8194 public function renderTag( $input, array $args, Parser $parser, PPFrame $frame ) {
 95+ $this->parser = $parser;
 96+
8297 $defaultParam = array_shift( $this->getDefaultParameters() );
8398
8499 // If there is a first default parameter, set the tag contents as it's value.
@@ -101,8 +116,7 @@
102117 public function renderFunction() {
103118 $args = func_get_args();
104119
105 - // No need for the parser...
106 - array_shift( $args );
 120+ $this->parser = array_shift( $args );
107121
108122 return array( $this->validateAndRender( $args, false ) );
109123 }
@@ -119,28 +133,26 @@
120134 */
121135 public function validateAndRender( array $arguments, $parsed ) {
122136 global $egValidatorErrorLevel;
123 -
124 - $validator = new Validator();
125137
126138 if ( $parsed ) {
127 - $validator->setParameters( $arguments, $this->getParameterInfo() );
 139+ $this->validator->setParameters( $arguments, $this->getParameterInfo() );
128140 }
129141 else {
130 - $validator->parseAndSetParams( $arguments, $this->getParameterInfo(), $this->getDefaultParameters() );
 142+ $this->validator->parseAndSetParams( $arguments, $this->getParameterInfo(), $this->getDefaultParameters() );
131143 }
132144
133 - $validator->validateAndFormatParameters();
 145+ $this->validator->validateAndFormatParameters();
134146
135 - if ( $validator->hasErrors() && $egValidatorErrorLevel < Validator_ERRORS_STRICT ) {
136 - $validator->correctInvalidParams();
 147+ if ( $this->validator->hasErrors() && $egValidatorErrorLevel < Validator_ERRORS_STRICT ) {
 148+ $this->validator->correctInvalidParams();
137149 }
138150
139 - if ( $validator->hasFatalError() ) {
 151+ if ( $this->validator->hasFatalError() ) {
140152 // TODO
141153 $output = 'Demo: fatal error';
142154 }
143155 else {
144 - $output = $this->render( $validator->getValidParams( false ) );
 156+ $output = $this->render( $this->validator->getValidParams( false ) );
145157 }
146158
147159 return $output;
Index: trunk/extensions/Validator/includes/ValidationManager.php
@@ -3,6 +3,8 @@
44 /**
55 * Class for parameter handling.
66 *
 7+ * @deprecated
 8+ *
79 * @file ValidationManager.php
810 * @ingroup Validator
911 *
Index: trunk/extensions/Validator/includes/Validator.php
@@ -129,27 +129,62 @@
130130 protected $errors = array();
131131
132132 /**
133 - * Constructor.
134133 *
 134+ *
135135 * @since 0.4
 136+ *
 137+ * @var string
136138 */
137 - public function __construct() {
138 - // TODO
139 - }
 139+ protected $element;
140140
141141 /**
 142+ * Constructor.
142143 *
 144+ * @param srting $element
143145 *
144146 * @since 0.4
145 - *
146 - * @return string
147147 */
148 - protected function getElement() {
149 - return '';
150 - // TODO
151 - }
 148+ public function __construct( $element = '' ) {
 149+ $this->element = $element;
 150+ }
152151
153152 /**
 153+ * Adds a new criteria type and the validation function that should validate values of this type.
 154+ * You can use this function to override existing criteria type handlers.
 155+ *
 156+ * @param string $criteriaName The name of the cirteria.
 157+ * @param array $functionName The functions location. If it's a global function, only the name,
 158+ * if it's in a class, first the class name, then the method name.
 159+ */
 160+ public static function addValidationFunction( $criteriaName, array $functionName ) {
 161+ self::$mValidationFunctions[$criteriaName] = $functionName;
 162+ }
 163+
 164+ /**
 165+ * Adds a new list criteria type and the validation function that should validate values of this type.
 166+ * You can use this function to override existing criteria type handlers.
 167+ *
 168+ * @param string $criteriaName The name of the list cirteria.
 169+ * @param array $functionName The functions location. If it's a global function, only the name,
 170+ * if it's in a class, first the class name, then the method name.
 171+ */
 172+ public static function addListValidationFunction( $criteriaName, array $functionName ) {
 173+ self::$mListValidationFunctions[strtolower( $criteriaName )] = $functionName;
 174+ }
 175+
 176+ /**
 177+ * Adds a new output format and the formatting function that should validate values of this type.
 178+ * You can use this function to override existing criteria type handlers.
 179+ *
 180+ * @param string $formatName The name of the format.
 181+ * @param array $functionName The functions location. If it's a global function, only the name,
 182+ * if it's in a class, first the class name, then the method name.
 183+ */
 184+ public static function addOutputFormat( $formatName, array $functionName ) {
 185+ self::$mOutputFormats[strtolower( $formatName )] = $functionName;
 186+ }
 187+
 188+ /**
154189 * Registers an error.
155190 *
156191 * @param string $message
@@ -160,7 +195,7 @@
161196 $error = new ValidatorError(
162197 $message,
163198 $severity,
164 - $this->getElement(),
 199+ $this->element,
165200 (array)$tags
166201 );
167202
@@ -392,7 +427,7 @@
393428 $paramName
394429 ),
395430 'missing'
396 - );
 431+ );
397432 }
398433 else {
399434 // Set the default value (or default 'default value' if none is provided), and ensure the type is correct.
@@ -815,43 +850,117 @@
816851 * @return boolean
817852 */
818853 public function hasFatalError() {
819 - // TODO
820 - return false;
 854+ $has = false;
 855+
 856+ foreach ( $this->errors as $error ) {
 857+ if ( $error->severity >= ValidatorError::SEVERITY_CRITICAL ) {
 858+ $has = true;
 859+ break;
 860+ }
 861+ }
 862+
 863+ return $has;
821864 }
822 -
823 - /**
824 - * Adds a new criteria type and the validation function that should validate values of this type.
825 - * You can use this function to override existing criteria type handlers.
826 - *
827 - * @param string $criteriaName The name of the cirteria.
828 - * @param array $functionName The functions location. If it's a global function, only the name,
829 - * if it's in a class, first the class name, then the method name.
830 - */
831 - public static function addValidationFunction( $criteriaName, array $functionName ) {
832 - self::$mValidationFunctions[$criteriaName] = $functionName;
833 - }
834865
835866 /**
836 - * Adds a new list criteria type and the validation function that should validate values of this type.
837 - * You can use this function to override existing criteria type handlers.
838 - *
839 - * @param string $criteriaName The name of the list cirteria.
840 - * @param array $functionName The functions location. If it's a global function, only the name,
841 - * if it's in a class, first the class name, then the method name.
 867+ * Returns an error message for a criteria validation that failed.
 868+ *
 869+ * @since 0.4
 870+ *
 871+ * @param string $criteria
 872+ * @param string $paramName
 873+ * @param string $paramValue
 874+ * @param array $args
 875+ * @param boolean $isList
 876+ * @param array $invalidItems
 877+ *
 878+ * @return string
842879 */
843 - public static function addListValidationFunction( $criteriaName, array $functionName ) {
844 - self::$mListValidationFunctions[strtolower( $criteriaName )] = $functionName;
 880+ protected function getCriteriaErrorMessage( $criteria, $paramName, $paramValue, array $args = array(), $isList = false, array $invalidItems = array() ) {
 881+ global $wgLang, $egValidatorErrorLevel;
 882+
 883+ if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW && $this->validator->hasErrors() ) {
 884+ $rawErrors = $this->validator->getErrors();
 885+
 886+ $errorList = '<b>' . wfMsgExt( 'validator_error_parameters', 'parsemag', count( $rawErrors ) ) . '</b><br /><i>';
 887+
 888+ $errors = array();
 889+
 890+ foreach ( $rawErrors as $error ) {
 891+ $error['name'] = '<b>' . Sanitizer::escapeId( $error['name'] ) . '</b>';
 892+
 893+ if ( $error['type'] == 'unknown' ) {
 894+ $errors[] = wfMsgExt( 'validator_error_unknown_argument', array( 'parsemag' ), $error['name'] );
 895+ }
 896+ elseif ( $error['type'] == 'missing' ) {
 897+ $errors[] = wfMsgExt( 'validator_error_required_missing', array( 'parsemag' ), $error['name'] );
 898+ }
 899+ elseif ( array_key_exists( 'list', $error ) && $error['list'] ) {
 900+ switch( $error['type'] ) {
 901+ case 'not_empty' :
 902+ $msg = wfMsgExt( 'validator_list_error_empty_argument', array( 'parsemag' ), $error['name'] );
 903+ break;
 904+ case 'in_range' :
 905+ $msg = wfMsgExt( 'validator_list_error_invalid_range', array( 'parsemag' ), $error['name'], '<b>' . $error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' );
 906+ break;
 907+ case 'is_numeric' :
 908+ $msg = wfMsgExt( 'validator_list_error_must_be_number', array( 'parsemag' ), $error['name'] );
 909+ break;
 910+ case 'is_integer' :
 911+ $msg = wfMsgExt( 'validator_list_error_must_be_integer', array( 'parsemag' ), $error['name'] );
 912+ break;
 913+ case 'in_array' :
 914+ $itemsText = $wgLang->listToText( $error['args'] );
 915+ $msg = wfMsgExt( 'validator_error_accepts_only', array( 'parsemag' ), $error['name'], $itemsText, count( $error['args'] ) );
 916+ break;
 917+ case 'invalid' : default :
 918+ $msg = wfMsgExt( 'validator_list_error_invalid_argument', array( 'parsemag' ), $error['name'] );
 919+ break;
 920+ }
 921+
 922+ if ( array_key_exists( 'invalid-items', $error ) ) {
 923+ $omitted = array();
 924+ foreach ( $error['invalid-items'] as $item ) $omitted[] = Sanitizer::escapeId( $item );
 925+ $msg .= ' ' . wfMsgExt( 'validator_list_omitted', array( 'parsemag' ),
 926+ $wgLang->listToText( $omitted ), count( $omitted ) );
 927+ }
 928+
 929+ $errors[] = $msg;
 930+ }
 931+ else {
 932+ switch( $error['type'] ) {
 933+ case 'not_empty' :
 934+ $errors[] = wfMsgExt( 'validator_error_empty_argument', array( 'parsemag' ), $error['name'] );
 935+ break;
 936+ case 'in_range' :
 937+ $errors[] = wfMsgExt( 'validator_error_invalid_range', array( 'parsemag' ), $error['name'], '<b>' . $error['args'][0] . '</b>', '<b>' . $error['args'][1] . '</b>' );
 938+ break;
 939+ case 'is_numeric' :
 940+ $errors[] = wfMsgExt( 'validator_error_must_be_number', array( 'parsemag' ), $error['name'] );
 941+ break;
 942+ case 'is_integer' :
 943+ $errors[] = wfMsgExt( 'validator_error_must_be_integer', array( 'parsemag' ), $error['name'] );
 944+ break;
 945+ case 'in_array' :
 946+ $itemsText = $wgLang->listToText( $error['args'] );
 947+ $errors[] = wfMsgExt( 'validator_error_accepts_only', array( 'parsemag' ), $error['name'], $itemsText, count( $error['args'] ) );
 948+ break;
 949+ case 'invalid' : default :
 950+ $errors[] = wfMsgExt( 'validator_error_invalid_argument', array( 'parsemag' ), '<b>' . htmlspecialchars( $error['value'] ) . '</b>', $error['name'] );
 951+ break;
 952+ }
 953+ }
 954+ }
 955+
 956+ return $errorList . implode( $errors, '<br />' ) . '</i><br />';
 957+ }
 958+ elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN && $this->validator->hasErrors() ) {
 959+ return '<b>' . wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), count( $this->validator->getErrors() ) ) . '</b>';
 960+ }
 961+ else {
 962+ return '';
 963+ }
 964+
845965 }
846966
847 - /**
848 - * Adds a new output format and the formatting function that should validate values of this type.
849 - * You can use this function to override existing criteria type handlers.
850 - *
851 - * @param string $formatName The name of the format.
852 - * @param array $functionName The functions location. If it's a global function, only the name,
853 - * if it's in a class, first the class name, then the method name.
854 - */
855 - public static function addOutputFormat( $formatName, array $functionName ) {
856 - self::$mOutputFormats[strtolower( $formatName )] = $functionName;
857 - }
858967 }
\ No newline at end of file
Index: trunk/extensions/Validator/Validator.i18n.php
@@ -23,7 +23,7 @@
2424 // General errors
2525 'validator_error_unknown_argument' => '$1 is not a valid parameter.',
2626 'validator_error_required_missing' => 'The required parameter $1 is not provided.',
27 - 'validator-error-override-argument' => 'Tried to override parameter $1 ($2) with $3',
 27+ 'validator-error-override-argument' => 'Tried to override parameter $1 (value: $2) with value "$3"',
2828
2929 // Criteria errors for single values
3030 'validator_error_empty_argument' => 'Parameter $1 can not have an empty value.',
Index: trunk/extensions/Validator/Validator.php
@@ -24,7 +24,7 @@
2525 die( 'Not an entry point.' );
2626 }
2727
28 -define( 'Validator_VERSION', '0.4 alpha-2' );
 28+define( 'Validator_VERSION', '0.4 alpha-3' );
2929
3030 // Constants indicating the strictness of the parameter validation.
3131 define( 'Validator_ERRORS_NONE', 0 );
@@ -45,6 +45,12 @@
4646 'descriptionmsg' => 'validator-desc',
4747 );
4848
 49+// This function has been deprecated in 1.16, but needed for earlier versions.
 50+// It's present in 1.16 as a stub, but lets check if it exists in case it gets removed at some point.
 51+if ( function_exists( 'wfLoadExtensionMessages' ) ) {
 52+ wfLoadExtensionMessages( 'Validator' );
 53+}
 54+
4955 // Autoload the general classes.
5056 $incDir = dirname( __FILE__ ) . '/includes/';
5157 $wgAutoloadClasses['Validator'] = $incDir . 'Validator.php';

Status & tagging log