2011-05-25 17:03:15 +00:00
< ? php
2012-04-26 08:47:10 +00:00
/**
* This file contains database error classes .
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License along
* with this program ; if not , write to the Free Software Foundation , Inc . ,
* 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
* http :// www . gnu . org / copyleft / gpl . html
*
* @ file
* @ ingroup Database
*/
2011-05-25 17:03:15 +00:00
/**
* Database error base class
* @ ingroup Database
*/
class DBError extends MWException {
2011-05-25 18:41:31 +00:00
/**
* @ var DatabaseBase
*/
2011-05-25 17:03:15 +00:00
public $db ;
/**
* Construct a database error
2011-05-25 18:41:31 +00:00
* @ param $db DatabaseBase object which threw the error
2011-05-25 17:03:15 +00:00
* @ param $error String A simple error message to be used for debugging
*/
function __construct ( DatabaseBase & $db , $error ) {
2011-07-04 15:00:30 +00:00
$this -> db = $db ;
2011-05-25 17:03:15 +00:00
parent :: __construct ( $error );
}
2011-05-25 18:58:02 +00:00
/**
* @ param $html string
* @ return string
*/
2011-05-25 17:03:15 +00:00
protected function getContentMessage ( $html ) {
if ( $html ) {
return nl2br ( htmlspecialchars ( $this -> getMessage () ) );
} else {
return $this -> getMessage ();
}
}
2011-05-25 18:58:02 +00:00
/**
* @ return string
*/
2011-05-25 17:03:15 +00:00
function getText () {
global $wgShowDBErrorBacktrace ;
$s = $this -> getContentMessage ( false ) . " \n " ;
if ( $wgShowDBErrorBacktrace ) {
$s .= " Backtrace: \n " . $this -> getTraceAsString () . " \n " ;
}
return $s ;
}
2011-05-25 18:58:02 +00:00
/**
* @ return string
*/
2011-05-25 17:03:15 +00:00
function getHTML () {
global $wgShowDBErrorBacktrace ;
$s = $this -> getContentMessage ( true );
if ( $wgShowDBErrorBacktrace ) {
$s .= '<p>Backtrace:</p><p>' . nl2br ( htmlspecialchars ( $this -> getTraceAsString () ) );
}
return $s ;
}
}
/**
* @ ingroup Database
*/
class DBConnectionError extends DBError {
public $error ;
function __construct ( DatabaseBase & $db , $error = 'unknown error' ) {
$msg = 'DB connection error' ;
if ( trim ( $error ) != '' ) {
$msg .= " : $error " ;
}
$this -> error = $error ;
parent :: __construct ( $db , $msg );
}
2011-11-29 21:04:20 +00:00
/**
* @ return bool
*/
2011-05-25 17:03:15 +00:00
function useOutputPage () {
// Not likely to work
return false ;
}
2011-09-16 17:58:50 +00:00
/**
* @ param $key
* @ param $fallback
* @ return string
*/
2011-05-25 17:03:15 +00:00
function msg ( $key , $fallback /*[, params...] */ ) {
global $wgLang ;
$args = array_slice ( func_get_args (), 2 );
if ( $this -> useMessageCache () ) {
$message = $wgLang -> getMessage ( $key );
} else {
$message = $fallback ;
}
return wfMsgReplaceArgs ( $message , $args );
}
2011-11-29 21:04:20 +00:00
/**
* @ return bool
*/
2011-05-25 17:03:15 +00:00
function getLogMessage () {
# Don't send to the exception log
return false ;
}
2011-05-25 18:58:02 +00:00
/**
* @ return string
*/
2011-05-25 17:03:15 +00:00
function getPageTitle () {
global $wgSitename ;
return htmlspecialchars ( $this -> msg ( 'dberr-header' , " $wgSitename has a problem " ) );
}
2011-05-25 18:58:02 +00:00
/**
* @ return string
*/
2011-05-25 17:03:15 +00:00
function getHTML () {
global $wgShowDBErrorBacktrace ;
$sorry = htmlspecialchars ( $this -> msg ( 'dberr-problems' , 'Sorry! This site is experiencing technical difficulties.' ) );
$again = htmlspecialchars ( $this -> msg ( 'dberr-again' , 'Try waiting a few minutes and reloading.' ) );
$info = htmlspecialchars ( $this -> msg ( 'dberr-info' , '(Can\'t contact the database server: $1)' ) );
# No database access
MessageCache :: singleton () -> disable ();
if ( trim ( $this -> error ) == '' ) {
$this -> error = $this -> db -> getProperty ( 'mServer' );
}
$this -> error = Html :: element ( 'span' , array ( 'dir' => 'ltr' ), $this -> error );
$noconnect = " <h1> $sorry </h1><p> $again </p><p><small> $info </small></p> " ;
$text = str_replace ( '$1' , $this -> error , $noconnect );
if ( $wgShowDBErrorBacktrace ) {
$text .= '<p>Backtrace:</p><p>' . nl2br ( htmlspecialchars ( $this -> getTraceAsString () ) );
}
$extra = $this -> searchForm ();
return " $text <hr /> $extra " ;
}
public function reportHTML (){
global $wgUseFileCache ;
# Check whether we can serve a file-cached copy of the page with the error underneath
if ( $wgUseFileCache ) {
try {
$cache = $this -> fileCachedPage ();
# Cached version on file system?
if ( $cache !== null ) {
# Hack: extend the body for error messages
$cache = str_replace ( array ( '</html>' , '</body>' ), '' , $cache );
# Add cache notice...
$cache .= '<div style="color:red;font-size:150%;font-weight:bold;">' .
htmlspecialchars ( $this -> msg ( 'dberr-cachederror' ,
'This is a cached copy of the requested page, and may not be up to date. ' ) ) .
'</div>' ;
# Output cached page with notices on bottom and re-close body
echo " { $cache } <hr /> { $this -> getHTML () } </body></html> " ;
return ;
}
} catch ( MWException $e ) {
// Do nothing, just use the default page
}
}
# We can't, cough and die in the usual fashion
2011-11-13 21:42:57 +00:00
parent :: reportHTML ();
2011-05-25 17:03:15 +00:00
}
2011-05-25 18:58:02 +00:00
/**
* @ return string
*/
2011-05-25 17:03:15 +00:00
function searchForm () {
2012-06-05 17:47:17 +00:00
global $wgSitename , $wgCanonicalServer , $wgRequest ;
2011-05-25 17:03:15 +00:00
$usegoogle = htmlspecialchars ( $this -> msg ( 'dberr-usegoogle' , 'You can try searching via Google in the meantime.' ) );
$outofdate = htmlspecialchars ( $this -> msg ( 'dberr-outofdate' , 'Note that their indexes of our content may be out of date.' ) );
$googlesearch = htmlspecialchars ( $this -> msg ( 'searchbutton' , 'Search' ) );
2011-07-04 15:00:30 +00:00
$search = htmlspecialchars ( $wgRequest -> getVal ( 'search' ) );
2011-05-25 17:03:15 +00:00
2012-06-05 17:47:17 +00:00
$server = htmlspecialchars ( $wgCanonicalServer );
2011-05-25 17:03:15 +00:00
$sitename = htmlspecialchars ( $wgSitename );
$trygoogle = <<< EOT
< div style = " margin: 1.5em " > $usegoogle < br />
< small > $outofdate </ small ></ div >
<!-- SiteSearch Google -->
2011-11-13 21:42:57 +00:00
< form method = " get " action = " //www.google.com/search " id = " googlesearch " >
2011-05-25 17:03:15 +00:00
< input type = " hidden " name = " domains " value = " $server " />
< input type = " hidden " name = " num " value = " 50 " />
< input type = " hidden " name = " ie " value = " UTF-8 " />
< input type = " hidden " name = " oe " value = " UTF-8 " />
< input type = " text " name = " q " size = " 31 " maxlength = " 255 " value = " $search " />
< input type = " submit " name = " btnG " value = " $googlesearch " />
< div >
< input type = " radio " name = " sitesearch " id = " gwiki " value = " $server " checked = " checked " />< label for = " gwiki " > $sitename </ label >
< input type = " radio " name = " sitesearch " id = " gWWW " value = " " />< label for = " gWWW " > WWW </ label >
</ div >
</ form >
<!-- SiteSearch Google -->
EOT ;
return $trygoogle ;
}
2011-05-25 18:58:02 +00:00
/**
* @ return string
*/
2011-05-25 17:03:15 +00:00
private function fileCachedPage () {
2011-11-02 20:01:22 +00:00
global $wgTitle , $wgOut , $wgRequest ;
2011-05-25 17:03:15 +00:00
if ( $wgOut -> isDisabled () ) {
2011-11-13 21:42:57 +00:00
return '' ; // Done already?
2011-05-25 17:03:15 +00:00
}
2011-11-02 20:01:22 +00:00
if ( $wgTitle ) { // use $wgTitle if we managed to set it
$t = $wgTitle -> getPrefixedDBkey ();
2011-05-25 17:03:15 +00:00
} else {
2011-11-02 20:01:22 +00:00
# Fallback to the raw title URL param. We can't use the Title
# class is it may hit the interwiki table and give a DB error.
# We may get a cache miss due to not sanitizing the title though.
$t = str_replace ( ' ' , '_' , $wgRequest -> getVal ( 'title' ) );
if ( $t == '' ) { // fallback to main page
$t = Title :: newFromText (
$this -> msg ( 'mainpage' , 'Main Page' ) ) -> getPrefixedDBkey ();
}
2011-05-25 17:03:15 +00:00
}
2011-09-29 08:18:20 +00:00
$cache = HTMLFileCache :: newFromTitle ( $t , 'view' );
if ( $cache -> isCached () ) {
return $cache -> fetchText ();
2011-05-25 17:03:15 +00:00
} else {
return '' ;
}
}
}
/**
* @ ingroup Database
*/
class DBQueryError extends DBError {
public $error , $errno , $sql , $fname ;
2011-11-29 21:04:20 +00:00
/**
* @ param $db DatabaseBase
2011-12-05 16:50:58 +00:00
* @ param $error string
* @ param $errno int | string
* @ param $sql string
* @ param $fname string
2011-11-29 21:04:20 +00:00
*/
2011-05-25 17:03:15 +00:00
function __construct ( DatabaseBase & $db , $error , $errno , $sql , $fname ) {
2011-11-14 10:34:23 +00:00
$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 " .
2011-05-25 17:03:15 +00:00
" Query: $sql\n " .
" Function: $fname\n " .
" Error: $errno $error\n " ;
parent :: __construct ( $db , $message );
$this -> error = $error ;
$this -> errno = $errno ;
$this -> sql = $sql ;
$this -> fname = $fname ;
}
2011-05-25 18:58:02 +00:00
/**
* @ param $html string
* @ return string
*/
2011-05-25 17:03:15 +00:00
function getContentMessage ( $html ) {
if ( $this -> useMessageCache () ) {
if ( $html ) {
2011-06-23 18:42:51 +00:00
$msg = 'dberrortext' ;
$sql = htmlspecialchars ( $this -> getSQL () );
$fname = htmlspecialchars ( $this -> fname );
$error = htmlspecialchars ( $this -> error );
} else {
$msg = 'dberrortextcl' ;
$sql = $this -> getSQL ();
$fname = $this -> fname ;
$error = $this -> error ;
2011-05-25 17:03:15 +00:00
}
2012-07-24 01:04:15 +00:00
return wfMessage ( $msg ) -> rawParams ( $sql , $fname , $this -> errno , $error ) -> text ();
2011-05-25 17:03:15 +00:00
} else {
return parent :: getContentMessage ( $html );
}
}
2011-05-25 18:58:02 +00:00
/**
* @ return String
*/
2011-05-25 17:03:15 +00:00
function getSQL () {
global $wgShowSQLErrors ;
if ( ! $wgShowSQLErrors ) {
return $this -> msg ( 'sqlhidden' , 'SQL hidden' );
} else {
return $this -> sql ;
}
}
2011-11-29 21:04:20 +00:00
/**
* @ return bool
*/
2011-05-25 17:03:15 +00:00
function getLogMessage () {
# Don't send to the exception log
return false ;
}
2011-05-25 18:58:02 +00:00
/**
* @ return String
*/
2011-05-25 17:03:15 +00:00
function getPageTitle () {
return $this -> msg ( 'databaseerror' , 'Database error' );
}
}
/**
* @ ingroup Database
*/
class DBUnexpectedError extends DBError {}