MediaWiki master
MediaWiki\Api\ApiErrorFormatter Class Reference

Formats errors and warnings for the API, and add them to the associated ApiResult. More...

Inherited by MediaWiki\Api\ApiErrorFormatter_BackCompat.

Collaboration diagram for MediaWiki\Api\ApiErrorFormatter:

Public Member Functions

 __construct (ApiResult $result, Language $lang, $format, $useDB=false)
 
 addError ( $modulePath, $msg, $code=null, $data=null)
 Add an error to the result.
 
 addMessagesFromStatus ( $modulePath, StatusValue $status, $types=[ 'warning', 'error'], array $filter=[])
 Add warnings and errors from a StatusValue object to the result.
 
 addWarning ( $modulePath, $msg, $code=null, $data=null)
 Add a warning to the result.
 
 arrayFromStatus (StatusValue $status, $type='error', $format=null)
 Format messages from a StatusValue as an array.
 
 formatException (Throwable $exception, array $options=[])
 Format a throwable as an array.
 
 formatMessage ( $msg, $format=null)
 Format a message as an array.
 
 getContextTitle ()
 Get the page used for rendering error messages, e.g.
 
 getFormat ()
 Fetch the format for this formatter.
 
 getLanguage ()
 Fetch the Language for this formatter.
 
 getMessageFromException (Throwable $exception, array $options=[])
 Get an ApiMessage from a throwable.
 
 newWithFormat ( $format)
 Return a formatter like this one but with a different format.
 
 setContextTitle (PageReference $title)
 Set the page used for rendering error messages, e.g.
 

Static Public Member Functions

static isValidApiCode ( $code)
 Test whether a code is a valid API error code.
 
static stripMarkup ( $text)
 Turn wikitext into something resembling plaintext.
 

Protected Member Functions

 addWarningOrError ( $tag, $modulePath, $msg)
 Actually add the warning or error to the result.
 
 formatMessageInternal ( $msg, $format)
 Format a message as an array.
 
 getDummyTitle ()
 Fetch a dummy title to set on Messages.
 

Protected Attributes

string $format = 'none'
 
Language $lang
 
ApiResult $result
 
bool $useDB = false
 

Detailed Description

Formats errors and warnings for the API, and add them to the associated ApiResult.

Since
1.25

Definition at line 44 of file ApiErrorFormatter.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Api\ApiErrorFormatter::__construct ( ApiResult $result,
Language $lang,
$format,
$useDB = false )
Parameters
ApiResult$resultInto which data will be added
Language$langUsed for i18n
string$format
  • plaintext: Error message as something vaguely like plaintext (it's basically wikitext with HTML tags stripped and entities decoded)
  • wikitext: Error message as wikitext
  • html: Error message as HTML
  • raw: Raw message key and parameters, no human-readable text
  • none: Code and data only, no human-readable text
bool$useDBWhether to use local translations for errors and warnings.

Definition at line 72 of file ApiErrorFormatter.php.

References MediaWiki\Api\ApiErrorFormatter\$format, MediaWiki\Api\ApiErrorFormatter\$lang, MediaWiki\Api\ApiErrorFormatter\$result, and MediaWiki\Api\ApiErrorFormatter\$useDB.

Member Function Documentation

◆ addError()

MediaWiki\Api\ApiErrorFormatter::addError ( $modulePath,
$msg,
$code = null,
$data = null )

Add an error to the result.

Parameters
string | null$modulePath
MessageSpecifier | array | string$msgWarning message. See ApiMessage::create().
string | null$codeSee ApiMessage::create().
array | null$dataSee ApiMessage::create().

Definition at line 181 of file ApiErrorFormatter.php.

◆ addMessagesFromStatus()

MediaWiki\Api\ApiErrorFormatter::addMessagesFromStatus ( $modulePath,
StatusValue $status,
$types = [ 'warning', 'error' ],
array $filter = [] )

Add warnings and errors from a StatusValue object to the result.

Parameters
string | null$modulePath
StatusValue$status
string[] | string$types'warning' and/or 'error'
string[]$filterMessages to filter out (since 1.33)

Definition at line 196 of file ApiErrorFormatter.php.

References StatusValue\getMessages(), and StatusValue\isGood().

Referenced by MediaWiki\Api\ApiErrorFormatter\arrayFromStatus().

◆ addWarning()

MediaWiki\Api\ApiErrorFormatter::addWarning ( $modulePath,
$msg,
$code = null,
$data = null )

Add a warning to the result.

Parameters
string | null$modulePath
MessageSpecifier | array | string$msgWarning message. See ApiMessage::create().
string | null$codeSee ApiMessage::create().
array | null$dataSee ApiMessage::create().

Definition at line 166 of file ApiErrorFormatter.php.

◆ addWarningOrError()

MediaWiki\Api\ApiErrorFormatter::addWarningOrError ( $tag,
$modulePath,
$msg )
protected

Actually add the warning or error to the result.

Parameters
string$tag'warning' or 'error'
string | null$modulePath
ApiMessage | ApiRawMessage$msg

Reimplemented in MediaWiki\Api\ApiErrorFormatter_BackCompat.

Definition at line 401 of file ApiErrorFormatter.php.

References $path.

◆ arrayFromStatus()

MediaWiki\Api\ApiErrorFormatter::arrayFromStatus ( StatusValue $status,
$type = 'error',
$format = null )

Format messages from a StatusValue as an array.

Parameters
StatusValue$status
string$type'warning' or 'error'
string | null$format
Returns
array

Reimplemented in MediaWiki\Api\ApiErrorFormatter_BackCompat.

Definition at line 295 of file ApiErrorFormatter.php.

References MediaWiki\Api\ApiErrorFormatter\addMessagesFromStatus(), StatusValue\getMessages(), and StatusValue\isGood().

◆ formatException()

MediaWiki\Api\ApiErrorFormatter::formatException ( Throwable $exception,
array $options = [] )

Format a throwable as an array.

Since
1.29
Parameters
Throwable$exception
array$optionsSee self::getMessageFromException(), plus
  • format: (string) Format override
Returns
array

Reimplemented in MediaWiki\Api\ApiErrorFormatter_BackCompat.

Definition at line 266 of file ApiErrorFormatter.php.

◆ formatMessage()

MediaWiki\Api\ApiErrorFormatter::formatMessage ( $msg,
$format = null )

Format a message as an array.

Parameters
Message | array | string$msgMessage. See ApiMessage::create().
string | null$format
Returns
array

Definition at line 280 of file ApiErrorFormatter.php.

◆ formatMessageInternal()

MediaWiki\Api\ApiErrorFormatter::formatMessageInternal ( $msg,
$format )
protected

Format a message as an array.

Since
1.29
Parameters
ApiMessage | ApiRawMessage$msg
string | null$format
Returns
array

Reimplemented in MediaWiki\Api\ApiErrorFormatter_BackCompat.

Definition at line 355 of file ApiErrorFormatter.php.

◆ getContextTitle()

MediaWiki\Api\ApiErrorFormatter::getContextTitle ( )

Get the page used for rendering error messages, e.g.

for wikitext magic words like {{PAGENAME}}

Since
1.37
Returns
PageReference

Definition at line 146 of file ApiErrorFormatter.php.

◆ getDummyTitle()

MediaWiki\Api\ApiErrorFormatter::getDummyTitle ( )
protected

Fetch a dummy title to set on Messages.

Definition at line 131 of file ApiErrorFormatter.php.

References MediaWiki\Page\PageReferenceValue\localReference(), and NS_SPECIAL.

◆ getFormat()

MediaWiki\Api\ApiErrorFormatter::getFormat ( )

Fetch the format for this formatter.

Since
1.32
Returns
string

Reimplemented in MediaWiki\Api\ApiErrorFormatter_BackCompat.

Definition at line 115 of file ApiErrorFormatter.php.

References MediaWiki\Api\ApiErrorFormatter\$format.

◆ getLanguage()

MediaWiki\Api\ApiErrorFormatter::getLanguage ( )

Fetch the Language for this formatter.

Since
1.29
Returns
Language

Definition at line 124 of file ApiErrorFormatter.php.

References MediaWiki\Api\ApiErrorFormatter\$lang.

Referenced by MediaWiki\Api\ApiErrorFormatter_BackCompat\__construct().

◆ getMessageFromException()

MediaWiki\Api\ApiErrorFormatter::getMessageFromException ( Throwable $exception,
array $options = [] )

Get an ApiMessage from a throwable.

Since
1.29
Parameters
Throwable$exception
array$options
  • wrap: (string|array|MessageSpecifier) Used to wrap the throwable's message if it's not an ILocalizedException. The throwable's message will be added as the final parameter.
  • code: (string) Default code
  • data: (array) Default extra data
Returns
IApiMessage

Definition at line 229 of file ApiErrorFormatter.php.

References $params, and wfEscapeWikiText().

◆ isValidApiCode()

static MediaWiki\Api\ApiErrorFormatter::isValidApiCode ( $code)
static

Test whether a code is a valid API error code.

A valid code contains only ASCII letters, numbers, underscore, and hyphen and is not the empty string.

For backwards compatibility, any code beginning 'internal_api_error_' is also allowed.

Parameters
string$code
Returns
bool

Definition at line 91 of file ApiErrorFormatter.php.

Referenced by MediaWiki\Api\setApiCode(), and MediaWiki\Api\ApiMain\substituteResultWithError().

◆ newWithFormat()

MediaWiki\Api\ApiErrorFormatter::newWithFormat ( $format)

Return a formatter like this one but with a different format.

Since
1.32
Parameters
string$formatNew format.
Returns
ApiErrorFormatter

Definition at line 106 of file ApiErrorFormatter.php.

References MediaWiki\Api\ApiErrorFormatter\$format, MediaWiki\Api\ApiErrorFormatter\$lang, MediaWiki\Api\ApiErrorFormatter\$result, and MediaWiki\Api\ApiErrorFormatter\$useDB.

◆ setContextTitle()

MediaWiki\Api\ApiErrorFormatter::setContextTitle ( PageReference $title)

Set the page used for rendering error messages, e.g.

for wikitext magic words like {{PAGENAME}}

Since
1.37
Parameters
PageReference$title

Definition at line 155 of file ApiErrorFormatter.php.

◆ stripMarkup()

static MediaWiki\Api\ApiErrorFormatter::stripMarkup ( $text)
static

Turn wikitext into something resembling plaintext.

Since
1.29
Parameters
string$text
Returns
string

Definition at line 319 of file ApiErrorFormatter.php.

Referenced by MediaWiki\Api\ApiUsageException\__construct(), MediaWiki\Api\ApiUsageException\__toString(), and MediaWiki\Api\ApiErrorFormatter_BackCompat\addWarningOrError().

Member Data Documentation

◆ $format

string MediaWiki\Api\ApiErrorFormatter::$format = 'none'
protected

◆ $lang

◆ $result

◆ $useDB

bool MediaWiki\Api\ApiErrorFormatter::$useDB = false
protected

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