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