MediaWiki master
LocalizedException.php
Go to the documentation of this file.
1<?php
6
7namespace MediaWiki\Exception;
8
9use Exception;
10use MediaWiki\Message\Message;
11use MediaWiki\Parser\Sanitizer;
12use Throwable;
13use Wikimedia\Message\MessageSpecifier;
14
26 protected $messageSpec;
27
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
48 public function getMessageObject() {
49 return Message::newFromSpecifier( $this->messageSpec );
50 }
51}
52
54class_alias( LocalizedException::class, 'LocalizedException' );
string array MessageSpecifier $messageSpec
__construct( $messageSpec, $code=0, ?Throwable $previous=null)
getMessageObject()
Return a Message object for this exception.Message
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Definition Message.php:492
Interface for MediaWiki-localized exceptions.