Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.14% covered (warning)
57.14%
8 / 14
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
BadTitleError
61.54% covered (warning)
61.54%
8 / 13
50.00% covered (danger)
50.00%
1 / 2
6.42
0.00% covered (danger)
0.00%
0 / 1
 __construct
28.57% covered (danger)
28.57%
2 / 7
0.00% covered (danger)
0.00%
0 / 1
6.28
 report
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Exception;
22
23use MediaWiki\Message\Message;
24use MediaWiki\Title\MalformedTitleException;
25
26/**
27 * Show an error page on a badtitle.
28 *
29 * We always emit a HTTP 404 error code since pages with an invalid title will
30 * never have any content. In the past this emitted a 400 error code to ensure
31 * caching proxies and mobile browsers don't cache it as valid content (T35646),
32 * but that had the disadvantage of telling caches in front of MediaWiki
33 * (Varnish, etc.), not to cache it either.
34 *
35 * @newable
36 * @since 1.19
37 * @ingroup Exception
38 */
39class BadTitleError extends ErrorPageError {
40    /**
41     * @stable to call
42     *
43     * @param string|Message|MalformedTitleException $msg A message key (default: 'badtitletext'), or
44     *     a MalformedTitleException to figure out things from
45     * @param array $params Parameter to wfMessage()
46     */
47    public function __construct( $msg = 'badtitletext', $params = [] ) {
48        if ( $msg instanceof MalformedTitleException ) {
49            $errorMessage = $msg->getErrorMessage();
50            if ( !$errorMessage ) {
51                parent::__construct( 'badtitle', 'badtitletext', [] );
52            } else {
53                $errorMessageParams = $msg->getErrorMessageParameters();
54                parent::__construct( 'badtitle', $errorMessage, $errorMessageParams );
55            }
56        } else {
57            parent::__construct( 'badtitle', $msg, $params );
58        }
59    }
60
61    /**
62     * @inheritDoc
63     */
64    public function report( $action = self::SEND_OUTPUT ) {
65        global $wgOut;
66
67        $wgOut->setStatusCode( 404 );
68
69        parent::report( self::STAGE_OUTPUT );
70
71        // Unconditionally cache the error for an hour, see T316932
72        $wgOut->enableClientCache();
73        $wgOut->setCdnMaxage( 3600 );
74
75        if ( $action === self::SEND_OUTPUT ) {
76            $wgOut->output();
77        }
78    }
79}
80
81/** @deprecated class alias since 1.44 */
82class_alias( BadTitleError::class, 'BadTitleError' );