MediaWiki  REL1_20
DatabaseError.php
Go to the documentation of this file.
00001 <?php
00028 class DBError extends MWException {
00029 
00033         public $db;
00034 
00040         function __construct( DatabaseBase &$db, $error ) {
00041                 $this->db = $db;
00042                 parent::__construct( $error );
00043         }
00044 
00049         protected function getContentMessage( $html ) {
00050                 if ( $html ) {
00051                         return nl2br( htmlspecialchars( $this->getMessage() ) );
00052                 } else {
00053                         return $this->getMessage();
00054                 }
00055         }
00056 
00060         function getText() {
00061                 global $wgShowDBErrorBacktrace;
00062 
00063                 $s = $this->getContentMessage( false ) . "\n";
00064 
00065                 if ( $wgShowDBErrorBacktrace ) {
00066                         $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
00067                 }
00068 
00069                 return $s;
00070         }
00071 
00075         function getHTML() {
00076                 global $wgShowDBErrorBacktrace;
00077 
00078                 $s = $this->getContentMessage( true );
00079 
00080                 if ( $wgShowDBErrorBacktrace ) {
00081                         $s .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
00082                 }
00083 
00084                 return $s;
00085         }
00086 }
00087 
00091 class DBConnectionError extends DBError {
00092         public $error;
00093 
00094         function __construct( DatabaseBase &$db, $error = 'unknown error' ) {
00095                 $msg = 'DB connection error';
00096 
00097                 if ( trim( $error ) != '' ) {
00098                         $msg .= ": $error";
00099                 }
00100 
00101                 $this->error = $error;
00102 
00103                 parent::__construct( $db, $msg );
00104         }
00105 
00109         function useOutputPage() {
00110                 // Not likely to work
00111                 return false;
00112         }
00113 
00119         function msg( $key, $fallback /*[, params...] */ ) {
00120                 global $wgLang;
00121 
00122                 $args = array_slice( func_get_args(), 2 );
00123 
00124                 if ( $this->useMessageCache() ) {
00125                         $message = $wgLang->getMessage( $key );
00126                 } else {
00127                         $message = $fallback;
00128                 }
00129                 return wfMsgReplaceArgs( $message, $args );
00130         }
00131 
00135         function getLogMessage() {
00136                 # Don't send to the exception log
00137                 return false;
00138         }
00139 
00143         function getPageTitle() {
00144                 global $wgSitename;
00145                 return htmlspecialchars( $this->msg( 'dberr-header', "$wgSitename has a problem" ) );
00146         }
00147 
00151         function getHTML() {
00152                 global $wgShowDBErrorBacktrace;
00153 
00154                 $sorry = htmlspecialchars( $this->msg( 'dberr-problems', 'Sorry! This site is experiencing technical difficulties.' ) );
00155                 $again = htmlspecialchars( $this->msg( 'dberr-again', 'Try waiting a few minutes and reloading.' ) );
00156                 $info  = htmlspecialchars( $this->msg( 'dberr-info', '(Can\'t contact the database server: $1)' ) );
00157 
00158                 # No database access
00159                 MessageCache::singleton()->disable();
00160 
00161                 if ( trim( $this->error ) == '' ) {
00162                         $this->error = $this->db->getProperty( 'mServer' );
00163                 }
00164 
00165                 $this->error = Html::element( 'span', array( 'dir' => 'ltr' ), $this->error );
00166 
00167                 $noconnect = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
00168                 $text = str_replace( '$1', $this->error, $noconnect );
00169 
00170                 if ( $wgShowDBErrorBacktrace ) {
00171                         $text .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
00172                 }
00173 
00174                 $extra = $this->searchForm();
00175 
00176                 return "$text<hr />$extra";
00177         }
00178 
00179         public function reportHTML(){
00180                 global $wgUseFileCache;
00181 
00182                 # Check whether we can serve a file-cached copy of the page with the error underneath
00183                 if ( $wgUseFileCache ) {
00184                         try {
00185                                 $cache = $this->fileCachedPage();
00186                                 # Cached version on file system?
00187                                 if ( $cache !== null ) {
00188                                         # Hack: extend the body for error messages
00189                                         $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
00190                                         # Add cache notice...
00191                                         $cache .= '<div style="color:red;font-size:150%;font-weight:bold;">'.
00192                                                 htmlspecialchars( $this->msg( 'dberr-cachederror',
00193                                                         'This is a cached copy of the requested page, and may not be up to date. ' ) ) .
00194                                                 '</div>';
00195 
00196                                         # Output cached page with notices on bottom and re-close body
00197                                         echo "{$cache}<hr />{$this->getHTML()}</body></html>";
00198                                         return;
00199                                 }
00200                         } catch ( MWException $e ) {
00201                                 // Do nothing, just use the default page
00202                         }
00203                 }
00204 
00205                 # We can't, cough and die in the usual fashion
00206                 parent::reportHTML();
00207         }
00208 
00212         function searchForm() {
00213                 global $wgSitename, $wgCanonicalServer, $wgRequest;
00214 
00215                 $usegoogle = htmlspecialchars( $this->msg( 'dberr-usegoogle', 'You can try searching via Google in the meantime.' ) );
00216                 $outofdate = htmlspecialchars( $this->msg( 'dberr-outofdate', 'Note that their indexes of our content may be out of date.' ) );
00217                 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
00218 
00219                 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
00220 
00221                 $server = htmlspecialchars( $wgCanonicalServer );
00222                 $sitename = htmlspecialchars( $wgSitename );
00223 
00224                 $trygoogle = <<<EOT
00225 <div style="margin: 1.5em">$usegoogle<br />
00226 <small>$outofdate</small></div>
00227 <!-- SiteSearch Google -->
00228 <form method="get" action="//www.google.com/search" id="googlesearch">
00229         <input type="hidden" name="domains" value="$server" />
00230         <input type="hidden" name="num" value="50" />
00231         <input type="hidden" name="ie" value="UTF-8" />
00232         <input type="hidden" name="oe" value="UTF-8" />
00233 
00234         <input type="text" name="q" size="31" maxlength="255" value="$search" />
00235         <input type="submit" name="btnG" value="$googlesearch" />
00236   <div>
00237         <input type="radio" name="sitesearch" id="gwiki" value="$server" checked="checked" /><label for="gwiki">$sitename</label>
00238         <input type="radio" name="sitesearch" id="gWWW" value="" /><label for="gWWW">WWW</label>
00239   </div>
00240 </form>
00241 <!-- SiteSearch Google -->
00242 EOT;
00243                 return $trygoogle;
00244         }
00245 
00249         private function fileCachedPage() {
00250                 global $wgTitle, $wgOut, $wgRequest;
00251 
00252                 if ( $wgOut->isDisabled() ) {
00253                         return ''; // Done already?
00254                 }
00255 
00256                 if ( $wgTitle ) { // use $wgTitle if we managed to set it
00257                         $t = $wgTitle->getPrefixedDBkey();
00258                 } else {
00259                         # Fallback to the raw title URL param. We can't use the Title
00260                         # class is it may hit the interwiki table and give a DB error.
00261                         # We may get a cache miss due to not sanitizing the title though.
00262                         $t = str_replace( ' ', '_', $wgRequest->getVal( 'title' ) );
00263                         if ( $t == '' ) { // fallback to main page
00264                                 $t = Title::newFromText(
00265                                         $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
00266                         }
00267                 }
00268 
00269                 $cache = HTMLFileCache::newFromTitle( $t, 'view' );
00270                 if ( $cache->isCached() ) {
00271                         return $cache->fetchText();
00272                 } else {
00273                         return '';
00274                 }
00275         }
00276 }
00277 
00281 class DBQueryError extends DBError {
00282         public $error, $errno, $sql, $fname;
00283 
00291         function __construct( DatabaseBase &$db, $error, $errno, $sql, $fname ) {
00292                 $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" .
00293                   "Query: $sql\n" .
00294                   "Function: $fname\n" .
00295                   "Error: $errno $error\n";
00296                 parent::__construct( $db, $message );
00297 
00298                 $this->error = $error;
00299                 $this->errno = $errno;
00300                 $this->sql = $sql;
00301                 $this->fname = $fname;
00302         }
00303 
00308         function getContentMessage( $html ) {
00309                 if ( $this->useMessageCache() ) {
00310                         if ( $html ) {
00311                                 $msg = 'dberrortext';
00312                                 $sql = htmlspecialchars( $this->getSQL() );
00313                                 $fname = htmlspecialchars( $this->fname );
00314                                 $error = htmlspecialchars( $this->error );
00315                         } else {
00316                                 $msg = 'dberrortextcl';
00317                                 $sql = $this->getSQL();
00318                                 $fname = $this->fname;
00319                                 $error = $this->error;
00320                         }
00321                         return wfMessage( $msg )->rawParams( $sql, $fname, $this->errno, $error )->text();
00322                 } else {
00323                         return parent::getContentMessage( $html );
00324                 }
00325         }
00326 
00330         function getSQL() {
00331                 global $wgShowSQLErrors;
00332 
00333                 if ( !$wgShowSQLErrors ) {
00334                         return $this->msg( 'sqlhidden', 'SQL hidden' );
00335                 } else {
00336                         return $this->sql;
00337                 }
00338         }
00339 
00343         function getLogMessage() {
00344                 # Don't send to the exception log
00345                 return false;
00346         }
00347 
00351         function getPageTitle() {
00352                 return $this->msg( 'databaseerror', 'Database error' );
00353         }
00354 }
00355 
00359 class DBUnexpectedError extends DBError {}