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
{
36 * Construct a database error
37 * @param $db DatabaseBase object which threw the error
38 * @param string $error A simple error message to be used for debugging
40 function __construct( DatabaseBase
$db = null, $error ) {
42 parent
::__construct( $error );
49 global $wgShowDBErrorBacktrace;
51 $s = $this->getTextContent() . "\n";
53 if ( $wgShowDBErrorBacktrace ) {
54 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
64 global $wgShowDBErrorBacktrace;
66 $s = $this->getHTMLContent();
68 if ( $wgShowDBErrorBacktrace ) {
69 $s .= '<p>Backtrace:</p><p>' .
70 nl2br( htmlspecialchars( $this->getTraceAsString() ) ) . '</p>';
79 protected function getTextContent() {
80 return $this->getMessage();
86 protected function getHTMLContent() {
87 return '<p>' . nl2br( htmlspecialchars( $this->getMessage() ) ) . '</p>';
94 class DBConnectionError
extends DBError
{
97 function __construct( DatabaseBase
$db = null, $error = 'unknown error' ) {
98 $msg = 'DB connection error';
100 if ( trim( $error ) != '' ) {
103 $error = $this->db
->getServer();
106 parent
::__construct( $db, $msg );
107 $this->error
= $error;
113 function useOutputPage() {
114 // Not likely to work
123 function msg( $key, $fallback /*[, params...] */ ) {
126 $args = array_slice( func_get_args(), 2 );
128 if ( $this->useMessageCache() ) {
129 $message = $wgLang->getMessage( $key );
131 $message = $fallback;
133 return wfMsgReplaceArgs( $message, $args );
139 function getLogMessage() {
140 # Don't send to the exception log
147 function getPageTitle() {
148 return $this->msg( 'dberr-header', 'This wiki has a problem' );
155 global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
157 $sorry = htmlspecialchars( $this->msg( 'dberr-problems', "Sorry!\nThis site is experiencing technical difficulties." ) );
158 $again = htmlspecialchars( $this->msg( 'dberr-again', 'Try waiting a few minutes and reloading.' ) );
160 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
162 '$1', Html
::element( 'span', array( 'dir' => 'ltr' ), $this->error
),
163 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot contact the database server: $1)' ) )
166 $info = htmlspecialchars( $this->msg( 'dberr-info-hidden', '(Cannot contact the database server)' ) );
170 MessageCache
::singleton()->disable();
172 $text = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
174 if ( $wgShowDBErrorBacktrace ) {
175 $text .= '<p>Backtrace:</p><p>' .
176 nl2br( htmlspecialchars( $this->getTraceAsString() ) ) . '</p>';
180 $text .= $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="color:red;font-size:150%;font-weight:bold;">' .
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>";
216 } catch ( MWException
$e ) {
217 // Do nothing, just use the default page
221 # We can't, cough and die in the usual fashion
222 parent
::reportHTML();
228 function searchForm() {
229 global $wgSitename, $wgCanonicalServer, $wgRequest;
231 $usegoogle = htmlspecialchars( $this->msg( 'dberr-usegoogle', 'You can try searching via Google in the meantime.' ) );
232 $outofdate = htmlspecialchars( $this->msg( 'dberr-outofdate', 'Note that their indexes of our content may be out of date.' ) );
233 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
235 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
237 $server = htmlspecialchars( $wgCanonicalServer );
238 $sitename = htmlspecialchars( $wgSitename );
241 <div style="margin: 1.5em">$usegoogle<br />
242 <small>$outofdate</small></div>
243 <!-- SiteSearch Google -->
244 <form method="get" action="//www.google.com/search" id="googlesearch">
245 <input type="hidden" name="domains" value="$server" />
246 <input type="hidden" name="num" value="50" />
247 <input type="hidden" name="ie" value="UTF-8" />
248 <input type="hidden" name="oe" value="UTF-8" />
250 <input type="text" name="q" size="31" maxlength="255" value="$search" />
251 <input type="submit" name="btnG" value="$googlesearch" />
253 <input type="radio" name="sitesearch" id="gwiki" value="$server" checked="checked" /><label for="gwiki">$sitename</label>
254 <input type="radio" name="sitesearch" id="gWWW" value="" /><label for="gWWW">WWW</label>
257 <!-- SiteSearch Google -->
265 private function fileCachedPage() {
266 global $wgTitle, $wgOut, $wgRequest;
268 if ( $wgOut->isDisabled() ) {
269 return ''; // Done already?
272 if ( $wgTitle ) { // use $wgTitle if we managed to set it
273 $t = $wgTitle->getPrefixedDBkey();
275 # Fallback to the raw title URL param. We can't use the Title
276 # class is it may hit the interwiki table and give a DB error.
277 # We may get a cache miss due to not sanitizing the title though.
278 $t = str_replace( ' ', '_', $wgRequest->getVal( 'title' ) );
279 if ( $t == '' ) { // fallback to main page
280 $t = Title
::newFromText(
281 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
285 $cache = HTMLFileCache
::newFromTitle( $t, 'view' );
286 if ( $cache->isCached() ) {
287 return $cache->fetchText();
297 class DBQueryError
extends DBError
{
298 public $error, $errno, $sql, $fname;
301 * @param $db DatabaseBase
302 * @param $error string
303 * @param $errno int|string
305 * @param $fname string
307 function __construct( DatabaseBase
$db, $error, $errno, $sql, $fname ) {
308 $message = "A database error has occurred. Did you forget to run maintenance/update.php after upgrading? See: https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
310 "Function: $fname\n" .
311 "Error: $errno $error\n";
312 parent
::__construct( $db, $message );
314 $this->error
= $error;
315 $this->errno
= $errno;
317 $this->fname
= $fname;
323 function getLogMessage() {
324 # Don't send to the exception log
331 function getPageTitle() {
332 return $this->msg( 'databaseerror', 'Database error' );
338 protected function getHTMLContent() {
339 $key = 'databaseerror-text';
340 $s = Html
::element( 'p', array(), $this->msg( $key, $this->getFallbackMessage( $key ) ) );
342 $details = $this->getTechnicalDetails();
345 foreach ( $details as $key => $detail ) {
347 '$1', call_user_func_array( 'Html::element', $detail ),
348 Html
::element( 'li', array(),
349 $this->msg( $key, $this->getFallbackMessage( $key ) )
362 protected function getTextContent() {
363 $key = 'databaseerror-textcl';
364 $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
366 foreach ( $this->getTechnicalDetails() as $key => $detail ) {
367 $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
374 * Make a list of technical details that can be shown to the user. This information can
375 * aid in debugging yet may be useful to an attacker trying to exploit a security weakness
376 * in the software or server configuration.
378 * Thus no such details are shown by default, though if $wgShowHostnames is true, only the
379 * full SQL query is hidden; in fact, the error message often does contain a hostname, and
380 * sites using this option probably don't care much about "security by obscurity". Of course,
381 * if $wgShowSQLErrors is true, the SQL query *is* shown.
383 * @return array: Keys are message keys; values are arrays of arguments for Html::element().
384 * Array will be empty if users are not allowed to see any of these details at all.
386 protected function getTechnicalDetails() {
387 global $wgShowHostnames, $wgShowSQLErrors;
389 $attribs = array( 'dir' => 'ltr' );
392 if ( $wgShowSQLErrors ) {
393 $details['databaseerror-query'] = array(
394 'div', array( 'class' => 'mw-code' ) +
$attribs, $this->sql
);
397 if ( $wgShowHostnames ||
$wgShowSQLErrors ) {
398 $errorMessage = $this->errno
. ' ' . $this->error
;
399 $details['databaseerror-function'] = array( 'code', $attribs, $this->fname
);
400 $details['databaseerror-error'] = array( 'samp', $attribs, $errorMessage );
407 * @param string $key Message key
408 * @return string: English message text
410 private function getFallbackMessage( $key ) {
412 'databaseerror-text' => 'A database query error has occurred.
413 This may indicate a bug in the software.',
414 'databaseerror-textcl' => 'A database query error has occurred.',
415 'databaseerror-query' => 'Query: $1',
416 'databaseerror-function' => 'Function: $1',
417 'databaseerror-error' => 'Error: $1',
419 return $messages[$key];
427 class DBUnexpectedError
extends DBError
{}