MediaWiki  master
MWException.php
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  }
38 
45  public function isLoggable() {
46  return true;
47  }
48 
54  public function useMessageCache() {
55  global $wgLang;
56 
57  foreach ( $this->getTrace() as $frame ) {
58  if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache::class ) {
59  return false;
60  }
61  }
62 
63  return $wgLang instanceof Language;
64  }
65 
75  public function msg( $key, $fallback, ...$params ) {
76  global $wgSitename;
77 
78  // FIXME: Keep logic in sync with MWExceptionRenderer::msg.
79  $res = false;
80  if ( $this->useMessageCache() ) {
81  try {
82  $res = wfMessage( $key, $params )->text();
83  } catch ( Exception $e ) {
84  }
85  }
86  if ( $res === false ) {
88  // If an exception happens inside message rendering,
89  // {{SITENAME}} sometimes won't be replaced.
90  $res = strtr( $res, [
91  '{{SITENAME}}' => $wgSitename,
92  ] );
93  }
94  return $res;
95  }
96 
104  public function getHTML() {
106 
107  if ( $wgShowExceptionDetails ) {
108  return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $this ) ) ) .
109  '</p><p>Backtrace:</p><p>' .
110  nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $this ) ) ) .
111  "</p>\n";
112  } else {
113  $logId = WebRequest::getRequestId();
115  return Html::errorBox(
116  htmlspecialchars(
117  '[' . $logId . '] ' .
118  gmdate( 'Y-m-d H:i:s' ) . ": " .
119  $this->msg( "internalerror-fatal-exception",
120  "Fatal exception of type $1",
121  $type,
122  $logId,
124  )
125  ) ) .
126  "<!-- Set \$wgShowExceptionDetails = true; " .
127  "at the bottom of LocalSettings.php to show detailed " .
128  "debugging information. -->";
129  }
130  }
131 
139  public function getText() {
141 
142  if ( $wgShowExceptionDetails ) {
143  return MWExceptionHandler::getLogMessage( $this ) .
144  "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n";
145  } else {
146  return "Set \$wgShowExceptionDetails = true; " .
147  "in LocalSettings.php to show detailed debugging information.\n";
148  }
149  }
150 
156  public function getPageTitle() {
157  return $this->msg( 'internalerror', 'Internal error' );
158  }
159 
163  public function reportHTML() {
164  global $wgOut, $wgSitename;
165  if ( $this->useOutputPage() ) {
166  $wgOut->prepareErrorPage( $this->getPageTitle() );
167  // Manually set the html title, since sometimes
168  // {{SITENAME}} does not get replaced for exceptions
169  // happening inside message rendering.
170  $wgOut->setHTMLTitle(
171  $this->msg(
172  'pagetitle',
173  "$1 - $wgSitename",
174  $this->getPageTitle()
175  )
176  );
177 
178  $wgOut->addHTML( $this->getHTML() );
179 
180  $wgOut->output();
181  } else {
182  self::header( 'Content-Type: text/html; charset=utf-8' );
183  echo "<!DOCTYPE html>\n" .
184  '<html><head>' .
185  // Mimick OutputPage::setPageTitle behaviour
186  '<title>' .
187  htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
188  '</title>' .
189  '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
190  "</head><body>\n";
191 
192  echo $this->getHTML();
193 
194  echo "</body></html>\n";
195  }
196  }
197 
202  public function report() {
203  global $wgMimeType;
204 
205  if ( defined( 'MW_API' ) ) {
206  // Unhandled API exception, we can't be sure that format printer is alive
207  self::header( 'MediaWiki-API-Error: internal_api_error_' . static::class );
208  wfHttpError( 500, 'Internal Server Error', $this->getText() );
209  } elseif ( self::isCommandLine() ) {
210  $message = $this->getText();
211  $this->writeToCommandLine( $message );
212  } else {
213  self::statusHeader( 500 );
214  self::header( "Content-Type: $wgMimeType; charset=utf-8" );
215 
216  $this->reportHTML();
217  }
218  }
219 
226  private function writeToCommandLine( $message ) {
227  // T17602: STDERR may not be available
228  if ( !defined( 'MW_PHPUNIT_TEST' ) && defined( 'STDERR' ) ) {
229  fwrite( STDERR, $message );
230  } else {
231  echo $message;
232  }
233  }
234 
241  public static function isCommandLine() {
242  return !empty( $GLOBALS['wgCommandLineMode'] );
243  }
244 
250  private static function header( $header ) {
251  if ( !headers_sent() ) {
252  header( $header );
253  }
254  }
255 
256  private static function statusHeader( $code ) {
257  if ( !headers_sent() ) {
259  }
260  }
261 }
static getLogMessage( $e)
Get a message formatting the exception message and its origin.
static getRequestId()
Get the unique request ID.
Definition: WebRequest.php:275
$wgSitename
Name of the site.
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2159
$wgMimeType
The default Content-Type header.
getPageTitle()
Return the title of the page when reporting this error in a HTTP response.
isLoggable()
Whether to log this exception in the exception debug log.
Definition: MWException.php:45
useMessageCache()
Can the extension use the Message class/wfMessage to get i18n-ed messages?
Definition: MWException.php:54
static statusHeader( $code)
$wgShowExceptionDetails
If set to true, uncaught exceptions will print the exception message and a complete stack trace to ou...
getHTML()
If $wgShowExceptionDetails is true, return a HTML message with a backtrace to the error...
$wgLang
Definition: Setup.php:931
msg( $key, $fallback,... $params)
Get a message from i18n.
Definition: MWException.php:75
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 use $formDescriptor instead 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
static getRedactedTraceAsString( $e)
Generate a string representation of an exception&#39;s stack trace.
$res
Definition: database.txt:21
wfMsgReplaceArgs( $message, $args)
Replace message parameter keys on the given formatted output.
static header( $code)
Output an HTTP status code header.
Definition: HttpStatus.php:96
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
$params
static getURL()
If the exception occurred in the course of responding to a request, returns the requested URL...
$GLOBALS['IP']
$header
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:780
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:9
static isCommandLine()
Check whether we are in command line mode or not to report the exception in the correct format...
getText()
Get the text to display when reporting the error on the command line.
static errorBox( $html, $heading='')
Return an error box.
Definition: Html.php:736
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:35
$fallback
Definition: MessagesAb.php:11
useOutputPage()
Should the exception use $wgOut to output the error?
Definition: MWException.php:32
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:52
reportHTML()
Output the exception report using HTML.
report()
Output a report about the exception and takes care of formatting.
$wgOut
Definition: Setup.php:936
static header( $header)
Send a header, if we haven&#39;t already sent them.
writeToCommandLine( $message)
Write a message to stderr falling back to stdout if stderr unavailable.