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