Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ErrorPageError
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getMessageObject
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 report
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Exception;
8
9use MediaWiki\Message\Message;
10use Wikimedia\Message\MessageSpecifier;
11
12/**
13 * An error page which can definitely be safely rendered using the OutputPage.
14 *
15 * @newable
16 * @stable to extend
17 *
18 * @since 1.7
19 * @ingroup Exception
20 */
21class ErrorPageError extends MWException implements ILocalizedException {
22    public const SEND_OUTPUT = 0;
23    public const STAGE_OUTPUT = 1;
24
25    /** @var string|MessageSpecifier */
26    public $title;
27    /** @var string|MessageSpecifier */
28    public $msg;
29    public array $params;
30
31    /**
32     * Note: these arguments are keys into wfMessage(), not text!
33     *
34     * @stable to call
35     *
36     * @param string|MessageSpecifier $title Message key (string) for page title, or a MessageSpecifier
37     * @param string|MessageSpecifier $msg Message key (string) for error text, or a MessageSpecifier
38     * @param array $params Array with parameters to wfMessage()
39     */
40    public function __construct( $title, $msg, $params = [] ) {
41        $this->title = $title;
42        $this->msg = $msg;
43        $this->params = $params;
44
45        // T46111: Messages in the log files should be in English and not
46        // customized by the local wiki. So get the default English version for
47        // passing to the parent constructor. Our overridden report() below
48        // makes sure that the page shown to the user is not forced to English.
49        $enMsg = $this->getMessageObject();
50        $enMsg->inLanguage( 'en' )->useDatabase( false );
51        parent::__construct( $enMsg->text() );
52    }
53
54    /**
55     * Return a Message object for this exception
56     * @since 1.29
57     * @return Message
58     */
59    public function getMessageObject() {
60        if ( $this->msg instanceof Message ) {
61            return clone $this->msg;
62        }
63        return wfMessage( $this->msg, $this->params );
64    }
65
66    /**
67     * @stable to override
68     * @param int $action
69     *
70     * @throws FatalError
71     * @throws MWException
72     */
73    public function report( $action = self::SEND_OUTPUT ) {
74        if ( self::isCommandLine() || defined( 'MW_API' ) ) {
75            MWExceptionRenderer::output( $this, MWExceptionRenderer::AS_PRETTY );
76        } else {
77            global $wgOut;
78            $wgOut->showErrorPage( $this->title, $this->msg, $this->params );
79            // Allow skipping of the final output step, so that web-based page views
80            // from MediaWiki.php, can inspect the staged OutputPage state, and perform
81            // graceful shutdown via prepareForOutput() first, just like for regular
82            // output when there isn't an error page.
83            if ( $action === self::SEND_OUTPUT ) {
84                $wgOut->output();
85            }
86        }
87    }
88}
89
90/** @deprecated class alias since 1.44 */
91class_alias( ErrorPageError::class, 'ErrorPageError' );