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 );
44 * Base class for the more common types of database errors. These are known to occur
45 * frequently, so we try to give friendly error messages for them.
50 class DBExpectedError
extends DBError
{
55 global $wgShowDBErrorBacktrace;
57 $s = $this->getTextContent() . "\n";
59 if ( $wgShowDBErrorBacktrace ) {
60 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
70 global $wgShowDBErrorBacktrace;
72 $s = $this->getHTMLContent();
74 if ( $wgShowDBErrorBacktrace ) {
75 $s .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
81 function getPageTitle() {
82 return $this->msg( 'databaseerror', 'Database error' );
88 protected function getTextContent() {
89 return $this->getMessage();
95 protected function getHTMLContent() {
96 return '<p>' . nl2br( htmlspecialchars( $this->getTextContent() ) ) . '</p>';
103 class DBConnectionError
extends DBExpectedError
{
104 /** @var string Error text */
108 * @param DatabaseBase $db Object throwing the error
109 * @param string $error Error text
111 function __construct( DatabaseBase
$db = null, $error = 'unknown error' ) {
112 $msg = 'DB connection error';
114 if ( trim( $error ) != '' ) {
117 $error = $this->db
->getServer();
120 parent
::__construct( $db, $msg );
121 $this->error
= $error;
127 function useOutputPage() {
128 // Not likely to work
134 * @param string $fallback Unescaped alternative error text in case the
135 * message cache cannot be used. Can contain parameters as in regular
136 * messages, that should be passed as additional parameters.
137 * @return string Unprocessed plain error text with parameters replaced
139 function msg( $key, $fallback /*[, params...] */ ) {
140 $args = array_slice( func_get_args(), 2 );
142 if ( $this->useMessageCache() ) {
143 return wfMessage( $key, $args )->useDatabase( false )->text();
145 return wfMsgReplaceArgs( $fallback, $args );
152 function isLoggable() {
153 // Don't send to the exception log, already in dberror log
158 * @return string Safe HTML
161 global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
163 $sorry = htmlspecialchars( $this->msg(
165 'Sorry! This site is experiencing technical difficulties.'
167 $again = htmlspecialchars( $this->msg(
169 'Try waiting a few minutes and reloading.'
172 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
174 '$1', Html
::element( 'span', [ 'dir' => 'ltr' ], $this->error
),
175 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot access the database: $1)' ) )
178 $info = htmlspecialchars( $this->msg(
180 '(Cannot access the database)'
185 MessageCache
::singleton()->disable();
187 $html = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
189 if ( $wgShowDBErrorBacktrace ) {
190 $html .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
194 $html .= $this->searchForm();
199 protected function getTextContent() {
200 global $wgShowHostnames, $wgShowSQLErrors;
202 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
203 return $this->getMessage();
205 return 'DB connection error';
210 * Output the exception report using HTML.
214 public function reportHTML() {
215 global $wgUseFileCache;
217 // Check whether we can serve a file-cached copy of the page with the error underneath
218 if ( $wgUseFileCache ) {
220 $cache = $this->fileCachedPage();
221 // Cached version on file system?
222 if ( $cache !== null ) {
223 // Hack: extend the body for error messages
224 $cache = str_replace( [ '</html>', '</body>' ], '', $cache );
225 // Add cache notice...
226 $cache .= '<div style="border:1px solid #ffd0d0;padding:1em;">' .
227 htmlspecialchars( $this->msg( 'dberr-cachederror',
228 'This is a cached copy of the requested page, and may not be up to date.' ) ) .
231 // Output cached page with notices on bottom and re-close body
232 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
236 } catch ( Exception
$e ) {
237 // Do nothing, just use the default page
241 // We can't, cough and die in the usual fashion
242 parent
::reportHTML();
248 function searchForm() {
249 global $wgSitename, $wgCanonicalServer, $wgRequest;
251 $usegoogle = htmlspecialchars( $this->msg(
253 'You can try searching via Google in the meantime.'
255 $outofdate = htmlspecialchars( $this->msg(
257 'Note that their indexes of our content may be out of date.'
259 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
261 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
263 $server = htmlspecialchars( $wgCanonicalServer );
264 $sitename = htmlspecialchars( $wgSitename );
267 <div style="margin: 1.5em">$usegoogle<br />
268 <small>$outofdate</small>
270 <form method="get" action="//www.google.com/search" id="googlesearch">
271 <input type="hidden" name="domains" value="$server" />
272 <input type="hidden" name="num" value="50" />
273 <input type="hidden" name="ie" value="UTF-8" />
274 <input type="hidden" name="oe" value="UTF-8" />
276 <input type="text" name="q" size="31" maxlength="255" value="$search" />
277 <input type="submit" name="btnG" value="$googlesearch" />
279 <label><input type="radio" name="sitesearch" value="$server" checked="checked" />$sitename</label>
280 <label><input type="radio" name="sitesearch" value="" />WWW</label>
291 private function fileCachedPage() {
292 $context = RequestContext
::getMain();
294 if ( $context->getOutput()->isDisabled() ) {
299 if ( $context->getTitle() ) {
300 // Use the main context's title if we managed to set it
301 $t = $context->getTitle()->getPrefixedDBkey();
303 // Fallback to the raw title URL param. We can't use the Title
304 // class is it may hit the interwiki table and give a DB error.
305 // We may get a cache miss due to not sanitizing the title though.
306 $t = str_replace( ' ', '_', $context->getRequest()->getVal( 'title' ) );
307 if ( $t == '' ) { // fallback to main page
308 $t = Title
::newFromText(
309 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
313 $cache = new HTMLFileCache( $t, 'view' );
314 if ( $cache->isCached() ) {
315 return $cache->fetchText();
325 class DBQueryError
extends DBExpectedError
{
326 public $error, $errno, $sql, $fname;
329 * @param DatabaseBase $db
330 * @param string $error
331 * @param int|string $errno
333 * @param string $fname
335 function __construct( DatabaseBase
$db, $error, $errno, $sql, $fname ) {
336 if ( $db->wasConnectionError( $errno ) ) {
337 $message = "A connection error occured. \n" .
339 "Function: $fname\n" .
340 "Error: $errno $error\n";
342 $message = "A database error has occurred. Did you forget to run " .
343 "maintenance/update.php after upgrading? See: " .
344 "https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
346 "Function: $fname\n" .
347 "Error: $errno $error\n";
349 parent
::__construct( $db, $message );
351 $this->error
= $error;
352 $this->errno
= $errno;
354 $this->fname
= $fname;
360 function getPageTitle() {
361 return $this->msg( 'databaseerror', 'Database error' );
367 protected function getHTMLContent() {
368 $key = 'databaseerror-text';
369 $s = Html
::element( 'p', [], $this->msg( $key, $this->getFallbackMessage( $key ) ) );
371 $details = $this->getTechnicalDetails();
374 foreach ( $details as $key => $detail ) {
376 '$1', call_user_func_array( 'Html::element', $detail ),
377 Html
::element( 'li', [],
378 $this->msg( $key, $this->getFallbackMessage( $key ) )
391 protected function getTextContent() {
392 $key = 'databaseerror-textcl';
393 $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
395 foreach ( $this->getTechnicalDetails() as $key => $detail ) {
396 $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
403 * Make a list of technical details that can be shown to the user. This information can
404 * aid in debugging yet may be useful to an attacker trying to exploit a security weakness
405 * in the software or server configuration.
407 * Thus no such details are shown by default, though if $wgShowHostnames is true, only the
408 * full SQL query is hidden; in fact, the error message often does contain a hostname, and
409 * sites using this option probably don't care much about "security by obscurity". Of course,
410 * if $wgShowSQLErrors is true, the SQL query *is* shown.
412 * @return array Keys are message keys; values are arrays of arguments for Html::element().
413 * Array will be empty if users are not allowed to see any of these details at all.
415 protected function getTechnicalDetails() {
416 global $wgShowHostnames, $wgShowSQLErrors;
418 $attribs = [ 'dir' => 'ltr' ];
421 if ( $wgShowSQLErrors ) {
422 $details['databaseerror-query'] = [
423 'div', [ 'class' => 'mw-code' ] +
$attribs, $this->sql
];
426 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
427 $errorMessage = $this->errno
. ' ' . $this->error
;
428 $details['databaseerror-function'] = [ 'code', $attribs, $this->fname
];
429 $details['databaseerror-error'] = [ 'samp', $attribs, $errorMessage ];
436 * @param string $key Message key
437 * @return string English message text
439 private function getFallbackMessage( $key ) {
441 'databaseerror-text' => 'A database query error has occurred.
442 This may indicate a bug in the software.',
443 'databaseerror-textcl' => 'A database query error has occurred.',
444 'databaseerror-query' => 'Query: $1',
445 'databaseerror-function' => 'Function: $1',
446 'databaseerror-error' => 'Error: $1',
449 return $messages[$key];
456 class DBUnexpectedError
extends DBError
{
462 class DBReadOnlyError
extends DBExpectedError
{
463 function getPageTitle() {
464 return $this->msg( 'readonly', 'Database is locked' );
471 class DBTransactionError
extends DBExpectedError
{