MediaWiki  1.28.0
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() {
56 
57  foreach ( $this->getTrace() as $frame ) {
58  if ( isset( $frame['class'] ) && $frame['class'] === 'LocalisationCache' ) {
59  return false;
60  }
61  }
62 
63  return $wgLang instanceof Language;
64  }
65 
73  public function runHooks( $name, $args = [] ) {
74  return MWExceptionRenderer::runHooks( $this, $name, $args );
75  }
76 
86  public function msg( $key, $fallback /*[, params...] */ ) {
87  $args = array_slice( func_get_args(), 2 );
88 
89  if ( $this->useMessageCache() ) {
90  try {
91  return wfMessage( $key, $args )->text();
92  } catch ( Exception $e ) {
93  }
94  }
95  return wfMsgReplaceArgs( $fallback, $args );
96  }
97 
105  public function getHTML() {
106  global $wgShowExceptionDetails;
107 
108  if ( $wgShowExceptionDetails ) {
109  return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $this ) ) ) .
110  '</p><p>Backtrace:</p><p>' .
111  nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $this ) ) ) .
112  "</p>\n";
113  } else {
114  $logId = WebRequest::getRequestId();
115  $type = get_class( $this );
116  return "<div class=\"errorbox\">" .
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  ) . "</div>\n" .
125  "<!-- Set \$wgShowExceptionDetails = true; " .
126  "at the bottom of LocalSettings.php to show detailed " .
127  "debugging information. -->";
128  }
129  }
130 
138  public function getText() {
139  global $wgShowExceptionDetails;
140 
141  if ( $wgShowExceptionDetails ) {
142  return MWExceptionHandler::getLogMessage( $this ) .
143  "\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n";
144  } else {
145  return "Set \$wgShowExceptionDetails = true; " .
146  "in LocalSettings.php to show detailed debugging information.\n";
147  }
148  }
149 
155  public function getPageTitle() {
156  return $this->msg( 'internalerror', 'Internal error' );
157  }
158 
162  public function reportHTML() {
164  if ( $this->useOutputPage() ) {
165  $wgOut->prepareErrorPage( $this->getPageTitle() );
166 
167  $hookResult = $this->runHooks( get_class( $this ) );
168  if ( $hookResult ) {
169  $wgOut->addHTML( $hookResult );
170  } else {
171  $wgOut->addHTML( $this->getHTML() );
172  }
173 
174  $wgOut->output();
175  } else {
176  self::header( 'Content-Type: text/html; charset=utf-8' );
177  echo "<!DOCTYPE html>\n" .
178  '<html><head>' .
179  // Mimick OutputPage::setPageTitle behaviour
180  '<title>' .
181  htmlspecialchars( $this->msg( 'pagetitle', "$1 - $wgSitename", $this->getPageTitle() ) ) .
182  '</title>' .
183  '<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
184  "</head><body>\n";
185 
186  $hookResult = $this->runHooks( get_class( $this ) . 'Raw' );
187  if ( $hookResult ) {
188  echo $hookResult;
189  } else {
190  echo $this->getHTML();
191  }
192 
193  echo "</body></html>\n";
194  }
195  }
196 
201  public function report() {
203 
204  if ( defined( 'MW_API' ) ) {
205  // Unhandled API exception, we can't be sure that format printer is alive
206  self::header( 'MediaWiki-API-Error: internal_api_error_' . get_class( $this ) );
207  wfHttpError( 500, 'Internal Server Error', $this->getText() );
208  } elseif ( self::isCommandLine() ) {
209  $message = $this->getText();
210  // T17602: STDERR may not be available
211  if ( defined( 'STDERR' ) ) {
212  fwrite( STDERR, $message );
213  } else {
214  echo $message;
215  }
216  } else {
217  self::statusHeader( 500 );
218  self::header( "Content-Type: $wgMimeType; charset=utf-8" );
219 
220  $this->reportHTML();
221  }
222  }
223 
230  public static function isCommandLine() {
231  return !empty( $GLOBALS['wgCommandLineMode'] );
232  }
233 
239  private static function header( $header ) {
240  if ( !headers_sent() ) {
241  header( $header );
242  }
243  }
244  private static function statusHeader( $code ) {
245  if ( !headers_sent() ) {
247  }
248  }
249 }
static statusHeader($code)
static getRequestId()
Get the unique request ID.
Definition: WebRequest.php:272
static getLogMessage($e)
Get a message formatting the exception message and its origin.
$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:2102
$wgMimeType
The default Content-Type header.
static header($code)
Output an HTTP status code header.
Definition: HttpStatus.php:96
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
wfHttpError($code, $label, $desc)
Provide a simple HTTP error.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static runHooks($e, $name, $args=[])
Run hook to allow extensions to modify the text of the exception.
getHTML()
If $wgShowExceptionDetails is true, return a HTML message with a backtrace to the error...
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
if($line===false) $args
Definition: cdb.php:64
wfMsgReplaceArgs($message, $args)
Replace message parameter keys on the given formatted output.
static header($header)
Send a header, if we haven't already sent them.
runHooks($name, $args=[])
Run hook to allow extensions to modify the text of the exception.
Definition: MWException.php:73
$GLOBALS['IP']
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 unsetoffset-wrap String Wrap the message in html(usually something like"&lt
static getURL()
If the exception occurred in the course of responding to a request, returns the requested URL...
$header
static getRedactedTraceAsString($e)
Generate a string representation of an exception's stack trace.
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
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:802
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.
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
msg($key, $fallback)
Get a message from i18n.
Definition: MWException.php:86
$fallback
Definition: MessagesAb.php:11
useOutputPage()
Should the exception use $wgOut to output the error?
Definition: MWException.php:32
reportHTML()
Output the exception report using HTML.
report()
Output a report about the exception and takes care of formatting.
$wgOut
Definition: Setup.php:816
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2491
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300