| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Api; |
| 25 |
|
| 26 |
use ILocalizedException; |
| 27 |
use MediaWiki\Language\Language; |
| 28 |
use MediaWiki\Language\RawMessage; |
| 29 |
use MediaWiki\Message\Message; |
| 30 |
use MediaWiki\Page\PageReference; |
| 31 |
use MediaWiki\Page\PageReferenceValue; |
| 32 |
use MediaWiki\Parser\Sanitizer; |
| 33 |
use StatusValue; |
| 34 |
use Throwable; |
| 35 |
use Wikimedia\Message\MessageSpecifier; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class ApiErrorFormatter { |
| 45 |
|
| 46 |
private static $dummyTitle = null; |
| 47 |
|
| 48 |
|
| 49 |
protected $result; |
| 50 |
|
| 51 |
|
| 52 |
protected $lang; |
| 53 |
|
| 54 |
private $title = null; |
| 55 |
|
| 56 |
protected $useDB = false; |
| 57 |
|
| 58 |
protected $format = 'none'; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function __construct( ApiResult $result, Language $lang, $format, $useDB = false ) { |
| 73 |
$this->result = $result; |
| 74 |
$this->lang = $lang; |
| 75 |
$this->useDB = $useDB; |
| 76 |
$this->format = $format; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public static function isValidApiCode( $code ) { |
| 92 |
return is_string( $code ) && ( |
| 93 |
preg_match( '/^[a-zA-Z0-9_-]+$/', $code ) || |
| 94 |
|
| 95 |
preg_match( '/^internal_api_error_[^\0\r\n]+$/', $code ) |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function newWithFormat( $format ) { |
| 107 |
return new self( $this->result, $this->lang, $format, $this->useDB ); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function getFormat() { |
| 116 |
return $this->format; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function getLanguage() { |
| 125 |
return $this->lang; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
protected function getDummyTitle(): PageReference { |
| 133 |
if ( self::$dummyTitle === null ) { |
| 134 |
self::$dummyTitle = PageReferenceValue::localReference( |
| 135 |
NS_SPECIAL, |
| 136 |
'Badtitle/' . __METHOD__ |
| 137 |
); |
| 138 |
} |
| 139 |
return self::$dummyTitle; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function getContextTitle(): PageReference { |
| 148 |
return $this->title ?: $this->getDummyTitle(); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function setContextTitle( PageReference $title ) { |
| 157 |
$this->title = $title; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public function addWarning( $modulePath, $msg, $code = null, $data = null ) { |
| 168 |
$msg = ApiMessage::create( $msg, $code, $data ) |
| 169 |
->inLanguage( $this->lang ) |
| 170 |
->page( $this->getContextTitle() ) |
| 171 |
->useDatabase( $this->useDB ); |
| 172 |
$this->addWarningOrError( 'warning', $modulePath, $msg ); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function addError( $modulePath, $msg, $code = null, $data = null ) { |
| 183 |
$msg = ApiMessage::create( $msg, $code, $data ) |
| 184 |
->inLanguage( $this->lang ) |
| 185 |
->page( $this->getContextTitle() ) |
| 186 |
->useDatabase( $this->useDB ); |
| 187 |
$this->addWarningOrError( 'error', $modulePath, $msg ); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public function addMessagesFromStatus( |
| 198 |
$modulePath, StatusValue $status, $types = [ 'warning', 'error' ], array $filter = [] |
| 199 |
) { |
| 200 |
if ( $status->isGood() ) { |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
$types = array_unique( (array)$types ); |
| 205 |
foreach ( $types as $type ) { |
| 206 |
foreach ( $status->getMessages( $type ) as $msg ) { |
| 207 |
$msg = ApiMessage::create( $msg ) |
| 208 |
->inLanguage( $this->lang ) |
| 209 |
->page( $this->getContextTitle() ) |
| 210 |
->useDatabase( $this->useDB ); |
| 211 |
if ( !in_array( $msg->getKey(), $filter, true ) ) { |
| 212 |
$this->addWarningOrError( $type, $modulePath, $msg ); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public function getMessageFromException( Throwable $exception, array $options = [] ) { |
| 231 |
$options += [ 'code' => null, 'data' => [] ]; |
| 232 |
|
| 233 |
if ( $exception instanceof ILocalizedException ) { |
| 234 |
$msg = $exception->getMessageObject(); |
| 235 |
$params = []; |
| 236 |
} elseif ( $exception instanceof MessageSpecifier ) { |
| 237 |
$msg = Message::newFromSpecifier( $exception ); |
| 238 |
$params = []; |
| 239 |
} else { |
| 240 |
if ( isset( $options['wrap'] ) ) { |
| 241 |
$msg = $options['wrap']; |
| 242 |
} else { |
| 243 |
$msg = new RawMessage( '$1' ); |
| 244 |
if ( !isset( $options['code'] ) ) { |
| 245 |
$class = preg_replace( '#^Wikimedia\\\\Rdbms\\\\#', '', get_class( $exception ) ); |
| 246 |
$options['code'] = 'internal_api_error_' . $class; |
| 247 |
$options['data']['errorclass'] = get_class( $exception ); |
| 248 |
} |
| 249 |
} |
| 250 |
$params = [ wfEscapeWikiText( $exception->getMessage() ) ]; |
| 251 |
} |
| 252 |
return ApiMessage::create( $msg, $options['code'], $options['data'] ) |
| 253 |
->params( $params ) |
| 254 |
->inLanguage( $this->lang ) |
| 255 |
->page( $this->getContextTitle() ) |
| 256 |
->useDatabase( $this->useDB ); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function formatException( Throwable $exception, array $options = [] ) { |
| 268 |
return $this->formatMessage( |
| 269 |
|
| 270 |
$this->getMessageFromException( $exception, $options ), |
| 271 |
$options['format'] ?? null |
| 272 |
); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public function formatMessage( $msg, $format = null ) { |
| 282 |
$msg = ApiMessage::create( $msg ) |
| 283 |
->inLanguage( $this->lang ) |
| 284 |
->page( $this->getContextTitle() ) |
| 285 |
->useDatabase( $this->useDB ); |
| 286 |
return $this->formatMessageInternal( $msg, $format ?: $this->format ); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public function arrayFromStatus( StatusValue $status, $type = 'error', $format = null ) { |
| 297 |
if ( $status->isGood() || !$status->getMessages() ) { |
| 298 |
return []; |
| 299 |
} |
| 300 |
|
| 301 |
$result = new ApiResult( 1_000_000 ); |
| 302 |
$formatter = new ApiErrorFormatter( |
| 303 |
$result, $this->lang, $format ?: $this->format, $this->useDB |
| 304 |
); |
| 305 |
$formatter->addMessagesFromStatus( null, $status, [ $type ] ); |
| 306 |
switch ( $type ) { |
| 307 |
case 'error': |
| 308 |
return (array)$result->getResultData( [ 'errors' ] ); |
| 309 |
case 'warning': |
| 310 |
return (array)$result->getResultData( [ 'warnings' ] ); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public static function stripMarkup( $text ) { |
| 321 |
|
| 322 |
$ret = preg_replace( '!</?(var|kbd|samp|code)>!', '"', $text ); |
| 323 |
|
| 324 |
|
| 325 |
return Sanitizer::stripAllTags( $ret ); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
private function formatRawMessage( MessageSpecifier $msg ) { |
| 334 |
$ret = [ |
| 335 |
'key' => $msg->getKey(), |
| 336 |
'params' => $msg->getParams(), |
| 337 |
]; |
| 338 |
ApiResult::setIndexedTagName( $ret['params'], 'param' ); |
| 339 |
|
| 340 |
|
| 341 |
foreach ( $ret['params'] as $i => $param ) { |
| 342 |
if ( $param instanceof MessageSpecifier ) { |
| 343 |
$ret['params'][$i] = [ 'message' => $this->formatRawMessage( $param ) ]; |
| 344 |
} |
| 345 |
} |
| 346 |
return $ret; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
protected function formatMessageInternal( $msg, $format ) { |
| 357 |
$value = [ 'code' => $msg->getApiCode() ]; |
| 358 |
switch ( $format ) { |
| 359 |
case 'plaintext': |
| 360 |
$value += [ |
| 361 |
'text' => self::stripMarkup( $msg->text() ), |
| 362 |
ApiResult::META_CONTENT => 'text', |
| 363 |
]; |
| 364 |
break; |
| 365 |
|
| 366 |
case 'wikitext': |
| 367 |
$value += [ |
| 368 |
'text' => $msg->text(), |
| 369 |
ApiResult::META_CONTENT => 'text', |
| 370 |
]; |
| 371 |
break; |
| 372 |
|
| 373 |
case 'html': |
| 374 |
$value += [ |
| 375 |
'html' => $msg->parse(), |
| 376 |
ApiResult::META_CONTENT => 'html', |
| 377 |
]; |
| 378 |
break; |
| 379 |
|
| 380 |
case 'raw': |
| 381 |
$value += $this->formatRawMessage( $msg ); |
| 382 |
break; |
| 383 |
|
| 384 |
case 'none': |
| 385 |
break; |
| 386 |
} |
| 387 |
$data = $msg->getApiData(); |
| 388 |
if ( $data ) { |
| 389 |
$value['data'] = $msg->getApiData() + [ |
| 390 |
ApiResult::META_TYPE => 'assoc', |
| 391 |
]; |
| 392 |
} |
| 393 |
return $value; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
protected function addWarningOrError( $tag, $modulePath, $msg ) { |
| 403 |
$value = $this->formatMessageInternal( $msg, $this->format ); |
| 404 |
if ( $modulePath !== null ) { |
| 405 |
$value += [ 'module' => $modulePath ]; |
| 406 |
} |
| 407 |
|
| 408 |
$path = [ $tag . 's' ]; |
| 409 |
$existing = $this->result->getResultData( $path ); |
| 410 |
if ( $existing === null || !in_array( $value, $existing ) ) { |
| 411 |
$flags = ApiResult::NO_SIZE_CHECK; |
| 412 |
if ( $existing === null ) { |
| 413 |
$flags |= ApiResult::ADD_ON_TOP; |
| 414 |
} |
| 415 |
$this->result->addValue( $path, null, $value, $flags ); |
| 416 |
$this->result->addIndexedTagName( $path, $tag ); |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
class_alias( ApiErrorFormatter::class, 'ApiErrorFormatter' ); |