8be8530c6b83941cf8821ea8e74e7152b97f7699
[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 /**
30 * @var DatabaseBase
31 */
32 public $db;
33
34 /**
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
38 */
39 function __construct( DatabaseBase $db = null, $error ) {
40 $this->db = $db;
41 parent::__construct( $error );
42 }
43
44 /**
45 * @return string
46 */
47 function getText() {
48 global $wgShowDBErrorBacktrace;
49
50 $s = $this->getTextContent() . "\n";
51
52 if ( $wgShowDBErrorBacktrace ) {
53 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
54 }
55
56 return $s;
57 }
58
59 /**
60 * @return string
61 */
62 function getHTML() {
63 global $wgShowDBErrorBacktrace;
64
65 $s = $this->getHTMLContent();
66
67 if ( $wgShowDBErrorBacktrace ) {
68 $s .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
69 }
70
71 return $s;
72 }
73
74 /**
75 * @return string
76 */
77 protected function getTextContent() {
78 return $this->getMessage();
79 }
80
81 /**
82 * @return string
83 */
84 protected function getHTMLContent() {
85 return '<p>' . nl2br( htmlspecialchars( $this->getMessage() ) ) . '</p>';
86 }
87 }
88
89 /**
90 * @ingroup Database
91 */
92 class DBConnectionError extends DBError {
93 public $error;
94
95 function __construct( DatabaseBase $db = null, $error = 'unknown error' ) {
96 $msg = 'DB connection error';
97
98 if ( trim( $error ) != '' ) {
99 $msg .= ": $error";
100 } elseif ( $db ) {
101 $error = $this->db->getServer();
102 }
103
104 parent::__construct( $db, $msg );
105 $this->error = $error;
106 }
107
108 /**
109 * @return bool
110 */
111 function useOutputPage() {
112 // Not likely to work
113 return false;
114 }
115
116 /**
117 * @param $key
118 * @param $fallback
119 * @return string
120 */
121 function msg( $key, $fallback /*[, params...] */ ) {
122 global $wgLang;
123
124 $args = array_slice( func_get_args(), 2 );
125
126 if ( $this->useMessageCache() ) {
127 $message = $wgLang->getMessage( $key );
128 } else {
129 $message = $fallback;
130 }
131
132 return wfMsgReplaceArgs( $message, $args );
133 }
134
135 /**
136 * @return boolean
137 */
138 function isLoggable() {
139 // Don't send to the exception log, already in dberror log
140 return false;
141 }
142
143 /**
144 * @return string
145 */
146 function getHTML() {
147 global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
148
149 $sorry = htmlspecialchars( $this->msg(
150 'dberr-problems',
151 'Sorry! This site is experiencing technical difficulties.'
152 ) );
153 $again = htmlspecialchars( $this->msg(
154 'dberr-again',
155 'Try waiting a few minutes and reloading.'
156 ) );
157
158 if ( $wgShowHostnames || $wgShowSQLErrors ) {
159 $info = str_replace(
160 '$1', Html::element( 'span', array( 'dir' => 'ltr' ), $this->error ),
161 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot contact the database server: $1)' ) )
162 );
163 } else {
164 $info = htmlspecialchars( $this->msg(
165 'dberr-info-hidden',
166 '(Cannot contact the database server)'
167 ) );
168 }
169
170 # No database access
171 MessageCache::singleton()->disable();
172
173 $html = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
174
175 if ( $wgShowDBErrorBacktrace ) {
176 $html .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
177 }
178
179 $html .= '<hr />';
180 $html .= $this->searchForm();
181
182 return $html;
183 }
184
185 protected function getTextContent() {
186 global $wgShowHostnames, $wgShowSQLErrors;
187
188 if ( $wgShowHostnames || $wgShowSQLErrors ) {
189 return $this->getMessage();
190 } else {
191 return 'DB connection error';
192 }
193 }
194
195 public function reportHTML() {
196 global $wgUseFileCache;
197
198 // Check whether we can serve a file-cached copy of the page with the error underneath
199 if ( $wgUseFileCache ) {
200 try {
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.' ) ) .
210 '</div>';
211
212 // Output cached page with notices on bottom and re-close body
213 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
214
215 return;
216 }
217 } catch ( MWException $e ) {
218 // Do nothing, just use the default page
219 }
220 }
221
222 // We can't, cough and die in the usual fashion
223 parent::reportHTML();
224 }
225
226 /**
227 * @return string
228 */
229 function searchForm() {
230 global $wgSitename, $wgCanonicalServer, $wgRequest;
231
232 $usegoogle = htmlspecialchars( $this->msg(
233 'dberr-usegoogle',
234 'You can try searching via Google in the meantime.'
235 ) );
236 $outofdate = htmlspecialchars( $this->msg(
237 'dberr-outofdate',
238 'Note that their indexes of our content may be out of date.'
239 ) );
240 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
241
242 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
243
244 $server = htmlspecialchars( $wgCanonicalServer );
245 $sitename = htmlspecialchars( $wgSitename );
246
247 $trygoogle = <<<EOT
248 <div style="margin: 1.5em">$usegoogle<br />
249 <small>$outofdate</small>
250 </div>
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" />
256
257 <input type="text" name="q" size="31" maxlength="255" value="$search" />
258 <input type="submit" name="btnG" value="$googlesearch" />
259 <p>
260 <label><input type="radio" name="sitesearch" value="$server" checked="checked" />$sitename</label>
261 <label><input type="radio" name="sitesearch" value="" />WWW</label>
262 </p>
263 </form>
264 EOT;
265
266 return $trygoogle;
267 }
268
269 /**
270 * @return string
271 */
272 private function fileCachedPage() {
273 global $wgTitle, $wgOut, $wgRequest;
274
275 if ( $wgOut->isDisabled() ) {
276 // Done already?
277 return '';
278 }
279
280 if ( $wgTitle ) {
281 // use $wgTitle if we managed to set it
282 $t = $wgTitle->getPrefixedDBkey();
283 } else {
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();
291 }
292 }
293
294 $cache = HTMLFileCache::newFromTitle( $t, 'view' );
295 if ( $cache->isCached() ) {
296 return $cache->fetchText();
297 } else {
298 return '';
299 }
300 }
301 }
302
303 /**
304 * @ingroup Database
305 */
306 class DBQueryError extends DBError {
307 public $error, $errno, $sql, $fname;
308
309 /**
310 * @param $db DatabaseBase
311 * @param $error string
312 * @param $errno int|string
313 * @param $sql string
314 * @param $fname string
315 */
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" .
320 "Query: $sql\n" .
321 "Function: $fname\n" .
322 "Error: $errno $error\n";
323 parent::__construct( $db, $message );
324
325 $this->error = $error;
326 $this->errno = $errno;
327 $this->sql = $sql;
328 $this->fname = $fname;
329 }
330
331 /**
332 * @return boolean
333 */
334 function isLoggable() {
335 // Don't send to the exception log, already in dberror log
336 return false;
337 }
338
339 /**
340 * @return string
341 */
342 function getPageTitle() {
343 return $this->msg( 'databaseerror', 'Database error' );
344 }
345
346 /**
347 * @return string
348 */
349 protected function getHTMLContent() {
350 $key = 'databaseerror-text';
351 $s = Html::element( 'p', array(), $this->msg( $key, $this->getFallbackMessage( $key ) ) );
352
353 $details = $this->getTechnicalDetails();
354 if ( $details ) {
355 $s .= '<ul>';
356 foreach ( $details as $key => $detail ) {
357 $s .= str_replace(
358 '$1', call_user_func_array( 'Html::element', $detail ),
359 Html::element( 'li', array(),
360 $this->msg( $key, $this->getFallbackMessage( $key ) )
361 )
362 );
363 }
364 $s .= '</ul>';
365 }
366
367 return $s;
368 }
369
370 /**
371 * @return string
372 */
373 protected function getTextContent() {
374 $key = 'databaseerror-textcl';
375 $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
376
377 foreach ( $this->getTechnicalDetails() as $key => $detail ) {
378 $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
379 }
380
381 return $s;
382 }
383
384 /**
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.
388 *
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.
393 *
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.
396 */
397 protected function getTechnicalDetails() {
398 global $wgShowHostnames, $wgShowSQLErrors;
399
400 $attribs = array( 'dir' => 'ltr' );
401 $details = array();
402
403 if ( $wgShowSQLErrors ) {
404 $details['databaseerror-query'] = array(
405 'div', array( 'class' => 'mw-code' ) + $attribs, $this->sql );
406 }
407
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 );
412 }
413
414 return $details;
415 }
416
417 /**
418 * @param string $key Message key
419 * @return string: English message text
420 */
421 private function getFallbackMessage( $key ) {
422 $messages = array(
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',
429 );
430
431 return $messages[$key];
432 }
433 }
434
435 /**
436 * @ingroup Database
437 */
438 class DBUnexpectedError extends DBError {
439 }