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