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