Index: trunk/extensions/Validator/Validator.class.php |
— | — | @@ -27,8 +27,6 @@ |
28 | 28 | * TODO: break on fatal errors, such as missing required parameters that are dependencies |
29 | 29 | * |
30 | 30 | * TODO: correct invalid parameters in the main loop, as to have correct dependency handling |
31 | | - * |
32 | | - * FIXME: lists are broken |
33 | 31 | */ |
34 | 32 | final class Validator { |
35 | 33 | |
— | — | @@ -540,7 +538,11 @@ |
541 | 539 | } |
542 | 540 | |
543 | 541 | /** |
544 | | - * Calls the formatting function for the provided output format with the provided value. |
| 542 | + * Calls the formatting function for the provided output format with these parameters: |
| 543 | + * - parameter value: ByRef for easy manipulation. |
| 544 | + * - parameter name: For lookups in the param info array. |
| 545 | + * - parameter array: All data about the parameters gathered so far (this includes dependencies!). |
| 546 | + * - output type info: Type info as provided by the parameter definition. This can be zero or more parameters. |
545 | 547 | * |
546 | 548 | * @param string $name |
547 | 549 | * @param array $typeInfo |
— | — | @@ -555,13 +557,6 @@ |
556 | 558 | } |
557 | 559 | |
558 | 560 | if ( array_key_exists( $outputType, self::$mOutputFormats ) ) { |
559 | | - /** |
560 | | - * Call the formatting function with these parameters: |
561 | | - * - parameter value: ByRef for easy manipulation. |
562 | | - * - parameter name: For lookups in the param info array. |
563 | | - * - parameter array: All data about the parameters gathered so far (this includes dependencies!). |
564 | | - * - output type info: Type info as provided by the parameter definition. This can be zero or more parameters. |
565 | | - */ |
566 | 561 | $parameters = array( &$this->mParameters[$name]['formatted-value'], $name, $this->mParameters ); |
567 | 562 | $parameters = array_merge( $parameters, $typeInfo ); |
568 | 563 | call_user_func_array( self::$mOutputFormats[$outputType], $parameters ); |
— | — | @@ -574,17 +569,24 @@ |
575 | 570 | /** |
576 | 571 | * Returns the valid parameters. |
577 | 572 | * |
| 573 | + * @param boolean $includeMetaData |
| 574 | + * |
578 | 575 | * @return array |
579 | 576 | */ |
580 | | - public function getValidParams() { |
581 | | - $validParams = array(); |
582 | | - |
583 | | - foreach( $this->mValidParams as $name ) { |
584 | | - $key = array_key_exists( 'formatted-value', $this->mParameters[$name] ) ? 'formatted-value' : 'value'; |
585 | | - $validParams[$name] = $this->mParameters[$name][$key]; |
| 577 | + public function getValidParams( $includeMetaData ) { |
| 578 | + if ( $includeMetaData ) { |
| 579 | + return $this->mValidParams; |
586 | 580 | } |
587 | | - |
588 | | - return $validParams; |
| 581 | + else { |
| 582 | + $validParams = array(); |
| 583 | + |
| 584 | + foreach( $this->mValidParams as $name ) { |
| 585 | + $key = array_key_exists( 'formatted-value', $this->mParameters[$name] ) ? 'formatted-value' : 'value'; |
| 586 | + $validParams[$name] = $this->mParameters[$name][$key]; |
| 587 | + } |
| 588 | + |
| 589 | + return $validParams; |
| 590 | + } |
589 | 591 | } |
590 | 592 | |
591 | 593 | /** |
— | — | @@ -610,7 +612,36 @@ |
611 | 613 | public function getErrors() { |
612 | 614 | return $this->mErrors; |
613 | 615 | } |
| 616 | + |
| 617 | + /** |
| 618 | + * @return boolean |
| 619 | + */ |
| 620 | + public function hasErrors() { |
| 621 | + return count( $this->mErrors ) > 0; |
| 622 | + } |
| 623 | + |
| 624 | + /** |
| 625 | + * Returns wether there are any fatal errors. Fatal errors are either missing or invalid required parameters, |
| 626 | + * or simply any sort of error when the validation level is equal to (or bigger then) Validator_ERRORS_STRICT. |
| 627 | + * |
| 628 | + * @return boolean |
| 629 | + */ |
| 630 | + public function hasFatalError() { |
| 631 | + global $egValidatorErrorLevel; |
| 632 | + $has = $this->hasErrors() && $egValidatorErrorLevel >= Validator_ERRORS_STRICT; |
| 633 | + |
| 634 | + if ( !$has ) { |
| 635 | + foreach ( $this->mErrors as $error ) { |
| 636 | + if ( $error['type'] == 'missing' ) { |
| 637 | + $has = true; |
| 638 | + break; |
| 639 | + } |
| 640 | + } |
| 641 | + } |
614 | 642 | |
| 643 | + return $has; |
| 644 | + } |
| 645 | + |
615 | 646 | /** |
616 | 647 | * Adds a new criteria type and the validation function that should validate values of this type. |
617 | 648 | * You can use this function to override existing criteria type handlers. |
Index: trunk/extensions/Validator/Validator_Manager.php |
— | — | @@ -26,8 +26,8 @@ |
27 | 27 | */ |
28 | 28 | final class ValidatorManager { |
29 | 29 | |
30 | | - private $errors = array(); |
31 | | - |
| 30 | + private $validator; |
| 31 | + |
32 | 32 | /** |
33 | 33 | * Validates the provided parameters, and corrects them depending on the error level. |
34 | 34 | * |
— | — | @@ -40,44 +40,27 @@ |
41 | 41 | public function manageParameters( array $rawParameters, array $parameterInfo, array $defaultParams = array() ) { |
42 | 42 | global $egValidatorErrorLevel; |
43 | 43 | |
44 | | - $validator = new Validator(); |
| 44 | + $this->validator = new Validator(); |
45 | 45 | |
46 | | - $validator->parseAndSetParams( $rawParameters, $parameterInfo, $defaultParams ); |
47 | | - $hasNoErrors = $validator->validateAndFormatParameters(); |
48 | | - |
49 | | - $hasFatalError = $hasNoErrors ? false : $this->hasFatalError(); |
50 | | - |
51 | | - if ( !$hasNoErrors ) { |
52 | | - if ( $egValidatorErrorLevel < Validator_ERRORS_STRICT ) $validator->correctInvalidParams(); |
53 | | - if ( $egValidatorErrorLevel >= Validator_ERRORS_WARN ) $this->errors = $validator->getErrors(); |
54 | | - } |
| 46 | + $this->validator->parseAndSetParams( $rawParameters, $parameterInfo, $defaultParams ); |
| 47 | + $this->validator->validateAndFormatParameters(); |
55 | 48 | |
56 | | - return !$hasFatalError ? $validator->getValidParams() : false; |
| 49 | + if ( $this->validator->hasErrors() && $egValidatorErrorLevel < Validator_ERRORS_STRICT ) { |
| 50 | + $this->validator->correctInvalidParams(); |
| 51 | + } |
| 52 | + |
| 53 | + return !$this->validator->hasFatalError(); |
57 | 54 | } |
58 | 55 | |
59 | 56 | /** |
60 | | - * Returns wether there are any fatal errors. Fatal errors are either missing or invalid required parameters, |
61 | | - * or simply any sort of error when the validation level is equal to (or bigger then) Validator_ERRORS_STRICT. |
| 57 | + * Returns an array with the valid parameters. |
62 | 58 | * |
63 | | - * Note: This function assumes it will only get called when there are errors. It will always return true |
64 | | - * when $egValidatorErrorLevel is Validator_ERRORS_STRICT or above. |
| 59 | + * @param boolean $includeMetaData |
65 | 60 | * |
66 | | - * @return boolean |
| 61 | + * @return array |
67 | 62 | */ |
68 | | - private function hasFatalError() { |
69 | | - global $egValidatorErrorLevel; |
70 | | - $has = $egValidatorErrorLevel >= Validator_ERRORS_STRICT; |
71 | | - |
72 | | - if ( !$has ) { |
73 | | - foreach ( $this->errors as $error ) { |
74 | | - if ( $error['type'] == 'missing' ) { |
75 | | - $has = true; |
76 | | - break; |
77 | | - } |
78 | | - } |
79 | | - } |
80 | | - |
81 | | - return $has; |
| 63 | + public function getParameters( $includeMetaData = true ) { |
| 64 | + return $this->validator->getValidParams( $includeMetaData ); |
82 | 65 | } |
83 | 66 | |
84 | 67 | /** |
— | — | @@ -87,14 +70,15 @@ |
88 | 71 | */ |
89 | 72 | public function getErrorList() { |
90 | 73 | global $wgLang, $egValidatorErrorLevel; |
91 | | - $errorCount = count( $this->errors ) ; |
92 | 74 | |
93 | | - if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW && $errorCount > 0 ) { |
94 | | - $errorList = '<b>' . wfMsgExt( 'validator_error_parameters', 'parsemag', $errorCount ) . '</b><br /><i>'; |
| 75 | + if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW && $this->validator->hasErrors() ) { |
| 76 | + $rawErrors = $this->validator->getErrors(); |
| 77 | + |
| 78 | + $errorList = '<b>' . wfMsgExt( 'validator_error_parameters', 'parsemag', count( $rawErrors ) ) . '</b><br /><i>'; |
95 | 79 | |
96 | 80 | $errors = array(); |
97 | 81 | |
98 | | - foreach ( $this->errors as $error ) { |
| 82 | + foreach ( $rawErrors as $error ) { |
99 | 83 | $error['name'] = '<b>' . Sanitizer::escapeId( $error['name'] ) . '</b>'; |
100 | 84 | |
101 | 85 | if ( $error['type'] == 'unknown' ) { |
— | — | @@ -162,8 +146,8 @@ |
163 | 147 | |
164 | 148 | return $errorList . implode( $errors, '<br />' ) . '</i><br />'; |
165 | 149 | } |
166 | | - elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN && $errorCount > 0 ) { |
167 | | - return '<b>' . wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), $errorCount ) . '</b>'; |
| 150 | + elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN && $this->validator->hasErrors() ) { |
| 151 | + return '<b>' . wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), count( $this->validator->getErrors() ) ) . '</b>'; |
168 | 152 | } |
169 | 153 | else { |
170 | 154 | return ''; |