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