21use Wikimedia\Assert\Assert;
66 protected $errors = [];
75 public $successCount = 0;
78 public $failCount = 0;
92 public static function newFatal( $message, ...$parameters ) {
93 $result =
new static();
94 $result->fatal( $message, ...$parameters );
104 public static function newGood( $value =
null ) {
105 $result =
new static();
106 $result->value = $value;
122 $errorsOnlyStatusValue = static::newGood();
123 $warningsOnlyStatusValue = static::newGood();
124 $warningsOnlyStatusValue->setResult(
true, $this->
getValue() );
125 $errorsOnlyStatusValue->setResult( $this->
isOK(), $this->
getValue() );
127 foreach ( $this->errors as $item ) {
128 if ( $item[
'type'] ===
'warning' ) {
129 $warningsOnlyStatusValue->errors[] = $item;
131 $errorsOnlyStatusValue->errors[] = $item;
135 return [ $errorsOnlyStatusValue, $warningsOnlyStatusValue ];
145 return $this->ok && !$this->errors;
174 return $this->errors;
196 $this->ok = (bool)$ok;
197 $this->value = $value;
218 private function addError( array $newError ) {
219 [
'type' => $newType,
'message' => $newKey,
'params' => $newParams ] = $newError;
221 Assert::parameter( $newParams === [],
222 '$parameters',
"must be empty when using a MessageSpecifier" );
223 $newParams = $newKey->getParams();
224 $newKey = $newKey->getKey();
227 foreach ( $this->errors as [
'type' => &$type,
'message' => $key,
'params' =>
$params ] ) {
230 $key = $key->getKey();
235 if ( $newKey === $key && $newParams ==
$params ) {
236 if ( $type ===
'warning' && $newType ===
'error' ) {
243 $this->errors[] = $newError;
257 public function warning( $message, ...$parameters ) {
258 return $this->addError( [
260 'message' => $message,
261 'params' => $parameters
275 public function error( $message, ...$parameters ) {
276 return $this->addError( [
278 'message' => $message,
279 'params' => $parameters
293 public function fatal( $message, ...$parameters ) {
295 return $this->
error( $message, ...$parameters );
305 public function merge( $other, $overwriteValue =
false ) {
306 if ( $this->statusData !==
null && $other->statusData !==
null ) {
307 throw new RuntimeException(
"Status cannot be merged, because they both have \$statusData" );
309 $this->statusData ??= $other->statusData;
312 foreach ( $other->errors as $error ) {
313 $this->addError( $error );
315 $this->ok = $this->ok && $other->ok;
316 if ( $overwriteValue ) {
317 $this->value = $other->value;
319 $this->successCount += $other->successCount;
320 $this->failCount += $other->failCount;
339 foreach ( $this->errors as $error ) {
340 if ( $error[
'type'] === $type ) {
360 Assert::parameter( $type === null || $type ===
'warning' || $type ===
'error',
361 '$type',
"must be null, 'warning', or 'error'" );
363 foreach ( $this->errors as $error ) {
364 if ( $type ===
null || $error[
'type'] === $type ) {
365 [
'message' => $key,
'params' =>
$params ] = $error;
385 foreach ( $this->errors as [
'message' => $key ] ) {
404 foreach ( $this->errors as [
'message' => $key ] ) {
406 $key = $key->getKey();
408 if ( !in_array( $key, $messages,
true ) ) {
429 foreach ( $this->errors as [
'message' => &$message,
'params' => &
$params ] ) {
452 $status = $this->isOK() ?
"OK" :
"Error";
453 if ( count( $this->errors ) ) {
454 $errorcount =
"collected " . ( count( $this->errors ) ) .
" message(s) on the way";
456 $errorcount =
"no errors detected";
458 if ( isset( $this->value ) ) {
459 $valstr = get_debug_type( $this->value ) .
" value set";
461 $valstr =
"no value set";
463 $out = sprintf(
"<%s, %s, %s>",
468 if ( count( $this->errors ) > 0 ) {
469 $hdr = sprintf(
"+-%'-8s-+-%'-25s-+-%'-36s-+\n",
"",
"",
"" );
471 foreach ( $this->errors as [
'type' => $type,
'message' => $key,
'params' =>
$params ] ) {
474 $key = $key->getKey();
477 $keyChunks = mb_str_split( $key, 25 );
478 $paramsChunks = mb_str_split( $this->flattenParams(
$params,
" | " ), 36 );
481 foreach ( array_map(
null, [ $type ], $keyChunks, $paramsChunks )
482 as [ $typeChunk, $keyChunk, $paramsChunk ]
484 $out .= sprintf(
"| %-8s | %-25s | %-36s |\n",
503 private function flattenParams( array
$params,
string $joiner =
', ' ): string {
506 if ( is_array( $p ) ) {
507 $r =
'[ ' . self::flattenParams( $p ) .
' ]';
509 $r =
'{ ' . $p->getKey() .
': ' . self::flattenParams( $p->getParams() ) .
' }';
516 $ret[] = mb_strlen( $r ) > 100 ? mb_substr( $r, 0, 99 ) .
"..." : $r;
518 return implode( $joiner, $ret );
532 foreach ( $this->getErrors() as $error ) {
533 if ( !$type || $error[
'type'] === $type ) {
535 $result[] = [ $error[
'message']->getKey(), ...$error[
'message']->getParams() ];
537 $result[] = [ $error[
'message'], ...$error[
'params'] ];
array $params
The job parameters.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
hasMessagesExcept(string ... $messages)
Returns true if any other message than the specified ones 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.
splitByErrorType()
Splits this StatusValue object into two new StatusValue objects, one which contains only the error me...
setOK( $ok)
Change operation status.
hasMessage(string $message)
Returns true if the specified message is present as a warning or error.
getStatusArray( $type=false)
Returns a list of status messages of the given type (or all if false)
replaceMessage(string $source, $dest)
If the specified source message exists, replace it with the specified destination message,...
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.