Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups().
[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 $error String A simple error message to be used for debugging
39 */
40 function __construct( DatabaseBase &$db, $error ) {
41 $this->db = $db;
42 parent::__construct( $error );
43 }
44
45 /**
46 * @param $html string
47 * @return string
48 */
49 protected function getContentMessage( $html ) {
50 if ( $html ) {
51 return nl2br( htmlspecialchars( $this->getMessage() ) );
52 } else {
53 return $this->getMessage();
54 }
55 }
56
57 /**
58 * @return string
59 */
60 function getText() {
61 global $wgShowDBErrorBacktrace;
62
63 $s = $this->getContentMessage( false ) . "\n";
64
65 if ( $wgShowDBErrorBacktrace ) {
66 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
67 }
68
69 return $s;
70 }
71
72 /**
73 * @return string
74 */
75 function getHTML() {
76 global $wgShowDBErrorBacktrace;
77
78 $s = $this->getContentMessage( true );
79
80 if ( $wgShowDBErrorBacktrace ) {
81 $s .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
82 }
83
84 return $s;
85 }
86 }
87
88 /**
89 * @ingroup Database
90 */
91 class DBConnectionError extends DBError {
92 public $error;
93
94 function __construct( DatabaseBase &$db, $error = 'unknown error' ) {
95 $msg = 'DB connection error';
96
97 if ( trim( $error ) != '' ) {
98 $msg .= ": $error";
99 }
100
101 $this->error = $error;
102
103 parent::__construct( $db, $msg );
104 }
105
106 /**
107 * @return bool
108 */
109 function useOutputPage() {
110 // Not likely to work
111 return false;
112 }
113
114 /**
115 * @param $key
116 * @param $fallback
117 * @return string
118 */
119 function msg( $key, $fallback /*[, params...] */ ) {
120 global $wgLang;
121
122 $args = array_slice( func_get_args(), 2 );
123
124 if ( $this->useMessageCache() ) {
125 $message = $wgLang->getMessage( $key );
126 } else {
127 $message = $fallback;
128 }
129 return wfMsgReplaceArgs( $message, $args );
130 }
131
132 /**
133 * @return bool
134 */
135 function getLogMessage() {
136 # Don't send to the exception log
137 return false;
138 }
139
140 /**
141 * @return string
142 */
143 function getPageTitle() {
144 global $wgSitename;
145 return htmlspecialchars( $this->msg( 'dberr-header', "$wgSitename has a problem" ) );
146 }
147
148 /**
149 * @return string
150 */
151 function getHTML() {
152 global $wgShowDBErrorBacktrace;
153
154 $sorry = htmlspecialchars( $this->msg( 'dberr-problems', 'Sorry! This site is experiencing technical difficulties.' ) );
155 $again = htmlspecialchars( $this->msg( 'dberr-again', 'Try waiting a few minutes and reloading.' ) );
156 $info = htmlspecialchars( $this->msg( 'dberr-info', '(Can\'t contact the database server: $1)' ) );
157
158 # No database access
159 MessageCache::singleton()->disable();
160
161 if ( trim( $this->error ) == '' ) {
162 $this->error = $this->db->getProperty( 'mServer' );
163 }
164
165 $this->error = Html::element( 'span', array( 'dir' => 'ltr' ), $this->error );
166
167 $noconnect = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
168 $text = str_replace( '$1', $this->error, $noconnect );
169
170 if ( $wgShowDBErrorBacktrace ) {
171 $text .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
172 }
173
174 $extra = $this->searchForm();
175
176 return "$text<hr />$extra";
177 }
178
179 public function reportHTML(){
180 global $wgUseFileCache;
181
182 # Check whether we can serve a file-cached copy of the page with the error underneath
183 if ( $wgUseFileCache ) {
184 try {
185 $cache = $this->fileCachedPage();
186 # Cached version on file system?
187 if ( $cache !== null ) {
188 # Hack: extend the body for error messages
189 $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
190 # Add cache notice...
191 $cache .= '<div style="color:red;font-size:150%;font-weight:bold;">'.
192 htmlspecialchars( $this->msg( 'dberr-cachederror',
193 'This is a cached copy of the requested page, and may not be up to date. ' ) ) .
194 '</div>';
195
196 # Output cached page with notices on bottom and re-close body
197 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
198 return;
199 }
200 } catch ( MWException $e ) {
201 // Do nothing, just use the default page
202 }
203 }
204
205 # We can't, cough and die in the usual fashion
206 parent::reportHTML();
207 }
208
209 /**
210 * @return string
211 */
212 function searchForm() {
213 global $wgSitename, $wgCanonicalServer, $wgRequest;
214
215 $usegoogle = htmlspecialchars( $this->msg( 'dberr-usegoogle', 'You can try searching via Google in the meantime.' ) );
216 $outofdate = htmlspecialchars( $this->msg( 'dberr-outofdate', 'Note that their indexes of our content may be out of date.' ) );
217 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
218
219 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
220
221 $server = htmlspecialchars( $wgCanonicalServer );
222 $sitename = htmlspecialchars( $wgSitename );
223
224 $trygoogle = <<<EOT
225 <div style="margin: 1.5em">$usegoogle<br />
226 <small>$outofdate</small></div>
227 <!-- SiteSearch Google -->
228 <form method="get" action="//www.google.com/search" id="googlesearch">
229 <input type="hidden" name="domains" value="$server" />
230 <input type="hidden" name="num" value="50" />
231 <input type="hidden" name="ie" value="UTF-8" />
232 <input type="hidden" name="oe" value="UTF-8" />
233
234 <input type="text" name="q" size="31" maxlength="255" value="$search" />
235 <input type="submit" name="btnG" value="$googlesearch" />
236 <div>
237 <input type="radio" name="sitesearch" id="gwiki" value="$server" checked="checked" /><label for="gwiki">$sitename</label>
238 <input type="radio" name="sitesearch" id="gWWW" value="" /><label for="gWWW">WWW</label>
239 </div>
240 </form>
241 <!-- SiteSearch Google -->
242 EOT;
243 return $trygoogle;
244 }
245
246 /**
247 * @return string
248 */
249 private function fileCachedPage() {
250 global $wgTitle, $wgOut, $wgRequest;
251
252 if ( $wgOut->isDisabled() ) {
253 return ''; // Done already?
254 }
255
256 if ( $wgTitle ) { // use $wgTitle if we managed to set it
257 $t = $wgTitle->getPrefixedDBkey();
258 } else {
259 # Fallback to the raw title URL param. We can't use the Title
260 # class is it may hit the interwiki table and give a DB error.
261 # We may get a cache miss due to not sanitizing the title though.
262 $t = str_replace( ' ', '_', $wgRequest->getVal( 'title' ) );
263 if ( $t == '' ) { // fallback to main page
264 $t = Title::newFromText(
265 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
266 }
267 }
268
269 $cache = HTMLFileCache::newFromTitle( $t, 'view' );
270 if ( $cache->isCached() ) {
271 return $cache->fetchText();
272 } else {
273 return '';
274 }
275 }
276 }
277
278 /**
279 * @ingroup Database
280 */
281 class DBQueryError extends DBError {
282 public $error, $errno, $sql, $fname;
283
284 /**
285 * @param $db DatabaseBase
286 * @param $error string
287 * @param $errno int|string
288 * @param $sql string
289 * @param $fname string
290 */
291 function __construct( DatabaseBase &$db, $error, $errno, $sql, $fname ) {
292 $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" .
293 "Query: $sql\n" .
294 "Function: $fname\n" .
295 "Error: $errno $error\n";
296 parent::__construct( $db, $message );
297
298 $this->error = $error;
299 $this->errno = $errno;
300 $this->sql = $sql;
301 $this->fname = $fname;
302 }
303
304 /**
305 * @param $html string
306 * @return string
307 */
308 function getContentMessage( $html ) {
309 if ( $this->useMessageCache() ) {
310 if ( $html ) {
311 $msg = 'dberrortext';
312 $sql = htmlspecialchars( $this->getSQL() );
313 $fname = htmlspecialchars( $this->fname );
314 $error = htmlspecialchars( $this->error );
315 } else {
316 $msg = 'dberrortextcl';
317 $sql = $this->getSQL();
318 $fname = $this->fname;
319 $error = $this->error;
320 }
321 return wfMsg( $msg, $sql, $fname, $this->errno, $error );
322 } else {
323 return parent::getContentMessage( $html );
324 }
325 }
326
327 /**
328 * @return String
329 */
330 function getSQL() {
331 global $wgShowSQLErrors;
332
333 if ( !$wgShowSQLErrors ) {
334 return $this->msg( 'sqlhidden', 'SQL hidden' );
335 } else {
336 return $this->sql;
337 }
338 }
339
340 /**
341 * @return bool
342 */
343 function getLogMessage() {
344 # Don't send to the exception log
345 return false;
346 }
347
348 /**
349 * @return String
350 */
351 function getPageTitle() {
352 return $this->msg( 'databaseerror', 'Database error' );
353 }
354 }
355
356 /**
357 * @ingroup Database
358 */
359 class DBUnexpectedError extends DBError {}