* 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;
- var $value;
+ public $ok = true;
+ public $value;
/** Counters for batch operations */
- public $successCount = 0, $failCount = 0;
+ public $successCount = 0;
+ public $failCount = 0;
+
/** Array to indicate which items of the batch operations were successful */
public $success = array();
- /*semi-private*/ var $errors = array();
- /*semi-private*/ var $cleanCallback = false;
+ public $errors = array();
+ public $cleanCallback = false;
/**
* Factory function for fatal errors
* @param $ok Boolean: whether the operation completed
* @param $value Mixed
*/
- function setResult( $ok, $value = null ) {
+ public function setResult( $ok, $value = null ) {
$this->ok = $ok;
$this->value = $value;
}
*
* @return Boolean
*/
- function isGood() {
+ public function isGood() {
return $this->ok && !$this->errors;
}
*
* @return Boolean
*/
- function isOK() {
+ public function isOK() {
return $this->ok;
}
*
* @param string|Message $message message name or object
*/
- function warning( $message /*, parameters... */ ) {
+ public function warning( $message /*, parameters... */ ) {
$params = array_slice( func_get_args(), 1 );
$this->errors[] = array(
'type' => 'warning',
*
* @param string|Message $message message name or object
*/
- function error( $message /*, parameters... */ ) {
+ public function error( $message /*, parameters... */ ) {
$params = array_slice( func_get_args(), 1 );
$this->errors[] = array(
'type' => 'error',
*
* @param string|Message $message message name or object
*/
- function fatal( $message /*, parameters... */ ) {
+ public function fatal( $message /*, parameters... */ ) {
$params = array_slice( func_get_args(), 1 );
$this->errors[] = array(
'type' => 'error',
/**
* Sanitize the callback parameter on wakeup, to avoid arbitrary execution.
*/
- function __wakeup() {
+ public function __wakeup() {
$this->cleanCallback = false;
}
* @param string $longContext a long enclosing context message name, for a list
* @return String
*/
- function getWikiText( $shortContext = false, $longContext = false ) {
+ public function getWikiText( $shortContext = false, $longContext = false ) {
if ( count( $this->errors ) == 0 ) {
if ( $this->ok ) {
$this->fatal( 'internalerror_info',
}
}
if ( count( $this->errors ) == 1 ) {
- $s = $this->getErrorMessage( $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->getErrorMessageArray( $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 ) {
return $s;
}
+ /**
+ * 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
+ */
+ public 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
} else {
$msg = wfMessage( $error );
}
- return $msg->plain();
+ return $msg;
}
/**
* @param $other Status Other Status object
* @param $overwriteValue Boolean: whether to override the "value" member
*/
- function merge( $other, $overwriteValue = false ) {
+ public function merge( $other, $overwriteValue = false ) {
$this->errors = array_merge( $this->errors, $other->errors );
$this->ok = $this->ok && $other->ok;
if ( $overwriteValue ) {
* @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() {
+ public function getErrorsArray() {
return $this->getStatusArray( "error" );
}
* @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() {
+ public function getWarningsArray() {
return $this->getStatusArray( "warning" );
}
/**
* Returns a list of status messages of the given type
* @param $type String
- *
* @return Array
*/
protected function getStatusArray( $type ) {
foreach ( $this->errors as $error ) {
if ( $error['type'] === $type ) {
if ( $error['message'] instanceof Message ) {
- $result[] = array_merge( array( $error['message']->getKey() ), $error['message']->getParams() );
+ $result[] = array_merge(
+ array( $error['message']->getKey() ),
+ $error['message']->getParams()
+ );
} elseif ( $error['params'] ) {
$result[] = array_merge( array( $error['message'] ), $error['params'] );
} else {
}
}
}
+
return $result;
}
* @param string $msg message name
* @return Boolean
*/
- function hasMessage( $msg ) {
+ public function hasMessage( $msg ) {
foreach ( $this->errors as $error ) {
if ( $error['message'] === $msg ) {
return true;
* @param $dest Message|String: Replacement message key or object
* @return bool Return true if the replacement was done, false otherwise.
*/
- function replaceMessage( $source, $dest ) {
+ public function replaceMessage( $source, $dest ) {
$replaced = false;
foreach ( $this->errors as $index => $error ) {
if ( $error['message'] === $source ) {
return $replaced;
}
- /**
- * Backward compatibility function for WikiError -> Status migration
- *
- * @return String
- */
- public function getMessage() {
- return $this->getWikiText();
- }
-
/**
* @return mixed
*/