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