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 );
88 function useOutputPage() {
98 function msg( $key, $fallback /*[, params...] */ ) {
101 $args = array_slice( func_get_args(), 2 );
103 if ( $this->useMessageCache() ) {
104 $message = $wgLang->getMessage( $key );
106 $message = $fallback;
108 return wfMsgReplaceArgs( $message, $args );
114 function getLogMessage() {
115 # Don't send to the exception log
122 function getPageTitle() {
124 return htmlspecialchars( $this->msg( 'dberr-header', "$wgSitename has a problem" ) );
131 global $wgShowDBErrorBacktrace;
133 $sorry = htmlspecialchars( $this->msg( 'dberr-problems', 'Sorry! This site is experiencing technical difficulties.' ) );
134 $again = htmlspecialchars( $this->msg( 'dberr-again', 'Try waiting a few minutes and reloading.' ) );
135 $info = htmlspecialchars( $this->msg( 'dberr-info', '(Can\'t contact the database server: $1)' ) );
138 MessageCache
::singleton()->disable();
140 if ( trim( $this->error
) == '' ) {
141 $this->error
= $this->db
->getProperty( 'mServer' );
144 $this->error
= Html
::element( 'span', array( 'dir' => 'ltr' ), $this->error
);
146 $noconnect = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
147 $text = str_replace( '$1', $this->error
, $noconnect );
149 if ( $wgShowDBErrorBacktrace ) {
150 $text .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
153 $extra = $this->searchForm();
155 return "$text<hr />$extra";
158 public function reportHTML(){
159 global $wgUseFileCache;
161 # Check whether we can serve a file-cached copy of the page with the error underneath
162 if ( $wgUseFileCache ) {
164 $cache = $this->fileCachedPage();
165 # Cached version on file system?
166 if ( $cache !== null ) {
167 # Hack: extend the body for error messages
168 $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
169 # Add cache notice...
170 $cache .= '<div style="color:red;font-size:150%;font-weight:bold;">'.
171 htmlspecialchars( $this->msg( 'dberr-cachederror',
172 'This is a cached copy of the requested page, and may not be up to date. ' ) ) .
175 # Output cached page with notices on bottom and re-close body
176 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
179 } catch ( MWException
$e ) {
180 // Do nothing, just use the default page
184 # We can't, cough and die in the usual fashion
185 parent
::reportHTML();
191 function searchForm() {
192 global $wgSitename, $wgServer, $wgRequest;
194 $usegoogle = htmlspecialchars( $this->msg( 'dberr-usegoogle', 'You can try searching via Google in the meantime.' ) );
195 $outofdate = htmlspecialchars( $this->msg( 'dberr-outofdate', 'Note that their indexes of our content may be out of date.' ) );
196 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
198 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
200 $server = htmlspecialchars( $wgServer );
201 $sitename = htmlspecialchars( $wgSitename );
204 <div style="margin: 1.5em">$usegoogle<br />
205 <small>$outofdate</small></div>
206 <!-- SiteSearch Google -->
207 <form method="get" action="//www.google.com/search" id="googlesearch">
208 <input type="hidden" name="domains" value="$server" />
209 <input type="hidden" name="num" value="50" />
210 <input type="hidden" name="ie" value="UTF-8" />
211 <input type="hidden" name="oe" value="UTF-8" />
213 <input type="text" name="q" size="31" maxlength="255" value="$search" />
214 <input type="submit" name="btnG" value="$googlesearch" />
216 <input type="radio" name="sitesearch" id="gwiki" value="$server" checked="checked" /><label for="gwiki">$sitename</label>
217 <input type="radio" name="sitesearch" id="gWWW" value="" /><label for="gWWW">WWW</label>
220 <!-- SiteSearch Google -->
228 private function fileCachedPage() {
229 global $wgTitle, $wgOut, $wgRequest;
231 if ( $wgOut->isDisabled() ) {
232 return ''; // Done already?
235 if ( $wgTitle ) { // use $wgTitle if we managed to set it
236 $t = $wgTitle->getPrefixedDBkey();
238 # Fallback to the raw title URL param. We can't use the Title
239 # class is it may hit the interwiki table and give a DB error.
240 # We may get a cache miss due to not sanitizing the title though.
241 $t = str_replace( ' ', '_', $wgRequest->getVal( 'title' ) );
242 if ( $t == '' ) { // fallback to main page
243 $t = Title
::newFromText(
244 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
248 $cache = HTMLFileCache
::newFromTitle( $t, 'view' );
249 if ( $cache->isCached() ) {
250 return $cache->fetchText();
260 class DBQueryError
extends DBError
{
261 public $error, $errno, $sql, $fname;
264 * @param $db DatabaseBase
265 * @param $error string
266 * @param $errno int|string
268 * @param $fname string
270 function __construct( DatabaseBase
&$db, $error, $errno, $sql, $fname ) {
271 $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" .
273 "Function: $fname\n" .
274 "Error: $errno $error\n";
275 parent
::__construct( $db, $message );
277 $this->error
= $error;
278 $this->errno
= $errno;
280 $this->fname
= $fname;
284 * @param $html string
287 function getContentMessage( $html ) {
288 if ( $this->useMessageCache() ) {
290 $msg = 'dberrortext';
291 $sql = htmlspecialchars( $this->getSQL() );
292 $fname = htmlspecialchars( $this->fname
);
293 $error = htmlspecialchars( $this->error
);
295 $msg = 'dberrortextcl';
296 $sql = $this->getSQL();
297 $fname = $this->fname
;
298 $error = $this->error
;
300 return wfMsg( $msg, $sql, $fname, $this->errno
, $error );
302 return parent
::getContentMessage( $html );
310 global $wgShowSQLErrors;
312 if ( !$wgShowSQLErrors ) {
313 return $this->msg( 'sqlhidden', 'SQL hidden' );
322 function getLogMessage() {
323 # Don't send to the exception log
330 function getPageTitle() {
331 return $this->msg( 'databaseerror', 'Database error' );
338 class DBUnexpectedError
extends DBError
{}