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 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Exception;
8
9use MediaWiki\Message\Message;
10use MediaWiki\Title\MalformedTitleException;
11
12/**
13 * Show an error page on a badtitle.
14 *
15 * We always emit a HTTP 404 error code since pages with an invalid title will
16 * never have any content. In the past this emitted a 400 error code to ensure
17 * caching proxies and mobile browsers don't cache it as valid content (T35646),
18 * but that had the disadvantage of telling caches in front of MediaWiki
19 * (Varnish, etc.), not to cache it either.
20 *
21 * @newable
22 * @since 1.19
23 * @ingroup Exception
24 */
25class BadTitleError extends ErrorPageError {
26    /**
27     * @stable to call
28     *
29     * @param string|Message|MalformedTitleException $msg A message key (default: 'badtitletext'), or
30     *     a MalformedTitleException to figure out things from
31     * @param array $params Parameter to wfMessage()
32     */
33    public function __construct( $msg = 'badtitletext', $params = [] ) {
34        if ( $msg instanceof MalformedTitleException ) {
35            $errorMessage = $msg->getErrorMessage();
36            if ( !$errorMessage ) {
37                parent::__construct( 'badtitle', 'badtitletext', [] );
38            } else {
39                $errorMessageParams = $msg->getErrorMessageParameters();
40                parent::__construct( 'badtitle', $errorMessage, $errorMessageParams );
41            }
42        } else {
43            parent::__construct( 'badtitle', $msg, $params );
44        }
45    }
46
47    /**
48     * @inheritDoc
49     */
50    public function report( $action = self::SEND_OUTPUT ) {
51        global $wgOut;
52
53        $wgOut->setStatusCode( 404 );
54
55        parent::report( self::STAGE_OUTPUT );
56
57        // Unconditionally cache the error for an hour, see T316932
58        $wgOut->enableClientCache();
59        $wgOut->setCdnMaxage( 3600 );
60
61        if ( $action === self::SEND_OUTPUT ) {
62            $wgOut->output();
63        }
64    }
65}
66
67/** @deprecated class alias since 1.44 */
68class_alias( BadTitleError::class, 'BadTitleError' );