Updated calls to Linker to call them statically and removed useless parameter to...
[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 function useOutputPage() {
86 // Not likely to work
87 return false;
88 }
89
90 function msg( $key, $fallback /*[, params...] */ ) {
91 global $wgLang;
92
93 $args = array_slice( func_get_args(), 2 );
94
95 if ( $this->useMessageCache() ) {
96 $message = $wgLang->getMessage( $key );
97 } else {
98 $message = $fallback;
99 }
100 return wfMsgReplaceArgs( $message, $args );
101 }
102
103 function getLogMessage() {
104 # Don't send to the exception log
105 return false;
106 }
107
108 /**
109 * @return string
110 */
111 function getPageTitle() {
112 global $wgSitename;
113 return htmlspecialchars( $this->msg( 'dberr-header', "$wgSitename has a problem" ) );
114 }
115
116 /**
117 * @return string
118 */
119 function getHTML() {
120 global $wgShowDBErrorBacktrace;
121
122 $sorry = htmlspecialchars( $this->msg( 'dberr-problems', 'Sorry! This site is experiencing technical difficulties.' ) );
123 $again = htmlspecialchars( $this->msg( 'dberr-again', 'Try waiting a few minutes and reloading.' ) );
124 $info = htmlspecialchars( $this->msg( 'dberr-info', '(Can\'t contact the database server: $1)' ) );
125
126 # No database access
127 MessageCache::singleton()->disable();
128
129 if ( trim( $this->error ) == '' ) {
130 $this->error = $this->db->getProperty( 'mServer' );
131 }
132
133 $this->error = Html::element( 'span', array( 'dir' => 'ltr' ), $this->error );
134
135 $noconnect = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
136 $text = str_replace( '$1', $this->error, $noconnect );
137
138 if ( $wgShowDBErrorBacktrace ) {
139 $text .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
140 }
141
142 $extra = $this->searchForm();
143
144 return "$text<hr />$extra";
145 }
146
147 public function reportHTML(){
148 global $wgUseFileCache;
149
150 # Check whether we can serve a file-cached copy of the page with the error underneath
151 if ( $wgUseFileCache ) {
152 try {
153 $cache = $this->fileCachedPage();
154 # Cached version on file system?
155 if ( $cache !== null ) {
156 # Hack: extend the body for error messages
157 $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
158 # Add cache notice...
159 $cache .= '<div style="color:red;font-size:150%;font-weight:bold;">'.
160 htmlspecialchars( $this->msg( 'dberr-cachederror',
161 'This is a cached copy of the requested page, and may not be up to date. ' ) ) .
162 '</div>';
163
164 # Output cached page with notices on bottom and re-close body
165 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
166 return;
167 }
168 } catch ( MWException $e ) {
169 // Do nothing, just use the default page
170 }
171 }
172
173 # We can't, cough and die in the usual fashion
174 return parent::reportHTML();
175 }
176
177 /**
178 * @return string
179 */
180 function searchForm() {
181 global $wgSitename, $wgServer;
182
183 $usegoogle = htmlspecialchars( $this->msg( 'dberr-usegoogle', 'You can try searching via Google in the meantime.' ) );
184 $outofdate = htmlspecialchars( $this->msg( 'dberr-outofdate', 'Note that their indexes of our content may be out of date.' ) );
185 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
186
187 $search = htmlspecialchars( @$_REQUEST['search'] );
188
189 $server = htmlspecialchars( $wgServer );
190 $sitename = htmlspecialchars( $wgSitename );
191
192 $trygoogle = <<<EOT
193 <div style="margin: 1.5em">$usegoogle<br />
194 <small>$outofdate</small></div>
195 <!-- SiteSearch Google -->
196 <form method="get" action="http://www.google.com/search" id="googlesearch">
197 <input type="hidden" name="domains" value="$server" />
198 <input type="hidden" name="num" value="50" />
199 <input type="hidden" name="ie" value="UTF-8" />
200 <input type="hidden" name="oe" value="UTF-8" />
201
202 <input type="text" name="q" size="31" maxlength="255" value="$search" />
203 <input type="submit" name="btnG" value="$googlesearch" />
204 <div>
205 <input type="radio" name="sitesearch" id="gwiki" value="$server" checked="checked" /><label for="gwiki">$sitename</label>
206 <input type="radio" name="sitesearch" id="gWWW" value="" /><label for="gWWW">WWW</label>
207 </div>
208 </form>
209 <!-- SiteSearch Google -->
210 EOT;
211 return $trygoogle;
212 }
213
214 /**
215 * @return string
216 */
217 private function fileCachedPage() {
218 global $wgTitle, $wgOut;
219
220 if ( $wgOut->isDisabled() ) {
221 return; // Done already?
222 }
223
224 if ( $wgTitle ) {
225 $t =& $wgTitle;
226 } else {
227 $t = Title::newFromText( $this->msg( 'mainpage', 'Main Page' ) );
228 }
229
230 $cache = new HTMLFileCache( $t );
231 if ( $cache->isFileCached() ) {
232 return $cache->fetchPageText();
233 } else {
234 return '';
235 }
236 }
237 }
238
239 /**
240 * @ingroup Database
241 */
242 class DBQueryError extends DBError {
243 public $error, $errno, $sql, $fname;
244
245 function __construct( DatabaseBase &$db, $error, $errno, $sql, $fname ) {
246 $message = "A database error has occurred. Did you forget to run maintenance/update.php after upgrading? See: http://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
247 "Query: $sql\n" .
248 "Function: $fname\n" .
249 "Error: $errno $error\n";
250 global $wgShowDBErrorBacktrace;
251 if( $wgShowDBErrorBacktrace ) {
252 $message .= $this->getTraceAsString();
253 }
254 parent::__construct( $db, $message );
255
256 $this->error = $error;
257 $this->errno = $errno;
258 $this->sql = $sql;
259 $this->fname = $fname;
260 }
261
262 /**
263 * @param $html string
264 * @return string
265 */
266 function getContentMessage( $html ) {
267 if ( $this->useMessageCache() ) {
268 $msg = $html ? 'dberrortext' : 'dberrortextcl';
269 $ret = wfMsg( $msg, $this->getSQL(),
270 $this->fname, $this->errno, $this->error );
271 if ( $html ) {
272 $ret = htmlspecialchars( $ret );
273 }
274 return $ret;
275 } else {
276 return parent::getContentMessage( $html );
277 }
278 }
279
280 /**
281 * @return String
282 */
283 function getSQL() {
284 global $wgShowSQLErrors;
285
286 if ( !$wgShowSQLErrors ) {
287 return $this->msg( 'sqlhidden', 'SQL hidden' );
288 } else {
289 return $this->sql;
290 }
291 }
292
293 function getLogMessage() {
294 # Don't send to the exception log
295 return false;
296 }
297
298 /**
299 * @return String
300 */
301 function getPageTitle() {
302 return $this->msg( 'databaseerror', 'Database error' );
303 }
304 }
305
306 /**
307 * @ingroup Database
308 */
309 class DBUnexpectedError extends DBError {}