23use Wikimedia\Assert\Assert;
63 protected $errors = [];
72 public $successCount = 0;
75 public $failCount = 0;
87 public static function newFatal( $message, ...$parameters ) {
88 $result =
new static();
89 $result->fatal( $message, ...$parameters );
99 public static function newGood( $value =
null ) {
100 $result =
new static();
101 $result->value = $value;
117 $errorsOnlyStatusValue = static::newGood();
118 $warningsOnlyStatusValue = static::newGood();
119 $warningsOnlyStatusValue->setResult(
true, $this->
getValue() );
120 $errorsOnlyStatusValue->setResult( $this->
isOK(), $this->
getValue() );
122 foreach ( $this->errors as $item ) {
123 if ( $item[
'type'] ===
'warning' ) {
124 $warningsOnlyStatusValue->errors[] = $item;
126 $errorsOnlyStatusValue->errors[] = $item;
130 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
140 return $this->ok && !$this->errors;
169 return $this->errors;
191 $this->ok = (bool)$ok;
192 $this->value = $value;
213 private function addError( array $newError ) {
214 [
'type' => $newType,
'message' => $newKey,
'params' => $newParams ] = $newError;
220 ' was deprecated in MediaWiki 1.43',
'1.43' );
222 $newParams = $newKey->getParams();
223 $newKey = $newKey->getKey();
226 foreach ( $this->errors as [
'type' => &$type,
'message' => $key,
'params' =>
$params ] ) {
229 $key = $key->getKey();
231 if ( $newKey === $key && $newParams ===
$params ) {
232 if ( $type ===
'warning' && $newType ===
'error' ) {
239 $this->errors[] = $newError;
251 public function warning( $message, ...$parameters ) {
252 $message = $this->normalizeMessage( $message );
254 return $this->addError( [
256 'message' => $message,
257 'params' => $parameters
269 public function error( $message, ...$parameters ) {
270 $message = $this->normalizeMessage( $message );
272 return $this->addError( [
274 'message' => $message,
275 'params' => $parameters
287 public function fatal( $message, ...$parameters ) {
289 return $this->
error( $message, ...$parameters );
299 public function merge( $other, $overwriteValue =
false ) {
300 if ( $this->statusData !==
null && $other->statusData !==
null ) {
301 throw new RuntimeException(
"Status cannot be merged, because they both have \$statusData" );
303 $this->statusData ??= $other->statusData;
306 foreach ( $other->errors as $error ) {
307 $this->addError( $error );
309 $this->ok = $this->ok && $other->ok;
310 if ( $overwriteValue ) {
311 $this->value = $other->value;
313 $this->successCount += $other->successCount;
314 $this->failCount += $other->failCount;
333 foreach ( $this->errors as $error ) {
334 if ( $error[
'type'] === $type ) {
351 Assert::parameter( $type === null || $type ===
'warning' || $type ===
'error',
352 '$type',
"must be null, 'warning', or 'error'" );
354 foreach ( $this->errors as $error ) {
355 if ( $type ===
null || $error[
'type'] === $type ) {
356 [
'message' => $key,
'params' =>
$params ] = $error;
380 ' was deprecated in MediaWiki 1.43',
'1.43' );
381 $message = $message->getKey();
384 ' was deprecated in MediaWiki 1.43',
'1.43' );
385 $message = $message->getKey();
388 foreach ( $this->errors as [
'message' => $key ] ) {
409 foreach ( $messages as $message ) {
412 ' was deprecated in MediaWiki 1.43',
'1.43' );
413 $message = $message->getKey();
416 ' was deprecated in MediaWiki 1.43',
'1.43' );
417 $message = $message->getKey();
419 $exceptedKeys[] = $message;
422 foreach ( $this->errors as [
'message' => $key ] ) {
424 $key = $key->getKey();
426 if ( !in_array( $key, $exceptedKeys,
true ) ) {
459 wfDeprecatedMsg(
'Passing MessageSpecifier as $source to replaceMessage()' .
460 ' was deprecated in MediaWiki 1.43',
'1.43' );
462 wfDeprecatedMsg(
'Passing MessageValue as $source to replaceMessage()' .
463 ' was deprecated in MediaWiki 1.43',
'1.43' );
467 $dest = $this->normalizeMessage( $dest );
469 foreach ( $this->errors as [
'message' => &$message,
'params' => &
$params ] ) {
492 $status = $this->isOK() ?
"OK" :
"Error";
493 if ( count( $this->errors ) ) {
494 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
496 $errorcount =
"no errors detected";
498 if ( isset( $this->value ) ) {
499 $valstr = gettype( $this->value ) .
" value set";
500 if ( is_object( $this->value ) ) {
501 $valstr .=
"\"" . get_class( $this->value ) .
"\" instance";
504 $valstr =
"no value set";
506 $out = sprintf(
"<%s, %s, %s>",
511 if ( count( $this->errors ) > 0 ) {
512 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
514 foreach ( $this->errors as [
'type' => $type,
'message' => $key,
'params' =>
$params ] ) {
517 $key = $key->getKey();
520 $keyChunks = mb_str_split( $key, 25 );
521 $paramsChunks = mb_str_split( $this->flattenParams(
$params,
" | " ), 36 );
524 foreach ( array_map(
null, [ $type ], $keyChunks, $paramsChunks )
525 as [ $typeChunk, $keyChunk, $paramsChunk ]
527 $out .= sprintf(
"| %-8s | %-25s | %-36s |\n",
546 private function flattenParams( array
$params,
string $joiner =
', ' ): string {
549 if ( is_array( $p ) ) {
550 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
552 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
557 $ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) .
"..." : $r;
559 return implode( $joiner, $ret );
573 foreach ( $this->getErrors() as $error ) {
574 if ( !$type || $error[
'type'] === $type ) {
576 $result[] = [ $error[
'message']->getKey(), ...$error[
'message']->getParams() ];
578 $result[] = [ $error[
'message'], ...$error[
'params'] ];
591 private function normalizeMessage( $message ) {
594 return $converter->convertMessageValue( $message );
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
array $params
The job parameters.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
hasMessage( $message)
Returns true if the specified message is present as a warning or error.
static newFatal( $message,... $parameters)
Factory function for fatal errors.
int $failCount
Counter for batch operations.
getErrors()
Get the list of errors.
getMessages(?string $type=null)
Returns a list of error messages, optionally only those of the given type.
replaceMessage( $source, $dest)
If the specified source message exists, replace it with the specified destination message,...
splitByErrorType()
Splits this StatusValue object into two new StatusValue objects, one which contains only the error me...
setOK( $ok)
Change operation status.
hasMessagesExcept(... $messages)
Returns true if any other message than the specified ones is present as a warning or error.
getStatusArray( $type=false)
Returns a list of status messages of the given type (or all if false)
isOK()
Returns whether the operation completed.
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
setResult( $ok, $value=null)
Change operation result.
merge( $other, $overwriteValue=false)
Merge another status object into this one.
mixed $statusData
arbitrary extra data about the operation
__toString()
Returns a string representation of the status for debugging.
error( $message,... $parameters)
Add an error, do not set fatal flag This can be used for non-fatal errors.
getErrorsByType( $type)
Returns a list of status messages of the given type.
warning( $message,... $parameters)
Add a new warning.
isGood()
Returns whether the operation completed and didn't have any error or warnings.
static newGood( $value=null)
Factory function for good results.
int $successCount
Counter for batch operations.
getKey()
Returns the message key.