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 return wfMessage( $key, $args )->useDatabase( false )->text();
141 return wfMsgReplaceArgs( $fallback, $args );
148 function isLoggable() {
149 // Don't send to the exception log, already in dberror log
154 * @return string Safe HTML
157 global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
159 $sorry = htmlspecialchars( $this->msg(
161 'Sorry! This site is experiencing technical difficulties.'
163 $again = htmlspecialchars( $this->msg(
165 'Try waiting a few minutes and reloading.'
168 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
170 '$1', Html
::element( 'span', array( 'dir' => 'ltr' ), $this->error
),
171 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot access the database: $1)' ) )
174 $info = htmlspecialchars( $this->msg(
176 '(Cannot access the database)'
181 MessageCache
::singleton()->disable();
183 $html = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
185 if ( $wgShowDBErrorBacktrace ) {
186 $html .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
190 $html .= $this->searchForm();
195 protected function getTextContent() {
196 global $wgShowHostnames, $wgShowSQLErrors;
198 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
199 return $this->getMessage();
201 return 'DB connection error';
206 * Output the exception report using HTML.
210 public function reportHTML() {
211 global $wgUseFileCache;
213 // Check whether we can serve a file-cached copy of the page with the error underneath
214 if ( $wgUseFileCache ) {
216 $cache = $this->fileCachedPage();
217 // Cached version on file system?
218 if ( $cache !== null ) {
219 // Hack: extend the body for error messages
220 $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
221 // Add cache notice...
222 $cache .= '<div style="border:1px solid #ffd0d0;padding:1em;">' .
223 htmlspecialchars( $this->msg( 'dberr-cachederror',
224 'This is a cached copy of the requested page, and may not be up to date.' ) ) .
227 // Output cached page with notices on bottom and re-close body
228 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
232 } catch ( Exception
$e ) {
233 // Do nothing, just use the default page
237 // We can't, cough and die in the usual fashion
238 parent
::reportHTML();
244 function searchForm() {
245 global $wgSitename, $wgCanonicalServer, $wgRequest;
247 $usegoogle = htmlspecialchars( $this->msg(
249 'You can try searching via Google in the meantime.'
251 $outofdate = htmlspecialchars( $this->msg(
253 'Note that their indexes of our content may be out of date.'
255 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
257 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
259 $server = htmlspecialchars( $wgCanonicalServer );
260 $sitename = htmlspecialchars( $wgSitename );
263 <div style="margin: 1.5em">$usegoogle<br />
264 <small>$outofdate</small>
266 <form method="get" action="//www.google.com/search" id="googlesearch">
267 <input type="hidden" name="domains" value="$server" />
268 <input type="hidden" name="num" value="50" />
269 <input type="hidden" name="ie" value="UTF-8" />
270 <input type="hidden" name="oe" value="UTF-8" />
272 <input type="text" name="q" size="31" maxlength="255" value="$search" />
273 <input type="submit" name="btnG" value="$googlesearch" />
275 <label><input type="radio" name="sitesearch" value="$server" checked="checked" />$sitename</label>
276 <label><input type="radio" name="sitesearch" value="" />WWW</label>
287 private function fileCachedPage() {
288 $context = RequestContext
::getMain();
290 if ( $context->getOutput()->isDisabled() ) {
295 if ( $context->getTitle() ) {
296 // Use the main context's title if we managed to set it
297 $t = $context->getTitle()->getPrefixedDBkey();
299 // Fallback to the raw title URL param. We can't use the Title
300 // class is it may hit the interwiki table and give a DB error.
301 // We may get a cache miss due to not sanitizing the title though.
302 $t = str_replace( ' ', '_', $context->getRequest()->getVal( 'title' ) );
303 if ( $t == '' ) { // fallback to main page
304 $t = Title
::newFromText(
305 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
309 $cache = new HTMLFileCache( $t, 'view' );
310 if ( $cache->isCached() ) {
311 return $cache->fetchText();
321 class DBQueryError
extends DBExpectedError
{
322 public $error, $errno, $sql, $fname;
325 * @param DatabaseBase $db
326 * @param string $error
327 * @param int|string $errno
329 * @param string $fname
331 function __construct( DatabaseBase
$db, $error, $errno, $sql, $fname ) {
332 $message = "A database error has occurred. Did you forget to run " .
333 "maintenance/update.php after upgrading? See: " .
334 "https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
336 "Function: $fname\n" .
337 "Error: $errno $error\n";
338 parent
::__construct( $db, $message );
340 $this->error
= $error;
341 $this->errno
= $errno;
343 $this->fname
= $fname;
349 function getPageTitle() {
350 return $this->msg( 'databaseerror', 'Database error' );
356 protected function getHTMLContent() {
357 $key = 'databaseerror-text';
358 $s = Html
::element( 'p', array(), $this->msg( $key, $this->getFallbackMessage( $key ) ) );
360 $details = $this->getTechnicalDetails();
363 foreach ( $details as $key => $detail ) {
365 '$1', call_user_func_array( 'Html::element', $detail ),
366 Html
::element( 'li', array(),
367 $this->msg( $key, $this->getFallbackMessage( $key ) )
380 protected function getTextContent() {
381 $key = 'databaseerror-textcl';
382 $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
384 foreach ( $this->getTechnicalDetails() as $key => $detail ) {
385 $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
392 * Make a list of technical details that can be shown to the user. This information can
393 * aid in debugging yet may be useful to an attacker trying to exploit a security weakness
394 * in the software or server configuration.
396 * Thus no such details are shown by default, though if $wgShowHostnames is true, only the
397 * full SQL query is hidden; in fact, the error message often does contain a hostname, and
398 * sites using this option probably don't care much about "security by obscurity". Of course,
399 * if $wgShowSQLErrors is true, the SQL query *is* shown.
401 * @return array Keys are message keys; values are arrays of arguments for Html::element().
402 * Array will be empty if users are not allowed to see any of these details at all.
404 protected function getTechnicalDetails() {
405 global $wgShowHostnames, $wgShowSQLErrors;
407 $attribs = array( 'dir' => 'ltr' );
410 if ( $wgShowSQLErrors ) {
411 $details['databaseerror-query'] = array(
412 'div', array( 'class' => 'mw-code' ) +
$attribs, $this->sql
);
415 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
416 $errorMessage = $this->errno
. ' ' . $this->error
;
417 $details['databaseerror-function'] = array( 'code', $attribs, $this->fname
);
418 $details['databaseerror-error'] = array( 'samp', $attribs, $errorMessage );
425 * @param string $key Message key
426 * @return string English message text
428 private function getFallbackMessage( $key ) {
430 'databaseerror-text' => 'A database query error has occurred.
431 This may indicate a bug in the software.',
432 'databaseerror-textcl' => 'A database query error has occurred.',
433 'databaseerror-query' => 'Query: $1',
434 'databaseerror-function' => 'Function: $1',
435 'databaseerror-error' => 'Error: $1',
438 return $messages[$key];
445 class DBUnexpectedError
extends DBError
{