Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiErrorFormatter_BackCompat.php
blob16562493365194603c27da5b8b56897febe8012e
1 <?php
2 /**
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
18 * @file
21 namespace MediaWiki\Api;
23 use MediaWiki\MediaWikiServices;
24 use StatusValue;
25 use Throwable;
27 /**
28 * Format errors and warnings in the old style, for backwards compatibility.
29 * @since 1.25
30 * @deprecated since 1.25; only for backwards compatibility, do not use
31 * @ingroup API
33 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
34 class ApiErrorFormatter_BackCompat extends ApiErrorFormatter {
36 /**
37 * @param ApiResult $result Into which data will be added
39 public function __construct( ApiResult $result ) {
40 parent::__construct(
41 $result,
42 MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' ),
43 'none',
44 false
48 public function getFormat() {
49 return 'bc';
52 public function arrayFromStatus( StatusValue $status, $type = 'error', $format = null ) {
53 if ( $status->isGood() ) {
54 return [];
57 $result = [];
58 foreach ( $status->getMessages( $type ) as $msg ) {
59 $msg = ApiMessage::create( $msg );
60 $error = [
61 'message' => $msg->getKey(),
62 'params' => $msg->getParams(),
63 'code' => $msg->getApiCode(),
64 'type' => $type,
66 ApiResult::setIndexedTagName( $error['params'], 'param' );
67 $result[] = $error;
69 ApiResult::setIndexedTagName( $result, $type );
71 return $result;
74 protected function formatMessageInternal( $msg, $format ) {
75 return [
76 'code' => $msg->getApiCode(),
77 'info' => $msg->text(),
78 ] + $msg->getApiData();
81 /**
82 * Format a throwable as an array
83 * @since 1.29
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'] )
103 $value = [
104 'code' => $msg->getApiCode(),
105 'info' => $value,
106 ] + $msg->getApiData();
107 $this->result->addValue( null, 'error', $value,
108 ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK );
110 } else {
111 if ( $modulePath === null ) {
112 $moduleName = 'unknown';
113 } else {
114 $i = strrpos( $modulePath, '+' );
115 $moduleName = $i === false ? $modulePath : substr( $modulePath, $i + 1 );
118 // Don't add duplicate warnings
119 $tag .= 's';
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" ) {
129 return;
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' );