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