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() ) {
50 return [];
51 }
52
53 $result = [];
54 foreach ( $status->getMessages( $type ) as $msg ) {
55 $msg = ApiMessage::create( $msg );
56 $error = [
57 'message' => $msg->getKey(),
58 'params' => $msg->getParams(),
59 'code' => $msg->getApiCode(),
60 'type' => $type,
61 ];
62 ApiResult::setIndexedTagName( $error['params'], 'param' );
63 $result[] = $error;
64 }
65 ApiResult::setIndexedTagName( $result, $type );
66
67 return $result;
68 }
69
70 protected function formatMessageInternal( $msg, $format ) {
71 return [
72 'code' => $msg->getApiCode(),
73 'info' => $msg->text(),
74 ] + $msg->getApiData();
75 }
76
85 public function formatException( Throwable $exception, array $options = [] ) {
86 $ret = parent::formatException( $exception, $options );
87 return empty( $options['bc'] ) ? $ret : $ret['info'];
88 }
89
90 protected function addWarningOrError( $tag, $modulePath, $msg ) {
91 $value = self::stripMarkup( $msg->text() );
92
93 if ( $tag === 'error' ) {
94 // In BC mode, only one error
95 $existingError = $this->result->getResultData( [ 'error' ] );
96 if ( !is_array( $existingError ) ||
97 !isset( $existingError['code'] ) || !isset( $existingError['info'] )
98 ) {
99 $value = [
100 'code' => $msg->getApiCode(),
101 'info' => $value,
102 ] + $msg->getApiData();
103 $this->result->addValue( null, 'error', $value,
104 ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK );
105 }
106 } else {
107 if ( $modulePath === null ) {
108 $moduleName = 'unknown';
109 } else {
110 $i = strrpos( $modulePath, '+' );
111 $moduleName = $i === false ? $modulePath : substr( $modulePath, $i + 1 );
112 }
113
114 // Don't add duplicate warnings
115 $tag .= 's';
116 $path = [ $tag, $moduleName ];
117 $oldWarning = $this->result->getResultData( [ $tag, $moduleName, $tag ] );
118 if ( $oldWarning !== null ) {
119 $warnPos = strpos( $oldWarning, $value );
120 // If $value was found in $oldWarning, check if it starts at 0 or after "\n"
121 if ( $warnPos !== false && ( $warnPos === 0 || $oldWarning[$warnPos - 1] === "\n" ) ) {
122 // Check if $value is followed by "\n" or the end of the $oldWarning
123 $warnPos += strlen( $value );
124 if ( strlen( $oldWarning ) <= $warnPos || $oldWarning[$warnPos] === "\n" ) {
125 return;
126 }
127 }
128 // If there is a warning already, append it to the existing one
129 $value = "$oldWarning\n$value";
130 }
131 $this->result->addContentValue( $path, $tag, $value,
132 ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK );
133 }
134 }
135}
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.
This class represents the result of the API operations.
Definition ApiResult.php:36
Service locator for MediaWiki core services.
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.