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
2013-03-11 17:15:01 +00:00
* @ param string $error A simple error message to be used for debugging
2011-05-25 17:03:15 +00:00
*/
2013-01-26 15:38:29 +00:00
function __construct ( DatabaseBase $db = null , $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
/**
* @ return string
*/
2011-05-25 17:03:15 +00:00
function getText () {
global $wgShowDBErrorBacktrace ;
2013-03-03 14:27:47 +00:00
$s = $this -> getTextContent () . " \n " ;
2011-05-25 17:03:15 +00:00
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 ;
2013-03-03 14:27:47 +00:00
$s = $this -> getHTMLContent ();
2011-05-25 17:03:15 +00:00
if ( $wgShowDBErrorBacktrace ) {
2013-09-19 16:22:51 +00:00
$s .= '<p>Backtrace:</p><pre>' . htmlspecialchars ( $this -> getTraceAsString () ) . '</pre>' ;
2011-05-25 17:03:15 +00:00
}
return $s ;
}
2013-03-03 14:27:47 +00:00
/**
* @ return string
*/
protected function getTextContent () {
return $this -> getMessage ();
}
/**
* @ return string
*/
protected function getHTMLContent () {
return '<p>' . nl2br ( htmlspecialchars ( $this -> getMessage () ) ) . '</p>' ;
}
2011-05-25 17:03:15 +00:00
}
/**
* @ ingroup Database
*/
class DBConnectionError extends DBError {
public $error ;
2013-01-26 15:38:29 +00:00
function __construct ( DatabaseBase $db = null , $error = 'unknown error' ) {
2011-05-25 17:03:15 +00:00
$msg = 'DB connection error' ;
if ( trim ( $error ) != '' ) {
$msg .= " : $error " ;
2013-03-03 14:27:47 +00:00
} elseif ( $db ) {
$error = $this -> db -> getServer ();
2011-05-25 17:03:15 +00:00
}
parent :: __construct ( $db , $msg );
2013-03-03 14:27:47 +00:00
$this -> error = $error ;
2011-05-25 17:03:15 +00:00
}
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 () {
2013-09-19 16:22:51 +00:00
// Don't send to the exception log
2011-05-25 17:03:15 +00:00
return false ;
}
2011-05-25 18:58:02 +00:00
/**
* @ return string
*/
2011-05-25 17:03:15 +00:00
function getHTML () {
2013-03-03 14:27:47 +00:00
global $wgShowDBErrorBacktrace , $wgShowHostnames , $wgShowSQLErrors ;
2011-05-25 17:03:15 +00:00
2013-09-19 16:22:51 +00:00
$sorry = htmlspecialchars ( $this -> msg ( 'dberr-problems' , 'Sorry! This site is experiencing technical difficulties.' ) );
2011-05-25 17:03:15 +00:00
$again = htmlspecialchars ( $this -> msg ( 'dberr-again' , 'Try waiting a few minutes and reloading.' ) );
2013-03-03 14:27:47 +00:00
if ( $wgShowHostnames || $wgShowSQLErrors ) {
$info = str_replace (
'$1' , Html :: element ( 'span' , array ( 'dir' => 'ltr' ), $this -> error ),
htmlspecialchars ( $this -> msg ( 'dberr-info' , '(Cannot contact the database server: $1)' ) )
);
} else {
$info = htmlspecialchars ( $this -> msg ( 'dberr-info-hidden' , '(Cannot contact the database server)' ) );
2011-05-25 17:03:15 +00:00
}
2013-03-03 14:27:47 +00:00
# No database access
MessageCache :: singleton () -> disable ();
2011-05-25 17:03:15 +00:00
2013-09-19 16:22:51 +00:00
$html = " <h1> $sorry </h1><p> $again </p><p><small> $info </small></p> " ;
2011-05-25 17:03:15 +00:00
if ( $wgShowDBErrorBacktrace ) {
2013-09-19 16:22:51 +00:00
$html .= '<p>Backtrace:</p><pre>' . htmlspecialchars ( $this -> getTraceAsString () ) . '</pre>' ;
2011-05-25 17:03:15 +00:00
}
2013-09-19 16:22:51 +00:00
$html .= '<hr />' ;
$html .= $this -> searchForm ();
2011-05-25 17:03:15 +00:00
2013-09-19 16:22:51 +00:00
return $html ;
2013-03-03 14:27:47 +00:00
}
protected function getTextContent () {
global $wgShowHostnames , $wgShowSQLErrors ;
if ( $wgShowHostnames || $wgShowSQLErrors ) {
return $this -> getMessage ();
} else {
return 'DB connection error' ;
}
2011-05-25 17:03:15 +00:00
}
2013-01-26 21:11:09 +00:00
public function reportHTML () {
2011-05-25 17:03:15 +00:00
global $wgUseFileCache ;
2013-09-19 16:22:51 +00:00
// Check whether we can serve a file-cached copy of the page with the error underneath
2011-05-25 17:03:15 +00:00
if ( $wgUseFileCache ) {
try {
$cache = $this -> fileCachedPage ();
2013-09-19 16:22:51 +00:00
// Cached version on file system?
2011-05-25 17:03:15 +00:00
if ( $cache !== null ) {
2013-09-19 16:22:51 +00:00
// Hack: extend the body for error messages
2011-05-25 17:03:15 +00:00
$cache = str_replace ( array ( '</html>' , '</body>' ), '' , $cache );
2013-09-19 16:22:51 +00:00
// Add cache notice...
$cache .= '<div style="border:1px solid #ffd0d0;padding:1em;">' .
2011-05-25 17:03:15 +00:00
htmlspecialchars ( $this -> msg ( 'dberr-cachederror' ,
2013-09-19 16:22:51 +00:00
'This is a cached copy of the requested page, and may not be up to date.' ) ) .
2011-05-25 17:03:15 +00:00
'</div>' ;
2013-09-19 16:22:51 +00:00
// Output cached page with notices on bottom and re-close body
2011-05-25 17:03:15 +00:00
echo " { $cache } <hr /> { $this -> getHTML () } </body></html> " ;
return ;
}
} catch ( MWException $e ) {
// Do nothing, just use the default page
}
}
2013-09-19 16:22:51 +00:00
// 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 />
2013-09-19 16:22:51 +00:00
< small > $outofdate </ small >
</ div >
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 " />
2013-09-19 16:22:51 +00:00
< p >
< label >< input type = " radio " name = " sitesearch " value = " $server " checked = " checked " /> $sitename </ label >
< label >< input type = " radio " name = " sitesearch " value = " " /> WWW </ label >
</ p >
2011-05-25 17:03:15 +00:00
</ form >
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 () ) {
2013-09-19 16:22:51 +00:00
// Done already?
return '' ;
2011-05-25 17:03:15 +00:00
}
2013-09-19 16:22:51 +00:00
if ( $wgTitle ) {
// use $wgTitle if we managed to set it
2011-11-02 20:01:22 +00:00
$t = $wgTitle -> getPrefixedDBkey ();
2011-05-25 17:03:15 +00:00
} else {
2013-09-19 16:22:51 +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.
2011-11-02 20:01:22 +00:00
$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
*/
2013-01-26 15:38:29 +00:00
function __construct ( DatabaseBase $db , $error , $errno , $sql , $fname ) {
2013-02-03 18:47:42 +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 " .
" Query: $sql\n " .
" Function: $fname\n " .
" Error: $errno $error\n " ;
2011-05-25 17:03:15 +00:00
parent :: __construct ( $db , $message );
$this -> error = $error ;
$this -> errno = $errno ;
$this -> sql = $sql ;
$this -> fname = $fname ;
}
2011-05-25 18:58:02 +00:00
/**
2013-03-03 14:27:47 +00:00
* @ return bool
*/
function getLogMessage () {
# Don't send to the exception log
return false ;
}
/**
* @ return String
*/
function getPageTitle () {
return $this -> msg ( 'databaseerror' , 'Database error' );
}
/**
2011-05-25 18:58:02 +00:00
* @ return string
*/
2013-03-03 14:27:47 +00:00
protected function getHTMLContent () {
$key = 'databaseerror-text' ;
$s = Html :: element ( 'p' , array (), $this -> msg ( $key , $this -> getFallbackMessage ( $key ) ) );
$details = $this -> getTechnicalDetails ();
if ( $details ) {
$s .= '<ul>' ;
foreach ( $details as $key => $detail ) {
$s .= str_replace (
'$1' , call_user_func_array ( 'Html::element' , $detail ),
Html :: element ( 'li' , array (),
$this -> msg ( $key , $this -> getFallbackMessage ( $key ) )
)
);
2011-05-25 17:03:15 +00:00
}
2013-03-03 14:27:47 +00:00
$s .= '</ul>' ;
2011-05-25 17:03:15 +00:00
}
2013-03-03 14:27:47 +00:00
return $s ;
2011-05-25 17:03:15 +00:00
}
2011-05-25 18:58:02 +00:00
/**
2013-03-03 14:27:47 +00:00
* @ return string
2011-05-25 18:58:02 +00:00
*/
2013-03-03 14:27:47 +00:00
protected function getTextContent () {
$key = 'databaseerror-textcl' ;
$s = $this -> msg ( $key , $this -> getFallbackMessage ( $key ) ) . " \n " ;
2011-05-25 17:03:15 +00:00
2013-03-03 14:27:47 +00:00
foreach ( $this -> getTechnicalDetails () as $key => $detail ) {
$s .= $this -> msg ( $key , $this -> getFallbackMessage ( $key ), $detail [ 2 ] ) . " \n " ;
2011-05-25 17:03:15 +00:00
}
2013-03-03 14:27:47 +00:00
return $s ;
2011-05-25 17:03:15 +00:00
}
2011-11-29 21:04:20 +00:00
/**
2013-03-03 14:27:47 +00:00
* Make a list of technical details that can be shown to the user . This information can
* aid in debugging yet may be useful to an attacker trying to exploit a security weakness
* in the software or server configuration .
*
* Thus no such details are shown by default , though if $wgShowHostnames is true , only the
* full SQL query is hidden ; in fact , the error message often does contain a hostname , and
* sites using this option probably don ' t care much about " security by obscurity " . Of course ,
* if $wgShowSQLErrors is true , the SQL query * is * shown .
*
* @ return array : Keys are message keys ; values are arrays of arguments for Html :: element () .
* Array will be empty if users are not allowed to see any of these details at all .
2011-11-29 21:04:20 +00:00
*/
2013-03-03 14:27:47 +00:00
protected function getTechnicalDetails () {
global $wgShowHostnames , $wgShowSQLErrors ;
$attribs = array ( 'dir' => 'ltr' );
$details = array ();
if ( $wgShowSQLErrors ) {
$details [ 'databaseerror-query' ] = array (
'div' , array ( 'class' => 'mw-code' ) + $attribs , $this -> sql );
}
if ( $wgShowHostnames || $wgShowSQLErrors ) {
$errorMessage = $this -> errno . ' ' . $this -> error ;
$details [ 'databaseerror-function' ] = array ( 'code' , $attribs , $this -> fname );
$details [ 'databaseerror-error' ] = array ( 'samp' , $attribs , $errorMessage );
}
return $details ;
2011-05-25 17:03:15 +00:00
}
2011-05-25 18:58:02 +00:00
/**
2013-03-03 14:27:47 +00:00
* @ param string $key Message key
* @ return string : English message text
2011-05-25 18:58:02 +00:00
*/
2013-03-03 14:27:47 +00:00
private function getFallbackMessage ( $key ) {
$messages = array (
'databaseerror-text' => ' A database query error has occurred .
This may indicate a bug in the software . ' ,
'databaseerror-textcl' => 'A database query error has occurred.' ,
'databaseerror-query' => 'Query: $1' ,
'databaseerror-function' => 'Function: $1' ,
'databaseerror-error' => 'Error: $1' ,
);
return $messages [ $key ];
2011-05-25 17:03:15 +00:00
}
2013-03-03 14:27:47 +00:00
2011-05-25 17:03:15 +00:00
}
/**
* @ ingroup Database
*/
class DBUnexpectedError extends DBError {}