| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Api; |
| 22 |
|
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use StatusValue; |
| 25 |
use Throwable; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class ApiErrorFormatter_BackCompat extends ApiErrorFormatter { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct( ApiResult $result ) { |
| 40 |
parent::__construct( |
| 41 |
$result, |
| 42 |
MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' ), |
| 43 |
'none', |
| 44 |
false |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
public function getFormat() { |
| 49 |
return 'bc'; |
| 50 |
} |
| 51 |
|
| 52 |
public function arrayFromStatus( StatusValue $status, $type = 'error', $format = null ) { |
| 53 |
if ( $status->isGood() ) { |
| 54 |
return []; |
| 55 |
} |
| 56 |
|
| 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, |
| 65 |
]; |
| 66 |
ApiResult::setIndexedTagName( $error['params'], 'param' ); |
| 67 |
$result[] = $error; |
| 68 |
} |
| 69 |
ApiResult::setIndexedTagName( $result, $type ); |
| 70 |
|
| 71 |
return $result; |
| 72 |
} |
| 73 |
|
| 74 |
protected function formatMessageInternal( $msg, $format ) { |
| 75 |
return [ |
| 76 |
'code' => $msg->getApiCode(), |
| 77 |
'info' => $msg->text(), |
| 78 |
] + $msg->getApiData(); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function formatException( Throwable $exception, array $options = [] ) { |
| 90 |
$ret = parent::formatException( $exception, $options ); |
| 91 |
return empty( $options['bc'] ) ? $ret : $ret['info']; |
| 92 |
} |
| 93 |
|
| 94 |
protected function addWarningOrError( $tag, $modulePath, $msg ) { |
| 95 |
$value = self::stripMarkup( $msg->text() ); |
| 96 |
|
| 97 |
if ( $tag === 'error' ) { |
| 98 |
|
| 99 |
$existingError = $this->result->getResultData( [ 'error' ] ); |
| 100 |
if ( !is_array( $existingError ) || |
| 101 |
!isset( $existingError['code'] ) || !isset( $existingError['info'] ) |
| 102 |
) { |
| 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 ); |
| 109 |
} |
| 110 |
} else { |
| 111 |
if ( $modulePath === null ) { |
| 112 |
$moduleName = 'unknown'; |
| 113 |
} else { |
| 114 |
$i = strrpos( $modulePath, '+' ); |
| 115 |
$moduleName = $i === false ? $modulePath : substr( $modulePath, $i + 1 ); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 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 |
|
| 125 |
if ( $warnPos !== false && ( $warnPos === 0 || $oldWarning[$warnPos - 1] === "\n" ) ) { |
| 126 |
|
| 127 |
$warnPos += strlen( $value ); |
| 128 |
if ( strlen( $oldWarning ) <= $warnPos || $oldWarning[$warnPos] === "\n" ) { |
| 129 |
return; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
$value = "$oldWarning\n$value"; |
| 134 |
} |
| 135 |
$this->result->addContentValue( $path, $tag, $value, |
| 136 |
ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP | ApiResult::NO_SIZE_CHECK ); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
class_alias( ApiErrorFormatter_BackCompat::class, 'ApiErrorFormatter_BackCompat' ); |