MediaWiki master
StatusValue Class Reference

Generic operation result class Has warning/error list, boolean status and arbitrary value. More...

Inherits Stringable.

Inherited by MediaWiki\Permissions\PermissionStatus, and MediaWiki\Status\Status.

Collaboration diagram for StatusValue:

Public Member Functions

 __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.
 
 fatal ( $message,... $parameters)
 Add an error and set OK to false, indicating that the operation as a whole was fatal.
 
 getErrors ()
 Get the list of errors.
 
 getErrorsByType ( $type)
 Returns a list of status messages of the given type.
 
 getMessages (?string $type=null)
 Returns a list of error messages, optionally only those of the given type.
 
 getValue ()
 
 hasMessage (string $message)
 Returns true if the specified message is present as a warning or error.
 
 hasMessagesExcept (string ... $messages)
 Returns true if any other message than the specified ones is present as a warning or error.
 
 isGood ()
 Returns whether the operation completed and didn't have any error or warnings.
 
 isOK ()
 Returns whether the operation completed.
 
 merge ( $other, $overwriteValue=false)
 Merge another status object into this one.
 
 replaceMessage (string $source, $dest)
 If the specified source message exists, replace it with the specified destination message, but keep the same parameters as in the original error.
 
 setOK ( $ok)
 Change operation status.
 
 setResult ( $ok, $value=null)
 Change operation result.
 
 splitByErrorType ()
 Splits this StatusValue object into two new StatusValue objects, one which contains only the error messages, and one that contains the warnings, only.
 
 warning ( $message,... $parameters)
 Add a new warning.
 

Static Public Member Functions

static newFatal ( $message,... $parameters)
 Factory function for fatal errors.
 
static newGood ( $value=null)
 Factory function for good results.
 

Public Attributes

int $failCount = 0
 Counter for batch operations.
 
mixed $statusData
 arbitrary extra data about the operation
 
bool[] $success = []
 Map of (key => bool) to indicate success of each part of batch operations.
 
int $successCount = 0
 Counter for batch operations.
 
mixed $value
 

Protected Member Functions

 getStatusArray ( $type=false)
 Returns a list of status messages of the given type (or all if false)
 

Protected Attributes

array[] $errors = []
 
bool $ok = true
 

Detailed Description

Generic operation result class Has warning/error list, boolean status and arbitrary value.

"Good" means the operation was completed with no warnings or errors.

"OK" means the operation was partially or wholly completed.

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 functions returning StatusValue objects is to return a StatusValue unconditionally, both on success and on failure (similarly to Option, Maybe, Promise etc. objects in other languages) – 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.

This class accepts any MessageSpecifier objects. The use of Message objects should be avoided when serializability is needed. Use MessageValue in that case instead.

Stability: newable
Stability: stable
to extend
Since
1.25

Definition at line 52 of file StatusValue.php.

Member Function Documentation

◆ __toString()

StatusValue::__toString ( )

Returns a string representation of the status for debugging.

This is fairly verbose and may change without notice.

Returns
string

Definition at line 451 of file StatusValue.php.

References $params.

◆ error()

StatusValue::error ( $message,
$parameters )

Add an error, do not set fatal flag This can be used for non-fatal errors.

Parameters
string | MessageSpecifier$messageMessage key or object @phpcs:ignore Generic.Files.LineLength
MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float>...$parameters See Message::params()
Returns
$this

Definition at line 275 of file StatusValue.php.

Referenced by MediaWiki\Auth\AuthManager\autoCreateUser(), and fatal().

◆ fatal()

StatusValue::fatal ( $message,
$parameters )

Add an error and set OK to false, indicating that the operation as a whole was fatal.

Parameters
string | MessageSpecifier$messageMessage key or object @phpcs:ignore Generic.Files.LineLength
MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float>...$parameters See Message::params()
Returns
$this

Definition at line 293 of file StatusValue.php.

References error().

Referenced by PermissionsError\__construct(), MediaWiki\Page\RollbackPage\authorizeRollback(), MediaWiki\Api\ApiBase\dieStatus(), Article\fetchRevisionRecord(), MediaWiki\EditPage\Constraint\BrokenRedirectConstraint\getLegacyStatus(), MediaWiki\EditPage\Constraint\DefaultTextConstraint\getLegacyStatus(), MediaWiki\EditPage\Constraint\ExistingSectionEditConstraint\getLegacyStatus(), MediaWiki\EditPage\Constraint\MissingCommentConstraint\getLegacyStatus(), MediaWiki\EditPage\Constraint\NewSectionMissingSubjectConstraint\getLegacyStatus(), MediaWiki\EditPage\Constraint\ReadOnlyConstraint\getLegacyStatus(), MediaWiki\EditPage\Constraint\SelfRedirectConstraint\getLegacyStatus(), MediaWiki\EditPage\Constraint\SimpleAntiSpamConstraint\getLegacyStatus(), MediaWiki\EditPage\Constraint\UnicodeConstraint\getLegacyStatus(), MediaWiki\EditPage\Constraint\UserRateLimitConstraint\getLegacyStatus(), MediaWiki\Status\StatusFormatter\getMessage(), MediaWiki\Status\StatusFormatter\getWikiText(), MediaWiki\Installer\PostgresInstaller\openConnectionToAnyDB(), MediaWiki\Block\UnblockUser\unblock(), MediaWiki\Block\UnblockUser\unblockUnsafe(), and MediaWiki\Settings\Config\ConfigSchemaAggregator\validateConfig().

◆ getErrors()

StatusValue::getErrors ( )

Get the list of errors.

Each error is a (message:string or MessageSpecifier,params:array) map

Deprecated
since 1.43 Use ->getMessages() instead
Returns
array[]

Definition at line 173 of file StatusValue.php.

Referenced by MediaWiki\Status\Status\__get(), MediaWiki\Status\StatusFormatter\getMessage(), and MediaWiki\Status\StatusFormatter\getWikiText().

◆ getErrorsByType()

StatusValue::getErrorsByType ( $type)

Returns a list of status messages of the given type.

Each entry is a map of:

  • message: string message key or MessageSpecifier
  • params: array list of parameters
Deprecated
since 1.43 Use ->getMessages( $type ) instead
Parameters
string$type
Returns
array[]

Definition at line 337 of file StatusValue.php.

◆ getMessages()

StatusValue::getMessages ( ?string $type = null)

Returns a list of error messages, optionally only those of the given type.

If the warning() or error() method was called with a MessageSpecifier object, this method is guaranteed to return the same object.

Since
1.43
Parameters
?string$typeIf provided, only return messages of the type 'warning' or 'error'
Returns
MessageSpecifier[]

Definition at line 359 of file StatusValue.php.

References $params.

Referenced by PermissionsError\__construct(), MediaWiki\Api\ApiBase\addBlockInfoToStatus(), MediaWiki\Api\ApiErrorFormatter\addMessagesFromStatus(), MediaWiki\Api\ApiErrorFormatter\arrayFromStatus(), MediaWiki\Api\ApiErrorFormatter_BackCompat\arrayFromStatus(), MediaWiki\Api\ApiBase\dieStatus(), MediaWiki\Output\OutputPage\showPermissionStatus(), MediaWiki\Installer\CliInstaller\showStatusMessage(), and MediaWiki\Installer\WebInstaller\showStatusMessage().

◆ getStatusArray()

StatusValue::getStatusArray ( $type = false)
protected

Returns a list of status messages of the given type (or all if false)

Access: internal
Only for use by Status.
Parameters
string | bool$type
Returns
array[]

Definition at line 529 of file StatusValue.php.

◆ getValue()

◆ hasMessage()

StatusValue::hasMessage ( string $message)

Returns true if the specified message is present as a warning or error.

Any message using the same key will be found (ignoring the message parameters).

Parameters
string$messageMessage key to search for
Returns
bool

Definition at line 384 of file StatusValue.php.

References Wikimedia\Message\MessageSpecifier\getKey().

Referenced by MediaWiki\Api\ApiBase\dieStatus().

◆ hasMessagesExcept()

StatusValue::hasMessagesExcept ( string ... $messages)

Returns true if any other message than the specified ones is present as a warning or error.

Any messages using the same keys will be found (ignoring the message parameters).

Parameters
string...$messages Message keys to search for
Returns
bool

Definition at line 403 of file StatusValue.php.

◆ isGood()

◆ isOK()

◆ merge()

StatusValue::merge ( $other,
$overwriteValue = false )

Merge another status object into this one.

Parameters
StatusValue$other
bool$overwriteValueWhether to override the "value" member
Returns
$this

Definition at line 305 of file StatusValue.php.

Referenced by ScopedLock\factory(), and Wikimedia\FileBackend\FileOpBatch\runParallelBatches().

◆ newFatal()

static StatusValue::newFatal ( $message,
$parameters )
static

Factory function for fatal errors.

Parameters
string | MessageSpecifier$messageMessage key or object @phpcs:ignore Generic.Files.LineLength
MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float>...$parameters See Message::params()
Returns
static

Definition at line 92 of file StatusValue.php.

Referenced by MediaWiki\User\TempUser\TempUserCreator\create().

◆ newGood()

static StatusValue::newGood ( $value = null)
static

Factory function for good results.

Parameters
mixed | null$value
Returns
static

Definition at line 104 of file StatusValue.php.

Referenced by LocalFileDeleteBatch\execute().

◆ replaceMessage()

StatusValue::replaceMessage ( string $source,
$dest )

If the specified source message exists, replace it with the specified destination message, but keep the same parameters as in the original error.

Any message using the same key will be replaced (ignoring the message parameters).

Parameters
string$sourceMessage key to search for
MessageSpecifier | string$destReplacement message key or object
Returns
bool Return true if the replacement was done, false otherwise.

Definition at line 426 of file StatusValue.php.

References $params, $source, and Wikimedia\Message\MessageSpecifier\getKey().

Referenced by MediaWiki\Api\ApiBase\addBlockInfoToStatus(), and MediaWiki\Api\ApiBase\dieStatus().

◆ setOK()

StatusValue::setOK ( $ok)

Change operation status.

Parameters
bool$ok
Returns
$this

Definition at line 183 of file StatusValue.php.

Referenced by MediaWiki\Status\Status\__set(), and MediaWiki\Api\ApiBase\dieStatus().

◆ setResult()

◆ splitByErrorType()

StatusValue::splitByErrorType ( )

Splits this StatusValue object into two new StatusValue objects, one which contains only the error messages, and one that contains the warnings, only.

The returned array is defined as: [ 0 => object(StatusValue) # the StatusValue with error messages, only 1 => object(StatusValue) # The StatusValue with warning messages, only ]

Returns
static[]

Reimplemented in MediaWiki\Status\Status.

Definition at line 121 of file StatusValue.php.

References getValue(), and isOK().

◆ warning()

StatusValue::warning ( $message,
$parameters )

Add a new warning.

Parameters
string | MessageSpecifier$messageMessage key or object @phpcs:ignore Generic.Files.LineLength
MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float>...$parameters See Message::params()
Returns
$this

Definition at line 257 of file StatusValue.php.

Referenced by MediaWiki\Auth\AuthManager\autoCreateUser().

Member Data Documentation

◆ $errors

array [] StatusValue::$errors = []
protected
Access: internal
Only for use by Status. Use self::getErrors() (get full list), self::splitByErrorType() (get errors/warnings), or self::fatal(), self::error() or self::warning() (add error/warning).

Definition at line 66 of file StatusValue.php.

◆ $failCount

int StatusValue::$failCount = 0

Counter for batch operations.

Definition at line 78 of file StatusValue.php.

◆ $ok

bool StatusValue::$ok = true
protected
Access: internal
Only for use by Status. Use self::isOK() or self::setOK().

Definition at line 58 of file StatusValue.php.

◆ $statusData

mixed StatusValue::$statusData

arbitrary extra data about the operation

Definition at line 81 of file StatusValue.php.

◆ $success

bool [] StatusValue::$success = []

Map of (key => bool) to indicate success of each part of batch operations.

Definition at line 72 of file StatusValue.php.

◆ $successCount

int StatusValue::$successCount = 0

Counter for batch operations.

Definition at line 75 of file StatusValue.php.

◆ $value

mixed StatusValue::$value

Definition at line 69 of file StatusValue.php.

Referenced by MediaWiki\Status\Status\__set().


The documentation for this class was generated from the following file: