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
22 * Format errors and warnings in the old style, for backwards compatibility.
24 * @deprecated Only for backwards compatibility, do not use
27 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
28 class ApiErrorFormatter_BackCompat
extends ApiErrorFormatter
{
31 * @param ApiResult $result Into which data will be added
33 public function __construct( ApiResult
$result ) {
34 parent
::__construct( $result, Language
::factory( 'en' ), 'none', false );
37 public function getFormat() {
41 public function arrayFromStatus( StatusValue
$status, $type = 'error', $format = null ) {
42 if ( $status->isGood() ||
!$status->getErrors() ) {
47 foreach ( $status->getErrorsByType( $type ) as $error ) {
48 $msg = ApiMessage
::create( $error );
50 'message' => $msg->getKey(),
51 'params' => $msg->getParams(),
52 'code' => $msg->getApiCode(),
54 ApiResult
::setIndexedTagName( $error['params'], 'param' );
57 ApiResult
::setIndexedTagName( $result, $type );
62 protected function formatMessageInternal( $msg, $format ) {
64 'code' => $msg->getApiCode(),
65 'info' => $msg->text(),
66 ] +
$msg->getApiData();
70 * Format an exception as an array
72 * @param Exception|Throwable $exception
73 * @param array $options See parent::formatException(), plus
74 * - bc: (bool) Return only the string, not an array
75 * @return array|string
77 public function formatException( $exception, array $options = [] ) {
78 $ret = parent
::formatException( $exception, $options );
79 return empty( $options['bc'] ) ?
$ret : $ret['info'];
82 protected function addWarningOrError( $tag, $modulePath, $msg ) {
83 $value = self
::stripMarkup( $msg->text() );
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'] )
92 'code' => $msg->getApiCode(),
94 ] +
$msg->getApiData();
95 $this->result
->addValue( null, 'error', $value,
96 ApiResult
::OVERRIDE | ApiResult
::ADD_ON_TOP | ApiResult
::NO_SIZE_CHECK
);
99 if ( $modulePath === null ) {
100 $moduleName = 'unknown';
102 $i = strrpos( $modulePath, '+' );
103 $moduleName = $i === false ?
$modulePath : substr( $modulePath, $i +
1 );
106 // Don't add duplicate warnings
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" ) {
120 // If there is a warning already, append it to the existing one
121 $value = "$oldWarning\n$value";
123 $this->result
->addContentValue( $path, $tag, $value,
124 ApiResult
::OVERRIDE | ApiResult
::ADD_ON_TOP | ApiResult
::NO_SIZE_CHECK
);