MediaWiki  REL1_31
Go to the documentation of this file.
1 <?php
26 class MWException extends Exception {
32  public function useOutputPage() {
33  return $this->useMessageCache() &&
34  !empty( $GLOBALS['wgFullyInitialised'] ) &&
35  !empty( $GLOBALS['wgOut'] ) &&
36  !defined( 'MEDIAWIKI_INSTALL' );
37  }
45  public function isLoggable() {
46  return true;
47  }
54  public function useMessageCache() {
57  foreach ( $this->getTrace() as $frame ) {
58  if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache::class ) {
59  return false;
60  }
61  }
63  return $wgLang instanceof Language;
64  }
75  public function msg( $key, $fallback /*[, params...] */ ) {
76  $args = array_slice( func_get_args(), 2 );
78  if ( $this->useMessageCache() ) {
79  try {
80  return wfMessage( $key, $args )->text();
81  } catch ( Exception $e ) {
82  }
83  }
84  return wfMsgReplaceArgs( $fallback, $args );
85  }
94  public function getHTML() {
98  return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $this ) ) ) .
99  '</p><p>Backtrace:</p><p>' .
100  nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $this ) ) ) .
101  "</p>\n";
102  } else {
103  $logId = WebRequest::getRequestId();
105  return Html::errorBox(
106  htmlspecialchars(
107  '[' . $logId . '] ' .
108  gmdate( 'Y-m-d H:i:s' ) . ": " .
109  $this->msg( "internalerror-fatal-exception",
110  "Fatal exception of type $1",
111  $type,
112  $logId,
114  )
115  ) ) .
116  "<!-- Set \$wgShowExceptionDetails = true; " .
117  "at the bottom of LocalSettings.php to show detailed " .
118  "debugging information. -->";
119  }
120  }
129  public function getText() {
132  if ( $wgShowExceptionDetails ) {
133  return MWExceptionHandler::getLogMessage( $this ) .
134  "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n";
135  } else {
136  return "Set \$wgShowExceptionDetails = true; " .
137  "in LocalSettings.php to show detailed debugging information.\n";
138  }
139  }
146  public function getPageTitle() {
147  return $this->msg( 'internalerror', 'Internal error' );
148  }
153  public function reportHTML() {
155  if ( $this->useOutputPage() ) {
156  $wgOut->prepareErrorPage( $this->getPageTitle() );
158  $wgOut->addHTML( $this->getHTML() );
160  $wgOut->output();
161  } else {
162  self::header( 'Content-Type: text/html; charset=utf-8' );
163  echo "<!DOCTYPE html>\n" .
164  '<html><head>' .
165  // Mimick OutputPage::setPageTitle behaviour
166  '<title>' .
167  htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
168  '</title>' .
169  '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
170  "</head><body>\n";
172  echo $this->getHTML();
174  echo "</body></html>\n";
175  }
176  }
182  public function report() {
185  if ( defined( 'MW_API' ) ) {
186  // Unhandled API exception, we can't be sure that format printer is alive
187  self::header( 'MediaWiki-API-Error: internal_api_error_' . static::class );
188  wfHttpError( 500, 'Internal Server Error', $this->getText() );
189  } elseif ( self::isCommandLine() ) {
190  $message = $this->getText();
191  // T17602: STDERR may not be available
192  if ( !defined( 'MW_PHPUNIT_TEST' ) && defined( 'STDERR' ) ) {
193  fwrite( STDERR, $message );
194  } else {
195  echo $message;
196  }
197  } else {
198  self::statusHeader( 500 );
199  self::header( "Content-Type: $wgMimeType; charset=utf-8" );
201  $this->reportHTML();
202  }
203  }
211  public static function isCommandLine() {
212  return !empty( $GLOBALS['wgCommandLineMode'] );
213  }
220  private static function header( $header ) {
221  if ( !headers_sent() ) {
222  header( $header );
223  }
224  }
225  private static function statusHeader( $code ) {
226  if ( !headers_sent() ) {
228  }
229  }
230 }
static errorBox( $html, $heading='')
Return an error box.
Definition: Html.php:713
The default Content-Type header.
Definition: DefaultSettings.php:3180
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
Definition: MessagesAb.php:11
wfMsgReplaceArgs( $message, $args)
Replace message parameter keys on the given formatted output.
Definition: GlobalFunctions.php:1393
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
Whether to log this exception in the exception debug log.
Definition: MWException.php:45
Can the extension use the Message class/wfMessage to get i18n-ed messages?
Definition: MWException.php:54
static isCommandLine()
Check whether we are in command line mode or not to report the exception in the correct format.
Definition: MWException.php:211
MediaWiki exception.
Definition: MWException.php:26
Get the text to display when reporting the error on the command line.
Definition: MWException.php:129
static getURL()
If the exception occurred in the course of responding to a request, returns the requested URL.
Definition: MWExceptionHandler.php:450
static getLogMessage( $e)
Get a message formatting the exception message and its origin.
Definition: MWExceptionHandler.php:465
Return the title of the page when reporting this error in a HTTP response.
Definition: MWException.php:146
If $wgShowExceptionDetails is true, return a HTML message with a backtrace to the error,...
Definition: MWException.php:94
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition: design.txt:56
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
Output a report about the exception and takes care of formatting.
Definition: MWException.php:182
Definition: updateCredits.php:35
Output the exception report using HTML.
Definition: MWException.php:153
Name of the site.
Definition: DefaultSettings.php:80
msg( $key, $fallback)
Get a message from i18n.
Definition: MWException.php:75
static header( $header)
Send a header, if we haven't already sent them.
Definition: MWException.php:220
if( $line===false) $args
Definition: cdb.php:64
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
Definition: GlobalFunctions.php:1751
static header( $code)
Output an HTTP status code header.
Definition: HttpStatus.php:96
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:865
static getRequestId()
Get the unique request ID.
Definition: WebRequest.php:271
If set to true, uncaught exceptions will print a complete stack trace to output.
Definition: DefaultSettings.php:6304
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
Definition: Setup.php:912
static getRedactedTraceAsString( $e)
Generate a string representation of an exception's stack trace.
Definition: MWExceptionHandler.php:343
static statusHeader( $code)
Definition: MWException.php:225
Internationalisation code.
Definition: Language.php:35
Definition: ComposerHookHandler.php:6
Should the exception use $wgOut to output the error?
Definition: MWException.php:32
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2171
Definition: testCompression.php:48