From cfbeb437a3bf688f07681d8b900f3d0a13f25497 Mon Sep 17 00:00:00 2001 From: Chad Horohoe Date: Tue, 13 May 2014 12:15:06 -0700 Subject: [PATCH] MWException: Don't send headers multiple times Ideally we wouldn't have sent headers yet, but in some weird code paths where we output stuff raw (like Export) we might have already sent them. All depends on when the failure happened. Should silence all of the "headers already sent" being seen from exports. Change-Id: I12e0532e93d30b2255f73a9d0e017c73e30c3e28 --- includes/exception/MWException.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/includes/exception/MWException.php b/includes/exception/MWException.php index 82303b91f6..782a44bdf8 100644 --- a/includes/exception/MWException.php +++ b/includes/exception/MWException.php @@ -219,7 +219,7 @@ class MWException extends Exception { $wgOut->output(); } else { - header( 'Content-Type: text/html; charset=utf-8' ); + self::header( 'Content-Type: text/html; charset=utf-8' ); echo "\n" . '' . // Mimick OutputPage::setPageTitle behaviour @@ -251,14 +251,14 @@ class MWException extends Exception { if ( defined( 'MW_API' ) ) { // Unhandled API exception, we can't be sure that format printer is alive - header( 'MediaWiki-API-Error: internal_api_error_' . get_class( $this ) ); + self::header( 'MediaWiki-API-Error: internal_api_error_' . get_class( $this ) ); wfHttpError( 500, 'Internal Server Error', $this->getText() ); } elseif ( self::isCommandLine() ) { MWExceptionHandler::printError( $this->getText() ); } else { - header( 'HTTP/1.1 500 MediaWiki exception' ); - header( 'Status: 500 MediaWiki exception', true ); - header( "Content-Type: $wgMimeType; charset=utf-8", true ); + self::header( 'HTTP/1.1 500 MediaWiki exception' ); + self::header( 'Status: 500 MediaWiki exception' ); + self::header( "Content-Type: $wgMimeType; charset=utf-8" ); $this->reportHTML(); } @@ -273,4 +273,14 @@ class MWException extends Exception { public static function isCommandLine() { return !empty( $GLOBALS['wgCommandLineMode'] ); } + + /** + * Send a header, if we haven't already sent them. We shouldn't, + * but sometimes we might in a weird case like Export + */ + private static function header( $header ) { + if ( !headers_sent() ) { + header( $header ); + } + } } -- 2.20.1