Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / db / DatabaseError.php
1 <?php
2 /**
3 * This file contains database error classes.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * Database error base class
26 * @ingroup Database
27 */
28 class DBError extends MWException {
29 /** @var DatabaseBase */
30 public $db;
31
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
36 */
37 function __construct( DatabaseBase $db = null, $error ) {
38 $this->db = $db;
39 parent::__construct( $error );
40 }
41 }
42
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.
46 *
47 * @ingroup Database
48 * @since 1.23
49 */
50 class DBExpectedError extends DBError {
51 /**
52 * @return string
53 */
54 function getText() {
55 global $wgShowDBErrorBacktrace;
56
57 $s = $this->getTextContent() . "\n";
58
59 if ( $wgShowDBErrorBacktrace ) {
60 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
61 }
62
63 return $s;
64 }
65
66 /**
67 * @return string
68 */
69 function getHTML() {
70 global $wgShowDBErrorBacktrace;
71
72 $s = $this->getHTMLContent();
73
74 if ( $wgShowDBErrorBacktrace ) {
75 $s .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
76 }
77
78 return $s;
79 }
80
81 function getPageTitle() {
82 return $this->msg( 'databaseerror', 'Database error' );
83 }
84
85 /**
86 * @return string
87 */
88 protected function getTextContent() {
89 return $this->getMessage();
90 }
91
92 /**
93 * @return string
94 */
95 protected function getHTMLContent() {
96 return '<p>' . nl2br( htmlspecialchars( $this->getTextContent() ) ) . '</p>';
97 }
98 }
99
100 /**
101 * @ingroup Database
102 */
103 class DBConnectionError extends DBExpectedError {
104 /** @var string Error text */
105 public $error;
106
107 /**
108 * @param DatabaseBase $db Object throwing the error
109 * @param string $error Error text
110 */
111 function __construct( DatabaseBase $db = null, $error = 'unknown error' ) {
112 $msg = 'DB connection error';
113
114 if ( trim( $error ) != '' ) {
115 $msg .= ": $error";
116 } elseif ( $db ) {
117 $error = $this->db->getServer();
118 }
119
120 parent::__construct( $db, $msg );
121 $this->error = $error;
122 }
123
124 /**
125 * @return bool
126 */
127 function useOutputPage() {
128 // Not likely to work
129 return false;
130 }
131
132 /**
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
138 */
139 function msg( $key, $fallback /*[, params...] */ ) {
140 $args = array_slice( func_get_args(), 2 );
141
142 if ( $this->useMessageCache() ) {
143 return wfMessage( $key, $args )->useDatabase( false )->text();
144 } else {
145 return wfMsgReplaceArgs( $fallback, $args );
146 }
147 }
148
149 /**
150 * @return bool
151 */
152 function isLoggable() {
153 // Don't send to the exception log, already in dberror log
154 return false;
155 }
156
157 /**
158 * @return string Safe HTML
159 */
160 function getHTML() {
161 global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
162
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 ) );
171
172 if ( $wgShowHostnames || $wgShowSQLErrors ) {
173 $info = str_replace(
174 '$1', Html::element( 'span', [ 'dir' => 'ltr' ], $this->error ),
175 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot access the database: $1)' ) )
176 );
177 } else {
178 $info = htmlspecialchars( $this->msg(
179 'dberr-info-hidden',
180 '(Cannot access the database)'
181 ) );
182 }
183
184 # No database access
185 MessageCache::singleton()->disable();
186
187 $html = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
188
189 if ( $wgShowDBErrorBacktrace ) {
190 $html .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
191 }
192
193 $html .= '<hr />';
194 $html .= $this->searchForm();
195
196 return $html;
197 }
198
199 protected function getTextContent() {
200 global $wgShowHostnames, $wgShowSQLErrors;
201
202 if ( $wgShowHostnames || $wgShowSQLErrors ) {
203 return $this->getMessage();
204 } else {
205 return 'DB connection error';
206 }
207 }
208
209 /**
210 * Output the exception report using HTML.
211 *
212 * @return void
213 */
214 public function reportHTML() {
215 global $wgUseFileCache;
216
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( [ '</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>';
230
231 // Output cached page with notices on bottom and re-close body
232 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
233
234 return;
235 }
236 } catch ( Exception $e ) {
237 // Do nothing, just use the default page
238 }
239 }
240
241 // We can't, cough and die in the usual fashion
242 parent::reportHTML();
243 }
244
245 /**
246 * @return string
247 */
248 function searchForm() {
249 global $wgSitename, $wgCanonicalServer, $wgRequest;
250
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' ) );
260
261 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
262
263 $server = htmlspecialchars( $wgCanonicalServer );
264 $sitename = htmlspecialchars( $wgSitename );
265
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" />
275
276 <input type="text" name="q" size="31" maxlength="255" value="$search" />
277 <input type="submit" name="btnG" value="$googlesearch" />
278 <p>
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;
284
285 return $trygoogle;
286 }
287
288 /**
289 * @return string
290 */
291 private function fileCachedPage() {
292 $context = RequestContext::getMain();
293
294 if ( $context->getOutput()->isDisabled() ) {
295 // Done already?
296 return '';
297 }
298
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();
310 }
311 }
312
313 $cache = new HTMLFileCache( $t, 'view' );
314 if ( $cache->isCached() ) {
315 return $cache->fetchText();
316 } else {
317 return '';
318 }
319 }
320 }
321
322 /**
323 * @ingroup Database
324 */
325 class DBQueryError extends DBExpectedError {
326 public $error, $errno, $sql, $fname;
327
328 /**
329 * @param DatabaseBase $db
330 * @param string $error
331 * @param int|string $errno
332 * @param string $sql
333 * @param string $fname
334 */
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";
348 }
349 parent::__construct( $db, $message );
350
351 $this->error = $error;
352 $this->errno = $errno;
353 $this->sql = $sql;
354 $this->fname = $fname;
355 }
356
357 /**
358 * @return string
359 */
360 function getPageTitle() {
361 return $this->msg( 'databaseerror', 'Database error' );
362 }
363
364 /**
365 * @return string
366 */
367 protected function getHTMLContent() {
368 $key = 'databaseerror-text';
369 $s = Html::element( 'p', [], $this->msg( $key, $this->getFallbackMessage( $key ) ) );
370
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', [],
378 $this->msg( $key, $this->getFallbackMessage( $key ) )
379 )
380 );
381 }
382 $s .= '</ul>';
383 }
384
385 return $s;
386 }
387
388 /**
389 * @return string
390 */
391 protected function getTextContent() {
392 $key = 'databaseerror-textcl';
393 $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
394
395 foreach ( $this->getTechnicalDetails() as $key => $detail ) {
396 $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
397 }
398
399 return $s;
400 }
401
402 /**
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.
406 *
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.
411 *
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.
414 */
415 protected function getTechnicalDetails() {
416 global $wgShowHostnames, $wgShowSQLErrors;
417
418 $attribs = [ 'dir' => 'ltr' ];
419 $details = [];
420
421 if ( $wgShowSQLErrors ) {
422 $details['databaseerror-query'] = [
423 'div', [ 'class' => 'mw-code' ] + $attribs, $this->sql ];
424 }
425
426 if ( $wgShowHostnames || $wgShowSQLErrors ) {
427 $errorMessage = $this->errno . ' ' . $this->error;
428 $details['databaseerror-function'] = [ 'code', $attribs, $this->fname ];
429 $details['databaseerror-error'] = [ 'samp', $attribs, $errorMessage ];
430 }
431
432 return $details;
433 }
434
435 /**
436 * @param string $key Message key
437 * @return string English message text
438 */
439 private function getFallbackMessage( $key ) {
440 $messages = [
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',
447 ];
448
449 return $messages[$key];
450 }
451 }
452
453 /**
454 * @ingroup Database
455 */
456 class DBUnexpectedError extends DBError {
457 }
458
459 /**
460 * @ingroup Database
461 */
462 class DBReadOnlyError extends DBExpectedError {
463 function getPageTitle() {
464 return $this->msg( 'readonly', 'Database is locked' );
465 }
466 }
467
468 /**
469 * @ingroup Database
470 */
471 class DBTransactionError extends DBExpectedError {
472 }