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>';
84 protected function getTextContent() {
85 return $this->getMessage();
91 protected function getHTMLContent() {
92 return '<p>' . nl2br( htmlspecialchars( $this->getTextContent() ) ) . '</p>';
99 class DBConnectionError
extends DBExpectedError
{
100 /** @var string Error text */
104 * @param DatabaseBase $db Object throwing the error
105 * @param string $error Error text
107 function __construct( DatabaseBase
$db = null, $error = 'unknown error' ) {
108 $msg = 'DB connection error';
110 if ( trim( $error ) != '' ) {
113 $error = $this->db
->getServer();
116 parent
::__construct( $db, $msg );
117 $this->error
= $error;
123 function useOutputPage() {
124 // Not likely to work
130 * @param string $fallback Unescaped alternative error text in case the
131 * message cache cannot be used. Can contain parameters as in regular
132 * messages, that should be passed as additional parameters.
133 * @return string Unprocessed plain error text with parameters replaced
135 function msg( $key, $fallback /*[, params...] */ ) {
136 $args = array_slice( func_get_args(), 2 );
138 if ( $this->useMessageCache() ) {
139 $msg = wfMessage( $key )->useDatabase( false );
141 $msg = new RawMessage( $fallback );
144 return $msg->params( $args )->text();
150 function isLoggable() {
151 // Don't send to the exception log, already in dberror log
156 * @return string Safe HTML
159 global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
161 $sorry = htmlspecialchars( $this->msg(
163 'Sorry! This site is experiencing technical difficulties.'
165 $again = htmlspecialchars( $this->msg(
167 'Try waiting a few minutes and reloading.'
170 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
172 '$1', Html
::element( 'span', array( 'dir' => 'ltr' ), $this->error
),
173 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot contact the database server: $1)' ) )
176 $info = htmlspecialchars( $this->msg(
178 '(Cannot contact the database server)'
183 MessageCache
::singleton()->disable();
185 $html = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
187 if ( $wgShowDBErrorBacktrace ) {
188 $html .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
192 $html .= $this->searchForm();
197 protected function getTextContent() {
198 global $wgShowHostnames, $wgShowSQLErrors;
200 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
201 return $this->getMessage();
203 return 'DB connection error';
208 * Output the exception report using HTML.
212 public function reportHTML() {
213 global $wgUseFileCache;
215 // Check whether we can serve a file-cached copy of the page with the error underneath
216 if ( $wgUseFileCache ) {
218 $cache = $this->fileCachedPage();
219 // Cached version on file system?
220 if ( $cache !== null ) {
221 // Hack: extend the body for error messages
222 $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
223 // Add cache notice...
224 $cache .= '<div style="border:1px solid #ffd0d0;padding:1em;">' .
225 htmlspecialchars( $this->msg( 'dberr-cachederror',
226 'This is a cached copy of the requested page, and may not be up to date.' ) ) .
229 // Output cached page with notices on bottom and re-close body
230 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
234 } catch ( MWException
$e ) {
235 // Do nothing, just use the default page
239 // We can't, cough and die in the usual fashion
240 parent
::reportHTML();
246 function searchForm() {
247 global $wgSitename, $wgCanonicalServer, $wgRequest;
249 $usegoogle = htmlspecialchars( $this->msg(
251 'You can try searching via Google in the meantime.'
253 $outofdate = htmlspecialchars( $this->msg(
255 'Note that their indexes of our content may be out of date.'
257 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
259 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
261 $server = htmlspecialchars( $wgCanonicalServer );
262 $sitename = htmlspecialchars( $wgSitename );
265 <div style="margin: 1.5em">$usegoogle<br />
266 <small>$outofdate</small>
268 <form method="get" action="//www.google.com/search" id="googlesearch">
269 <input type="hidden" name="domains" value="$server" />
270 <input type="hidden" name="num" value="50" />
271 <input type="hidden" name="ie" value="UTF-8" />
272 <input type="hidden" name="oe" value="UTF-8" />
274 <input type="text" name="q" size="31" maxlength="255" value="$search" />
275 <input type="submit" name="btnG" value="$googlesearch" />
277 <label><input type="radio" name="sitesearch" value="$server" checked="checked" />$sitename</label>
278 <label><input type="radio" name="sitesearch" value="" />WWW</label>
289 private function fileCachedPage() {
290 $context = RequestContext
::getMain();
292 if ( $context->getOutput()->isDisabled() ) {
297 if ( $context->getTitle() ) {
298 // Use the main context's title if we managed to set it
299 $t = $context->getTitle()->getPrefixedDBkey();
301 // Fallback to the raw title URL param. We can't use the Title
302 // class is it may hit the interwiki table and give a DB error.
303 // We may get a cache miss due to not sanitizing the title though.
304 $t = str_replace( ' ', '_', $context->getRequest()->getVal( 'title' ) );
305 if ( $t == '' ) { // fallback to main page
306 $t = Title
::newFromText(
307 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
311 $cache = new HTMLFileCache( $t, 'view' );
312 if ( $cache->isCached() ) {
313 return $cache->fetchText();
323 class DBQueryError
extends DBExpectedError
{
324 public $error, $errno, $sql, $fname;
327 * @param DatabaseBase $db
328 * @param string $error
329 * @param int|string $errno
331 * @param string $fname
333 function __construct( DatabaseBase
$db, $error, $errno, $sql, $fname ) {
334 $message = "A database error has occurred. Did you forget to run " .
335 "maintenance/update.php after upgrading? See: " .
336 "https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
338 "Function: $fname\n" .
339 "Error: $errno $error\n";
340 parent
::__construct( $db, $message );
342 $this->error
= $error;
343 $this->errno
= $errno;
345 $this->fname
= $fname;
351 function getPageTitle() {
352 return $this->msg( 'databaseerror', 'Database error' );
358 protected function getHTMLContent() {
359 $key = 'databaseerror-text';
360 $s = Html
::element( 'p', array(), $this->msg( $key, $this->getFallbackMessage( $key ) ) );
362 $details = $this->getTechnicalDetails();
365 foreach ( $details as $key => $detail ) {
367 '$1', call_user_func_array( 'Html::element', $detail ),
368 Html
::element( 'li', array(),
369 $this->msg( $key, $this->getFallbackMessage( $key ) )
382 protected function getTextContent() {
383 $key = 'databaseerror-textcl';
384 $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
386 foreach ( $this->getTechnicalDetails() as $key => $detail ) {
387 $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
394 * Make a list of technical details that can be shown to the user. This information can
395 * aid in debugging yet may be useful to an attacker trying to exploit a security weakness
396 * in the software or server configuration.
398 * Thus no such details are shown by default, though if $wgShowHostnames is true, only the
399 * full SQL query is hidden; in fact, the error message often does contain a hostname, and
400 * sites using this option probably don't care much about "security by obscurity". Of course,
401 * if $wgShowSQLErrors is true, the SQL query *is* shown.
403 * @return array Keys are message keys; values are arrays of arguments for Html::element().
404 * Array will be empty if users are not allowed to see any of these details at all.
406 protected function getTechnicalDetails() {
407 global $wgShowHostnames, $wgShowSQLErrors;
409 $attribs = array( 'dir' => 'ltr' );
412 if ( $wgShowSQLErrors ) {
413 $details['databaseerror-query'] = array(
414 'div', array( 'class' => 'mw-code' ) +
$attribs, $this->sql
);
417 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
418 $errorMessage = $this->errno
. ' ' . $this->error
;
419 $details['databaseerror-function'] = array( 'code', $attribs, $this->fname
);
420 $details['databaseerror-error'] = array( 'samp', $attribs, $errorMessage );
427 * @param string $key Message key
428 * @return string English message text
430 private function getFallbackMessage( $key ) {
432 'databaseerror-text' => 'A database query error has occurred.
433 This may indicate a bug in the software.',
434 'databaseerror-textcl' => 'A database query error has occurred.',
435 'databaseerror-query' => 'Query: $1',
436 'databaseerror-function' => 'Function: $1',
437 'databaseerror-error' => 'Error: $1',
440 return $messages[$key];
447 class DBUnexpectedError
extends DBError
{