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