r74505 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74504‎ | r74505 | r74506 >
Date:12:11, 8 October 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.4 - improving error listing in the listerrors parser hook
Modified paths:
  • /trunk/extensions/Validator/Validator.i18n.php (modified) (history)
  • /trunk/extensions/Validator/Validator_Settings.php (modified) (history)
  • /trunk/extensions/Validator/includes/ParserHook.php (modified) (history)
  • /trunk/extensions/Validator/includes/ValidationErrorHandler.php (modified) (history)
  • /trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/includes/ValidationErrorHandler.php
@@ -45,70 +45,14 @@
4646 }
4747
4848 /**
49 - * Returns a list of errors in wikitext.
 49+ * Returns a list of all registered errors.
5050 *
5151 * @since 0.4
5252 *
53 - * @param integer $minSeverity
54 - *
55 - * @return string
 53+ * @return array of ValidationError
5654 */
57 - public static function getErrorList( $minSeverity = ValidationError::SEVERITY_MINOR ) {
58 - $elementHtml = array();
59 -
60 - if ( count( self::$errors ) == 0 ) {
61 - return false;
62 - }
63 -
64 - $elements = array_keys( self::$errors );
65 - natcasesort( $elements );
66 -
67 - foreach ( $elements as $element ) {
68 - $elementErrors = self::getErrorListForElement( $element, $minSeverity );
69 -
70 - if ( $elementErrors ) {
71 - $elementHtml[] = $elementErrors;
72 - }
73 - }
74 -
75 - // TODO: i18n
76 - return "== Errors ==\n\n" . implode( "\n\n", $elementHtml );
 55+ public static function getErrors() {
 56+ return self::$errors;
7757 }
7858
79 - /**
80 - * Returns wikitext listing the errors for a single element.
81 - *
82 - * @since 0.4
83 - *
84 - * @param string $element
85 - * @param integer $minSeverity
86 - *
87 - * @return string
88 - */
89 - public static function getErrorListForElement( $element, $minSeverity = ValidationError::SEVERITY_MINOR ) {
90 - $errors = array();
91 -
92 - if ( array_key_exists( $element, self::$errors ) ) {
93 - foreach ( self::$errors[$element] as $error ) {
94 - if ( $error->severity >= $minSeverity ) {
95 - $errors[] = $error;
96 - }
97 - }
98 - }
99 -
100 - if ( count( $errors ) > 0 ) {
101 - $lines = array();
102 -
103 - foreach ( $errors as $error ) {
104 - // TODO: switch on severity
105 - $lines[] = "* $error->message";
106 - }
107 -
108 - return "=== $element ===\n\n" . implode( "\n", $lines );
109 - }
110 - else {
111 - return false;
112 - }
113 - }
114 -
11559 }
\ No newline at end of file
Index: trunk/extensions/Validator/includes/parserHooks/Validator_ListErrors.php
@@ -69,12 +69,14 @@
7070 * @return array
7171 */
7272 protected function getParameterInfo( $type ) {
 73+ global $egValidatorErrListMin;
 74+
7375 return array(
7476 'minseverity' => array(
7577 'criteria' => array(
7678 'in_array' => array_keys( self::$severityMap )
7779 ),
78 - 'default' => 'minor'
 80+ 'default' => $egValidatorErrListMin
7981 )
8082 );
8183 }
@@ -102,14 +104,103 @@
103105 * @return string
104106 */
105107 public function render( array $parameters ) {
106 - $errorList = ValidationErrorHandler::getErrorList( self::$severityMap[$parameters['minseverity']] );
 108+ $errorList = $this->getErrorList(
 109+ ValidationErrorHandler::getErrors(),
 110+ self::$severityMap[$parameters['minseverity']]
 111+ );
107112
108 - if ( $errorList ) {
109 - return $this->parser->recursiveTagParse( $errorList );
110 - }
111 - else {
 113+ return $this->parser->recursiveTagParse( $errorList );
 114+ }
 115+
 116+ /**
 117+ * Returns a list of errors in wikitext.
 118+ *
 119+ * @since 0.4
 120+ *
 121+ * @param array $errors
 122+ * @param integer $minSeverity
 123+ *
 124+ * @return string
 125+ */
 126+ public function getErrorList( array $errors, $minSeverity = ValidationError::SEVERITY_MINOR ) {
 127+ $elementHtml = array();
 128+
 129+ if ( count( $errors ) == 0 ) {
112130 return '';
113131 }
 132+
 133+ $elements = array_keys( $errors );
 134+ natcasesort( $elements );
 135+
 136+ foreach ( $elements as $element ) {
 137+ $elementErrors = $this->getErrorListForElement( $errors[$element], $element, $minSeverity );
 138+
 139+ if ( $elementErrors ) {
 140+ $elementHtml[] = $elementErrors;
 141+ }
 142+ }
 143+
 144+ return count( $elementHtml ) > 0 ?
 145+ '== ' . wfMsg( 'validator-listerrors-errors' ) . " ==\n\n" . implode( "\n\n", $elementHtml ) : '';
114146 }
115147
 148+ /**
 149+ * Returns wikitext listing the errors for a single element.
 150+ *
 151+ * @since 0.4
 152+ *
 153+ * @param array $allErrors
 154+ * @param string $element
 155+ * @param integer $minSeverity
 156+ *
 157+ * @return mixed String or false
 158+ */
 159+ public function getErrorListForElement( array $allErrors, $element, $minSeverity = ValidationError::SEVERITY_MINOR ) {
 160+ $errors = array();
 161+
 162+ foreach ( $allErrors as $error ) {
 163+ if ( $error->hasSeverity( $minSeverity ) ) {
 164+ $errors[] = $error;
 165+ }
 166+ }
 167+
 168+ if ( count( $errors ) > 0 ) {
 169+ $lines = array();
 170+
 171+ foreach ( $errors as $error ) {
 172+ // TODO: switch on severity
 173+ $lines[] = '* ' . wfMsgExt(
 174+ 'validator-listerrors-severity-message',
 175+ 'parsemag',
 176+ $this->getSeverityMessage( $error->getSeverity() ),
 177+ $error->message
 178+ );
 179+ }
 180+
 181+ return "=== $element ===\n\n" . implode( "\n", $lines );
 182+ }
 183+ else {
 184+ return false;
 185+ }
 186+ }
 187+
 188+ /**
 189+ * Returns a message for a severity.
 190+ *
 191+ * @since 0.4
 192+ *
 193+ * @param integer $severity
 194+ *
 195+ * @return string
 196+ */
 197+ protected function getSeverityMessage( $severity ) {
 198+ static $reverseMap = false;
 199+
 200+ if ( $reverseMap === false ) {
 201+ $reverseMap = array_flip( self::$severityMap );
 202+ }
 203+
 204+ return wfMsg( 'validator-listerrors-' . $reverseMap[$severity] );
 205+ }
 206+
116207 }
\ No newline at end of file
Index: trunk/extensions/Validator/includes/ParserHook.php
@@ -284,9 +284,9 @@
285285 * @return string
286286 */
287287 protected function renderFatalError( ValidationError $error ) {
288 - return '<span class="errorbox">' .
 288+ return '<div><span class="errorbox">' .
289289 htmlspecialchars( wfMsgExt( 'validator-fatal-error', 'parsemag', $error->getMessage() ) ) .
290 - '</span>';
 290+ '</span></div><br /><br />';
291291 }
292292
293293 /**
Index: trunk/extensions/Validator/Validator.i18n.php
@@ -17,6 +17,7 @@
1818 $messages['en'] = array(
1919 'validator-desc' => 'Provides generic parameter handling support for other extensions',
2020
 21+ // General
2122 'validator-warning' => "'''Warning:''' $1",
2223 'validator-error' => "'''Error:''' $1",
2324 'validator-fatal-error' => "Fatal error: $1",
@@ -29,6 +30,15 @@
3031 'validator_error_required_missing' => 'The required parameter "$1" is not provided.',
3132 'validator-error-override-argument' => 'Tried to override parameter $1 (value: $2) with value "$3"',
3233
 34+ // Listerrors
 35+ 'validator-listerrors-errors' => 'Errors',
 36+ 'validator-listerrors-severity-message' => '($1) $2', // $1 = severity; $2 = message
 37+ 'validator-listerrors-minor' => 'Minor',
 38+ 'validator-listerrors-low' => 'Low',
 39+ 'validator-listerrors-normal' => 'Normal',
 40+ 'validator-listerrors-high' => 'High',
 41+ 'validator-listerrors-fatal' => 'Fatal',
 42+
3343 // Criteria errors for single values
3444 'validator_error_empty_argument' => 'Parameter $1 can not have an empty value.',
3545 'validator_error_must_be_number' => 'Parameter $1 can only be a number.',
Index: trunk/extensions/Validator/Validator_Settings.php
@@ -23,12 +23,14 @@
2424
2525 # Maps actions to error severity.
2626 # ACTION_LOG will cause the error to be logged
27 -# ACTION_WARN will cause a notice that there is an error to be shown
28 -# ACTION_SHOW will cause an error message to be shown
29 -# ACTION_DEMAND will cause an error and prevent rendering of the regular output
 27+# ACTION_WARN will cause a notice that there is an error to be shown inline
 28+# ACTION_SHOW will cause an error message to be shown inline
 29+# ACTION_DEMAND will cause an error message to be shown inline and prevent rendering of the regular output
3030 $egErrorActions = array(
3131 ValidationError::SEVERITY_MINOR => ValidationError::ACTION_LOG,
3232 ValidationError::SEVERITY_LOW => ValidationError::ACTION_WARN,
3333 ValidationError::SEVERITY_NORMAL => ValidationError::ACTION_SHOW,
3434 ValidationError::SEVERITY_HIGH => ValidationError::ACTION_DEMAND,
35 -);
\ No newline at end of file
 35+);
 36+
 37+$egValidatorErrListMin = 'minor';
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r74507Follow-up r74505: optional for Translatewikiraymond12:20, 8 October 2010

Status & tagging log