http://www.mediawiki.org/wiki/Special:Code/MediaWiki/74059
Revision: 74059
Author: jeroendedauw
Date: 2010-10-01 07:01:47 +0000 (Fri, 01 Oct 2010)
Log Message:
-----------
Changes for 0.4 - work on error handling
Modified Paths:
--------------
trunk/extensions/Validator/Validator.i18n.php
trunk/extensions/Validator/includes/Parameter.php
trunk/extensions/Validator/includes/ParserHook.php
trunk/extensions/Validator/includes/ValidationError.php
trunk/extensions/Validator/includes/Validator.php
Modified: trunk/extensions/Validator/Validator.i18n.php
===================================================================
--- trunk/extensions/Validator/Validator.i18n.php 2010-10-01 07:01:22 UTC
(rev 74058)
+++ trunk/extensions/Validator/Validator.i18n.php 2010-10-01 07:01:47 UTC
(rev 74059)
@@ -21,8 +21,9 @@
'validator_warning_parameters' => 'There {{PLURAL:$1|is an error|are
errors}} in your syntax.',
// General errors
+ 'validator-error' => '<b>Error</b>: $1',
'validator_error_unknown_argument' => '$1 is not a valid parameter.',
- 'validator_error_required_missing' => 'The required parameter $1 is not
provided.',
+ 'validator_error_required_missing' => 'The required parameter "$1" is
not provided.',
'validator-error-override-argument' => 'Tried to override parameter $1
(value: $2) with value "$3"',
// Criteria errors for single values
Modified: trunk/extensions/Validator/includes/Parameter.php
===================================================================
--- trunk/extensions/Validator/includes/Parameter.php 2010-10-01 07:01:22 UTC
(rev 74058)
+++ trunk/extensions/Validator/includes/Parameter.php 2010-10-01 07:01:47 UTC
(rev 74059)
@@ -441,8 +441,8 @@
protected function doValidation() {
if ( $this->setCount == 0 ) {
if ( $this->isRequired() ) {
- // TODO: fatal error
- $success = false;
+ // This should not occur, so thorw an exception.
+ throw new Exception( 'Attempted to validate a
required parameter without first setting a value.' );
}
else {
$success = true;
Modified: trunk/extensions/Validator/includes/ParserHook.php
===================================================================
--- trunk/extensions/Validator/includes/ParserHook.php 2010-10-01 07:01:22 UTC
(rev 74058)
+++ trunk/extensions/Validator/includes/ParserHook.php 2010-10-01 07:01:47 UTC
(rev 74059)
@@ -156,18 +156,30 @@
$this->validator->validateParameters();
- if ( $this->validator->hasFatalError() ) {
- // TODO
- $output = 'Demo: fatal error';
+ $fatalError = $this->validator->hasFatalError();
+
+ if ( $fatalError === false ) {
+ $output = $this->render(
$this->validator->getParameterValues() );
}
else {
- $output = $this->render(
$this->validator->getParameterValues() );
+ $output = $this->renderError( $fatalError );
}
return $output;
}
/**
+ * Creates and returns the output when a fatal error prevent regular
rendering.
+ *
+ * @since 0.4
+ *
+ * @return string
+ */
+ protected function renderError( ValidationError $error ) {
+ return wfMsgExt( 'validator-error', 'parsemag',
$error->getMessage() );
+ }
+
+ /**
* Returns an array containing the parameter info.
* Override in deriving classes to add parameter info.
*
Modified: trunk/extensions/Validator/includes/ValidationError.php
===================================================================
--- trunk/extensions/Validator/includes/ValidationError.php 2010-10-01
07:01:22 UTC (rev 74058)
+++ trunk/extensions/Validator/includes/ValidationError.php 2010-10-01
07:01:47 UTC (rev 74059)
@@ -54,4 +54,15 @@
$this->tags = $tags;
}
+ /**
+ * Returns the error message describing the error.
+ *
+ * @since 0.4
+ *
+ * @return string
+ */
+ public function getMessage() {
+ return $this->message;
+ }
+
}
\ No newline at end of file
Modified: trunk/extensions/Validator/includes/Validator.php
===================================================================
--- trunk/extensions/Validator/includes/Validator.php 2010-10-01 07:01:22 UTC
(rev 74058)
+++ trunk/extensions/Validator/includes/Validator.php 2010-10-01 07:01:47 UTC
(rev 74059)
@@ -243,8 +243,14 @@
$setUservalue = $this->attemptToSetUserValue(
$parameter );
+ // If the parameter is required but not provided,
register a fatal error and stop processing.
if ( !$setUservalue && $parameter->isRequired() ) {
- // TODO: FATAL
+ $this->registerNewError(
+ wfMsgExt(
'validator_error_required_missing', 'parsemag', $paramName ),
+ array(),
+ ValidationError::SEVERITY_CRITICAL
+ );
+ break;
}
else {
$validationSucceeded = $parameter->validate();
@@ -375,22 +381,20 @@
}
/**
- * Returns wether there are any fatal errors. Fatal errors are either
missing or invalid required parameters,
- * or simply any sort of error when the validation level is equal to
(or bigger then) Validator_ERRORS_STRICT.
+ * Returns false when there are no fatal errors or an ValidationError
when one is found.
+ * Fatal errors are either missing or invalid required parameters, or
simply any sort of
+ * error when the validation level is equal to (or bigger then)
Validator_ERRORS_STRICT.
*
- * @return boolean
+ * @return mixed false or ValidationError
*/
public function hasFatalError() {
- $has = false;
-
foreach ( $this->errors as $error ) {
if ( $error->severity >=
ValidationError::SEVERITY_CRITICAL ) {
- $has = true;
- break;
+ return $error;
}
}
- return $has;
+ return false;
}
}
\ No newline at end of file
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs