MediaWiki master
ApiErrorFormatter_BackCompat.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Api;
8
10use StatusValue;
11use Throwable;
12
19// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
21
25 public function __construct( ApiResult $result ) {
26 parent::__construct(
27 $result,
28 MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' ),
29 'none',
30 false
31 );
32 }
33
35 public function getFormat() {
36 return 'bc';
37 }
38
40 public function arrayFromStatus( StatusValue $status, $type = 'error', $format = null ) {
41 if ( $status->isGood() ) {
42 return [];
43 }
44
45 $result = [];
46 foreach ( $status->getMessages( $type ) as $msg ) {
47 $msg = ApiMessage::create( $msg );
48 $error = [
49 'message' => $msg->getKey(),
50 'params' => $msg->getParams(),
51 'code' => $msg->getApiCode(),
52 'type' => $type,
53 ];
54 ApiResult::setIndexedTagName( $error['params'], 'param' );
55 $result[] = $error;
56 }
58
59 return $result;
60 }
61
63 protected function formatMessageInternal( $msg, $format ) {
64 return [
65 'code' => $msg->getApiCode(),
66 'info' => $msg->text(),
67 ] + $msg->getApiData();
68 }
69
78 public function formatException( Throwable $exception, array $options = [] ) {
79 $ret = parent::formatException( $exception, $options );
80 return empty( $options['bc'] ) ? $ret : $ret['info'];
81 }
82
84 protected function addWarningOrError( $tag, $modulePath, $msg ) {
85 $value = self::stripMarkup( $msg->text() );
86
87 if ( $tag === 'error' ) {
88 // In BC mode, only one error
89 $existingError = $this->result->getResultData( [ 'error' ] );
90 if ( !is_array( $existingError ) ||
91 !isset( $existingError['code'] ) || !isset( $existingError['info'] )
92 ) {
93 $value = [
94 'code' => $msg->getApiCode(),
95 'info' => $value,
96 ] + $msg->getApiData();
97 $this->result->addValue( null, 'error', $value,
99 }
100 } else {
101 if ( $modulePath === null ) {
102 $moduleName = 'unknown';
103 } else {
104 $i = strrpos( $modulePath, '+' );
105 $moduleName = $i === false ? $modulePath : substr( $modulePath, $i + 1 );
106 }
107
108 // Don't add duplicate warnings
109 $tag .= 's';
110 $path = [ $tag, $moduleName ];
111 $oldWarning = $this->result->getResultData( [ $tag, $moduleName, $tag ] );
112 if ( $oldWarning !== null ) {
113 $warnPos = strpos( $oldWarning, $value );
114 // If $value was found in $oldWarning, check if it starts at 0 or after "\n"
115 if ( $warnPos !== false && ( $warnPos === 0 || $oldWarning[$warnPos - 1] === "\n" ) ) {
116 // Check if $value is followed by "\n" or the end of the $oldWarning
117 $warnPos += strlen( $value );
118 if ( strlen( $oldWarning ) <= $warnPos || $oldWarning[$warnPos] === "\n" ) {
119 return;
120 }
121 }
122 // If there is a warning already, append it to the existing one
123 $value = "$oldWarning\n$value";
124 }
125 $this->result->addContentValue( $path, $tag, $value,
127 }
128 }
129}
130
132class_alias( ApiErrorFormatter_BackCompat::class, 'ApiErrorFormatter_BackCompat' );
Format errors and warnings in the old style, for backwards compatibility.
getFormat()
Fetch the format for this formatter.1.32 string
addWarningOrError( $tag, $modulePath, $msg)
Actually add the warning or error to the result.
formatMessageInternal( $msg, $format)
Format a message as an array.1.29 array
arrayFromStatus(StatusValue $status, $type='error', $format=null)
Format messages from a StatusValue as an array.array
formatException(Throwable $exception, array $options=[])
Format a throwable as an array.
Formats errors and warnings for the API, and add them to the associated ApiResult.
getLanguage()
Fetch the Language for this formatter.
static stripMarkup( $text)
Turn wikitext into something resembling plaintext.
static create( $msg, $code=null, ?array $data=null)
Create an IApiMessage for the message.
This class represents the result of the API operations.
Definition ApiResult.php:34
const OVERRIDE
Override existing value in addValue(), setValue(), and similar functions.
Definition ApiResult.php:40
const ADD_ON_TOP
For addValue(), setValue() and similar functions, if the value does not exist, add it as the first el...
Definition ApiResult.php:48
const NO_SIZE_CHECK
For addValue() and similar functions, do not check size while adding a value Don't use this unless yo...
Definition ApiResult.php:57
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
getMessages(?string $type=null)
Returns a list of error messages, optionally only those of the given type.
isGood()
Returns whether the operation completed and didn't have any error or warnings.