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