MediaWiki  master
ApiErrorFormatter Class Reference

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

Inheritance diagram for ApiErrorFormatter:
Collaboration diagram for 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. More...
 
 addMessagesFromStatus ( $modulePath, StatusValue $status, $types=[ 'warning', 'error'], array $filter=[])
 Add warnings and errors from a StatusValue object to the result. More...
 
 addWarning ( $modulePath, $msg, $code=null, $data=null)
 Add a warning to the result. More...
 
 arrayFromStatus (StatusValue $status, $type='error', $format=null)
 Format messages from a StatusValue as an array. More...
 
 formatException (Throwable $exception, array $options=[])
 Format a throwable as an array. More...
 
 formatMessage ( $msg, $format=null)
 Format a message as an array. More...
 
 getContextTitle ()
 Get the page used for rendering error messages, e.g. More...
 
 getFormat ()
 Fetch the format for this formatter. More...
 
 getLanguage ()
 Fetch the Language for this formatter. More...
 
 getMessageFromException (Throwable $exception, array $options=[])
 Get an ApiMessage from a throwable. More...
 
 newWithFormat ( $format)
 Return a formatter like this one but with a different format. More...
 
 setContextTitle (PageReference $title)
 Set the page used for rendering error messages, e.g. More...
 

Static Public Member Functions

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

Protected Member Functions

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

Protected Attributes

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

Private Member Functions

 formatRawMessage (MessageSpecifier $msg)
 Format a Message object for raw format. More...
 

Private Attributes

PageReference null $title = null
 page used for rendering error messages, or null to use the dummy title More...
 

Static Private Attributes

static PageReference $dummyTitle = null
 Dummy title to silence warnings from MessageCache::parse() More...
 

Detailed Description

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

Since
1.25

Definition at line 34 of file ApiErrorFormatter.php.

Constructor & Destructor Documentation

◆ __construct()

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 60 of file ApiErrorFormatter.php.

References $format, $lang, $result, and $useDB.

Member Function Documentation

◆ addError()

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

Add an error to the result.

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

Definition at line 170 of file ApiErrorFormatter.php.

References addWarningOrError(), ApiMessage\create(), and getContextTitle().

◆ addMessagesFromStatus()

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 185 of file ApiErrorFormatter.php.

References addWarningOrError(), ApiMessage\create(), getContextTitle(), StatusValue\getErrors(), and StatusValue\isGood().

◆ addWarning()

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

Add a warning to the result.

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

Definition at line 155 of file ApiErrorFormatter.php.

References addWarningOrError(), ApiMessage\create(), and getContextTitle().

◆ addWarningOrError()

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 ApiErrorFormatter_BackCompat.

Definition at line 401 of file ApiErrorFormatter.php.

References $path, ApiResult\ADD_ON_TOP, formatMessageInternal(), and ApiResult\NO_SIZE_CHECK.

Referenced by addError(), addMessagesFromStatus(), and addWarning().

◆ arrayFromStatus()

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 ApiErrorFormatter_BackCompat.

Definition at line 293 of file ApiErrorFormatter.php.

References $format, $result, $type, StatusValue\getErrors(), ApiResult\getResultData(), and StatusValue\isGood().

◆ formatException()

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 ApiErrorFormatter_BackCompat.

Definition at line 264 of file ApiErrorFormatter.php.

References formatMessage(), and getMessageFromException().

◆ formatMessage()

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 278 of file ApiErrorFormatter.php.

References $format, ApiMessage\create(), formatMessageInternal(), and getContextTitle().

Referenced by formatException().

◆ formatMessageInternal()

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 ApiErrorFormatter_BackCompat.

Definition at line 355 of file ApiErrorFormatter.php.

References $format, formatRawMessage(), ApiResult\META_CONTENT, ApiResult\META_TYPE, and stripMarkup().

Referenced by addWarningOrError(), and formatMessage().

◆ formatRawMessage()

ApiErrorFormatter::formatRawMessage ( MessageSpecifier  $msg)
private

Format a Message object for raw format.

Parameters
MessageSpecifier$msg
Returns
array

Definition at line 332 of file ApiErrorFormatter.php.

References MessageSpecifier\getKey(), MessageSpecifier\getParams(), and ApiResult\setIndexedTagName().

Referenced by formatMessageInternal().

◆ getContextTitle()

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 135 of file ApiErrorFormatter.php.

References getDummyTitle().

Referenced by addError(), addMessagesFromStatus(), addWarning(), formatMessage(), and getMessageFromException().

◆ getDummyTitle()

ApiErrorFormatter::getDummyTitle ( )
protected

Fetch a dummy title to set on Messages.

Returns
PageReference

Definition at line 120 of file ApiErrorFormatter.php.

References $dummyTitle, and NS_SPECIAL.

Referenced by getContextTitle().

◆ getFormat()

ApiErrorFormatter::getFormat ( )

Fetch the format for this formatter.

Since
1.32
Returns
string

Reimplemented in ApiErrorFormatter_BackCompat.

Definition at line 103 of file ApiErrorFormatter.php.

References $format.

◆ getLanguage()

ApiErrorFormatter::getLanguage ( )

Fetch the Language for this formatter.

Since
1.29
Returns
Language

Definition at line 112 of file ApiErrorFormatter.php.

References $lang.

Referenced by ApiErrorFormatter_BackCompat\__construct().

◆ getMessageFromException()

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 227 of file ApiErrorFormatter.php.

References ApiMessage\create(), getContextTitle(), Message\newFromSpecifier(), and wfEscapeWikiText().

Referenced by formatException().

◆ isValidApiCode()

static 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 79 of file ApiErrorFormatter.php.

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

◆ newWithFormat()

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 94 of file ApiErrorFormatter.php.

References $format, $lang, $result, and $useDB.

◆ setContextTitle()

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 144 of file ApiErrorFormatter.php.

References $title.

◆ stripMarkup()

static ApiErrorFormatter::stripMarkup (   $text)
static

Turn wikitext into something resembling plaintext.

Since
1.29
Parameters
string$text
Returns
string

Definition at line 317 of file ApiErrorFormatter.php.

References Sanitizer\stripAllTags().

Referenced by ApiUsageException\__construct(), ApiUsageException\__toString(), ApiErrorFormatter_BackCompat\addWarningOrError(), ApiLogin\formatMessage(), and formatMessageInternal().

Member Data Documentation

◆ $dummyTitle

PageReference ApiErrorFormatter::$dummyTitle = null
staticprivate

Dummy title to silence warnings from MessageCache::parse()

Definition at line 36 of file ApiErrorFormatter.php.

Referenced by getDummyTitle().

◆ $format

ApiErrorFormatter::$format = 'none'
protected

◆ $lang

Language ApiErrorFormatter::$lang
protected

Definition at line 42 of file ApiErrorFormatter.php.

Referenced by __construct(), getLanguage(), and newWithFormat().

◆ $result

◆ $title

PageReference null ApiErrorFormatter::$title = null
private

page used for rendering error messages, or null to use the dummy title

Definition at line 44 of file ApiErrorFormatter.php.

Referenced by setContextTitle().

◆ $useDB

ApiErrorFormatter::$useDB = false
protected

Definition at line 45 of file ApiErrorFormatter.php.

Referenced by __construct(), and newWithFormat().


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