Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 7 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| LocalizedException | |
0.00% |
0 / 6 |
|
0.00% |
0 / 2 |
6 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getMessageObject | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Exception; |
| 8 | |
| 9 | use Exception; |
| 10 | use MediaWiki\Message\Message; |
| 11 | use MediaWiki\Parser\Sanitizer; |
| 12 | use Throwable; |
| 13 | use Wikimedia\Message\MessageSpecifier; |
| 14 | |
| 15 | /** |
| 16 | * Basic localized exception. |
| 17 | * |
| 18 | * @newable |
| 19 | * @stable to extend |
| 20 | * @since 1.29 |
| 21 | * @ingroup Exception |
| 22 | * @note Don't use this in a situation where MessageCache is not functional. |
| 23 | */ |
| 24 | class LocalizedException extends Exception implements ILocalizedException { |
| 25 | /** @var string|array|MessageSpecifier */ |
| 26 | protected $messageSpec; |
| 27 | |
| 28 | /** |
| 29 | * @stable to call |
| 30 | * @param string|array|MessageSpecifier $messageSpec See Message::newFromSpecifier |
| 31 | * @param int $code |
| 32 | * @param Throwable|null $previous The previous exception used for the exception |
| 33 | * chaining. |
| 34 | */ |
| 35 | public function __construct( $messageSpec, $code = 0, ?Throwable $previous = null ) { |
| 36 | $this->messageSpec = $messageSpec; |
| 37 | |
| 38 | // Exception->getMessage() should be in plain English, not localized. |
| 39 | // So fetch the English version of the message, without local |
| 40 | // customizations, and make a basic attempt to turn markup into text. |
| 41 | $msg = $this->getMessageObject()->inLanguage( 'en' )->useDatabase( false )->text(); |
| 42 | $msg = preg_replace( '!</?(var|kbd|samp|code)>!', '"', $msg ); |
| 43 | $msg = Sanitizer::stripAllTags( $msg ); |
| 44 | parent::__construct( $msg, $code, $previous ); |
| 45 | } |
| 46 | |
| 47 | /** @inheritDoc */ |
| 48 | public function getMessageObject() { |
| 49 | return Message::newFromSpecifier( $this->messageSpec ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** @deprecated class alias since 1.44 */ |
| 54 | class_alias( LocalizedException::class, 'LocalizedException' ); |