http://www.mediawiki.org/wiki/Special:Code/MediaWiki/72194
Revision: 72194
Author: jeroendedauw
Date: 2010-09-02 12:45:10 +0000 (Thu, 02 Sep 2010)
Log Message:
-----------
Changes for 0.4 - adding more OOP way of dealing with errors
Modified Paths:
--------------
trunk/extensions/Validator/Validator.php
trunk/extensions/Validator/includes/ParserHook.php
trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php
Added Paths:
-----------
trunk/extensions/Validator/includes/Validator_Error.php
trunk/extensions/Validator/includes/Validator_ErrorHandler.php
Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php 2010-09-02 10:33:56 UTC (rev
72193)
+++ trunk/extensions/Validator/Validator.php 2010-09-02 12:45:10 UTC (rev
72194)
@@ -53,6 +53,8 @@
$wgAutoloadClasses['ValidationFormats'] = $incDir .
'ValidationFormats.php';
$wgAutoloadClasses['ValidationManager'] = $incDir .
'ValidationManager.php';
$wgAutoloadClasses['TopologicalSort'] = $incDir .
'TopologicalSort.php';
+$wgAutoloadClasses['ValidatorError'] = $incDir .
'Validator_Error.php';
+$wgAutoloadClasses['ValidatorErrorHandler'] = $incDir .
'Validator_ErrorHandler.php';
$wgAutoloadClasses['ValidatorListErrors'] = $incDir .
'parserHooks/Validator_ListErrors.php';
unset( $incDir );
Modified: trunk/extensions/Validator/includes/ParserHook.php
===================================================================
--- trunk/extensions/Validator/includes/ParserHook.php 2010-09-02 10:33:56 UTC
(rev 72193)
+++ trunk/extensions/Validator/includes/ParserHook.php 2010-09-02 12:45:10 UTC
(rev 72194)
@@ -105,7 +105,7 @@
/**
* Takes care of validation and rendering, and returns the output.
*
- * @since 04
+ * @since 0.4
*
* @param array $arguments
* @param boolean $parsed
@@ -143,6 +143,8 @@
* Handles any errors that occured. Messages that should be added the
the regular
* output are returned.
*
+ * @since 0.4
+ *
* @param ValidatorManager $manager
*
* @return string
Added: trunk/extensions/Validator/includes/Validator_Error.php
===================================================================
--- trunk/extensions/Validator/includes/Validator_Error.php
(rev 0)
+++ trunk/extensions/Validator/includes/Validator_Error.php 2010-09-02
12:45:10 UTC (rev 72194)
@@ -0,0 +1,45 @@
+<?php
+
+/**
+ * Error class.
+ *
+ * @since 0.4
+ *
+ * @file Validator_Error.php
+ * @ingroup Validator
+ *
+ * @author Jeroen De Dauw
+ */
+class ValidatorError {
+
+ const SEVERITY_MINOR = 0;
+ const SEVERITY_LOW = 1;
+ const SEVERITY_NORMAL = 2;
+ const SEVERITY_HIGH = 3;
+ const SEVERITY_CRITICAL = 4;
+
+ public $message;
+ public $severity;
+
+ /**
+ * Where the error occured.
+ *
+ * @since 0.4
+ *
+ * @var mixed: string or false
+ */
+ protected $element;
+
+ /**
+ * @since 0.4
+ *
+ * @param string $message
+ * @param integer $severity
+ */
+ public function __construct( $message, $severity =
ValidatorError::SEVERITY_NORMAL, $element = false ) {
+ $this->message = $message;
+ $this->severity = $severity;
+ $this->element = $element;
+ }
+
+}
\ No newline at end of file
Property changes on: trunk/extensions/Validator/includes/Validator_Error.php
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/extensions/Validator/includes/Validator_ErrorHandler.php
===================================================================
--- trunk/extensions/Validator/includes/Validator_ErrorHandler.php
(rev 0)
+++ trunk/extensions/Validator/includes/Validator_ErrorHandler.php
2010-09-02 12:45:10 UTC (rev 72194)
@@ -0,0 +1,104 @@
+<?php
+
+/**
+ * Static class for error handling.
+ *
+ * @since 0.4
+ *
+ * @file Validator_ErrorHandler.php
+ * @ingroup Validator
+ *
+ * @author Jeroen De Dauw
+ */
+final class ValidatorErrorHandler {
+
+ protected static $errors;
+
+ /**
+ * Adds a single ValidatorError.
+ *
+ * @since 0.4
+ *
+ * @param string $errorMessage
+ * @param integer $severity
+ */
+ public static function addError( ValidatorError $error ) {
+ self::$errors[$error->element ? $error->element : 'unknown'][]
= $error;
+ }
+
+ /**
+ * Adds a list of ValidatorError.
+ *
+ * @since 0.4
+ *
+ * @param array $errors
+ */
+ public static function addErrors( array $errors ) {
+ foreach ( $errors as $error ) {
+ self::addError( $error );
+ }
+ }
+
+ /**
+ * Returns a list of errors in wikitext.
+ *
+ * @since 0.4
+ *
+ * @param integer $minSeverity
+ *
+ * @return string
+ */
+ public static function getErrorList( $minSeverity =
ValidatorError::SEVERITY_MINOR ) {
+ $elementHtml = array();
+
+ $elements = array_keys( self::$errors );
+ natcasesort( $elements );
+
+ foreach ( $elements as $element ) {
+ $elementErrors = self::getErrorListForElement(
$element, $minSeverity );
+
+ if ( $elementErrors ) {
+ $elementHtml[] = $elementErrors;
+ }
+ }
+
+ return implode( "\n", $elementHtml );
+ }
+
+ /**
+ * Returns wikitext listing the errors for a single element.
+ *
+ * @since 0.4
+ *
+ * @param string $element
+ * @param integer $minSeverity
+ *
+ * @return string
+ */
+ public static function getErrorListForElement( $element, $minSeverity =
ValidatorError::SEVERITY_MINOR ) {
+ $errors = array();
+
+ if ( array_key_exists( $element, self::$errors ) ) {
+ foreach ( self::$errors[$element] as $error ) {
+ if ( $error->severity >= $minSeverity ) {
+ $errors[] = $error;
+ }
+ }
+ }
+
+ if ( count( $errors ) > 0 ) {
+ $lines = array();
+
+ foreach ( $errors as $error ) {
+ // TODO: switch on severity
+ $lines[] = "* $error->message";
+ }
+
+ return "== $element ==\n\n" . implode( "\n", $lines );
+ }
+ else {
+ return false;
+ }
+ }
+
+}
\ No newline at end of file
Property changes on:
trunk/extensions/Validator/includes/Validator_ErrorHandler.php
___________________________________________________________________
Added: svn:eol-style
+ native
Modified:
trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php
===================================================================
--- trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php
2010-09-02 10:33:56 UTC (rev 72193)
+++ trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php
2010-09-02 12:45:10 UTC (rev 72194)
@@ -82,6 +82,8 @@
public function render( array $parameters ) {
$output = ''; // TODO
+
+
return $output;
}
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs