X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FStatus.php;h=a35af6e8c6be9560da0aa7137e09cff36353ebc9;hb=80847dcb32f4f6ecf198d3ff4c8bc71333832c75;hp=e5788733cf95939d22c1ba9c2016dac834aefced;hpb=8472c6344819b79051bf947f0f2142e24bc8aba5;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Status.php b/includes/Status.php index e5788733cf..a35af6e8c6 100644 --- a/includes/Status.php +++ b/includes/Status.php @@ -37,34 +37,10 @@ * 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 StatusValue */ - protected $sv; - - /** @var mixed */ - public $value; - /** @var array Map of (key => bool) to indicate success of each part of batch operations */ - public $success = []; - /** @var int Counter for batch operations */ - public $successCount = 0; - /** @var int Counter for batch operations */ - public $failCount = 0; - +class Status extends StatusValue { /** @var callable */ public $cleanCallback = false; - /** - * @param StatusValue $sv [optional] - */ - public function __construct( StatusValue $sv = null ) { - $this->sv = ( $sv === null ) ? new StatusValue() : $sv; - // B/C field aliases - $this->value =& $this->sv->value; - $this->successCount =& $this->sv->successCount; - $this->failCount =& $this->sv->failCount; - $this->success =& $this->sv->success; - } - /** * Succinct helper method to wrap a StatusValue * @@ -77,99 +53,83 @@ class Status { * @return Status */ public static function wrap( $sv ) { - return $sv instanceof Status ? $sv : new self( $sv ); - } + if ( $sv instanceof static ) { + return $sv; + } - /** - * Factory function for fatal errors - * - * @param string|Message $message Message name or object - * @return Status - */ - public static function newFatal( $message /*, parameters...*/ ) { - return new self( call_user_func_array( - [ 'StatusValue', 'newFatal' ], func_get_args() - ) ); + $result = new static(); + $result->ok =& $sv->ok; + $result->errors =& $sv->errors; + $result->value =& $sv->value; + $result->successCount =& $sv->successCount; + $result->failCount =& $sv->failCount; + $result->success =& $sv->success; + + return $result; } /** - * Factory function for good results + * Backwards compatibility logic * - * @param mixed $value - * @return Status + * @param string $name + * @return mixed + * @throws RuntimeException */ - public static function newGood( $value = null ) { - $sv = new StatusValue(); - $sv->value = $value; + function __get( $name ) { + if ( $name === 'ok' ) { + return $this->isOK(); + } elseif ( $name === 'errors' ) { + return $this->getErrors(); + } - return new self( $sv ); + throw new RuntimeException( "Cannot get '$name' property." ); } /** * Change operation result + * Backwards compatibility logic * - * @param bool $ok Whether the operation completed + * @param string $name * @param mixed $value + * @throws RuntimeException */ - public function setResult( $ok, $value = null ) { - $this->sv->setResult( $ok, $value ); - } - - /** - * Returns the wrapped StatusValue object - * @return StatusValue - * @since 1.27 - */ - public function getStatusValue() { - return $this->sv; - } - - /** - * Returns whether the operation completed and didn't have any error or - * warnings - * - * @return bool - */ - public function isGood() { - return $this->sv->isGood(); - } - - /** - * Returns whether the operation completed - * - * @return bool - */ - public function isOK() { - return $this->sv->isOK(); + function __set( $name, $value ) { + if ( $name === 'ok' ) { + $this->setOK( $value ); + } elseif ( !property_exists( $this, $name ) ) { + // Caller is using undeclared ad-hoc properties + $this->$name = $value; + } else { + throw new RuntimeException( "Cannot set '$name' property." ); + } } /** - * Add a new warning + * Splits this Status object into two new Status objects, one which contains only + * the error messages, and one that contains the warnings, only. The returned array is + * defined as: + * [ + * 0 => object(Status) # the Status with error messages, only + * 1 => object(Status) # The Status with warning messages, only + * ] * - * @param string|Message $message Message name or object + * @return Status[] */ - public function warning( $message /*, parameters... */ ) { - call_user_func_array( [ $this->sv, 'warning' ], func_get_args() ); - } + public function splitByErrorType() { + list( $errorsOnlyStatus, $warningsOnlyStatus ) = parent::splitByErrorType(); + $errorsOnlyStatus->cleanCallback = + $warningsOnlyStatus->cleanCallback = $this->cleanCallback; - /** - * Add an error, do not set fatal flag - * This can be used for non-fatal errors - * - * @param string|Message $message Message name or object - */ - public function error( $message /*, parameters... */ ) { - call_user_func_array( [ $this->sv, 'error' ], func_get_args() ); + return [ $errorsOnlyStatus, $warningsOnlyStatus ]; } /** - * Add an error and set OK to false, indicating that the operation - * as a whole was fatal - * - * @param string|Message $message Message name or object + * Returns the wrapped StatusValue object + * @return StatusValue + * @since 1.27 */ - public function fatal( $message /*, parameters... */ ) { - call_user_func_array( [ $this->sv, 'fatal' ], func_get_args() ); + public function getStatusValue() { + return $this; } /** @@ -217,16 +177,16 @@ class Status { public function getWikiText( $shortContext = false, $longContext = false, $lang = null ) { $lang = $this->languageFromParam( $lang ); - $rawErrors = $this->sv->getErrors(); + $rawErrors = $this->getErrors(); if ( count( $rawErrors ) == 0 ) { - if ( $this->sv->isOK() ) { - $this->sv->fatal( 'internalerror_info', + if ( $this->isOK() ) { + $this->fatal( 'internalerror_info', __METHOD__ . " called for a good result, this is incorrect\n" ); } else { - $this->sv->fatal( 'internalerror_info', + $this->fatal( 'internalerror_info', __METHOD__ . ": Invalid result object: no error text but not OK\n" ); } - $rawErrors = $this->sv->getErrors(); // just added a fatal + $rawErrors = $this->getErrors(); // just added a fatal } if ( count( $rawErrors ) == 1 ) { $s = $this->getErrorMessage( $rawErrors[0], $lang )->plain(); @@ -265,24 +225,24 @@ class Status { * * If both parameters are missing, and there is only one error, no bullet will be added. * - * @param string|string[] $shortContext A message name or an array of message names. - * @param string|string[] $longContext A message name or an array of message names. + * @param string|string[]|bool $shortContext A message name or an array of message names. + * @param string|string[]|bool $longContext A message name or an array of message names. * @param string|Language $lang Language to use for processing messages * @return Message */ public function getMessage( $shortContext = false, $longContext = false, $lang = null ) { $lang = $this->languageFromParam( $lang ); - $rawErrors = $this->sv->getErrors(); + $rawErrors = $this->getErrors(); if ( count( $rawErrors ) == 0 ) { - if ( $this->sv->isOK() ) { - $this->sv->fatal( 'internalerror_info', + if ( $this->isOK() ) { + $this->fatal( 'internalerror_info', __METHOD__ . " called for a good result, this is incorrect\n" ); } else { - $this->sv->fatal( 'internalerror_info', + $this->fatal( 'internalerror_info', __METHOD__ . ": Invalid result object: no error text but not OK\n" ); } - $rawErrors = $this->sv->getErrors(); // just added a fatal + $rawErrors = $this->getErrors(); // just added a fatal } if ( count( $rawErrors ) == 1 ) { $s = $this->getErrorMessage( $rawErrors[0], $lang ); @@ -313,11 +273,12 @@ class Status { } /** - * Return the message for a single error. - * @param mixed $error 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 - * message and the remaining items as the params. + * Return the message for a single error + * + * The code string can be used a message key with per-language versions. + * If $error is an array, the "params" field is a list of parameters for the message. + * + * @param array|string $error Code string or (key: code string, params: string[]) map * @param string|Language $lang Language to use for processing messages * @return Message */ @@ -333,8 +294,10 @@ class Status { $msg = wfMessage( $msgName, array_map( 'wfEscapeWikiText', $this->cleanParams( $error ) ) ); } - } else { + } elseif ( is_string( $error ) ) { $msg = wfMessage( $error ); + } else { + throw new UnexpectedValueException( "Got " . get_class( $error ) . " for key." ); } $msg->inLanguage( $this->languageFromParam( $lang ) ); @@ -342,12 +305,11 @@ class Status { } /** - * Get the error message as HTML. This is done by parsing the wikitext error - * message. - * @param string $shortContext A short enclosing context message name, to + * Get the error message as HTML. This is done by parsing the wikitext error message + * @param string|bool $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 - * @param string|Language $lang Language to use for processing messages + * @param string|bool $longContext A long enclosing context message name, for a list + * @param string|Language|null $lang Language to use for processing messages * @return string */ public function getHTML( $shortContext = false, $longContext = false, $lang = null ) { @@ -370,16 +332,6 @@ class Status { }, $errors ); } - /** - * Merge another status object into this one - * - * @param Status $other Other Status object - * @param bool $overwriteValue Whether to override the "value" member - */ - public function merge( $other, $overwriteValue = false ) { - $this->sv->merge( $other->sv, $overwriteValue ); - } - /** * Get the list of errors (but not warnings) * @@ -413,7 +365,7 @@ class Status { protected function getStatusArray( $type = false ) { $result = []; - foreach ( $this->sv->getErrors() as $error ) { + foreach ( $this->getErrors() as $error ) { if ( $type === false || $error['type'] === $type ) { if ( $error['message'] instanceof MessageSpecifier ) { $result[] = array_merge( @@ -431,92 +383,6 @@ class Status { return $result; } - /** - * Returns a list of status messages of the given type, with message and - * params left untouched, like a sane version of getStatusArray - * - * Each entry is a map of: - * - message: string message key or MessageSpecifier - * - params: array list of parameters - * - * @param string $type - * @return array - */ - public function getErrorsByType( $type ) { - return $this->sv->getErrorsByType( $type ); - } - - /** - * Returns true if the specified message is present as a warning or error - * - * @param string|Message $message Message key or object to search for - * - * @return bool - */ - public function hasMessage( $message ) { - return $this->sv->hasMessage( $message ); - } - - /** - * If the specified source message exists, replace it with the specified - * destination message, but keep the same parameters as in the original error. - * - * 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. - * - * @param Message|string $source Message key or object to search for - * @param Message|string $dest Replacement message key or object - * @return bool Return true if the replacement was done, false otherwise. - */ - public function replaceMessage( $source, $dest ) { - return $this->sv->replaceMessage( $source, $dest ); - } - - /** - * @return mixed - */ - public function getValue() { - return $this->sv->getValue(); - } - - /** - * Backwards compatibility logic - * - * @param string $name - */ - function __get( $name ) { - if ( $name === 'ok' ) { - return $this->sv->isOK(); - } elseif ( $name === 'errors' ) { - return $this->sv->getErrors(); - } - throw new Exception( "Cannot get '$name' property." ); - } - - /** - * Backwards compatibility logic - * - * @param string $name - * @param mixed $value - */ - function __set( $name, $value ) { - if ( $name === 'ok' ) { - $this->sv->setOK( $value ); - } elseif ( !property_exists( $this, $name ) ) { - // Caller is using undeclared ad-hoc properties - $this->$name = $value; - } else { - throw new Exception( "Cannot set '$name' property." ); - } - } - - /** - * @return string - */ - public function __toString() { - return $this->sv->__toString(); - } - /** * Don't save the callback when serializing, because Closures can't be * serialized and we're going to clear it in __wakeup anyway.