From 80be14530c7e0748fa9b6a01ad045f5d37b48cc3 Mon Sep 17 00:00:00 2001
From: Tim Starling
Date: Fri, 4 Sep 2009 01:49:34 +0000
Subject: [PATCH] Added $wgShowDBErrorBacktrace, to allow users to easily
gather backtraces for database connection and query errors. We've had a few
LocalisationCache bugs that required backtraces to track down.
---
RELEASE-NOTES | 2 ++
includes/DefaultSettings.php | 5 ++++
includes/db/Database.php | 44 +++++++++++++++++++++++-------------
3 files changed, 35 insertions(+), 16 deletions(-)
diff --git a/RELEASE-NOTES b/RELEASE-NOTES
index 8a20f61b05..83fd27da7c 100644
--- a/RELEASE-NOTES
+++ b/RELEASE-NOTES
@@ -201,6 +201,8 @@ this. Was used when mwEmbed was going to be an extension.
display-in-limit-form.
* (bug 18880) LogEventsList::showLogExtract() can now take a string-by-reference
and add its HTML to it, rather than having to go straight to $wgOut.
+* Added $wgShowDBErrorBacktrace, to allow users to easily gather backtraces for
+ database connection and query errors.
=== Bug fixes in 1.16 ===
diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index d5e0555b28..985a6e4f93 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -1172,6 +1172,11 @@ $wgColorErrors = true;
*/
$wgShowExceptionDetails = false;
+/**
+ * If true, show a backtrace for database errors
+ */
+$wgShowDBErrorBacktrace = false;
+
/**
* Expose backend server host names through the API and various HTML comments
*/
diff --git a/includes/db/Database.php b/includes/db/Database.php
index a8659b68bc..34e33dbb61 100644
--- a/includes/db/Database.php
+++ b/includes/db/Database.php
@@ -2420,6 +2420,15 @@ class DBError extends MWException {
$this->db =& $db;
parent::__construct( $error );
}
+
+ function getText() {
+ global $wgShowDBErrorBacktrace;
+ $s = $this->getMessage() . "\n";
+ if ( $wgShowDBErrorBacktrace ) {
+ $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
+ }
+ return $s;
+ }
}
/**
@@ -2447,10 +2456,6 @@ class DBConnectionError extends DBError {
return false;
}
- function getText() {
- return $this->getMessage() . "\n";
- }
-
function getLogMessage() {
# Don't send to the exception log
return false;
@@ -2467,7 +2472,7 @@ class DBConnectionError extends DBError {
}
function getHTML() {
- global $wgLang, $wgMessageCache, $wgUseFileCache;
+ global $wgLang, $wgMessageCache, $wgUseFileCache, $wgShowDBErrorBacktrace;
$sorry = 'Sorry! This site is experiencing technical difficulties.';
$again = 'Try waiting a few minutes and reloading.';
@@ -2491,12 +2496,9 @@ class DBConnectionError extends DBError {
$noconnect = "$sorry
$again
$info
";
$text = str_replace( '$1', $this->error, $noconnect );
- /*
- if ( $GLOBALS['wgShowExceptionDetails'] ) {
- $text .= '
Backtrace:
' .
- nl2br( htmlspecialchars( $this->getTraceAsString() ) ) .
- "
\n";
- }*/
+ if ( $wgShowDBErrorBacktrace ) {
+ $text .= 'Backtrace:
' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
+ }
$extra = $this->searchForm();
@@ -2613,11 +2615,16 @@ class DBQueryError extends DBError {
}
function getText() {
+ global $wgShowDBErrorBacktrace;
if ( $this->useMessageCache() ) {
- return wfMsg( 'dberrortextcl', htmlspecialchars( $this->getSQL() ),
- htmlspecialchars( $this->fname ), $this->errno, htmlspecialchars( $this->error ) ) . "\n";
+ $s = wfMsg( 'dberrortextcl', htmlspecialchars( $this->getSQL() ),
+ htmlspecialchars( $this->fname ), $this->errno, htmlspecialchars( $this->error ) ) . "\n";
+ if ( $wgShowDBErrorBacktrace ) {
+ $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
+ }
+ return $s;
} else {
- return $this->getMessage();
+ return parent::getText();
}
}
@@ -2640,12 +2647,17 @@ class DBQueryError extends DBError {
}
function getHTML() {
+ global $wgShowDBErrorBacktrace;
if ( $this->useMessageCache() ) {
- return wfMsgNoDB( 'dberrortext', htmlspecialchars( $this->getSQL() ),
+ $s = wfMsgNoDB( 'dberrortext', htmlspecialchars( $this->getSQL() ),
htmlspecialchars( $this->fname ), $this->errno, htmlspecialchars( $this->error ) );
} else {
- return nl2br( htmlspecialchars( $this->getMessage() ) );
+ $s = nl2br( htmlspecialchars( $this->getMessage() ) );
+ }
+ if ( $wgShowDBErrorBacktrace ) {
+ $s .= '
Backtrace:
' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
}
+ return $s;
}
}
--
2.20.1