3 * This file contains database error classes.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Database error base class
28 class DBError
extends MWException
{
29 /** @var DatabaseBase */
33 * Construct a database error
34 * @param DatabaseBase $db Object which threw the error
35 * @param string $error A simple error message to be used for debugging
37 function __construct( DatabaseBase
$db = null, $error ) {
39 parent
::__construct( $error );
46 global $wgShowDBErrorBacktrace;
48 $s = $this->getTextContent() . "\n";
50 if ( $wgShowDBErrorBacktrace ) {
51 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
61 global $wgShowDBErrorBacktrace;
63 $s = $this->getHTMLContent();
65 if ( $wgShowDBErrorBacktrace ) {
66 $s .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
75 protected function getTextContent() {
76 return $this->getMessage();
82 protected function getHTMLContent() {
83 return '<p>' . nl2br( htmlspecialchars( $this->getMessage() ) ) . '</p>';
90 class DBConnectionError
extends DBError
{
91 /** @var string Error text */
95 * @param DatabaseBase $db Object throwing the error
96 * @param string $error Error text
98 function __construct( DatabaseBase
$db = null, $error = 'unknown error' ) {
99 $msg = 'DB connection error';
101 if ( trim( $error ) != '' ) {
104 $error = $this->db
->getServer();
107 parent
::__construct( $db, $msg );
108 $this->error
= $error;
114 function useOutputPage() {
115 // Not likely to work
121 * @param string $fallback Unescaped alternative error text in case the
122 * message cache cannot be used. Can contain parameters as in regular
123 * messages, that should be passed as additional parameters.
124 * @return string Unprocessed plain error text with parameters replaced
126 function msg( $key, $fallback /*[, params...] */ ) {
129 $args = array_slice( func_get_args(), 2 );
131 if ( $this->useMessageCache() ) {
132 $message = $wgLang->getMessage( $key );
134 $message = $fallback;
137 return wfMsgReplaceArgs( $message, $args );
143 function isLoggable() {
144 // Don't send to the exception log, already in dberror log
149 * @return string Safe HTML
152 global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
154 $sorry = htmlspecialchars( $this->msg(
156 'Sorry! This site is experiencing technical difficulties.'
158 $again = htmlspecialchars( $this->msg(
160 'Try waiting a few minutes and reloading.'
163 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
165 '$1', Html
::element( 'span', array( 'dir' => 'ltr' ), $this->error
),
166 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot contact the database server: $1)' ) )
169 $info = htmlspecialchars( $this->msg(
171 '(Cannot contact the database server)'
176 MessageCache
::singleton()->disable();
178 $html = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
180 if ( $wgShowDBErrorBacktrace ) {
181 $html .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
185 $html .= $this->searchForm();
190 protected function getTextContent() {
191 global $wgShowHostnames, $wgShowSQLErrors;
193 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
194 return $this->getMessage();
196 return 'DB connection error';
201 * Output the exception report using HTML.
205 public function reportHTML() {
206 global $wgUseFileCache;
208 // Check whether we can serve a file-cached copy of the page with the error underneath
209 if ( $wgUseFileCache ) {
211 $cache = $this->fileCachedPage();
212 // Cached version on file system?
213 if ( $cache !== null ) {
214 // Hack: extend the body for error messages
215 $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
216 // Add cache notice...
217 $cache .= '<div style="border:1px solid #ffd0d0;padding:1em;">' .
218 htmlspecialchars( $this->msg( 'dberr-cachederror',
219 'This is a cached copy of the requested page, and may not be up to date.' ) ) .
222 // Output cached page with notices on bottom and re-close body
223 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
227 } catch ( MWException
$e ) {
228 // Do nothing, just use the default page
232 // We can't, cough and die in the usual fashion
233 parent
::reportHTML();
239 function searchForm() {
240 global $wgSitename, $wgCanonicalServer, $wgRequest;
242 $usegoogle = htmlspecialchars( $this->msg(
244 'You can try searching via Google in the meantime.'
246 $outofdate = htmlspecialchars( $this->msg(
248 'Note that their indexes of our content may be out of date.'
250 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
252 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
254 $server = htmlspecialchars( $wgCanonicalServer );
255 $sitename = htmlspecialchars( $wgSitename );
258 <div style="margin: 1.5em">$usegoogle<br />
259 <small>$outofdate</small>
261 <form method="get" action="//www.google.com/search" id="googlesearch">
262 <input type="hidden" name="domains" value="$server" />
263 <input type="hidden" name="num" value="50" />
264 <input type="hidden" name="ie" value="UTF-8" />
265 <input type="hidden" name="oe" value="UTF-8" />
267 <input type="text" name="q" size="31" maxlength="255" value="$search" />
268 <input type="submit" name="btnG" value="$googlesearch" />
270 <label><input type="radio" name="sitesearch" value="$server" checked="checked" />$sitename</label>
271 <label><input type="radio" name="sitesearch" value="" />WWW</label>
282 private function fileCachedPage() {
283 $context = RequestContext
::getMain();
285 if ( $context->getOutput()->isDisabled() ) {
290 if ( $context->getTitle() ) {
291 // Use the main context's title if we managed to set it
292 $t = $context->getTitle()->getPrefixedDBkey();
294 // Fallback to the raw title URL param. We can't use the Title
295 // class is it may hit the interwiki table and give a DB error.
296 // We may get a cache miss due to not sanitizing the title though.
297 $t = str_replace( ' ', '_', $context->getRequest()->getVal( 'title' ) );
298 if ( $t == '' ) { // fallback to main page
299 $t = Title
::newFromText(
300 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
304 $cache = HTMLFileCache
::newFromTitle( $t, 'view' );
305 if ( $cache->isCached() ) {
306 return $cache->fetchText();
316 class DBQueryError
extends DBError
{
317 public $error, $errno, $sql, $fname;
320 * @param DatabaseBase $db
321 * @param string $error
322 * @param int|string $errno
324 * @param string $fname
326 function __construct( DatabaseBase
$db, $error, $errno, $sql, $fname ) {
327 $message = "A database error has occurred. Did you forget to run " .
328 "maintenance/update.php after upgrading? See: " .
329 "https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
331 "Function: $fname\n" .
332 "Error: $errno $error\n";
333 parent
::__construct( $db, $message );
335 $this->error
= $error;
336 $this->errno
= $errno;
338 $this->fname
= $fname;
344 function isLoggable() {
345 // Don't send to the exception log, already in dberror log
352 function getPageTitle() {
353 return $this->msg( 'databaseerror', 'Database error' );
359 protected function getHTMLContent() {
360 $key = 'databaseerror-text';
361 $s = Html
::element( 'p', array(), $this->msg( $key, $this->getFallbackMessage( $key ) ) );
363 $details = $this->getTechnicalDetails();
366 foreach ( $details as $key => $detail ) {
368 '$1', call_user_func_array( 'Html::element', $detail ),
369 Html
::element( 'li', array(),
370 $this->msg( $key, $this->getFallbackMessage( $key ) )
383 protected function getTextContent() {
384 $key = 'databaseerror-textcl';
385 $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
387 foreach ( $this->getTechnicalDetails() as $key => $detail ) {
388 $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
395 * Make a list of technical details that can be shown to the user. This information can
396 * aid in debugging yet may be useful to an attacker trying to exploit a security weakness
397 * in the software or server configuration.
399 * Thus no such details are shown by default, though if $wgShowHostnames is true, only the
400 * full SQL query is hidden; in fact, the error message often does contain a hostname, and
401 * sites using this option probably don't care much about "security by obscurity". Of course,
402 * if $wgShowSQLErrors is true, the SQL query *is* shown.
404 * @return array Keys are message keys; values are arrays of arguments for Html::element().
405 * Array will be empty if users are not allowed to see any of these details at all.
407 protected function getTechnicalDetails() {
408 global $wgShowHostnames, $wgShowSQLErrors;
410 $attribs = array( 'dir' => 'ltr' );
413 if ( $wgShowSQLErrors ) {
414 $details['databaseerror-query'] = array(
415 'div', array( 'class' => 'mw-code' ) +
$attribs, $this->sql
);
418 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
419 $errorMessage = $this->errno
. ' ' . $this->error
;
420 $details['databaseerror-function'] = array( 'code', $attribs, $this->fname
);
421 $details['databaseerror-error'] = array( 'samp', $attribs, $errorMessage );
428 * @param string $key Message key
429 * @return string English message text
431 private function getFallbackMessage( $key ) {
433 'databaseerror-text' => 'A database query error has occurred.
434 This may indicate a bug in the software.',
435 'databaseerror-textcl' => 'A database query error has occurred.',
436 'databaseerror-query' => 'Query: $1',
437 'databaseerror-function' => 'Function: $1',
438 'databaseerror-error' => 'Error: $1',
441 return $messages[$key];
448 class DBUnexpectedError
extends DBError
{