4 * Database error base class
7 class DBError
extends MWException
{
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 ) {
21 parent
::__construct( $error );
28 protected function getContentMessage( $html ) {
30 return nl2br( htmlspecialchars( $this->getMessage() ) );
32 return $this->getMessage();
40 global $wgShowDBErrorBacktrace;
42 $s = $this->getContentMessage( false ) . "\n";
44 if ( $wgShowDBErrorBacktrace ) {
45 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
55 global $wgShowDBErrorBacktrace;
57 $s = $this->getContentMessage( true );
59 if ( $wgShowDBErrorBacktrace ) {
60 $s .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
70 class DBConnectionError
extends DBError
{
73 function __construct( DatabaseBase
&$db, $error = 'unknown error' ) {
74 $msg = 'DB connection error';
76 if ( trim( $error ) != '' ) {
80 $this->error
= $error;
82 parent
::__construct( $db, $msg );
85 function useOutputPage() {
95 function msg( $key, $fallback /*[, params...] */ ) {
98 $args = array_slice( func_get_args(), 2 );
100 if ( $this->useMessageCache() ) {
101 $message = $wgLang->getMessage( $key );
103 $message = $fallback;
105 return wfMsgReplaceArgs( $message, $args );
108 function getLogMessage() {
109 # Don't send to the exception log
116 function getPageTitle() {
118 return htmlspecialchars( $this->msg( 'dberr-header', "$wgSitename has a problem" ) );
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)' ) );
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 ) {
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. ' ) ) .
169 # Output cached page with notices on bottom and re-close body
170 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
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();
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 );
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" />
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>
214 <!-- SiteSearch Google -->
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();
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();
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" .
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;
271 $this->fname
= $fname;
275 * @param $html string
278 function getContentMessage( $html ) {
279 if ( $this->useMessageCache() ) {
281 $msg = 'dberrortext';
282 $sql = htmlspecialchars( $this->getSQL() );
283 $fname = htmlspecialchars( $this->fname
);
284 $error = htmlspecialchars( $this->error
);
286 $msg = 'dberrortextcl';
287 $sql = $this->getSQL();
288 $fname = $this->fname
;
289 $error = $this->error
;
291 return wfMsg( $msg, $sql, $fname, $this->errno
, $error );
293 return parent
::getContentMessage( $html );
301 global $wgShowSQLErrors;
303 if ( !$wgShowSQLErrors ) {
304 return $this->msg( 'sqlhidden', 'SQL hidden' );
310 function getLogMessage() {
311 # Don't send to the exception log
318 function getPageTitle() {
319 return $this->msg( 'databaseerror', 'Database error' );
326 class DBUnexpectedError
extends DBError
{}