* fixed ipblocks.ipb_by_text field, removed default blank not null (fixed install...
[mediawiki.git] / includes / db / DatabaseError.php
blobd7bfe93312958a5b86794f883b1fe2068037e3cd
1 <?php
3 /**
4 * Database error base class
5 * @ingroup Database
6 */
7 class DBError extends MWException {
9 /**
10 * @var DatabaseBase
12 public $db;
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
19 function __construct( DatabaseBase &$db, $error ) {
20 $this->db = $db;
21 parent::__construct( $error );
24 /**
25 * @param $html string
26 * @return string
28 protected function getContentMessage( $html ) {
29 if ( $html ) {
30 return nl2br( htmlspecialchars( $this->getMessage() ) );
31 } else {
32 return $this->getMessage();
36 /**
37 * @return string
39 function getText() {
40 global $wgShowDBErrorBacktrace;
42 $s = $this->getContentMessage( false ) . "\n";
44 if ( $wgShowDBErrorBacktrace ) {
45 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
48 return $s;
51 /**
52 * @return string
54 function getHTML() {
55 global $wgShowDBErrorBacktrace;
57 $s = $this->getContentMessage( true );
59 if ( $wgShowDBErrorBacktrace ) {
60 $s .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
63 return $s;
67 /**
68 * @ingroup Database
70 class DBConnectionError extends DBError {
71 public $error;
73 function __construct( DatabaseBase &$db, $error = 'unknown error' ) {
74 $msg = 'DB connection error';
76 if ( trim( $error ) != '' ) {
77 $msg .= ": $error";
80 $this->error = $error;
82 parent::__construct( $db, $msg );
85 function useOutputPage() {
86 // Not likely to work
87 return false;
90 /**
91 * @param $key
92 * @param $fallback
93 * @return string
95 function msg( $key, $fallback /*[, params...] */ ) {
96 global $wgLang;
98 $args = array_slice( func_get_args(), 2 );
100 if ( $this->useMessageCache() ) {
101 $message = $wgLang->getMessage( $key );
102 } else {
103 $message = $fallback;
105 return wfMsgReplaceArgs( $message, $args );
108 function getLogMessage() {
109 # Don't send to the exception log
110 return false;
114 * @return string
116 function getPageTitle() {
117 global $wgSitename;
118 return htmlspecialchars( $this->msg( 'dberr-header', "$wgSitename has a problem" ) );
122 * @return string
124 function getHTML() {
125 global $wgShowDBErrorBacktrace;
127 $sorry = htmlspecialchars( $this->msg( 'dberr-problems', 'Sorry! This site is experiencing technical difficulties.' ) );
128 $again = htmlspecialchars( $this->msg( 'dberr-again', 'Try waiting a few minutes and reloading.' ) );
129 $info = htmlspecialchars( $this->msg( 'dberr-info', '(Can\'t contact the database server: $1)' ) );
131 # No database access
132 MessageCache::singleton()->disable();
134 if ( trim( $this->error ) == '' ) {
135 $this->error = $this->db->getProperty( 'mServer' );
138 $this->error = Html::element( 'span', array( 'dir' => 'ltr' ), $this->error );
140 $noconnect = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
141 $text = str_replace( '$1', $this->error, $noconnect );
143 if ( $wgShowDBErrorBacktrace ) {
144 $text .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
147 $extra = $this->searchForm();
149 return "$text<hr />$extra";
152 public function reportHTML(){
153 global $wgUseFileCache;
155 # Check whether we can serve a file-cached copy of the page with the error underneath
156 if ( $wgUseFileCache ) {
157 try {
158 $cache = $this->fileCachedPage();
159 # Cached version on file system?
160 if ( $cache !== null ) {
161 # Hack: extend the body for error messages
162 $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
163 # Add cache notice...
164 $cache .= '<div style="color:red;font-size:150%;font-weight:bold;">'.
165 htmlspecialchars( $this->msg( 'dberr-cachederror',
166 'This is a cached copy of the requested page, and may not be up to date. ' ) ) .
167 '</div>';
169 # Output cached page with notices on bottom and re-close body
170 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
171 return;
173 } catch ( MWException $e ) {
174 // Do nothing, just use the default page
178 # We can't, cough and die in the usual fashion
179 return parent::reportHTML();
183 * @return string
185 function searchForm() {
186 global $wgSitename, $wgServer, $wgRequest;
188 $usegoogle = htmlspecialchars( $this->msg( 'dberr-usegoogle', 'You can try searching via Google in the meantime.' ) );
189 $outofdate = htmlspecialchars( $this->msg( 'dberr-outofdate', 'Note that their indexes of our content may be out of date.' ) );
190 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
192 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
194 $server = htmlspecialchars( $wgServer );
195 $sitename = htmlspecialchars( $wgSitename );
197 $trygoogle = <<<EOT
198 <div style="margin: 1.5em">$usegoogle<br />
199 <small>$outofdate</small></div>
200 <!-- SiteSearch Google -->
201 <form method="get" action="http://www.google.com/search" id="googlesearch">
202 <input type="hidden" name="domains" value="$server" />
203 <input type="hidden" name="num" value="50" />
204 <input type="hidden" name="ie" value="UTF-8" />
205 <input type="hidden" name="oe" value="UTF-8" />
207 <input type="text" name="q" size="31" maxlength="255" value="$search" />
208 <input type="submit" name="btnG" value="$googlesearch" />
209 <div>
210 <input type="radio" name="sitesearch" id="gwiki" value="$server" checked="checked" /><label for="gwiki">$sitename</label>
211 <input type="radio" name="sitesearch" id="gWWW" value="" /><label for="gWWW">WWW</label>
212 </div>
213 </form>
214 <!-- SiteSearch Google -->
215 EOT;
216 return $trygoogle;
220 * @return string
222 private function fileCachedPage() {
223 global $wgTitle, $wgOut, $wgRequest;
225 if ( $wgOut->isDisabled() ) {
226 return; // Done already?
229 if ( $wgTitle ) { // use $wgTitle if we managed to set it
230 $t = $wgTitle->getPrefixedDBkey();
231 } else {
232 # Fallback to the raw title URL param. We can't use the Title
233 # class is it may hit the interwiki table and give a DB error.
234 # We may get a cache miss due to not sanitizing the title though.
235 $t = str_replace( ' ', '_', $wgRequest->getVal( 'title' ) );
236 if ( $t == '' ) { // fallback to main page
237 $t = Title::newFromText(
238 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
242 $cache = HTMLFileCache::newFromTitle( $t, 'view' );
243 if ( $cache->isCached() ) {
244 return $cache->fetchText();
245 } else {
246 return '';
252 * @ingroup Database
254 class DBQueryError extends DBError {
255 public $error, $errno, $sql, $fname;
257 function __construct( DatabaseBase &$db, $error, $errno, $sql, $fname ) {
258 $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" .
259 "Query: $sql\n" .
260 "Function: $fname\n" .
261 "Error: $errno $error\n";
262 global $wgShowDBErrorBacktrace;
263 if( $wgShowDBErrorBacktrace ) {
264 $message .= $this->getTraceAsString();
266 parent::__construct( $db, $message );
268 $this->error = $error;
269 $this->errno = $errno;
270 $this->sql = $sql;
271 $this->fname = $fname;
275 * @param $html string
276 * @return string
278 function getContentMessage( $html ) {
279 if ( $this->useMessageCache() ) {
280 if ( $html ) {
281 $msg = 'dberrortext';
282 $sql = htmlspecialchars( $this->getSQL() );
283 $fname = htmlspecialchars( $this->fname );
284 $error = htmlspecialchars( $this->error );
285 } else {
286 $msg = 'dberrortextcl';
287 $sql = $this->getSQL();
288 $fname = $this->fname;
289 $error = $this->error;
291 return wfMsg( $msg, $sql, $fname, $this->errno, $error );
292 } else {
293 return parent::getContentMessage( $html );
298 * @return String
300 function getSQL() {
301 global $wgShowSQLErrors;
303 if ( !$wgShowSQLErrors ) {
304 return $this->msg( 'sqlhidden', 'SQL hidden' );
305 } else {
306 return $this->sql;
310 function getLogMessage() {
311 # Don't send to the exception log
312 return false;
316 * @return String
318 function getPageTitle() {
319 return $this->msg( 'databaseerror', 'Database error' );
324 * @ingroup Database
326 class DBUnexpectedError extends DBError {}