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