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
{
35 * Construct a database error
36 * @param $db DatabaseBase object which threw the error
37 * @param string $error A simple error message to be used for debugging
39 function __construct( DatabaseBase
$db = null, $error ) {
41 parent
::__construct( $error );
48 global $wgShowDBErrorBacktrace;
50 $s = $this->getTextContent() . "\n";
52 if ( $wgShowDBErrorBacktrace ) {
53 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
63 global $wgShowDBErrorBacktrace;
65 $s = $this->getHTMLContent();
67 if ( $wgShowDBErrorBacktrace ) {
68 $s .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
77 protected function getTextContent() {
78 return $this->getMessage();
84 protected function getHTMLContent() {
85 return '<p>' . nl2br( htmlspecialchars( $this->getMessage() ) ) . '</p>';
92 class DBConnectionError
extends DBError
{
95 function __construct( DatabaseBase
$db = null, $error = 'unknown error' ) {
96 $msg = 'DB connection error';
98 if ( trim( $error ) != '' ) {
101 $error = $this->db
->getServer();
104 parent
::__construct( $db, $msg );
105 $this->error
= $error;
111 function useOutputPage() {
112 // Not likely to work
121 function msg( $key, $fallback /*[, params...] */ ) {
124 $args = array_slice( func_get_args(), 2 );
126 if ( $this->useMessageCache() ) {
127 $message = $wgLang->getMessage( $key );
129 $message = $fallback;
132 return wfMsgReplaceArgs( $message, $args );
138 function isLoggable() {
139 // Don't send to the exception log, already in dberror log
147 global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
149 $sorry = htmlspecialchars( $this->msg(
151 'Sorry! This site is experiencing technical difficulties.'
153 $again = htmlspecialchars( $this->msg(
155 'Try waiting a few minutes and reloading.'
158 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
160 '$1', Html
::element( 'span', array( 'dir' => 'ltr' ), $this->error
),
161 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot contact the database server: $1)' ) )
164 $info = htmlspecialchars( $this->msg(
166 '(Cannot contact the database server)'
171 MessageCache
::singleton()->disable();
173 $html = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
175 if ( $wgShowDBErrorBacktrace ) {
176 $html .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
180 $html .= $this->searchForm();
185 protected function getTextContent() {
186 global $wgShowHostnames, $wgShowSQLErrors;
188 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
189 return $this->getMessage();
191 return 'DB connection error';
195 public function reportHTML() {
196 global $wgUseFileCache;
198 // Check whether we can serve a file-cached copy of the page with the error underneath
199 if ( $wgUseFileCache ) {
201 $cache = $this->fileCachedPage();
202 // Cached version on file system?
203 if ( $cache !== null ) {
204 // Hack: extend the body for error messages
205 $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
206 // Add cache notice...
207 $cache .= '<div style="border:1px solid #ffd0d0;padding:1em;">' .
208 htmlspecialchars( $this->msg( 'dberr-cachederror',
209 'This is a cached copy of the requested page, and may not be up to date.' ) ) .
212 // Output cached page with notices on bottom and re-close body
213 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
217 } catch ( MWException
$e ) {
218 // Do nothing, just use the default page
222 // We can't, cough and die in the usual fashion
223 parent
::reportHTML();
229 function searchForm() {
230 global $wgSitename, $wgCanonicalServer, $wgRequest;
232 $usegoogle = htmlspecialchars( $this->msg(
234 'You can try searching via Google in the meantime.'
236 $outofdate = htmlspecialchars( $this->msg(
238 'Note that their indexes of our content may be out of date.'
240 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
242 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
244 $server = htmlspecialchars( $wgCanonicalServer );
245 $sitename = htmlspecialchars( $wgSitename );
248 <div style="margin: 1.5em">$usegoogle<br />
249 <small>$outofdate</small>
251 <form method="get" action="//www.google.com/search" id="googlesearch">
252 <input type="hidden" name="domains" value="$server" />
253 <input type="hidden" name="num" value="50" />
254 <input type="hidden" name="ie" value="UTF-8" />
255 <input type="hidden" name="oe" value="UTF-8" />
257 <input type="text" name="q" size="31" maxlength="255" value="$search" />
258 <input type="submit" name="btnG" value="$googlesearch" />
260 <label><input type="radio" name="sitesearch" value="$server" checked="checked" />$sitename</label>
261 <label><input type="radio" name="sitesearch" value="" />WWW</label>
272 private function fileCachedPage() {
273 global $wgTitle, $wgOut, $wgRequest;
275 if ( $wgOut->isDisabled() ) {
281 // use $wgTitle if we managed to set it
282 $t = $wgTitle->getPrefixedDBkey();
284 // Fallback to the raw title URL param. We can't use the Title
285 // class is it may hit the interwiki table and give a DB error.
286 // We may get a cache miss due to not sanitizing the title though.
287 $t = str_replace( ' ', '_', $wgRequest->getVal( 'title' ) );
288 if ( $t == '' ) { // fallback to main page
289 $t = Title
::newFromText(
290 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
294 $cache = HTMLFileCache
::newFromTitle( $t, 'view' );
295 if ( $cache->isCached() ) {
296 return $cache->fetchText();
306 class DBQueryError
extends DBError
{
307 public $error, $errno, $sql, $fname;
310 * @param $db DatabaseBase
311 * @param $error string
312 * @param $errno int|string
314 * @param $fname string
316 function __construct( DatabaseBase
$db, $error, $errno, $sql, $fname ) {
317 $message = "A database error has occurred. Did you forget to run " .
318 "maintenance/update.php after upgrading? See: " .
319 "https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
321 "Function: $fname\n" .
322 "Error: $errno $error\n";
323 parent
::__construct( $db, $message );
325 $this->error
= $error;
326 $this->errno
= $errno;
328 $this->fname
= $fname;
334 function isLoggable() {
335 // Don't send to the exception log, already in dberror log
342 function getPageTitle() {
343 return $this->msg( 'databaseerror', 'Database error' );
349 protected function getHTMLContent() {
350 $key = 'databaseerror-text';
351 $s = Html
::element( 'p', array(), $this->msg( $key, $this->getFallbackMessage( $key ) ) );
353 $details = $this->getTechnicalDetails();
356 foreach ( $details as $key => $detail ) {
358 '$1', call_user_func_array( 'Html::element', $detail ),
359 Html
::element( 'li', array(),
360 $this->msg( $key, $this->getFallbackMessage( $key ) )
373 protected function getTextContent() {
374 $key = 'databaseerror-textcl';
375 $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
377 foreach ( $this->getTechnicalDetails() as $key => $detail ) {
378 $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
385 * Make a list of technical details that can be shown to the user. This information can
386 * aid in debugging yet may be useful to an attacker trying to exploit a security weakness
387 * in the software or server configuration.
389 * Thus no such details are shown by default, though if $wgShowHostnames is true, only the
390 * full SQL query is hidden; in fact, the error message often does contain a hostname, and
391 * sites using this option probably don't care much about "security by obscurity". Of course,
392 * if $wgShowSQLErrors is true, the SQL query *is* shown.
394 * @return array: Keys are message keys; values are arrays of arguments for Html::element().
395 * Array will be empty if users are not allowed to see any of these details at all.
397 protected function getTechnicalDetails() {
398 global $wgShowHostnames, $wgShowSQLErrors;
400 $attribs = array( 'dir' => 'ltr' );
403 if ( $wgShowSQLErrors ) {
404 $details['databaseerror-query'] = array(
405 'div', array( 'class' => 'mw-code' ) +
$attribs, $this->sql
);
408 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
409 $errorMessage = $this->errno
. ' ' . $this->error
;
410 $details['databaseerror-function'] = array( 'code', $attribs, $this->fname
);
411 $details['databaseerror-error'] = array( 'samp', $attribs, $errorMessage );
418 * @param string $key Message key
419 * @return string: English message text
421 private function getFallbackMessage( $key ) {
423 'databaseerror-text' => 'A database query error has occurred.
424 This may indicate a bug in the software.',
425 'databaseerror-textcl' => 'A database query error has occurred.',
426 'databaseerror-query' => 'Query: $1',
427 'databaseerror-function' => 'Function: $1',
428 'databaseerror-error' => 'Error: $1',
431 return $messages[$key];
438 class DBUnexpectedError
extends DBError
{