r74062 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74061‎ | r74062 | r74063 >
Date:10:19, 1 October 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.4 - work on error handling
Modified paths:
  • /trunk/extensions/Validator/includes/ItemParameterCriterion.php (modified) (history)
  • /trunk/extensions/Validator/includes/Parameter.php (modified) (history)
  • /trunk/extensions/Validator/includes/Validator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/includes/ItemParameterCriterion.php
@@ -87,15 +87,32 @@
8888 }
8989
9090 if ( $result->hasInvalidItems() ) {
 91+ $allInvalid = count( $result->getInvalidItems() ) == count( $parameter->value );
 92+
 93+ // If the parameter is required and all items are invalid, it's fatal.
 94+ // Else it's high for required, and normal for non-required parameters.
 95+ if ( $parameter->isRequired() ) {
 96+ $severity = $allInvalid ? ValidationError::SEVERITY_FATAL : ValidationError::SEVERITY_HIGH;
 97+ }
 98+ else {
 99+ $severity = $allInvalid ? ValidationError::SEVERITY_NORMAL : ValidationError::SEVERITY_LOW;
 100+ }
 101+
91102 $result->addError(
92 - new ValidationError( $this->getListErrorMessage( $parameter, $result->getInvalidItems() ) )
 103+ new ValidationError(
 104+ $this->getListErrorMessage( $parameter, $result->getInvalidItems() ),
 105+ $severity
 106+ )
93107 );
94108 }
95109 }
96110 else {
97111 if ( !$this->doValidation( $parameter->value ) ) {
98112 $result->addError(
99 - new ValidationError( $this->getItemErrorMessage( $parameter ) )
 113+ new ValidationError(
 114+ $this->getItemErrorMessage( $parameter ),
 115+ $parameter->isRequired() ? ValidationError::SEVERITY_FATAL : ValidationError::SEVERITY_NORMAL
 116+ )
100117 );
101118 }
102119 }
Index: trunk/extensions/Validator/includes/Parameter.php
@@ -433,10 +433,9 @@
434434
435435 /**
436436 * Validates the parameter value.
 437+ * Also sets the value to the default when it's not set or invalid, assuming there is a default.
437438 *
438439 * @since 0.4
439 - *
440 - * @return boolean Indicates if there was any validation error
441440 */
442441 protected function doValidation() {
443442 if ( $this->setCount == 0 ) {
@@ -445,42 +444,35 @@
446445 throw new Exception( 'Attempted to validate a required parameter without first setting a value.' );
447446 }
448447 else {
449 - $success = true;
450448 $this->setToDefault();
451449 }
452450 }
453451 else {
454 - $success = $this->validateCriteria();
 452+ $this->validateCriteria();
 453+
 454+ if ( count( $this->errors ) > 0 && !$this->hasFatalError() ) {
 455+ $this->setToDefault();
 456+ }
455457 }
456 -
457 - return $success;
458458 }
459459
460460 /**
461461 * Validates the provided value against all criteria.
462462 *
463463 * @since 0.4
464 - *
465 - * @return boolean Indicates if there was any validation error
466464 */
467465 protected function validateCriteria() {
468 - $success = true;
469 -
470466 foreach ( $this->getCriteria() as $criterion ) {
471467 $validationResult = $criterion->validate( $this );
472468
473469 if ( !$validationResult->isValid() ) {
474 - $success = false;
475 -
476470 $this->handleValidationError( $validationResult );
477471
478 - if ( !self::$accumulateParameterErrors ) {
479 - break;
 472+ if ( !self::$accumulateParameterErrors || $this->hasFatalError() ) {
 473+ break;
480474 }
481475 }
482476 }
483 -
484 - return $success;
485477 }
486478
487479 /**
@@ -753,4 +745,19 @@
754746 $this->applyManipulationsToDefault = $doOrDoNot;
755747 }
756748
 749+ /**
 750+ * Returns false when there are no fatal errors or an ValidationError when one is found.
 751+ *
 752+ * @return mixed false or ValidationError
 753+ */
 754+ public function hasFatalError() {
 755+ foreach ( $this->errors as $error ) {
 756+ if ( $error->isFatal() ) {
 757+ return true;
 758+ }
 759+ }
 760+
 761+ return false;
 762+ }
 763+
757764 }
\ No newline at end of file
Index: trunk/extensions/Validator/includes/Validator.php
@@ -250,17 +250,20 @@
251251 $this->registerNewError(
252252 wfMsgExt( 'validator_error_required_missing', 'parsemag', $paramName ),
253253 array(),
254 - ValidationError::SEVERITY_CRITICAL
 254+ ValidationError::SEVERITY_FATAL
255255 );
256256 break;
257257 }
258258 else {
259 - $validationSucceeded = $parameter->validate();
 259+ $parameter->validate();
260260
261 - if ( !$validationSucceeded ) {
262 - foreach ( $parameter->getErrors() as $error ) {
263 - $this->registerError( $error );
264 - }
 261+ foreach ( $parameter->getErrors() as $error ) {
 262+ $this->registerError( $error );
 263+ }
 264+
 265+ if ( $parameter->hasFatalError() ) {
 266+ // If there was a fatal error, and the parameter is required, stop processing.
 267+ break;
265268 }
266269
267270 $initialSet = $this->parameters;
@@ -293,7 +296,7 @@
294297 if ( !array_key_exists( $paramName, $initialParamSet ) ) {
295298 $this->paramsTohandle[] = $paramName;
296299 }
297 - }
 300+ }
298301 }
299302
300303 $dependencies = array();
@@ -390,8 +393,6 @@
391394 * @return mixed false or ValidationError
392395 */
393396 public function hasFatalError() {
394 - global $egErrorLevel;
395 -
396397 foreach ( $this->errors as $error ) {
397398 if ( $error->isFatal() ) {
398399 return $error;

Status & tagging log