MediaWiki REL1_34
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}
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.
formatException( $exception, array $options=[])
Format an exception as an array.
addWarningOrError( $tag, $modulePath, $msg)
Actually add the warning or error to the result.
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.
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: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.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
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.