Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / db / DatabaseError.php
blob86950a89a9226d58be8052c5352086cfb41a57ce
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 /**
82 * @return string
84 protected function getTextContent() {
85 return $this->getMessage();
88 /**
89 * @return string
91 protected function getHTMLContent() {
92 return '<p>' . nl2br( htmlspecialchars( $this->getTextContent() ) ) . '</p>';
96 /**
97 * @ingroup Database
99 class DBConnectionError extends DBExpectedError {
100 /** @var string Error text */
101 public $error;
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 ) != '' ) {
111 $msg .= ": $error";
112 } elseif ( $db ) {
113 $error = $this->db->getServer();
116 parent::__construct( $db, $msg );
117 $this->error = $error;
121 * @return bool
123 function useOutputPage() {
124 // Not likely to work
125 return false;
129 * @param string $key
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();
140 } else {
141 return wfMsgReplaceArgs( $fallback, $args );
146 * @return bool
148 function isLoggable() {
149 // Don't send to the exception log, already in dberror log
150 return false;
154 * @return string Safe HTML
156 function getHTML() {
157 global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
159 $sorry = htmlspecialchars( $this->msg(
160 'dberr-problems',
161 'Sorry! This site is experiencing technical difficulties.'
162 ) );
163 $again = htmlspecialchars( $this->msg(
164 'dberr-again',
165 'Try waiting a few minutes and reloading.'
166 ) );
168 if ( $wgShowHostnames || $wgShowSQLErrors ) {
169 $info = str_replace(
170 '$1', Html::element( 'span', array( 'dir' => 'ltr' ), $this->error ),
171 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot access the database: $1)' ) )
173 } else {
174 $info = htmlspecialchars( $this->msg(
175 'dberr-info-hidden',
176 '(Cannot access the database)'
177 ) );
180 # No database access
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>';
189 $html .= '<hr />';
190 $html .= $this->searchForm();
192 return $html;
195 protected function getTextContent() {
196 global $wgShowHostnames, $wgShowSQLErrors;
198 if ( $wgShowHostnames || $wgShowSQLErrors ) {
199 return $this->getMessage();
200 } else {
201 return 'DB connection error';
206 * Output the exception report using HTML.
208 * @return void
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 ) {
215 try {
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.' ) ) .
225 '</div>';
227 // Output cached page with notices on bottom and re-close body
228 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
230 return;
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();
242 * @return string
244 function searchForm() {
245 global $wgSitename, $wgCanonicalServer, $wgRequest;
247 $usegoogle = htmlspecialchars( $this->msg(
248 'dberr-usegoogle',
249 'You can try searching via Google in the meantime.'
250 ) );
251 $outofdate = htmlspecialchars( $this->msg(
252 'dberr-outofdate',
253 'Note that their indexes of our content may be out of date.'
254 ) );
255 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
257 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
259 $server = htmlspecialchars( $wgCanonicalServer );
260 $sitename = htmlspecialchars( $wgSitename );
262 $trygoogle = <<<EOT
263 <div style="margin: 1.5em">$usegoogle<br />
264 <small>$outofdate</small>
265 </div>
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>
277 </p>
278 </form>
279 EOT;
281 return $trygoogle;
285 * @return string
287 private function fileCachedPage() {
288 $context = RequestContext::getMain();
290 if ( $context->getOutput()->isDisabled() ) {
291 // Done already?
292 return '';
295 if ( $context->getTitle() ) {
296 // Use the main context's title if we managed to set it
297 $t = $context->getTitle()->getPrefixedDBkey();
298 } else {
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();
312 } else {
313 return '';
319 * @ingroup Database
321 class DBQueryError extends DBExpectedError {
322 public $error, $errno, $sql, $fname;
325 * @param DatabaseBase $db
326 * @param string $error
327 * @param int|string $errno
328 * @param string $sql
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" .
335 "Query: $sql\n" .
336 "Function: $fname\n" .
337 "Error: $errno $error\n";
338 parent::__construct( $db, $message );
340 $this->error = $error;
341 $this->errno = $errno;
342 $this->sql = $sql;
343 $this->fname = $fname;
347 * @return string
349 function getPageTitle() {
350 return $this->msg( 'databaseerror', 'Database error' );
354 * @return string
356 protected function getHTMLContent() {
357 $key = 'databaseerror-text';
358 $s = Html::element( 'p', array(), $this->msg( $key, $this->getFallbackMessage( $key ) ) );
360 $details = $this->getTechnicalDetails();
361 if ( $details ) {
362 $s .= '<ul>';
363 foreach ( $details as $key => $detail ) {
364 $s .= str_replace(
365 '$1', call_user_func_array( 'Html::element', $detail ),
366 Html::element( 'li', array(),
367 $this->msg( $key, $this->getFallbackMessage( $key ) )
371 $s .= '</ul>';
374 return $s;
378 * @return string
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";
388 return $s;
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' );
408 $details = array();
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 );
421 return $details;
425 * @param string $key Message key
426 * @return string English message text
428 private function getFallbackMessage( $key ) {
429 $messages = array(
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];
443 * @ingroup Database
445 class DBUnexpectedError extends DBError {