Index: trunk/extensions/Validator/includes/ValidationError.php |
— | — | @@ -12,12 +12,18 @@ |
13 | 13 | */ |
14 | 14 | class ValidationError { |
15 | 15 | |
16 | | - const SEVERITY_MINOR = 0; |
17 | | - const SEVERITY_LOW = 1; |
18 | | - const SEVERITY_NORMAL = 2; |
19 | | - const SEVERITY_HIGH = 3; |
20 | | - const SEVERITY_CRITICAL = 4; |
| 16 | + const SEVERITY_MINOR = 0; // Minor error. ie a decapriation notice |
| 17 | + const SEVERITY_LOW = 1; // Lower-then-normal severity. ie an unknown parameter |
| 18 | + const SEVERITY_NORMAL = 2; // Normal severiry. ie an invalid value provided |
| 19 | + const SEVERITY_HIGH = 3; // Higher-then-normal severity. ie an invalid value for a significant parameter |
| 20 | + const SEVERITY_FATAL = 4; // Fatal error. Either a missing or an invalid required parameter |
21 | 21 | |
| 22 | + const ACTION_IGNORE = 0; // Ignore the error |
| 23 | + const ACTION_LOG = 1; // Log the error |
| 24 | + const ACTION_WARN = 2; // Warn that there is an error |
| 25 | + const ACTION_SHOW = 3; // Show the error |
| 26 | + const ACTION_DEMAND = 4; // Show the error and don't render output |
| 27 | + |
22 | 28 | public $message; |
23 | 29 | public $severity; |
24 | 30 | |
— | — | @@ -65,4 +71,114 @@ |
66 | 72 | return $this->message; |
67 | 73 | } |
68 | 74 | |
| 75 | + /** |
| 76 | + * Returns the severity of the error. |
| 77 | + * |
| 78 | + * @since 0.4 |
| 79 | + * |
| 80 | + * @return integer Element of the ValidationError::SEVERITY_ enum |
| 81 | + */ |
| 82 | + public function getSeverity() { |
| 83 | + return $this->severity; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns if the severity is equal to or bigger then the provided one. |
| 88 | + * |
| 89 | + * @since 0.4 |
| 90 | + * |
| 91 | + * @return boolean |
| 92 | + */ |
| 93 | + public function hasSeverity( $severity ) { |
| 94 | + return $this->severity >= $severity; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns the action associated with the errors severity. |
| 99 | + * |
| 100 | + * @since 0.4 |
| 101 | + * |
| 102 | + * @return integer Element of the ValidationError::ACTION_ enum |
| 103 | + */ |
| 104 | + public function getAction() { |
| 105 | + global $egErrorActions; |
| 106 | + |
| 107 | + if ( $this->severity === self::SEVERITY_FATAL ) { |
| 108 | + // This action should not be configurable, as lowering it would break in the Validator class. |
| 109 | + return self::ACTION_DEMAND; |
| 110 | + } |
| 111 | + else if ( array_key_exists( $this->severity, $egErrorActions ) ) { |
| 112 | + return $egErrorActions[$this->severity]; |
| 113 | + } |
| 114 | + else { |
| 115 | + throw new Exception( "No action associated with error severity '$this->severity'" ); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns if the action associated with the severity is equal to or bigger then the provided one. |
| 121 | + * |
| 122 | + * @since 0.4 |
| 123 | + * |
| 124 | + * @return boolean |
| 125 | + */ |
| 126 | + public function hasAction( $action ) { |
| 127 | + return $this->getAction() >= $action; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Returns if the error is fatal. |
| 132 | + * |
| 133 | + * @since 0.4 |
| 134 | + * |
| 135 | + * @return boolean |
| 136 | + */ |
| 137 | + public function isFatal() { |
| 138 | + return $this->hasSeverity( self::SEVERITY_FATAL ); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Returns if the error should be logged. |
| 143 | + * |
| 144 | + * @since 0.4 |
| 145 | + * |
| 146 | + * @return boolean |
| 147 | + */ |
| 148 | + public function shouldLog() { |
| 149 | + return $this->hasAction( self::ACTION_LOG ); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns if there should be a warning that errors are present. |
| 154 | + * |
| 155 | + * @since 0.4 |
| 156 | + * |
| 157 | + * @return boolean |
| 158 | + */ |
| 159 | + public function shouldWarn() { |
| 160 | + return $this->hasAction( self::ACTION_WARN ); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Returns if the error message should be shown. |
| 165 | + * |
| 166 | + * @since 0.4 |
| 167 | + * |
| 168 | + * @return boolean |
| 169 | + */ |
| 170 | + public function shouldShow() { |
| 171 | + return $this->hasAction( self::ACTION_SHOW ); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Returns if the error message should be shown, and the output not be rendered. |
| 176 | + * |
| 177 | + * @since 0.4 |
| 178 | + * |
| 179 | + * @return boolean |
| 180 | + */ |
| 181 | + public function shouldDemand() { |
| 182 | + return $this->hasAction( self::ACTION_DEMAND ); |
| 183 | + } |
| 184 | + |
69 | 185 | } |
\ No newline at end of file |
Index: trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php |
— | — | @@ -25,7 +25,7 @@ |
26 | 26 | 'low' => ValidationError::SEVERITY_LOW, |
27 | 27 | 'normal' => ValidationError::SEVERITY_NORMAL, |
28 | 28 | 'high' => ValidationError::SEVERITY_HIGH, |
29 | | - 'critical' => ValidationError::SEVERITY_CRITICAL, |
| 29 | + 'fatal' => ValidationError::SEVERITY_FATAL |
30 | 30 | ); |
31 | 31 | |
32 | 32 | /** |
Index: trunk/extensions/Validator/includes/ParserHook.php |
— | — | @@ -97,7 +97,7 @@ |
98 | 98 | * @param minxed $input string or null |
99 | 99 | * @param array $args |
100 | 100 | * @param Parser $parser |
101 | | - * @param PPFrame $frame Available from 1.16 - commented for bc for now |
| 101 | + * @param PPFrame $frame Available from 1.16 - commented out for bc for now |
102 | 102 | * |
103 | 103 | * @return string |
104 | 104 | */ |
— | — | @@ -160,26 +160,85 @@ |
161 | 161 | |
162 | 162 | if ( $fatalError === false ) { |
163 | 163 | $output = $this->render( $this->validator->getParameterValues() ); |
| 164 | + $output = $this->renderErrors( $output ); |
164 | 165 | } |
165 | 166 | else { |
166 | | - $output = $this->renderError( $fatalError ); |
| 167 | + $output = $this->renderFatalError( $fatalError ); |
167 | 168 | } |
168 | 169 | |
169 | 170 | return $output; |
170 | 171 | } |
171 | 172 | |
172 | 173 | /** |
| 174 | + * Returns the ValidationError objects for the errors and warnings that should be displayed. |
| 175 | + * |
| 176 | + * @since 0.4 |
| 177 | + * |
| 178 | + * @return array of array of ValidationError |
| 179 | + */ |
| 180 | + protected function getErrorsToDisplay() { |
| 181 | + $errors = array(); |
| 182 | + $warnings = array(); |
| 183 | + |
| 184 | + foreach ( $this->validator->getErrors() as $error ) { |
| 185 | + // Check if the severity of the error is high enough to display it. |
| 186 | + if ( $error->shouldShow() ) { |
| 187 | + $errors[] = $error; |
| 188 | + } |
| 189 | + else if ( $error->shouldWarn() ) { |
| 190 | + $warnings[] = $error; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + return array( 'errors' => $errors, 'warnings' => $warnings ); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
173 | 198 | * Creates and returns the output when a fatal error prevent regular rendering. |
174 | 199 | * |
175 | 200 | * @since 0.4 |
176 | 201 | * |
| 202 | + * @param ValidationError $error |
| 203 | + * |
177 | 204 | * @return string |
178 | 205 | */ |
179 | | - protected function renderError( ValidationError $error ) { |
| 206 | + protected function renderFatalError( ValidationError $error ) { |
180 | 207 | return wfMsgExt( 'validator-error', 'parsemag', $error->getMessage() ); |
181 | 208 | } |
182 | 209 | |
183 | 210 | /** |
| 211 | + * @since 0.4 |
| 212 | + * |
| 213 | + * @param string $output |
| 214 | + * |
| 215 | + * @return string |
| 216 | + */ |
| 217 | + protected function renderErrors( $output ) { |
| 218 | + $displayStuff = $this->getErrorsToDisplay(); |
| 219 | + |
| 220 | + if ( count( $displayStuff['errors'] ) > 0 ) { |
| 221 | + $output .= wfMsgExt( 'validator_error_parameters', 'parsemag', count( $displayStuff['errors'] ) ); |
| 222 | + |
| 223 | + foreach( $displayStuff['errors'] as $error ) { |
| 224 | + $output .= '<br />* ' . $error->getMessage(); |
| 225 | + } |
| 226 | + |
| 227 | + if ( count( $displayStuff['warnings'] ) > 0 ) { |
| 228 | + $output .= '<br />* ' . wfMsgExt( 'validator-warning-adittional-errors', 'parsemag', count( $displayStuff['warnings'] ) ); |
| 229 | + } |
| 230 | + } |
| 231 | + else if ( count( $displayStuff['warnings'] ) > 0 ) { |
| 232 | + $output .= wfMsgExt( |
| 233 | + 'validator-warning', |
| 234 | + 'parsemag', |
| 235 | + wfMsgExt( 'validator_warning_parameters', 'parsemag', count( $displayStuff['warnings'] ) ) |
| 236 | + ); |
| 237 | + } |
| 238 | + |
| 239 | + return $output; |
| 240 | + } |
| 241 | + |
| 242 | + /** |
184 | 243 | * Returns an array containing the parameter info. |
185 | 244 | * Override in deriving classes to add parameter info. |
186 | 245 | * |
Index: trunk/extensions/Validator/includes/Validator.php |
— | — | @@ -388,8 +388,10 @@ |
389 | 389 | * @return mixed false or ValidationError |
390 | 390 | */ |
391 | 391 | public function hasFatalError() { |
| 392 | + global $egErrorLevel; |
| 393 | + |
392 | 394 | foreach ( $this->errors as $error ) { |
393 | | - if ( $error->severity >= ValidationError::SEVERITY_CRITICAL ) { |
| 395 | + if ( $error->isFatal() ) { |
394 | 396 | return $error; |
395 | 397 | } |
396 | 398 | } |
Index: trunk/extensions/Validator/Validator.i18n.php |
— | — | @@ -17,11 +17,14 @@ |
18 | 18 | $messages['en'] = array( |
19 | 19 | 'validator-desc' => 'Provides generic parameter handling support for other extensions', |
20 | 20 | |
| 21 | + 'validator-warning' => '<b>Warning</b>: $1', |
| 22 | + 'validator-error' => '<b>Error</b>: $1', |
| 23 | + 'validator-fatal-error' => '<b>Fatal error</b>: $1', |
21 | 24 | 'validator_error_parameters' => 'The following {{PLURAL:$1|error has|errors have}} been detected in your syntax:', |
22 | 25 | 'validator_warning_parameters' => 'There {{PLURAL:$1|is an error|are errors}} in your syntax.', |
| 26 | + 'validator-warning-adittional-errors' => '... and {{PLURAL:$1|one more issue|multiple more issues}}.', |
23 | 27 | |
24 | 28 | // General errors |
25 | | - 'validator-error' => '<b>Error</b>: $1', |
26 | 29 | 'validator_error_unknown_argument' => '$1 is not a valid parameter.', |
27 | 30 | 'validator_error_required_missing' => 'The required parameter "$1" is not provided.', |
28 | 31 | 'validator-error-override-argument' => 'Tried to override parameter $1 (value: $2) with value "$3"', |
Index: trunk/extensions/Validator/Validator.php |
— | — | @@ -26,13 +26,6 @@ |
27 | 27 | |
28 | 28 | define( 'Validator_VERSION', '0.4 alpha-6' ); |
29 | 29 | |
30 | | -// Constants indicating the strictness of the parameter validation. |
31 | | -define( 'Validator_ERRORS_NONE', 0 ); |
32 | | -define( 'Validator_ERRORS_LOG', 1 ); |
33 | | -define( 'Validator_ERRORS_WARN', 2 ); |
34 | | -define( 'Validator_ERRORS_SHOW', 3 ); |
35 | | -define( 'Validator_ERRORS_STRICT', 4 ); |
36 | | - |
37 | 30 | // Register the internationalization file. |
38 | 31 | $wgExtensionMessagesFiles['Validator'] = dirname( __FILE__ ) . '/Validator.i18n.php'; |
39 | 32 | |
Index: trunk/extensions/Validator/Validator_Settings.php |
— | — | @@ -21,14 +21,10 @@ |
22 | 22 | $wgHooks['ParserFirstCallInit'][] = 'ValidatorListErrors::staticInit'; |
23 | 23 | $wgHooks['LanguageGetMagic'][] = 'ValidatorListErrors::staticMagic'; |
24 | 24 | |
25 | | -# Validator_ERRORS_NONE : Validator will not show any errors, and make the best of the input it got. |
26 | | -# Validator_ERRORS_WARN : Validator will make the best of the input it got, but will show a warning that there are errors. |
27 | | -# Validator_ERRORS_SHOW : Validator will make the best of the input it got, but will show a list of all errors. |
28 | | -# Validator_ERRORS_STRICT : Validator will only show regular output when there are no errors, if there are, a list of them will be shown. |
29 | | -$egErrorLevel = array( |
30 | | - ValidationError::SEVERITY_MINOR => Validator_ERRORS_LOG, |
31 | | - ValidationError::SEVERITY_LOW => Validator_ERRORS_LOG, |
32 | | - ValidationError::SEVERITY_NORMAL => Validator_ERRORS_WARN, |
33 | | - ValidationError::SEVERITY_HIGH => Validator_ERRORS_SHOW, |
34 | | - ValidationError::SEVERITY_CRITICAL => Validator_ERRORS_STRICT, |
| 25 | +// TODO: document |
| 26 | +$egErrorActions = array( |
| 27 | + ValidationError::SEVERITY_MINOR => ValidationError::ACTION_LOG, |
| 28 | + ValidationError::SEVERITY_LOW => ValidationError::ACTION_WARN, |
| 29 | + ValidationError::SEVERITY_NORMAL => ValidationError::ACTION_SHOW, |
| 30 | + ValidationError::SEVERITY_HIGH => ValidationError::ACTION_DEMAND, |
35 | 31 | ); |
\ No newline at end of file |