MWException.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. * MediaWiki exception
  22. *
  23. * @ingroup Exception
  24. */
  25. class MWException extends Exception {
  26. /**
  27. * Should the exception use $wgOut to output the error?
  28. *
  29. * @return bool
  30. */
  31. public function useOutputPage() {
  32. return $this->useMessageCache() &&
  33. !empty( $GLOBALS['wgFullyInitialised'] ) &&
  34. !empty( $GLOBALS['wgOut'] ) &&
  35. !defined( 'MEDIAWIKI_INSTALL' );
  36. }
  37. /**
  38. * Whether to log this exception in the exception debug log.
  39. *
  40. * @since 1.23
  41. * @return bool
  42. */
  43. public function isLoggable() {
  44. return true;
  45. }
  46. /**
  47. * Can the extension use the Message class/wfMessage to get i18n-ed messages?
  48. *
  49. * @return bool
  50. */
  51. public function useMessageCache() {
  52. global $wgLang;
  53. foreach ( $this->getTrace() as $frame ) {
  54. if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache::class ) {
  55. return false;
  56. }
  57. }
  58. return $wgLang instanceof Language;
  59. }
  60. /**
  61. * Get a message from i18n
  62. *
  63. * @param string $key Message name
  64. * @param string $fallback Default message if the message cache can't be
  65. * called by the exception
  66. * The function also has other parameters that are arguments for the message
  67. * @return string Message with arguments replaced
  68. */
  69. public function msg( $key, $fallback /*[, params...] */ ) {
  70. global $wgSitename;
  71. $args = array_slice( func_get_args(), 2 );
  72. $res = false;
  73. if ( $this->useMessageCache() ) {
  74. try {
  75. $res = wfMessage( $key, $args )->text();
  76. } catch ( Exception $e ) {
  77. }
  78. }
  79. if ( $res === false ) {
  80. $res = wfMsgReplaceArgs( $fallback, $args );
  81. // If an exception happens inside message rendering,
  82. // {{SITENAME}} sometimes won't be replaced.
  83. $res = preg_replace( '/\{\{SITENAME\}\}/', $wgSitename, $res );
  84. }
  85. return $res;
  86. }
  87. /**
  88. * If $wgShowExceptionDetails is true, return a HTML message with a
  89. * backtrace to the error, otherwise show a message to ask to set it to true
  90. * to show that information.
  91. *
  92. * @return string Html to output
  93. */
  94. public function getHTML() {
  95. global $wgShowExceptionDetails;
  96. if ( $wgShowExceptionDetails ) {
  97. return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $this ) ) ) .
  98. '</p><p>Backtrace:</p><p>' .
  99. nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $this ) ) ) .
  100. "</p>\n";
  101. } else {
  102. $logId = WebRequest::getRequestId();
  103. $type = static::class;
  104. return Html::errorBox(
  105. htmlspecialchars(
  106. '[' . $logId . '] ' .
  107. gmdate( 'Y-m-d H:i:s' ) . ": " .
  108. $this->msg( "internalerror-fatal-exception",
  109. "Fatal exception of type $1",
  110. $type,
  111. $logId,
  112. MWExceptionHandler::getURL( $this )
  113. )
  114. ) ) .
  115. "<!-- Set \$wgShowExceptionDetails = true; " .
  116. "at the bottom of LocalSettings.php to show detailed " .
  117. "debugging information. -->";
  118. }
  119. }
  120. /**
  121. * Get the text to display when reporting the error on the command line.
  122. * If $wgShowExceptionDetails is true, return a text message with a
  123. * backtrace to the error.
  124. *
  125. * @return string
  126. */
  127. public function getText() {
  128. global $wgShowExceptionDetails;
  129. if ( $wgShowExceptionDetails ) {
  130. return MWExceptionHandler::getLogMessage( $this ) .
  131. "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n";
  132. } else {
  133. return "Set \$wgShowExceptionDetails = true; " .
  134. "in LocalSettings.php to show detailed debugging information.\n";
  135. }
  136. }
  137. /**
  138. * Return the title of the page when reporting this error in a HTTP response.
  139. *
  140. * @return string
  141. */
  142. public function getPageTitle() {
  143. return $this->msg( 'internalerror', 'Internal error' );
  144. }
  145. /**
  146. * Output the exception report using HTML.
  147. */
  148. public function reportHTML() {
  149. global $wgOut, $wgSitename;
  150. if ( $this->useOutputPage() ) {
  151. $wgOut->prepareErrorPage( $this->getPageTitle() );
  152. // Manually set the html title, since sometimes
  153. // {{SITENAME}} does not get replaced for exceptions
  154. // happening inside message rendering.
  155. $wgOut->setHTMLTitle(
  156. $this->msg(
  157. 'pagetitle',
  158. "$1 - $wgSitename",
  159. $this->getPageTitle()
  160. )
  161. );
  162. $wgOut->addHTML( $this->getHTML() );
  163. $wgOut->output();
  164. } else {
  165. self::header( 'Content-Type: text/html; charset=utf-8' );
  166. echo "<!DOCTYPE html>\n" .
  167. '<html><head>' .
  168. // Mimick OutputPage::setPageTitle behaviour
  169. '<title>' .
  170. htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
  171. '</title>' .
  172. '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
  173. "</head><body>\n";
  174. echo $this->getHTML();
  175. echo "</body></html>\n";
  176. }
  177. }
  178. /**
  179. * Output a report about the exception and takes care of formatting.
  180. * It will be either HTML or plain text based on isCommandLine().
  181. */
  182. public function report() {
  183. global $wgMimeType;
  184. if ( defined( 'MW_API' ) ) {
  185. // Unhandled API exception, we can't be sure that format printer is alive
  186. self::header( 'MediaWiki-API-Error: internal_api_error_' . static::class );
  187. wfHttpError( 500, 'Internal Server Error', $this->getText() );
  188. } elseif ( self::isCommandLine() ) {
  189. $message = $this->getText();
  190. // T17602: STDERR may not be available
  191. if ( !defined( 'MW_PHPUNIT_TEST' ) && defined( 'STDERR' ) ) {
  192. fwrite( STDERR, $message );
  193. } else {
  194. echo $message;
  195. }
  196. } else {
  197. self::statusHeader( 500 );
  198. self::header( "Content-Type: $wgMimeType; charset=utf-8" );
  199. $this->reportHTML();
  200. }
  201. }
  202. /**
  203. * Check whether we are in command line mode or not to report the exception
  204. * in the correct format.
  205. *
  206. * @return bool
  207. */
  208. public static function isCommandLine() {
  209. return !empty( $GLOBALS['wgCommandLineMode'] );
  210. }
  211. /**
  212. * Send a header, if we haven't already sent them. We shouldn't,
  213. * but sometimes we might in a weird case like Export
  214. * @param string $header
  215. */
  216. private static function header( $header ) {
  217. if ( !headers_sent() ) {
  218. header( $header );
  219. }
  220. }
  221. private static function statusHeader( $code ) {
  222. if ( !headers_sent() ) {
  223. HttpStatus::header( $code );
  224. }
  225. }
  226. }