When serializing Message, don't try to unstub StubUserLang
[mediawiki.git] / includes / db / DatabaseError.php
blob4993eacd8380b5d013ace990049ded7802484e8f
1 <?php
2 /**
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
20 * @file
21 * @ingroup Database
24 /**
25 * Database error base class
26 * @ingroup Database
28 class DBError extends MWException {
29 /** @var DatabaseBase */
30 public $db;
32 /**
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 ) {
38 $this->db = $db;
39 parent::__construct( $error );
43 /**
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.
47 * @ingroup Database
48 * @since 1.23
50 class DBExpectedError extends DBError {
51 /**
52 * @return string
54 function getText() {
55 global $wgShowDBErrorBacktrace;
57 $s = $this->getTextContent() . "\n";
59 if ( $wgShowDBErrorBacktrace ) {
60 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
63 return $s;
66 /**
67 * @return string
69 function getHTML() {
70 global $wgShowDBErrorBacktrace;
72 $s = $this->getHTMLContent();
74 if ( $wgShowDBErrorBacktrace ) {
75 $s .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
78 return $s;
81 function getPageTitle() {
82 return $this->msg( 'databaseerror', 'Database error' );
85 /**
86 * @return string
88 protected function getTextContent() {
89 return $this->getMessage();
92 /**
93 * @return string
95 protected function getHTMLContent() {
96 return '<p>' . nl2br( htmlspecialchars( $this->getTextContent() ) ) . '</p>';
101 * @ingroup Database
103 class DBConnectionError extends DBExpectedError {
104 /** @var string Error text */
105 public $error;
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 ) != '' ) {
115 $msg .= ": $error";
116 } elseif ( $db ) {
117 $error = $this->db->getServer();
120 parent::__construct( $db, $msg );
121 $this->error = $error;
125 * @return bool
127 function useOutputPage() {
128 // Not likely to work
129 return false;
133 * @param string $key
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();
144 } else {
145 return wfMsgReplaceArgs( $fallback, $args );
150 * @return bool
152 function isLoggable() {
153 // Don't send to the exception log, already in dberror log
154 return false;
158 * @return string Safe HTML
160 function getHTML() {
161 global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
163 $sorry = htmlspecialchars( $this->msg(
164 'dberr-problems',
165 'Sorry! This site is experiencing technical difficulties.'
166 ) );
167 $again = htmlspecialchars( $this->msg(
168 'dberr-again',
169 'Try waiting a few minutes and reloading.'
170 ) );
172 if ( $wgShowHostnames || $wgShowSQLErrors ) {
173 $info = str_replace(
174 '$1', Html::element( 'span', array( 'dir' => 'ltr' ), $this->error ),
175 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot access the database: $1)' ) )
177 } else {
178 $info = htmlspecialchars( $this->msg(
179 'dberr-info-hidden',
180 '(Cannot access the database)'
181 ) );
184 # No database access
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>';
193 $html .= '<hr />';
194 $html .= $this->searchForm();
196 return $html;
199 protected function getTextContent() {
200 global $wgShowHostnames, $wgShowSQLErrors;
202 if ( $wgShowHostnames || $wgShowSQLErrors ) {
203 return $this->getMessage();
204 } else {
205 return 'DB connection error';
210 * Output the exception report using HTML.
212 * @return void
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 ) {
219 try {
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( array( '</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.' ) ) .
229 '</div>';
231 // Output cached page with notices on bottom and re-close body
232 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
234 return;
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();
246 * @return string
248 function searchForm() {
249 global $wgSitename, $wgCanonicalServer, $wgRequest;
251 $usegoogle = htmlspecialchars( $this->msg(
252 'dberr-usegoogle',
253 'You can try searching via Google in the meantime.'
254 ) );
255 $outofdate = htmlspecialchars( $this->msg(
256 'dberr-outofdate',
257 'Note that their indexes of our content may be out of date.'
258 ) );
259 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
261 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
263 $server = htmlspecialchars( $wgCanonicalServer );
264 $sitename = htmlspecialchars( $wgSitename );
266 $trygoogle = <<<EOT
267 <div style="margin: 1.5em">$usegoogle<br />
268 <small>$outofdate</small>
269 </div>
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>
281 </p>
282 </form>
283 EOT;
285 return $trygoogle;
289 * @return string
291 private function fileCachedPage() {
292 $context = RequestContext::getMain();
294 if ( $context->getOutput()->isDisabled() ) {
295 // Done already?
296 return '';
299 if ( $context->getTitle() ) {
300 // Use the main context's title if we managed to set it
301 $t = $context->getTitle()->getPrefixedDBkey();
302 } else {
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();
316 } else {
317 return '';
323 * @ingroup Database
325 class DBQueryError extends DBExpectedError {
326 public $error, $errno, $sql, $fname;
329 * @param DatabaseBase $db
330 * @param string $error
331 * @param int|string $errno
332 * @param string $sql
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" .
338 "Query: $sql\n" .
339 "Function: $fname\n" .
340 "Error: $errno $error\n";
341 } else {
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" .
345 "Query: $sql\n" .
346 "Function: $fname\n" .
347 "Error: $errno $error\n";
349 parent::__construct( $db, $message );
351 $this->error = $error;
352 $this->errno = $errno;
353 $this->sql = $sql;
354 $this->fname = $fname;
358 * @return string
360 function getPageTitle() {
361 return $this->msg( 'databaseerror', 'Database error' );
365 * @return string
367 protected function getHTMLContent() {
368 $key = 'databaseerror-text';
369 $s = Html::element( 'p', array(), $this->msg( $key, $this->getFallbackMessage( $key ) ) );
371 $details = $this->getTechnicalDetails();
372 if ( $details ) {
373 $s .= '<ul>';
374 foreach ( $details as $key => $detail ) {
375 $s .= str_replace(
376 '$1', call_user_func_array( 'Html::element', $detail ),
377 Html::element( 'li', array(),
378 $this->msg( $key, $this->getFallbackMessage( $key ) )
382 $s .= '</ul>';
385 return $s;
389 * @return string
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";
399 return $s;
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 = array( 'dir' => 'ltr' );
419 $details = array();
421 if ( $wgShowSQLErrors ) {
422 $details['databaseerror-query'] = array(
423 'div', array( 'class' => 'mw-code' ) + $attribs, $this->sql );
426 if ( $wgShowHostnames || $wgShowSQLErrors ) {
427 $errorMessage = $this->errno . ' ' . $this->error;
428 $details['databaseerror-function'] = array( 'code', $attribs, $this->fname );
429 $details['databaseerror-error'] = array( 'samp', $attribs, $errorMessage );
432 return $details;
436 * @param string $key Message key
437 * @return string English message text
439 private function getFallbackMessage( $key ) {
440 $messages = array(
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];
454 * @ingroup Database
456 class DBUnexpectedError extends DBError {
460 * @ingroup Database
462 class DBReadOnlyError extends DBExpectedError {
463 function getPageTitle() {
464 return $this->msg( 'readonly', 'Database is locked' );
469 * @ingroup Database
471 class DBTransactionError extends DBExpectedError {