Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / db / DatabaseError.php
blob55095c3829443a7f94f94abbc8134ed1bc76c967
1 <?php
2 /**
3 * This file contains database error classes.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Database
24 /**
25 * Database error base class
26 * @ingroup Database
28 class DBError extends MWException {
30 /**
31 * @var DatabaseBase
33 public $db;
35 /**
36 * Construct a database error
37 * @param $db DatabaseBase object which threw the error
38 * @param string $error A simple error message to be used for debugging
40 function __construct( DatabaseBase $db = null, $error ) {
41 $this->db = $db;
42 parent::__construct( $error );
45 /**
46 * @param $html string
47 * @return string
49 protected function getContentMessage( $html ) {
50 if ( $html ) {
51 return nl2br( htmlspecialchars( $this->getMessage() ) );
52 } else {
53 return $this->getMessage();
57 /**
58 * @return string
60 function getText() {
61 global $wgShowDBErrorBacktrace;
63 $s = $this->getContentMessage( false ) . "\n";
65 if ( $wgShowDBErrorBacktrace ) {
66 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
69 return $s;
72 /**
73 * @return string
75 function getHTML() {
76 global $wgShowDBErrorBacktrace;
78 $s = $this->getContentMessage( true );
80 if ( $wgShowDBErrorBacktrace ) {
81 $s .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
84 return $s;
88 /**
89 * @ingroup Database
91 class DBConnectionError extends DBError {
92 public $error;
94 function __construct( DatabaseBase $db = null, $error = 'unknown error' ) {
95 $msg = 'DB connection error';
97 if ( trim( $error ) != '' ) {
98 $msg .= ": $error";
101 $this->error = $error;
103 parent::__construct( $db, $msg );
107 * @return bool
109 function useOutputPage() {
110 // Not likely to work
111 return false;
115 * @param $key
116 * @param $fallback
117 * @return string
119 function msg( $key, $fallback /*[, params...] */ ) {
120 global $wgLang;
122 $args = array_slice( func_get_args(), 2 );
124 if ( $this->useMessageCache() ) {
125 $message = $wgLang->getMessage( $key );
126 } else {
127 $message = $fallback;
129 return wfMsgReplaceArgs( $message, $args );
133 * @return bool
135 function getLogMessage() {
136 # Don't send to the exception log
137 return false;
141 * @return string
143 function getPageTitle() {
144 global $wgSitename;
145 return htmlspecialchars( $this->msg( 'dberr-header', "$wgSitename has a problem" ) );
149 * @return string
151 function getHTML() {
152 global $wgShowDBErrorBacktrace;
154 $sorry = htmlspecialchars( $this->msg( 'dberr-problems', 'Sorry! This site is experiencing technical difficulties.' ) );
155 $again = htmlspecialchars( $this->msg( 'dberr-again', 'Try waiting a few minutes and reloading.' ) );
156 $info = htmlspecialchars( $this->msg( 'dberr-info', '(Can\'t contact the database server: $1)' ) );
158 # No database access
159 MessageCache::singleton()->disable();
161 if ( trim( $this->error ) == '' && $this->db ) {
162 $this->error = $this->db->getProperty( 'mServer' );
165 $this->error = Html::element( 'span', array( 'dir' => 'ltr' ), $this->error );
167 $noconnect = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
168 $text = str_replace( '$1', $this->error, $noconnect );
170 if ( $wgShowDBErrorBacktrace ) {
171 $text .= '<p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $this->getTraceAsString() ) );
174 $extra = $this->searchForm();
176 return "$text<hr />$extra";
179 public function reportHTML() {
180 global $wgUseFileCache;
182 # Check whether we can serve a file-cached copy of the page with the error underneath
183 if ( $wgUseFileCache ) {
184 try {
185 $cache = $this->fileCachedPage();
186 # Cached version on file system?
187 if ( $cache !== null ) {
188 # Hack: extend the body for error messages
189 $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
190 # Add cache notice...
191 $cache .= '<div style="color:red;font-size:150%;font-weight:bold;">' .
192 htmlspecialchars( $this->msg( 'dberr-cachederror',
193 'This is a cached copy of the requested page, and may not be up to date. ' ) ) .
194 '</div>';
196 # Output cached page with notices on bottom and re-close body
197 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
198 return;
200 } catch ( MWException $e ) {
201 // Do nothing, just use the default page
205 # We can't, cough and die in the usual fashion
206 parent::reportHTML();
210 * @return string
212 function searchForm() {
213 global $wgSitename, $wgCanonicalServer, $wgRequest;
215 $usegoogle = htmlspecialchars( $this->msg( 'dberr-usegoogle', 'You can try searching via Google in the meantime.' ) );
216 $outofdate = htmlspecialchars( $this->msg( 'dberr-outofdate', 'Note that their indexes of our content may be out of date.' ) );
217 $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
219 $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
221 $server = htmlspecialchars( $wgCanonicalServer );
222 $sitename = htmlspecialchars( $wgSitename );
224 $trygoogle = <<<EOT
225 <div style="margin: 1.5em">$usegoogle<br />
226 <small>$outofdate</small></div>
227 <!-- SiteSearch Google -->
228 <form method="get" action="//www.google.com/search" id="googlesearch">
229 <input type="hidden" name="domains" value="$server" />
230 <input type="hidden" name="num" value="50" />
231 <input type="hidden" name="ie" value="UTF-8" />
232 <input type="hidden" name="oe" value="UTF-8" />
234 <input type="text" name="q" size="31" maxlength="255" value="$search" />
235 <input type="submit" name="btnG" value="$googlesearch" />
236 <div>
237 <input type="radio" name="sitesearch" id="gwiki" value="$server" checked="checked" /><label for="gwiki">$sitename</label>
238 <input type="radio" name="sitesearch" id="gWWW" value="" /><label for="gWWW">WWW</label>
239 </div>
240 </form>
241 <!-- SiteSearch Google -->
242 EOT;
243 return $trygoogle;
247 * @return string
249 private function fileCachedPage() {
250 global $wgTitle, $wgOut, $wgRequest;
252 if ( $wgOut->isDisabled() ) {
253 return ''; // Done already?
256 if ( $wgTitle ) { // use $wgTitle if we managed to set it
257 $t = $wgTitle->getPrefixedDBkey();
258 } else {
259 # Fallback to the raw title URL param. We can't use the Title
260 # class is it may hit the interwiki table and give a DB error.
261 # We may get a cache miss due to not sanitizing the title though.
262 $t = str_replace( ' ', '_', $wgRequest->getVal( 'title' ) );
263 if ( $t == '' ) { // fallback to main page
264 $t = Title::newFromText(
265 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
269 $cache = HTMLFileCache::newFromTitle( $t, 'view' );
270 if ( $cache->isCached() ) {
271 return $cache->fetchText();
272 } else {
273 return '';
279 * @ingroup Database
281 class DBQueryError extends DBError {
282 public $error, $errno, $sql, $fname;
285 * @param $db DatabaseBase
286 * @param $error string
287 * @param $errno int|string
288 * @param $sql string
289 * @param $fname string
291 function __construct( DatabaseBase $db, $error, $errno, $sql, $fname ) {
292 $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" .
293 "Query: $sql\n" .
294 "Function: $fname\n" .
295 "Error: $errno $error\n";
296 parent::__construct( $db, $message );
298 $this->error = $error;
299 $this->errno = $errno;
300 $this->sql = $sql;
301 $this->fname = $fname;
305 * @param $html string
306 * @return string
308 function getContentMessage( $html ) {
309 if ( $this->useMessageCache() ) {
310 if ( $html ) {
311 $msg = 'dberrortext';
312 $sql = htmlspecialchars( $this->getSQL() );
313 $fname = htmlspecialchars( $this->fname );
314 $error = htmlspecialchars( $this->error );
315 } else {
316 $msg = 'dberrortextcl';
317 $sql = $this->getSQL();
318 $fname = $this->fname;
319 $error = $this->error;
321 return wfMessage( $msg )->rawParams( $sql, $fname, $this->errno, $error )->text();
322 } else {
323 return parent::getContentMessage( $html );
328 * @return String
330 function getSQL() {
331 global $wgShowSQLErrors;
333 if ( !$wgShowSQLErrors ) {
334 return $this->msg( 'sqlhidden', 'SQL hidden' );
335 } else {
336 return $this->sql;
341 * @return bool
343 function getLogMessage() {
344 # Don't send to the exception log
345 return false;
349 * @return String
351 function getPageTitle() {
352 return $this->msg( 'databaseerror', 'Database error' );
357 * @ingroup Database
359 class DBUnexpectedError extends DBError {}