r74060 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74059‎ | r74060 | r74061 >
Date:09:14, 1 October 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.4 - work on error handling
Modified paths:
  • /trunk/extensions/Validator/Validator.i18n.php (modified) (history)
  • /trunk/extensions/Validator/Validator.php (modified) (history)
  • /trunk/extensions/Validator/Validator_Settings.php (modified) (history)
  • /trunk/extensions/Validator/includes/ParserHook.php (modified) (history)
  • /trunk/extensions/Validator/includes/ValidationError.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/ValidationError.php
@@ -12,12 +12,18 @@
1313 */
1414 class ValidationError {
1515
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
2121
 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+
2228 public $message;
2329 public $severity;
2430
@@ -65,4 +71,114 @@
6672 return $this->message;
6773 }
6874
 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+
69185 }
\ No newline at end of file
Index: trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php
@@ -25,7 +25,7 @@
2626 'low' => ValidationError::SEVERITY_LOW,
2727 'normal' => ValidationError::SEVERITY_NORMAL,
2828 'high' => ValidationError::SEVERITY_HIGH,
29 - 'critical' => ValidationError::SEVERITY_CRITICAL,
 29+ 'fatal' => ValidationError::SEVERITY_FATAL
3030 );
3131
3232 /**
Index: trunk/extensions/Validator/includes/ParserHook.php
@@ -97,7 +97,7 @@
9898 * @param minxed $input string or null
9999 * @param array $args
100100 * @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
102102 *
103103 * @return string
104104 */
@@ -160,26 +160,85 @@
161161
162162 if ( $fatalError === false ) {
163163 $output = $this->render( $this->validator->getParameterValues() );
 164+ $output = $this->renderErrors( $output );
164165 }
165166 else {
166 - $output = $this->renderError( $fatalError );
 167+ $output = $this->renderFatalError( $fatalError );
167168 }
168169
169170 return $output;
170171 }
171172
172173 /**
 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+ /**
173198 * Creates and returns the output when a fatal error prevent regular rendering.
174199 *
175200 * @since 0.4
176201 *
 202+ * @param ValidationError $error
 203+ *
177204 * @return string
178205 */
179 - protected function renderError( ValidationError $error ) {
 206+ protected function renderFatalError( ValidationError $error ) {
180207 return wfMsgExt( 'validator-error', 'parsemag', $error->getMessage() );
181208 }
182209
183210 /**
 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+ /**
184243 * Returns an array containing the parameter info.
185244 * Override in deriving classes to add parameter info.
186245 *
Index: trunk/extensions/Validator/includes/Validator.php
@@ -388,8 +388,10 @@
389389 * @return mixed false or ValidationError
390390 */
391391 public function hasFatalError() {
 392+ global $egErrorLevel;
 393+
392394 foreach ( $this->errors as $error ) {
393 - if ( $error->severity >= ValidationError::SEVERITY_CRITICAL ) {
 395+ if ( $error->isFatal() ) {
394396 return $error;
395397 }
396398 }
Index: trunk/extensions/Validator/Validator.i18n.php
@@ -17,11 +17,14 @@
1818 $messages['en'] = array(
1919 'validator-desc' => 'Provides generic parameter handling support for other extensions',
2020
 21+ 'validator-warning' => '<b>Warning</b>: $1',
 22+ 'validator-error' => '<b>Error</b>: $1',
 23+ 'validator-fatal-error' => '<b>Fatal error</b>: $1',
2124 'validator_error_parameters' => 'The following {{PLURAL:$1|error has|errors have}} been detected in your syntax:',
2225 '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}}.',
2327
2428 // General errors
25 - 'validator-error' => '<b>Error</b>: $1',
2629 'validator_error_unknown_argument' => '$1 is not a valid parameter.',
2730 'validator_error_required_missing' => 'The required parameter "$1" is not provided.',
2831 'validator-error-override-argument' => 'Tried to override parameter $1 (value: $2) with value "$3"',
Index: trunk/extensions/Validator/Validator.php
@@ -26,13 +26,6 @@
2727
2828 define( 'Validator_VERSION', '0.4 alpha-6' );
2929
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 -
3730 // Register the internationalization file.
3831 $wgExtensionMessagesFiles['Validator'] = dirname( __FILE__ ) . '/Validator.i18n.php';
3932
Index: trunk/extensions/Validator/Validator_Settings.php
@@ -21,14 +21,10 @@
2222 $wgHooks['ParserFirstCallInit'][] = 'ValidatorListErrors::staticInit';
2323 $wgHooks['LanguageGetMagic'][] = 'ValidatorListErrors::staticMagic';
2424
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,
3531 );
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r74061Follow up to r74060jeroendedauw09:23, 1 October 2010
r74069Follow-up r74060: Use wiki syntax instead of HTMLraymond13:53, 1 October 2010

Status & tagging log