MediaWiki master
ApiErrorFormatter_BackCompat.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Api;
22
24use StatusValue;
25use Throwable;
26
33// phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
35
39 public function __construct( ApiResult $result ) {
40 parent::__construct(
41 $result,
42 MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' ),
43 'none',
44 false
45 );
46 }
47
48 public function getFormat() {
49 return 'bc';
50 }
51
52 public function arrayFromStatus( StatusValue $status, $type = 'error', $format = null ) {
53 if ( $status->isGood() ) {
54 return [];
55 }
56
57 $result = [];
58 foreach ( $status->getMessages( $type ) as $msg ) {
59 $msg = ApiMessage::create( $msg );
60 $error = [
61 'message' => $msg->getKey(),
62 'params' => $msg->getParams(),
63 'code' => $msg->getApiCode(),
64 'type' => $type,
65 ];
66 ApiResult::setIndexedTagName( $error['params'], 'param' );
67 $result[] = $error;
68 }
70
71 return $result;
72 }
73
74 protected function formatMessageInternal( $msg, $format ) {
75 return [
76 'code' => $msg->getApiCode(),
77 'info' => $msg->text(),
78 ] + $msg->getApiData();
79 }
80
89 public function formatException( Throwable $exception, array $options = [] ) {
90 $ret = parent::formatException( $exception, $options );
91 return empty( $options['bc'] ) ? $ret : $ret['info'];
92 }
93
94 protected function addWarningOrError( $tag, $modulePath, $msg ) {
95 $value = self::stripMarkup( $msg->text() );
96
97 if ( $tag === 'error' ) {
98 // In BC mode, only one error
99 $existingError = $this->result->getResultData( [ 'error' ] );
100 if ( !is_array( $existingError ) ||
101 !isset( $existingError['code'] ) || !isset( $existingError['info'] )
102 ) {
103 $value = [
104 'code' => $msg->getApiCode(),
105 'info' => $value,
106 ] + $msg->getApiData();
107 $this->result->addValue( null, 'error', $value,
109 }
110 } else {
111 if ( $modulePath === null ) {
112 $moduleName = 'unknown';
113 } else {
114 $i = strrpos( $modulePath, '+' );
115 $moduleName = $i === false ? $modulePath : substr( $modulePath, $i + 1 );
116 }
117
118 // Don't add duplicate warnings
119 $tag .= 's';
120 $path = [ $tag, $moduleName ];
121 $oldWarning = $this->result->getResultData( [ $tag, $moduleName, $tag ] );
122 if ( $oldWarning !== null ) {
123 $warnPos = strpos( $oldWarning, $value );
124 // If $value was found in $oldWarning, check if it starts at 0 or after "\n"
125 if ( $warnPos !== false && ( $warnPos === 0 || $oldWarning[$warnPos - 1] === "\n" ) ) {
126 // Check if $value is followed by "\n" or the end of the $oldWarning
127 $warnPos += strlen( $value );
128 if ( strlen( $oldWarning ) <= $warnPos || $oldWarning[$warnPos] === "\n" ) {
129 return;
130 }
131 }
132 // If there is a warning already, append it to the existing one
133 $value = "$oldWarning\n$value";
134 }
135 $this->result->addContentValue( $path, $tag, $value,
137 }
138 }
139}
140
142class_alias( ApiErrorFormatter_BackCompat::class, 'ApiErrorFormatter_BackCompat' );
Format errors and warnings in the old style, for backwards compatibility.
getFormat()
Fetch the format for this formatter.
addWarningOrError( $tag, $modulePath, $msg)
Actually add the warning or error to the result.
formatMessageInternal( $msg, $format)
Format a message as an array.
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.
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:43
const OVERRIDE
Override existing value in addValue(), setValue(), and similar functions.
Definition ApiResult.php:49
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:57
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:66
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.