Replace wfMsgReplaceArgs by RawMessage
[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 /**
82 * @return string
83 */
84 protected function getTextContent() {
85 return $this->getMessage();
86 }
87
88 /**
89 * @return string
90 */
91 protected function getHTMLContent() {
92 return '<p>' . nl2br( htmlspecialchars( $this->getTextContent() ) ) . '</p>';
93 }
94 }
95
96 /**
97 * @ingroup Database
98 */
99 class DBConnectionError extends DBExpectedError {
100 /** @var string Error text */
101 public $error;
102
103 /**
104 * @param DatabaseBase $db Object throwing the error
105 * @param string $error Error text
106 */
107 function __construct( DatabaseBase $db = null, $error = 'unknown error' ) {
108 $msg = 'DB connection error';
109
110 if ( trim( $error ) != '' ) {
111 $msg .= ": $error";
112 } elseif ( $db ) {
113 $error = $this->db->getServer();
114 }
115
116 parent::__construct( $db, $msg );
117 $this->error = $error;
118 }
119
120 /**
121 * @return bool
122 */
123 function useOutputPage() {
124 // Not likely to work
125 return false;
126 }
127
128 /**
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
134 */
135 function msg( $key, $fallback /*[, params...] */ ) {
136 $args = array_slice( func_get_args(), 2 );
137
138 if ( $this->useMessageCache() ) {
139 $msg = wfMessage( $key )->useDatabase( false );
140 } else {
141 $msg = new RawMessage( $fallback );
142 }
143
144 return $msg->params( $args )->text();
145 }
146
147 /**
148 * @return bool
149 */
150 function isLoggable() {
151 // Don't send to the exception log, already in dberror log
152 return false;
153 }
154
155 /**
156 * @return string Safe HTML
157 */
158 function getHTML() {
159 global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
160
161 $sorry = htmlspecialchars( $this->msg(
162 'dberr-problems',
163 'Sorry! This site is experiencing technical difficulties.'
164 ) );
165 $again = htmlspecialchars( $this->msg(
166 'dberr-again',
167 'Try waiting a few minutes and reloading.'
168 ) );
169
170 if ( $wgShowHostnames || $wgShowSQLErrors ) {
171 $info = str_replace(
172 '$1', Html::element( 'span', array( 'dir' => 'ltr' ), $this->error ),
173 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot contact the database server: $1)' ) )
174 );
175 } else {
176 $info = htmlspecialchars( $this->msg(
177 'dberr-info-hidden',
178 '(Cannot contact the database server)'
179 ) );
180 }
181
182 # No database access
183 MessageCache::singleton()->disable();
184
185 $html = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
186
187 if ( $wgShowDBErrorBacktrace ) {
188 $html .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
189 }
190
191 $html .= '<hr />';
192 $html .= $this->searchForm();
193
194 return $html;
195 }
196
197 protected function getTextContent() {
198 global $wgShowHostnames, $wgShowSQLErrors;
199
200 if ( $wgShowHostnames || $wgShowSQLErrors ) {
201 return $this->getMessage();
202 } else {
203 return 'DB connection error';
204 }
205 }
206
207 /**
208 * Output the exception report using HTML.
209 *
210 * @return void
211 */
212 public function reportHTML() {
213 global $wgUseFileCache;
214
215 // Check whether we can serve a file-cached copy of the page with the error underneath
216 if ( $wgUseFileCache ) {
217 try {
218 $cache = $this->fileCachedPage();
219 // Cached version on file system?
220 if ( $cache !== null ) {
221 // Hack: extend the body for error messages
222 $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
223 // Add cache notice...
224 $cache .= '<div style="border:1px solid #ffd0d0;padding:1em;">' .
225 htmlspecialchars( $this->msg( 'dberr-cachederror',
226 'This is a cached copy of the requested page, and may not be up to date.' ) ) .
227 '</div>';
228
229 // Output cached page with notices on bottom and re-close body
230 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
231
232 return;
233 }
234 } catch ( MWException $e ) {
235 // Do nothing, just use the default page
236 }
237 }
238
239 // We can't, cough and die in the usual fashion
240 parent::reportHTML();
241 }
242
243 /**
244 * @return string
245 */
246 function searchForm() {
247 global $wgSitename, $wgCanonicalServer, $wgRequest;
248
249 $usegoogle = htmlspecialchars( $this->msg(
250 'dberr-usegoogle',
251 'You can try searching via Google in the meantime.'
252 ) );
253 $outofdate = htmlspecialchars( $this->msg(
254 'dberr-outofdate',
255 'Note that their indexes of our content may be out of date.'
256 ) );
257 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
258
259 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
260
261 $server = htmlspecialchars( $wgCanonicalServer );
262 $sitename = htmlspecialchars( $wgSitename );
263
264 $trygoogle = <<<EOT
265 <div style="margin: 1.5em">$usegoogle<br />
266 <small>$outofdate</small>
267 </div>
268 <form method="get" action="//www.google.com/search" id="googlesearch">
269 <input type="hidden" name="domains" value="$server" />
270 <input type="hidden" name="num" value="50" />
271 <input type="hidden" name="ie" value="UTF-8" />
272 <input type="hidden" name="oe" value="UTF-8" />
273
274 <input type="text" name="q" size="31" maxlength="255" value="$search" />
275 <input type="submit" name="btnG" value="$googlesearch" />
276 <p>
277 <label><input type="radio" name="sitesearch" value="$server" checked="checked" />$sitename</label>
278 <label><input type="radio" name="sitesearch" value="" />WWW</label>
279 </p>
280 </form>
281 EOT;
282
283 return $trygoogle;
284 }
285
286 /**
287 * @return string
288 */
289 private function fileCachedPage() {
290 $context = RequestContext::getMain();
291
292 if ( $context->getOutput()->isDisabled() ) {
293 // Done already?
294 return '';
295 }
296
297 if ( $context->getTitle() ) {
298 // Use the main context's title if we managed to set it
299 $t = $context->getTitle()->getPrefixedDBkey();
300 } else {
301 // Fallback to the raw title URL param. We can't use the Title
302 // class is it may hit the interwiki table and give a DB error.
303 // We may get a cache miss due to not sanitizing the title though.
304 $t = str_replace( ' ', '_', $context->getRequest()->getVal( 'title' ) );
305 if ( $t == '' ) { // fallback to main page
306 $t = Title::newFromText(
307 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
308 }
309 }
310
311 $cache = new HTMLFileCache( $t, 'view' );
312 if ( $cache->isCached() ) {
313 return $cache->fetchText();
314 } else {
315 return '';
316 }
317 }
318 }
319
320 /**
321 * @ingroup Database
322 */
323 class DBQueryError extends DBExpectedError {
324 public $error, $errno, $sql, $fname;
325
326 /**
327 * @param DatabaseBase $db
328 * @param string $error
329 * @param int|string $errno
330 * @param string $sql
331 * @param string $fname
332 */
333 function __construct( DatabaseBase $db, $error, $errno, $sql, $fname ) {
334 $message = "A database error has occurred. Did you forget to run " .
335 "maintenance/update.php after upgrading? See: " .
336 "https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
337 "Query: $sql\n" .
338 "Function: $fname\n" .
339 "Error: $errno $error\n";
340 parent::__construct( $db, $message );
341
342 $this->error = $error;
343 $this->errno = $errno;
344 $this->sql = $sql;
345 $this->fname = $fname;
346 }
347
348 /**
349 * @return string
350 */
351 function getPageTitle() {
352 return $this->msg( 'databaseerror', 'Database error' );
353 }
354
355 /**
356 * @return string
357 */
358 protected function getHTMLContent() {
359 $key = 'databaseerror-text';
360 $s = Html::element( 'p', array(), $this->msg( $key, $this->getFallbackMessage( $key ) ) );
361
362 $details = $this->getTechnicalDetails();
363 if ( $details ) {
364 $s .= '<ul>';
365 foreach ( $details as $key => $detail ) {
366 $s .= str_replace(
367 '$1', call_user_func_array( 'Html::element', $detail ),
368 Html::element( 'li', array(),
369 $this->msg( $key, $this->getFallbackMessage( $key ) )
370 )
371 );
372 }
373 $s .= '</ul>';
374 }
375
376 return $s;
377 }
378
379 /**
380 * @return string
381 */
382 protected function getTextContent() {
383 $key = 'databaseerror-textcl';
384 $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
385
386 foreach ( $this->getTechnicalDetails() as $key => $detail ) {
387 $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
388 }
389
390 return $s;
391 }
392
393 /**
394 * Make a list of technical details that can be shown to the user. This information can
395 * aid in debugging yet may be useful to an attacker trying to exploit a security weakness
396 * in the software or server configuration.
397 *
398 * Thus no such details are shown by default, though if $wgShowHostnames is true, only the
399 * full SQL query is hidden; in fact, the error message often does contain a hostname, and
400 * sites using this option probably don't care much about "security by obscurity". Of course,
401 * if $wgShowSQLErrors is true, the SQL query *is* shown.
402 *
403 * @return array Keys are message keys; values are arrays of arguments for Html::element().
404 * Array will be empty if users are not allowed to see any of these details at all.
405 */
406 protected function getTechnicalDetails() {
407 global $wgShowHostnames, $wgShowSQLErrors;
408
409 $attribs = array( 'dir' => 'ltr' );
410 $details = array();
411
412 if ( $wgShowSQLErrors ) {
413 $details['databaseerror-query'] = array(
414 'div', array( 'class' => 'mw-code' ) + $attribs, $this->sql );
415 }
416
417 if ( $wgShowHostnames || $wgShowSQLErrors ) {
418 $errorMessage = $this->errno . ' ' . $this->error;
419 $details['databaseerror-function'] = array( 'code', $attribs, $this->fname );
420 $details['databaseerror-error'] = array( 'samp', $attribs, $errorMessage );
421 }
422
423 return $details;
424 }
425
426 /**
427 * @param string $key Message key
428 * @return string English message text
429 */
430 private function getFallbackMessage( $key ) {
431 $messages = array(
432 'databaseerror-text' => 'A database query error has occurred.
433 This may indicate a bug in the software.',
434 'databaseerror-textcl' => 'A database query error has occurred.',
435 'databaseerror-query' => 'Query: $1',
436 'databaseerror-function' => 'Function: $1',
437 'databaseerror-error' => 'Error: $1',
438 );
439
440 return $messages[$key];
441 }
442 }
443
444 /**
445 * @ingroup Database
446 */
447 class DBUnexpectedError extends DBError {
448 }