diff options
Diffstat (limited to 'includes/Status.php')
-rw-r--r-- | includes/Status.php | 149 |
1 files changed, 112 insertions, 37 deletions
diff --git a/includes/Status.php b/includes/Status.php index 10dfb516..928f8ebd 100644 --- a/includes/Status.php +++ b/includes/Status.php @@ -31,6 +31,11 @@ * An operation which is not OK should have errors so that the user can be * informed as to what went wrong. Calling the fatal() function sets an error * message and simultaneously switches off the OK flag. + * + * The recommended pattern for Status objects is to return a Status object + * unconditionally, i.e. both on success and on failure -- so that the + * developer of the calling code is reminded that the function can fail, and + * so that a lack of error-handling will be explicit. */ class Status { var $ok = true; @@ -47,7 +52,7 @@ class Status { /** * Factory function for fatal errors * - * @param $message String: message name + * @param string|Message $message message name or object * @return Status */ static function newFatal( $message /*, parameters...*/ ) { @@ -103,7 +108,7 @@ class Status { /** * Add a new warning * - * @param $message String: message name + * @param string|Message $message message name or object */ function warning( $message /*, parameters... */ ) { $params = array_slice( func_get_args(), 1 ); @@ -117,7 +122,7 @@ class Status { * Add an error, do not set fatal flag * This can be used for non-fatal errors * - * @param $message String: message name + * @param string|Message $message message name or object */ function error( $message /*, parameters... */ ) { $params = array_slice( func_get_args(), 1 ); @@ -131,7 +136,7 @@ class Status { * Add an error and set OK to false, indicating that the operation * as a whole was fatal * - * @param $message String: message name + * @param string|Message $message message name or object */ function fatal( $message /*, parameters... */ ) { $params = array_slice( func_get_args(), 1 ); @@ -167,31 +172,34 @@ class Status { /** * Get the error list as a wikitext formatted list * - * @param $shortContext String: a short enclosing context message name, to + * @param string $shortContext a short enclosing context message name, to * be used when there is a single error - * @param $longContext String: a long enclosing context message name, for a list + * @param string $longContext a long enclosing context message name, for a list * @return String */ function getWikiText( $shortContext = false, $longContext = false ) { if ( count( $this->errors ) == 0 ) { if ( $this->ok ) { $this->fatal( 'internalerror_info', - __METHOD__." called for a good result, this is incorrect\n" ); + __METHOD__ . " called for a good result, this is incorrect\n" ); } else { $this->fatal( 'internalerror_info', - __METHOD__.": Invalid result object: no error text but not OK\n" ); + __METHOD__ . ": Invalid result object: no error text but not OK\n" ); } } if ( count( $this->errors ) == 1 ) { - $s = $this->getWikiTextForError( $this->errors[0], $this->errors[0] ); + $s = $this->getErrorMessage( $this->errors[0] )->plain(); if ( $shortContext ) { $s = wfMessage( $shortContext, $s )->plain(); } elseif ( $longContext ) { $s = wfMessage( $longContext, "* $s\n" )->plain(); } } else { - $s = '* '. implode("\n* ", - $this->getWikiTextArray( $this->errors ) ) . "\n"; + $errors = $this->getErrorMessageArray( $this->errors ); + foreach ( $errors as &$error ) { + $error = $error->plain(); + } + $s = '* ' . implode( "\n* ", $errors ) . "\n"; if ( $longContext ) { $s = wfMessage( $longContext, $s )->plain(); } elseif ( $shortContext ) { @@ -202,7 +210,57 @@ class Status { } /** - * Return the wiki text for a single error. + * Get the error list as a Message object + * + * @param string $shortContext a short enclosing context message name, to + * be used when there is a single error + * @param string $longContext a long enclosing context message name, for a list + * @return Message + */ + function getMessage( $shortContext = false, $longContext = false ) { + if ( count( $this->errors ) == 0 ) { + if ( $this->ok ) { + $this->fatal( 'internalerror_info', + __METHOD__ . " called for a good result, this is incorrect\n" ); + } else { + $this->fatal( 'internalerror_info', + __METHOD__ . ": Invalid result object: no error text but not OK\n" ); + } + } + if ( count( $this->errors ) == 1 ) { + $s = $this->getErrorMessage( $this->errors[0] ); + if ( $shortContext ) { + $s = wfMessage( $shortContext, $s ); + } elseif ( $longContext ) { + $wrapper = new RawMessage( "* \$1\n" ); + $wrapper->params( $s )->parse(); + $s = wfMessage( $longContext, $wrapper ); + } + } else { + $msgs = $this->getErrorMessageArray( $this->errors ); + $msgCount = count( $msgs ); + + if ( $shortContext ) { + $msgCount++; + } + + $wrapper = new RawMessage( '* $' . implode( "\n* \$", range( 1, $msgCount ) ) ); + $s = $wrapper->params( $msgs )->parse(); + + if ( $longContext ) { + $s = wfMessage( $longContext, $wrapper ); + } elseif ( $shortContext ) { + $wrapper = new RawMessage( "\n\$1\n", $wrapper ); + $wrapper->parse(); + $s = wfMessage( $shortContext, $wrapper ); + } + } + + return $s; + } + + /** + * Return the message for a single error. * @param $error Mixed With an array & two values keyed by * 'message' and 'params', use those keys-value pairs. * Otherwise, if its an array, just use the first value as the @@ -210,19 +268,35 @@ class Status { * * @return String */ - protected function getWikiTextForError( $error ) { + protected function getErrorMessage( $error ) { if ( is_array( $error ) ) { - if ( isset( $error['message'] ) && isset( $error['params'] ) ) { - return wfMessage( $error['message'], - array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) )->plain(); + if ( isset( $error['message'] ) && $error['message'] instanceof Message ) { + $msg = $error['message']; + } elseif ( isset( $error['message'] ) && isset( $error['params'] ) ) { + $msg = wfMessage( $error['message'], + array_map( 'wfEscapeWikiText', $this->cleanParams( $error['params'] ) ) ); } else { - $message = array_shift($error); - return wfMessage( $message, - array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) )->plain(); + $msgName = array_shift( $error ); + $msg = wfMessage( $msgName, + array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) ); } } else { - return wfMessage( $error )->plain(); + $msg = wfMessage( $error ); } + return $msg; + } + + /** + * Get the error message as HTML. This is done by parsing the wikitext error + * message. + * + * @note: this does not perform a full wikitext to HTML conversion, it merely applies + * a message transformation. + * @todo figure out whether that is actually The Right Thing. + */ + public function getHTML( $shortContext = false, $longContext = false ) { + $text = $this->getWikiText( $shortContext, $longContext ); + return MessageCache::singleton()->transform( $text, true ); } /** @@ -230,8 +304,8 @@ class Status { * @param $errors Array * @return Array */ - function getWikiTextArray( $errors ) { - return array_map( array( $this, 'getWikiTextForError' ), $errors ); + protected function getErrorMessageArray( $errors ) { + return array_map( array( $this, 'getErrorMessage' ), $errors ); } /** @@ -253,7 +327,8 @@ class Status { /** * Get the list of errors (but not warnings) * - * @return Array + * @return array A list in which each entry is an array with a message key as its first element. + * The remaining array elements are the message parameters. */ function getErrorsArray() { return $this->getStatusArray( "error" ); @@ -262,7 +337,8 @@ class Status { /** * Get the list of warnings (but not errors) * - * @return Array + * @return array A list in which each entry is an array with a message key as its first element. + * The remaining array elements are the message parameters. */ function getWarningsArray() { return $this->getStatusArray( "warning" ); @@ -278,7 +354,9 @@ class Status { $result = array(); foreach ( $this->errors as $error ) { if ( $error['type'] === $type ) { - if( $error['params'] ) { + if ( $error['message'] instanceof Message ) { + $result[] = array_merge( array( $error['message']->getKey() ), $error['message']->getParams() ); + } elseif ( $error['params'] ) { $result[] = array_merge( array( $error['message'] ), $error['params'] ); } else { $result[] = array( $error['message'] ); @@ -309,7 +387,10 @@ class Status { /** * Returns true if the specified message is present as a warning or error * - * @param $msg String: message name + * Note, due to the lack of tools for comparing Message objects, this + * function will not work when using a Message object as a parameter. + * + * @param string $msg message name * @return Boolean */ function hasMessage( $msg ) { @@ -325,9 +406,12 @@ class Status { * If the specified source message exists, replace it with the specified * destination message, but keep the same parameters as in the original error. * - * Return true if the replacement was done, false otherwise. + * Note, due to the lack of tools for comparing Message objects, this + * function will not work when using a Message object as the search parameter. * - * @return bool + * @param $source Message|String: Message key or object to search for + * @param $dest Message|String: Replacement message key or object + * @return bool Return true if the replacement was done, false otherwise. */ function replaceMessage( $source, $dest ) { $replaced = false; @@ -341,15 +425,6 @@ class Status { } /** - * Backward compatibility function for WikiError -> Status migration - * - * @return String - */ - public function getMessage() { - return $this->getWikiText(); - } - - /** * @return mixed */ public function getValue() { |