MediaWiki REL1_27
DatabaseError.php
Go to the documentation of this file.
1<?php
28class DBError extends MWException {
30 public $db;
31
37 function __construct( DatabaseBase $db = null, $error ) {
38 $this->db = $db;
39 parent::__construct( $error );
40 }
41}
42
50class DBExpectedError extends DBError {
54 function getText() {
56
57 $s = $this->getTextContent() . "\n";
58
59 if ( $wgShowDBErrorBacktrace ) {
60 $s .= "Backtrace:\n" . $this->getTraceAsString() . "\n";
61 }
62
63 return $s;
64 }
65
69 function getHTML() {
71
72 $s = $this->getHTMLContent();
73
74 if ( $wgShowDBErrorBacktrace ) {
75 $s .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
76 }
77
78 return $s;
79 }
80
81 function getPageTitle() {
82 return $this->msg( 'databaseerror', 'Database error' );
83 }
84
88 protected function getTextContent() {
89 return $this->getMessage();
90 }
91
95 protected function getHTMLContent() {
96 return '<p>' . nl2br( htmlspecialchars( $this->getTextContent() ) ) . '</p>';
97 }
98}
99
105 public $error;
106
111 function __construct( DatabaseBase $db = null, $error = 'unknown error' ) {
112 $msg = 'DB connection error';
113
114 if ( trim( $error ) != '' ) {
115 $msg .= ": $error";
116 } elseif ( $db ) {
117 $error = $this->db->getServer();
118 }
119
120 parent::__construct( $db, $msg );
121 $this->error = $error;
122 }
123
127 function useOutputPage() {
128 // Not likely to work
129 return false;
130 }
131
139 function msg( $key, $fallback /*[, params...] */ ) {
140 $args = array_slice( func_get_args(), 2 );
141
142 if ( $this->useMessageCache() ) {
143 return wfMessage( $key, $args )->useDatabase( false )->text();
144 } else {
146 }
147 }
148
152 function isLoggable() {
153 // Don't send to the exception log, already in dberror log
154 return false;
155 }
156
160 function getHTML() {
162
163 $sorry = htmlspecialchars( $this->msg(
164 'dberr-problems',
165 'Sorry! This site is experiencing technical difficulties.'
166 ) );
167 $again = htmlspecialchars( $this->msg(
168 'dberr-again',
169 'Try waiting a few minutes and reloading.'
170 ) );
171
172 if ( $wgShowHostnames || $wgShowSQLErrors ) {
173 $info = str_replace(
174 '$1', Html::element( 'span', [ 'dir' => 'ltr' ], $this->error ),
175 htmlspecialchars( $this->msg( 'dberr-info', '(Cannot access the database: $1)' ) )
176 );
177 } else {
178 $info = htmlspecialchars( $this->msg(
179 'dberr-info-hidden',
180 '(Cannot access the database)'
181 ) );
182 }
183
184 # No database access
185 MessageCache::singleton()->disable();
186
187 $html = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
188
189 if ( $wgShowDBErrorBacktrace ) {
190 $html .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
191 }
192
193 $html .= '<hr />';
194
195 return $html;
196 }
197
198 protected function getTextContent() {
200
202 return $this->getMessage();
203 } else {
204 return 'DB connection error';
205 }
206 }
207
213 public function reportHTML() {
215
216 // Check whether we can serve a file-cached copy of the page with the error underneath
217 if ( $wgUseFileCache ) {
218 try {
219 $cache = $this->fileCachedPage();
220 // Cached version on file system?
221 if ( $cache !== null ) {
222 // Hack: extend the body for error messages
223 $cache = str_replace( [ '</html>', '</body>' ], '', $cache );
224 // Add cache notice...
225 $cache .= '<div style="border:1px solid #ffd0d0;padding:1em;">' .
226 htmlspecialchars( $this->msg( 'dberr-cachederror',
227 'This is a cached copy of the requested page, and may not be up to date.' ) ) .
228 '</div>';
229
230 // Output cached page with notices on bottom and re-close body
231 echo "{$cache}<hr />{$this->getHTML()}</body></html>";
232
233 return;
234 }
235 } catch ( Exception $e ) {
236 // Do nothing, just use the default page
237 }
238 }
239
240 // We can't, cough and die in the usual fashion
241 parent::reportHTML();
242 }
243
247 private function fileCachedPage() {
249
250 if ( $context->getOutput()->isDisabled() ) {
251 // Done already?
252 return '';
253 }
254
255 if ( $context->getTitle() ) {
256 // Use the main context's title if we managed to set it
257 $t = $context->getTitle()->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( ' ', '_', $context->getRequest()->getVal( 'title' ) );
263 if ( $t == '' ) { // fallback to main page
265 $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
266 }
267 }
268
269 $cache = new HTMLFileCache( $t, 'view' );
270 if ( $cache->isCached() ) {
271 return $cache->fetchText();
272 } else {
273 return '';
274 }
275 }
276}
277
283
292 if ( $db->wasConnectionError( $errno ) ) {
293 $message = "A connection error occured. \n" .
294 "Query: $sql\n" .
295 "Function: $fname\n" .
296 "Error: $errno $error\n";
297 } else {
298 $message = "A database error has occurred. Did you forget to run " .
299 "maintenance/update.php after upgrading? See: " .
300 "https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
301 "Query: $sql\n" .
302 "Function: $fname\n" .
303 "Error: $errno $error\n";
304 }
305 parent::__construct( $db, $message );
306
307 $this->error = $error;
308 $this->errno = $errno;
309 $this->sql = $sql;
310 $this->fname = $fname;
311 }
312
316 function getPageTitle() {
317 return $this->msg( 'databaseerror', 'Database error' );
318 }
319
323 protected function getHTMLContent() {
324 $key = 'databaseerror-text';
325 $s = Html::element( 'p', [], $this->msg( $key, $this->getFallbackMessage( $key ) ) );
326
327 $details = $this->getTechnicalDetails();
328 if ( $details ) {
329 $s .= '<ul>';
330 foreach ( $details as $key => $detail ) {
331 $s .= str_replace(
332 '$1', call_user_func_array( 'Html::element', $detail ),
333 Html::element( 'li', [],
334 $this->msg( $key, $this->getFallbackMessage( $key ) )
335 )
336 );
337 }
338 $s .= '</ul>';
339 }
340
341 return $s;
342 }
343
347 protected function getTextContent() {
348 $key = 'databaseerror-textcl';
349 $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
350
351 foreach ( $this->getTechnicalDetails() as $key => $detail ) {
352 $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
353 }
354
355 return $s;
356 }
357
371 protected function getTechnicalDetails() {
373
374 $attribs = [ 'dir' => 'ltr' ];
375 $details = [];
376
377 if ( $wgShowSQLErrors ) {
378 $details['databaseerror-query'] = [
379 'div', [ 'class' => 'mw-code' ] + $attribs, $this->sql ];
380 }
381
383 $errorMessage = $this->errno . ' ' . $this->error;
384 $details['databaseerror-function'] = [ 'code', $attribs, $this->fname ];
385 $details['databaseerror-error'] = [ 'samp', $attribs, $errorMessage ];
386 }
387
388 return $details;
389 }
390
395 private function getFallbackMessage( $key ) {
396 $messages = [
397 'databaseerror-text' => 'A database query error has occurred.
398This may indicate a bug in the software.',
399 'databaseerror-textcl' => 'A database query error has occurred.',
400 'databaseerror-query' => 'Query: $1',
401 'databaseerror-function' => 'Function: $1',
402 'databaseerror-error' => 'Error: $1',
403 ];
404
405 return $messages[$key];
406 }
407}
408
413}
414
419 function getPageTitle() {
420 return $this->msg( 'readonly', 'Database is locked' );
421 }
422}
423
$wgShowHostnames
Expose backend server host names through the API and various HTML comments.
$wgShowDBErrorBacktrace
If true, show a backtrace for database errors.
$wgUseFileCache
This will cache static pages for non-logged-in users to reduce database traffic on public sites.
$wgShowSQLErrors
Whether to show "we're sorry, but there has been a database error" pages.
wfMsgReplaceArgs( $message, $args)
Replace message parameter keys on the given formatted output.
$messages
$fallback
if( $line===false) $args
Definition cdb.php:64
msg( $key, $fallback)
__construct(DatabaseBase $db=null, $error='unknown error')
reportHTML()
Output the exception report using HTML.
string $error
Error text.
Database error base class.
__construct(DatabaseBase $db=null, $error)
Construct a database error.
DatabaseBase $db
Base class for the more common types of database errors.
getPageTitle()
Return the title of the page when reporting this error in a HTTP response.
getFallbackMessage( $key)
__construct(DatabaseBase $db, $error, $errno, $sql, $fname)
getTechnicalDetails()
Make a list of technical details that can be shown to the user.
getPageTitle()
Return the title of the page when reporting this error in a HTTP response.
Database abstraction object.
Definition Database.php:32
wasConnectionError( $errno)
Determines if the given query error was a connection drop STUB.
Page view caching in the file system.
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition Html.php:230
MediaWiki exception.
useMessageCache()
Can the extension use the Message class/wfMessage to get i18n-ed messages?
msg( $key, $fallback)
Get a message from i18n.
static singleton()
Get the signleton instance of this class.
static getMain()
Static methods.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition Title.php:277
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:1819
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:1818
returning false will NOT prevent logging $e
Definition hooks.txt:1940
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$context
Definition load.php:44
$cache
Definition mcc.php:33
SQLite shares the MySQL schema file at maintenance tables sql
Definition sqlite.txt:1