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 }
57 ApiResult::setIndexedTagName( $result, $type );
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,
96 ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK );
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,
124 ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK );
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
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.