2014-02-24 20:13:49 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MediaWiki exception
|
|
|
|
|
*
|
2020-06-26 12:56:03 +00:00
|
|
|
* @newable
|
2020-07-13 09:00:30 +00:00
|
|
|
* @stable to extend
|
2020-06-26 12:56:03 +00:00
|
|
|
*
|
2014-02-24 20:13:49 +00:00
|
|
|
* @ingroup Exception
|
|
|
|
|
*/
|
|
|
|
|
class MWException extends Exception {
|
|
|
|
|
/**
|
|
|
|
|
* Should the exception use $wgOut to output the error?
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function useOutputPage() {
|
2014-02-24 20:13:49 +00:00
|
|
|
return $this->useMessageCache() &&
|
|
|
|
|
!empty( $GLOBALS['wgFullyInitialised'] ) &&
|
|
|
|
|
!empty( $GLOBALS['wgOut'] ) &&
|
2020-03-25 16:52:48 +00:00
|
|
|
!defined( 'MEDIAWIKI_INSTALL' ) &&
|
|
|
|
|
// Don't send a skinned HTTP 500 page to API clients.
|
|
|
|
|
!defined( 'MW_API' );
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether to log this exception in the exception debug log.
|
|
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-07-10 12:11:14 +00:00
|
|
|
*
|
2014-02-24 20:13:49 +00:00
|
|
|
* @since 1.23
|
2014-04-14 19:43:18 +00:00
|
|
|
* @return bool
|
2014-02-24 20:13:49 +00:00
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function isLoggable() {
|
2014-02-24 20:13:49 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Can the extension use the Message class/wfMessage to get i18n-ed messages?
|
|
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-07-10 12:11:14 +00:00
|
|
|
*
|
2014-02-24 20:13:49 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function useMessageCache() {
|
2014-02-24 20:13:49 +00:00
|
|
|
foreach ( $this->getTrace() as $frame ) {
|
2018-01-13 00:02:09 +00:00
|
|
|
if ( isset( $frame['class'] ) && $frame['class'] === LocalisationCache::class ) {
|
2014-02-24 20:13:49 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-02 09:35:57 +00:00
|
|
|
return true;
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a message from i18n
|
|
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @param string $key Message name
|
|
|
|
|
* @param string $fallback Default message if the message cache can't be
|
2014-02-24 20:13:49 +00:00
|
|
|
* called by the exception
|
Get rid of unnecessary func_get_args() and friends
HHVM does not support variadic arguments with type hints. This is
mostly not a big problem, because we can just drop the type hint, but
for some reason PHPUnit adds a type hint of "array" when it creates
mocks, so a class with a variadic method can't be mocked (at least in
some cases). As such, I left alone all the classes that seem like
someone might like to mock them, like Title and User. If anyone wants
to mock them in the future, they'll have to switch back to
func_get_args(). Some of the changes are definitely safe, like
functions and test classes.
In most cases, func_get_args() (and/or func_get_arg(), func_num_args() )
were only present because the code was written before we required PHP
5.6, and writing them as variadic functions is strictly superior. In
some cases I left them alone, aside from HHVM compatibility:
* Forwarding all arguments to another function. It's useful to keep
func_get_args() here where we want to keep the list of expected
arguments and their meanings in the function signature line for
documentation purposes, but don't want to copy-paste a long line of
argument names.
* Handling deprecated calling conventions.
* One or two miscellaneous cases where we're basically using the
arguments individually but want to use them as an array as well for
some reason.
Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
2018-10-08 14:10:45 +00:00
|
|
|
* @param mixed ...$params To pass to wfMessage()
|
2014-04-14 19:43:18 +00:00
|
|
|
* @return string Message with arguments replaced
|
2014-02-24 20:13:49 +00:00
|
|
|
*/
|
Get rid of unnecessary func_get_args() and friends
HHVM does not support variadic arguments with type hints. This is
mostly not a big problem, because we can just drop the type hint, but
for some reason PHPUnit adds a type hint of "array" when it creates
mocks, so a class with a variadic method can't be mocked (at least in
some cases). As such, I left alone all the classes that seem like
someone might like to mock them, like Title and User. If anyone wants
to mock them in the future, they'll have to switch back to
func_get_args(). Some of the changes are definitely safe, like
functions and test classes.
In most cases, func_get_args() (and/or func_get_arg(), func_num_args() )
were only present because the code was written before we required PHP
5.6, and writing them as variadic functions is strictly superior. In
some cases I left them alone, aside from HHVM compatibility:
* Forwarding all arguments to another function. It's useful to keep
func_get_args() here where we want to keep the list of expected
arguments and their meanings in the function signature line for
documentation purposes, but don't want to copy-paste a long line of
argument names.
* Handling deprecated calling conventions.
* One or two miscellaneous cases where we're basically using the
arguments individually but want to use them as an array as well for
some reason.
Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
2018-10-08 14:10:45 +00:00
|
|
|
public function msg( $key, $fallback, ...$params ) {
|
2018-09-28 21:44:36 +00:00
|
|
|
// FIXME: Keep logic in sync with MWExceptionRenderer::msg.
|
2017-10-18 06:27:12 +00:00
|
|
|
$res = false;
|
2014-02-24 20:13:49 +00:00
|
|
|
if ( $this->useMessageCache() ) {
|
2015-02-12 23:01:54 +00:00
|
|
|
try {
|
2019-06-29 15:17:33 +00:00
|
|
|
$res = wfMessage( $key, ...$params )->text();
|
2015-03-15 01:59:28 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
}
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|
2017-10-18 06:27:12 +00:00
|
|
|
if ( $res === false ) {
|
exception: Simplify MWExceptionRenderer to reduce influence of config
Remove use of two configuration globals. This follows-up to
commit 47adb6d65a (I1a691f01cd82e60) which aimed to access all config
via MediaWikiServices, but that isn't safe during error handling.
These two were only used in the low-level "plain text" and
"basic HTML" error pages, which already can't have any branding,
skinning or localisation; and are not how most exceptions are rendered.
in those edge cases, we can use the name of the software instead of
(trying) to use the name of the site.
== Remove use of $wgSitename ==
In msg() and reportHTML(), remove use of $wgSitename in favour of a
generic fallback matching DefaulSettings.php. This does not affect
the common case of runtime fatals and timeouts, as we're only
changing the fallback after wfMessage() fails in msg(), or when
OutputPage is unavailable, which is typically only if services,
localisation or DB are also down (or not yet loaded). Most exceptions
happen when and after those have initialised fine.
Test case 1:
(Clean state) Edit ViewAction::show() to add `throw new RuntimeException();`
as its first statement, then try to view the main page.
This error page is unchanged. It is skinned, localised, and still uses
the configured sitename in the doc title.
Test case 2:
Edit index.php to call foo() instead of wfIndexMain(),
then try to view the main page. Before, this "minimal HTML" error page
would have a doc title of "Internal error - MyWiki", and now
"Internal error - MediaWiki".
Test case 3:
(Clean state) Edit Message::text() to add `throw new RuntimeException();`,
then try to view the main page. This results in a "plain text" error
page that doesn't even have an HTML doc, and is also unchanged.
== Remove use of $wgMimeType ==
In output(), remove use of $wgMimeType and remove the Content-Type
header that it was used for. This was redundant because the next
statements (reportHTML) already outputs Content-type. In reportHTML,
we sometimes delegate to OutputPage (if safe) and that honours
$wgMimeType already. In other cases, it is handled inline in with a
basic HTML error page, and that branch also sets the Content-Type
header already. In one case (reportOutageHTML) a header was not yet
set. For that one, I've added the missing header call and made it
explicitly text/html.
This is technically a bugfix, because our basic HTML error page is
HTML5, whereas $wgMimeType (which exists to allow enabling XHTML)
can be XHTML which we weren't following. In OutputPage and
Html::htmlHeader that would normally result in outputting `<?xml`.
Test case:
Edit ViewAction::show(), and add `throw new RuntimeException();`
as its first statement, then try to view the main page.
In devtools>network, there is still a proper Content-Type
and charset on the error page document.
Change-Id: I03cfa2b6155fb711582164852e7cab4c325a1b92
2022-01-12 23:05:34 +00:00
|
|
|
// Fallback to static message text and generic sitename.
|
|
|
|
|
// Avoid live config as this must work before Setup/MediaWikiServices finish.
|
Get rid of unnecessary func_get_args() and friends
HHVM does not support variadic arguments with type hints. This is
mostly not a big problem, because we can just drop the type hint, but
for some reason PHPUnit adds a type hint of "array" when it creates
mocks, so a class with a variadic method can't be mocked (at least in
some cases). As such, I left alone all the classes that seem like
someone might like to mock them, like Title and User. If anyone wants
to mock them in the future, they'll have to switch back to
func_get_args(). Some of the changes are definitely safe, like
functions and test classes.
In most cases, func_get_args() (and/or func_get_arg(), func_num_args() )
were only present because the code was written before we required PHP
5.6, and writing them as variadic functions is strictly superior. In
some cases I left them alone, aside from HHVM compatibility:
* Forwarding all arguments to another function. It's useful to keep
func_get_args() here where we want to keep the list of expected
arguments and their meanings in the function signature line for
documentation purposes, but don't want to copy-paste a long line of
argument names.
* Handling deprecated calling conventions.
* One or two miscellaneous cases where we're basically using the
arguments individually but want to use them as an array as well for
some reason.
Change-Id: I066ec95a7beb7c0665146195a08e7cce1222c788
2018-10-08 14:10:45 +00:00
|
|
|
$res = wfMsgReplaceArgs( $fallback, $params );
|
2018-09-27 16:04:48 +00:00
|
|
|
$res = strtr( $res, [
|
exception: Simplify MWExceptionRenderer to reduce influence of config
Remove use of two configuration globals. This follows-up to
commit 47adb6d65a (I1a691f01cd82e60) which aimed to access all config
via MediaWikiServices, but that isn't safe during error handling.
These two were only used in the low-level "plain text" and
"basic HTML" error pages, which already can't have any branding,
skinning or localisation; and are not how most exceptions are rendered.
in those edge cases, we can use the name of the software instead of
(trying) to use the name of the site.
== Remove use of $wgSitename ==
In msg() and reportHTML(), remove use of $wgSitename in favour of a
generic fallback matching DefaulSettings.php. This does not affect
the common case of runtime fatals and timeouts, as we're only
changing the fallback after wfMessage() fails in msg(), or when
OutputPage is unavailable, which is typically only if services,
localisation or DB are also down (or not yet loaded). Most exceptions
happen when and after those have initialised fine.
Test case 1:
(Clean state) Edit ViewAction::show() to add `throw new RuntimeException();`
as its first statement, then try to view the main page.
This error page is unchanged. It is skinned, localised, and still uses
the configured sitename in the doc title.
Test case 2:
Edit index.php to call foo() instead of wfIndexMain(),
then try to view the main page. Before, this "minimal HTML" error page
would have a doc title of "Internal error - MyWiki", and now
"Internal error - MediaWiki".
Test case 3:
(Clean state) Edit Message::text() to add `throw new RuntimeException();`,
then try to view the main page. This results in a "plain text" error
page that doesn't even have an HTML doc, and is also unchanged.
== Remove use of $wgMimeType ==
In output(), remove use of $wgMimeType and remove the Content-Type
header that it was used for. This was redundant because the next
statements (reportHTML) already outputs Content-type. In reportHTML,
we sometimes delegate to OutputPage (if safe) and that honours
$wgMimeType already. In other cases, it is handled inline in with a
basic HTML error page, and that branch also sets the Content-Type
header already. In one case (reportOutageHTML) a header was not yet
set. For that one, I've added the missing header call and made it
explicitly text/html.
This is technically a bugfix, because our basic HTML error page is
HTML5, whereas $wgMimeType (which exists to allow enabling XHTML)
can be XHTML which we weren't following. In OutputPage and
Html::htmlHeader that would normally result in outputting `<?xml`.
Test case:
Edit ViewAction::show(), and add `throw new RuntimeException();`
as its first statement, then try to view the main page.
In devtools>network, there is still a proper Content-Type
and charset on the error page document.
Change-Id: I03cfa2b6155fb711582164852e7cab4c325a1b92
2022-01-12 23:05:34 +00:00
|
|
|
'{{SITENAME}}' => 'MediaWiki',
|
2018-09-27 16:04:48 +00:00
|
|
|
] );
|
2017-10-18 06:27:12 +00:00
|
|
|
}
|
|
|
|
|
return $res;
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If $wgShowExceptionDetails is true, return a HTML message with a
|
|
|
|
|
* backtrace to the error, otherwise show a message to ask to set it to true
|
|
|
|
|
* to show that information.
|
|
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-07-10 12:11:14 +00:00
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @return string Html to output
|
2014-02-24 20:13:49 +00:00
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function getHTML() {
|
2022-01-12 22:09:05 +00:00
|
|
|
global $wgShowExceptionDetails;
|
2014-02-24 20:13:49 +00:00
|
|
|
|
2022-01-12 22:09:05 +00:00
|
|
|
if ( $wgShowExceptionDetails ) {
|
2014-02-24 20:13:49 +00:00
|
|
|
return '<p>' . nl2br( htmlspecialchars( MWExceptionHandler::getLogMessage( $this ) ) ) .
|
2014-04-24 19:50:01 +00:00
|
|
|
'</p><p>Backtrace:</p><p>' .
|
|
|
|
|
nl2br( htmlspecialchars( MWExceptionHandler::getRedactedTraceAsString( $this ) ) ) .
|
2014-02-24 20:13:49 +00:00
|
|
|
"</p>\n";
|
|
|
|
|
} else {
|
Provide a unique request identifier
When MediaWiki encounters an unhandled exception, the error message it produces
includes a randomly-generated token, which allows the exception details to be
looked up in the error logs. This is useful but narrow: would it not be useful
to have the ability to retrieve all log records associated with a particular
request, rather than just exception details? (Hint: yes.)
So: introduce the notion of a request-global unique ID, retrievable via
WebRequest::getRequestId(). When MediaWiki is behind Apache + mod_unique_id
(which provides the same facility) or some other software which sets a
UNIQUE_ID envvar, the value of that envvar is used as the request ID.
Otherwise, it is a randomly-generated 24-character string.
The request ID supplants exception-specific IDs; MWExceptionHandler::getLogId()
is deprecated, accordingly. The request ID is also added as an annotation to
all Monolog-processed log records, and is exposed client-side as 'wgRequestId'.
This allows developers to associate a page view with log records even when the
page view does not result in an unhandled exception. (For the WMF, I also
intend to add it as an annotation to profiling data).
The request ID is not a tracking token; it does not persist, and it is
associated with a backend request, not with a particular user or a particular
session. Like the data in the NewPP report, the request ID is designed to be
cacheable, so that if, for example, a developer notices something weird in the
HTML, s/he can associate the output with a backend request regardless of
whether the response was served from the cache or directly from the backend.
Some prior art:
* https://httpd.apache.org/docs/2.4/mod/mod_unique_id.html
* http://api.rubyonrails.org/classes/ActionDispatch/RequestId.html
* https://github.com/dabapps/django-log-request-id
* https://packagist.org/packages/php-middleware/request-id
* https://github.com/rhyselsmore/flask-request-id
Change-Id: Iaf90c20c330e0470b9b98627a0228cadefd301d1
2016-03-25 01:43:23 +00:00
|
|
|
$logId = WebRequest::getRequestId();
|
2017-03-07 02:14:14 +00:00
|
|
|
$type = static::class;
|
2017-09-28 18:42:32 +00:00
|
|
|
return Html::errorBox(
|
2017-10-18 05:28:43 +00:00
|
|
|
htmlspecialchars(
|
|
|
|
|
'[' . $logId . '] ' .
|
|
|
|
|
gmdate( 'Y-m-d H:i:s' ) . ": " .
|
|
|
|
|
$this->msg( "internalerror-fatal-exception",
|
|
|
|
|
"Fatal exception of type $1",
|
|
|
|
|
$type,
|
|
|
|
|
$logId,
|
2018-12-01 17:09:58 +00:00
|
|
|
MWExceptionHandler::getURL()
|
2017-10-18 05:28:43 +00:00
|
|
|
)
|
2017-09-28 18:42:32 +00:00
|
|
|
) ) .
|
2014-02-24 20:13:49 +00:00
|
|
|
"<!-- Set \$wgShowExceptionDetails = true; " .
|
|
|
|
|
"at the bottom of LocalSettings.php to show detailed " .
|
|
|
|
|
"debugging information. -->";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the text to display when reporting the error on the command line.
|
|
|
|
|
* If $wgShowExceptionDetails is true, return a text message with a
|
|
|
|
|
* backtrace to the error.
|
|
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-07-10 12:11:14 +00:00
|
|
|
*
|
2014-02-24 20:13:49 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function getText() {
|
2022-01-12 22:09:05 +00:00
|
|
|
global $wgShowExceptionDetails;
|
2014-02-24 20:13:49 +00:00
|
|
|
|
2022-01-12 22:09:05 +00:00
|
|
|
if ( $wgShowExceptionDetails ) {
|
2014-02-24 20:13:49 +00:00
|
|
|
return MWExceptionHandler::getLogMessage( $this ) .
|
|
|
|
|
"\nBacktrace:\n" . MWExceptionHandler::getRedactedTraceAsString( $this ) . "\n";
|
|
|
|
|
} else {
|
|
|
|
|
return "Set \$wgShowExceptionDetails = true; " .
|
|
|
|
|
"in LocalSettings.php to show detailed debugging information.\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the title of the page when reporting this error in a HTTP response.
|
|
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-07-10 12:11:14 +00:00
|
|
|
*
|
2014-02-24 20:13:49 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function getPageTitle() {
|
2014-03-14 05:02:45 +00:00
|
|
|
return $this->msg( 'internalerror', 'Internal error' );
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Output the exception report using HTML.
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2014-02-24 20:13:49 +00:00
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function reportHTML() {
|
exception: Simplify MWExceptionRenderer to reduce influence of config
Remove use of two configuration globals. This follows-up to
commit 47adb6d65a (I1a691f01cd82e60) which aimed to access all config
via MediaWikiServices, but that isn't safe during error handling.
These two were only used in the low-level "plain text" and
"basic HTML" error pages, which already can't have any branding,
skinning or localisation; and are not how most exceptions are rendered.
in those edge cases, we can use the name of the software instead of
(trying) to use the name of the site.
== Remove use of $wgSitename ==
In msg() and reportHTML(), remove use of $wgSitename in favour of a
generic fallback matching DefaulSettings.php. This does not affect
the common case of runtime fatals and timeouts, as we're only
changing the fallback after wfMessage() fails in msg(), or when
OutputPage is unavailable, which is typically only if services,
localisation or DB are also down (or not yet loaded). Most exceptions
happen when and after those have initialised fine.
Test case 1:
(Clean state) Edit ViewAction::show() to add `throw new RuntimeException();`
as its first statement, then try to view the main page.
This error page is unchanged. It is skinned, localised, and still uses
the configured sitename in the doc title.
Test case 2:
Edit index.php to call foo() instead of wfIndexMain(),
then try to view the main page. Before, this "minimal HTML" error page
would have a doc title of "Internal error - MyWiki", and now
"Internal error - MediaWiki".
Test case 3:
(Clean state) Edit Message::text() to add `throw new RuntimeException();`,
then try to view the main page. This results in a "plain text" error
page that doesn't even have an HTML doc, and is also unchanged.
== Remove use of $wgMimeType ==
In output(), remove use of $wgMimeType and remove the Content-Type
header that it was used for. This was redundant because the next
statements (reportHTML) already outputs Content-type. In reportHTML,
we sometimes delegate to OutputPage (if safe) and that honours
$wgMimeType already. In other cases, it is handled inline in with a
basic HTML error page, and that branch also sets the Content-Type
header already. In one case (reportOutageHTML) a header was not yet
set. For that one, I've added the missing header call and made it
explicitly text/html.
This is technically a bugfix, because our basic HTML error page is
HTML5, whereas $wgMimeType (which exists to allow enabling XHTML)
can be XHTML which we weren't following. In OutputPage and
Html::htmlHeader that would normally result in outputting `<?xml`.
Test case:
Edit ViewAction::show(), and add `throw new RuntimeException();`
as its first statement, then try to view the main page.
In devtools>network, there is still a proper Content-Type
and charset on the error page document.
Change-Id: I03cfa2b6155fb711582164852e7cab4c325a1b92
2022-01-12 23:05:34 +00:00
|
|
|
global $wgOut;
|
2014-02-24 20:13:49 +00:00
|
|
|
if ( $this->useOutputPage() ) {
|
|
|
|
|
$wgOut->prepareErrorPage( $this->getPageTitle() );
|
2017-10-18 06:27:12 +00:00
|
|
|
// Manually set the html title, since sometimes
|
|
|
|
|
// {{SITENAME}} does not get replaced for exceptions
|
|
|
|
|
// happening inside message rendering.
|
|
|
|
|
$wgOut->setHTMLTitle(
|
exception: Simplify MWExceptionRenderer to reduce influence of config
Remove use of two configuration globals. This follows-up to
commit 47adb6d65a (I1a691f01cd82e60) which aimed to access all config
via MediaWikiServices, but that isn't safe during error handling.
These two were only used in the low-level "plain text" and
"basic HTML" error pages, which already can't have any branding,
skinning or localisation; and are not how most exceptions are rendered.
in those edge cases, we can use the name of the software instead of
(trying) to use the name of the site.
== Remove use of $wgSitename ==
In msg() and reportHTML(), remove use of $wgSitename in favour of a
generic fallback matching DefaulSettings.php. This does not affect
the common case of runtime fatals and timeouts, as we're only
changing the fallback after wfMessage() fails in msg(), or when
OutputPage is unavailable, which is typically only if services,
localisation or DB are also down (or not yet loaded). Most exceptions
happen when and after those have initialised fine.
Test case 1:
(Clean state) Edit ViewAction::show() to add `throw new RuntimeException();`
as its first statement, then try to view the main page.
This error page is unchanged. It is skinned, localised, and still uses
the configured sitename in the doc title.
Test case 2:
Edit index.php to call foo() instead of wfIndexMain(),
then try to view the main page. Before, this "minimal HTML" error page
would have a doc title of "Internal error - MyWiki", and now
"Internal error - MediaWiki".
Test case 3:
(Clean state) Edit Message::text() to add `throw new RuntimeException();`,
then try to view the main page. This results in a "plain text" error
page that doesn't even have an HTML doc, and is also unchanged.
== Remove use of $wgMimeType ==
In output(), remove use of $wgMimeType and remove the Content-Type
header that it was used for. This was redundant because the next
statements (reportHTML) already outputs Content-type. In reportHTML,
we sometimes delegate to OutputPage (if safe) and that honours
$wgMimeType already. In other cases, it is handled inline in with a
basic HTML error page, and that branch also sets the Content-Type
header already. In one case (reportOutageHTML) a header was not yet
set. For that one, I've added the missing header call and made it
explicitly text/html.
This is technically a bugfix, because our basic HTML error page is
HTML5, whereas $wgMimeType (which exists to allow enabling XHTML)
can be XHTML which we weren't following. In OutputPage and
Html::htmlHeader that would normally result in outputting `<?xml`.
Test case:
Edit ViewAction::show(), and add `throw new RuntimeException();`
as its first statement, then try to view the main page.
In devtools>network, there is still a proper Content-Type
and charset on the error page document.
Change-Id: I03cfa2b6155fb711582164852e7cab4c325a1b92
2022-01-12 23:05:34 +00:00
|
|
|
$this->msg( 'pagetitle', '$1 - MediaWiki', $this->getPageTitle() )
|
2017-10-18 06:27:12 +00:00
|
|
|
);
|
2014-02-24 20:13:49 +00:00
|
|
|
|
2017-05-21 08:29:41 +00:00
|
|
|
$wgOut->addHTML( $this->getHTML() );
|
exception: Simplify MWExceptionRenderer to reduce influence of config
Remove use of two configuration globals. This follows-up to
commit 47adb6d65a (I1a691f01cd82e60) which aimed to access all config
via MediaWikiServices, but that isn't safe during error handling.
These two were only used in the low-level "plain text" and
"basic HTML" error pages, which already can't have any branding,
skinning or localisation; and are not how most exceptions are rendered.
in those edge cases, we can use the name of the software instead of
(trying) to use the name of the site.
== Remove use of $wgSitename ==
In msg() and reportHTML(), remove use of $wgSitename in favour of a
generic fallback matching DefaulSettings.php. This does not affect
the common case of runtime fatals and timeouts, as we're only
changing the fallback after wfMessage() fails in msg(), or when
OutputPage is unavailable, which is typically only if services,
localisation or DB are also down (or not yet loaded). Most exceptions
happen when and after those have initialised fine.
Test case 1:
(Clean state) Edit ViewAction::show() to add `throw new RuntimeException();`
as its first statement, then try to view the main page.
This error page is unchanged. It is skinned, localised, and still uses
the configured sitename in the doc title.
Test case 2:
Edit index.php to call foo() instead of wfIndexMain(),
then try to view the main page. Before, this "minimal HTML" error page
would have a doc title of "Internal error - MyWiki", and now
"Internal error - MediaWiki".
Test case 3:
(Clean state) Edit Message::text() to add `throw new RuntimeException();`,
then try to view the main page. This results in a "plain text" error
page that doesn't even have an HTML doc, and is also unchanged.
== Remove use of $wgMimeType ==
In output(), remove use of $wgMimeType and remove the Content-Type
header that it was used for. This was redundant because the next
statements (reportHTML) already outputs Content-type. In reportHTML,
we sometimes delegate to OutputPage (if safe) and that honours
$wgMimeType already. In other cases, it is handled inline in with a
basic HTML error page, and that branch also sets the Content-Type
header already. In one case (reportOutageHTML) a header was not yet
set. For that one, I've added the missing header call and made it
explicitly text/html.
This is technically a bugfix, because our basic HTML error page is
HTML5, whereas $wgMimeType (which exists to allow enabling XHTML)
can be XHTML which we weren't following. In OutputPage and
Html::htmlHeader that would normally result in outputting `<?xml`.
Test case:
Edit ViewAction::show(), and add `throw new RuntimeException();`
as its first statement, then try to view the main page.
In devtools>network, there is still a proper Content-Type
and charset on the error page document.
Change-Id: I03cfa2b6155fb711582164852e7cab4c325a1b92
2022-01-12 23:05:34 +00:00
|
|
|
// Content-Type is set by OutputPage::output
|
2014-02-24 20:13:49 +00:00
|
|
|
$wgOut->output();
|
|
|
|
|
} else {
|
exception: Simplify MWExceptionRenderer to reduce influence of config
Remove use of two configuration globals. This follows-up to
commit 47adb6d65a (I1a691f01cd82e60) which aimed to access all config
via MediaWikiServices, but that isn't safe during error handling.
These two were only used in the low-level "plain text" and
"basic HTML" error pages, which already can't have any branding,
skinning or localisation; and are not how most exceptions are rendered.
in those edge cases, we can use the name of the software instead of
(trying) to use the name of the site.
== Remove use of $wgSitename ==
In msg() and reportHTML(), remove use of $wgSitename in favour of a
generic fallback matching DefaulSettings.php. This does not affect
the common case of runtime fatals and timeouts, as we're only
changing the fallback after wfMessage() fails in msg(), or when
OutputPage is unavailable, which is typically only if services,
localisation or DB are also down (or not yet loaded). Most exceptions
happen when and after those have initialised fine.
Test case 1:
(Clean state) Edit ViewAction::show() to add `throw new RuntimeException();`
as its first statement, then try to view the main page.
This error page is unchanged. It is skinned, localised, and still uses
the configured sitename in the doc title.
Test case 2:
Edit index.php to call foo() instead of wfIndexMain(),
then try to view the main page. Before, this "minimal HTML" error page
would have a doc title of "Internal error - MyWiki", and now
"Internal error - MediaWiki".
Test case 3:
(Clean state) Edit Message::text() to add `throw new RuntimeException();`,
then try to view the main page. This results in a "plain text" error
page that doesn't even have an HTML doc, and is also unchanged.
== Remove use of $wgMimeType ==
In output(), remove use of $wgMimeType and remove the Content-Type
header that it was used for. This was redundant because the next
statements (reportHTML) already outputs Content-type. In reportHTML,
we sometimes delegate to OutputPage (if safe) and that honours
$wgMimeType already. In other cases, it is handled inline in with a
basic HTML error page, and that branch also sets the Content-Type
header already. In one case (reportOutageHTML) a header was not yet
set. For that one, I've added the missing header call and made it
explicitly text/html.
This is technically a bugfix, because our basic HTML error page is
HTML5, whereas $wgMimeType (which exists to allow enabling XHTML)
can be XHTML which we weren't following. In OutputPage and
Html::htmlHeader that would normally result in outputting `<?xml`.
Test case:
Edit ViewAction::show(), and add `throw new RuntimeException();`
as its first statement, then try to view the main page.
In devtools>network, there is still a proper Content-Type
and charset on the error page document.
Change-Id: I03cfa2b6155fb711582164852e7cab4c325a1b92
2022-01-12 23:05:34 +00:00
|
|
|
self::header( 'Content-Type: text/html; charset=UTF-8' );
|
2014-02-24 20:13:49 +00:00
|
|
|
echo "<!DOCTYPE html>\n" .
|
|
|
|
|
'<html><head>' .
|
2022-01-09 17:30:20 +00:00
|
|
|
// Mimic OutputPage::setPageTitle behaviour
|
2014-04-24 19:50:01 +00:00
|
|
|
'<title>' .
|
exception: Simplify MWExceptionRenderer to reduce influence of config
Remove use of two configuration globals. This follows-up to
commit 47adb6d65a (I1a691f01cd82e60) which aimed to access all config
via MediaWikiServices, but that isn't safe during error handling.
These two were only used in the low-level "plain text" and
"basic HTML" error pages, which already can't have any branding,
skinning or localisation; and are not how most exceptions are rendered.
in those edge cases, we can use the name of the software instead of
(trying) to use the name of the site.
== Remove use of $wgSitename ==
In msg() and reportHTML(), remove use of $wgSitename in favour of a
generic fallback matching DefaulSettings.php. This does not affect
the common case of runtime fatals and timeouts, as we're only
changing the fallback after wfMessage() fails in msg(), or when
OutputPage is unavailable, which is typically only if services,
localisation or DB are also down (or not yet loaded). Most exceptions
happen when and after those have initialised fine.
Test case 1:
(Clean state) Edit ViewAction::show() to add `throw new RuntimeException();`
as its first statement, then try to view the main page.
This error page is unchanged. It is skinned, localised, and still uses
the configured sitename in the doc title.
Test case 2:
Edit index.php to call foo() instead of wfIndexMain(),
then try to view the main page. Before, this "minimal HTML" error page
would have a doc title of "Internal error - MyWiki", and now
"Internal error - MediaWiki".
Test case 3:
(Clean state) Edit Message::text() to add `throw new RuntimeException();`,
then try to view the main page. This results in a "plain text" error
page that doesn't even have an HTML doc, and is also unchanged.
== Remove use of $wgMimeType ==
In output(), remove use of $wgMimeType and remove the Content-Type
header that it was used for. This was redundant because the next
statements (reportHTML) already outputs Content-type. In reportHTML,
we sometimes delegate to OutputPage (if safe) and that honours
$wgMimeType already. In other cases, it is handled inline in with a
basic HTML error page, and that branch also sets the Content-Type
header already. In one case (reportOutageHTML) a header was not yet
set. For that one, I've added the missing header call and made it
explicitly text/html.
This is technically a bugfix, because our basic HTML error page is
HTML5, whereas $wgMimeType (which exists to allow enabling XHTML)
can be XHTML which we weren't following. In OutputPage and
Html::htmlHeader that would normally result in outputting `<?xml`.
Test case:
Edit ViewAction::show(), and add `throw new RuntimeException();`
as its first statement, then try to view the main page.
In devtools>network, there is still a proper Content-Type
and charset on the error page document.
Change-Id: I03cfa2b6155fb711582164852e7cab4c325a1b92
2022-01-12 23:05:34 +00:00
|
|
|
htmlspecialchars( $this->msg( 'pagetitle', '$1 - MediaWiki', $this->getPageTitle() ) ) .
|
2014-04-24 19:50:01 +00:00
|
|
|
'</title>' .
|
2014-02-24 20:13:49 +00:00
|
|
|
'<style>body { font-family: sans-serif; margin: 0; padding: 0.5em 2em; }</style>' .
|
|
|
|
|
"</head><body>\n";
|
|
|
|
|
|
2017-05-21 08:29:41 +00:00
|
|
|
echo $this->getHTML();
|
2014-02-24 20:13:49 +00:00
|
|
|
|
|
|
|
|
echo "</body></html>\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Output a report about the exception and takes care of formatting.
|
|
|
|
|
* It will be either HTML or plain text based on isCommandLine().
|
2020-07-10 12:11:14 +00:00
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2014-02-24 20:13:49 +00:00
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function report() {
|
2016-10-02 00:02:12 +00:00
|
|
|
if ( defined( 'MW_API' ) ) {
|
2017-03-07 02:14:14 +00:00
|
|
|
self::header( 'MediaWiki-API-Error: internal_api_error_' . static::class );
|
2020-03-25 16:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( self::isCommandLine() ) {
|
2016-10-02 00:02:12 +00:00
|
|
|
$message = $this->getText();
|
2019-02-17 11:36:13 +00:00
|
|
|
$this->writeToCommandLine( $message );
|
2016-10-02 00:02:12 +00:00
|
|
|
} else {
|
|
|
|
|
self::statusHeader( 500 );
|
|
|
|
|
$this->reportHTML();
|
|
|
|
|
}
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-17 11:36:13 +00:00
|
|
|
/**
|
|
|
|
|
* Write a message to stderr falling back to stdout if stderr unavailable
|
|
|
|
|
*
|
|
|
|
|
* @param string $message
|
|
|
|
|
* @suppress SecurityCheck-XSS
|
|
|
|
|
*/
|
|
|
|
|
private function writeToCommandLine( $message ) {
|
|
|
|
|
// T17602: STDERR may not be available
|
|
|
|
|
if ( !defined( 'MW_PHPUNIT_TEST' ) && defined( 'STDERR' ) ) {
|
|
|
|
|
fwrite( STDERR, $message );
|
|
|
|
|
} else {
|
|
|
|
|
echo $message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-24 20:13:49 +00:00
|
|
|
/**
|
|
|
|
|
* Check whether we are in command line mode or not to report the exception
|
|
|
|
|
* in the correct format.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public static function isCommandLine() {
|
2014-02-24 20:13:49 +00:00
|
|
|
return !empty( $GLOBALS['wgCommandLineMode'] );
|
|
|
|
|
}
|
2014-05-13 19:15:06 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a header, if we haven't already sent them. We shouldn't,
|
|
|
|
|
* but sometimes we might in a weird case like Export
|
2014-08-14 18:22:52 +00:00
|
|
|
* @param string $header
|
2014-05-13 19:15:06 +00:00
|
|
|
*/
|
|
|
|
|
private static function header( $header ) {
|
|
|
|
|
if ( !headers_sent() ) {
|
|
|
|
|
header( $header );
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-11 01:17:43 +00:00
|
|
|
|
2015-06-01 14:31:52 +00:00
|
|
|
private static function statusHeader( $code ) {
|
|
|
|
|
if ( !headers_sent() ) {
|
|
|
|
|
HttpStatus::header( $code );
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-24 20:13:49 +00:00
|
|
|
}
|