3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Api
;
23 use MediaWiki\MediaWikiServices
;
28 * Format errors and warnings in the old style, for backwards compatibility.
30 * @deprecated since 1.25; only for backwards compatibility, do not use
33 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
34 class ApiErrorFormatter_BackCompat
extends ApiErrorFormatter
{
37 * @param ApiResult $result Into which data will be added
39 public function __construct( ApiResult
$result ) {
42 MediaWikiServices
::getInstance()->getLanguageFactory()->getLanguage( 'en' ),
48 public function getFormat() {
52 public function arrayFromStatus( StatusValue
$status, $type = 'error', $format = null ) {
53 if ( $status->isGood() ) {
58 foreach ( $status->getMessages( $type ) as $msg ) {
59 $msg = ApiMessage
::create( $msg );
61 'message' => $msg->getKey(),
62 'params' => $msg->getParams(),
63 'code' => $msg->getApiCode(),
66 ApiResult
::setIndexedTagName( $error['params'], 'param' );
69 ApiResult
::setIndexedTagName( $result, $type );
74 protected function formatMessageInternal( $msg, $format ) {
76 'code' => $msg->getApiCode(),
77 'info' => $msg->text(),
78 ] +
$msg->getApiData();
82 * Format a throwable as an array
84 * @param Throwable $exception
85 * @param array $options See parent::formatException(), plus
86 * - bc: (bool) Return only the string, not an array
87 * @return array|string
89 public function formatException( Throwable
$exception, array $options = [] ) {
90 $ret = parent
::formatException( $exception, $options );
91 return empty( $options['bc'] ) ?
$ret : $ret['info'];
94 protected function addWarningOrError( $tag, $modulePath, $msg ) {
95 $value = self
::stripMarkup( $msg->text() );
97 if ( $tag === 'error' ) {
98 // In BC mode, only one error
99 $existingError = $this->result
->getResultData( [ 'error' ] );
100 if ( !is_array( $existingError ) ||
101 !isset( $existingError['code'] ) ||
!isset( $existingError['info'] )
104 'code' => $msg->getApiCode(),
106 ] +
$msg->getApiData();
107 $this->result
->addValue( null, 'error', $value,
108 ApiResult
::OVERRIDE | ApiResult
::ADD_ON_TOP | ApiResult
::NO_SIZE_CHECK
);
111 if ( $modulePath === null ) {
112 $moduleName = 'unknown';
114 $i = strrpos( $modulePath, '+' );
115 $moduleName = $i === false ?
$modulePath : substr( $modulePath, $i +
1 );
118 // Don't add duplicate warnings
120 $path = [ $tag, $moduleName ];
121 $oldWarning = $this->result
->getResultData( [ $tag, $moduleName, $tag ] );
122 if ( $oldWarning !== null ) {
123 $warnPos = strpos( $oldWarning, $value );
124 // If $value was found in $oldWarning, check if it starts at 0 or after "\n"
125 if ( $warnPos !== false && ( $warnPos === 0 ||
$oldWarning[$warnPos - 1] === "\n" ) ) {
126 // Check if $value is followed by "\n" or the end of the $oldWarning
127 $warnPos +
= strlen( $value );
128 if ( strlen( $oldWarning ) <= $warnPos ||
$oldWarning[$warnPos] === "\n" ) {
132 // If there is a warning already, append it to the existing one
133 $value = "$oldWarning\n$value";
135 $this->result
->addContentValue( $path, $tag, $value,
136 ApiResult
::OVERRIDE | ApiResult
::ADD_ON_TOP | ApiResult
::NO_SIZE_CHECK
);
141 /** @deprecated class alias since 1.43 */
142 class_alias( ApiErrorFormatter_BackCompat
::class, 'ApiErrorFormatter_BackCompat' );