MediaWiki  1.23.2
DatabaseError.php
Go to the documentation of this file.
1 <?php
28 class 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 
50 class DBExpectedError extends DBError {
54  function getText() {
55  global $wgShowDBErrorBacktrace;
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() {
70  global $wgShowDBErrorBacktrace;
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 
84  protected function getTextContent() {
85  return $this->getMessage();
86  }
87 
91  protected function getHTMLContent() {
92  return '<p>' . nl2br( htmlspecialchars( $this->getTextContent() ) ) . '</p>';
93  }
94 }
95 
101  public $error;
102 
107  function __construct( DatabaseBase $db = null, $error = 'unknown error' ) {
108  $msg = 'DB connection error';
109 
110  if ( trim( $error ) != '' ) {
111  $msg .= ": $error";
112  } elseif ( $db ) {
113  $error = $this->db->getServer();
114  }
115 
116  parent::__construct( $db, $msg );
117  $this->error = $error;
118  }
119 
123  function useOutputPage() {
124  // Not likely to work
125  return false;
126  }
127 
135  function msg( $key, $fallback /*[, params...] */ ) {
136  global $wgLang;
137 
138  $args = array_slice( func_get_args(), 2 );
139 
140  if ( $this->useMessageCache() ) {
141  $message = $wgLang->getMessage( $key );
142  } else {
143  $message = $fallback;
144  }
145 
146  return wfMsgReplaceArgs( $message, $args );
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() {
161  global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
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', array( 'dir' => 'ltr' ), $this->error ),
175  htmlspecialchars( $this->msg( 'dberr-info', '(Cannot contact the database server: $1)' ) )
176  );
177  } else {
178  $info = htmlspecialchars( $this->msg(
179  'dberr-info-hidden',
180  '(Cannot contact the database server)'
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  $html .= $this->searchForm();
195 
196  return $html;
197  }
198 
199  protected function getTextContent() {
200  global $wgShowHostnames, $wgShowSQLErrors;
201 
202  if ( $wgShowHostnames || $wgShowSQLErrors ) {
203  return $this->getMessage();
204  } else {
205  return 'DB connection error';
206  }
207  }
208 
214  public function reportHTML() {
215  global $wgUseFileCache;
216 
217  // Check whether we can serve a file-cached copy of the page with the error underneath
218  if ( $wgUseFileCache ) {
219  try {
220  $cache = $this->fileCachedPage();
221  // Cached version on file system?
222  if ( $cache !== null ) {
223  // Hack: extend the body for error messages
224  $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
225  // Add cache notice...
226  $cache .= '<div style="border:1px solid #ffd0d0;padding:1em;">' .
227  htmlspecialchars( $this->msg( 'dberr-cachederror',
228  'This is a cached copy of the requested page, and may not be up to date.' ) ) .
229  '</div>';
230 
231  // Output cached page with notices on bottom and re-close body
232  echo "{$cache}<hr />{$this->getHTML()}</body></html>";
233 
234  return;
235  }
236  } catch ( MWException $e ) {
237  // Do nothing, just use the default page
238  }
239  }
240 
241  // We can't, cough and die in the usual fashion
242  parent::reportHTML();
243  }
244 
248  function searchForm() {
249  global $wgSitename, $wgCanonicalServer, $wgRequest;
250 
251  $usegoogle = htmlspecialchars( $this->msg(
252  'dberr-usegoogle',
253  'You can try searching via Google in the meantime.'
254  ) );
255  $outofdate = htmlspecialchars( $this->msg(
256  'dberr-outofdate',
257  'Note that their indexes of our content may be out of date.'
258  ) );
259  $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
260 
261  $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
262 
263  $server = htmlspecialchars( $wgCanonicalServer );
264  $sitename = htmlspecialchars( $wgSitename );
265 
266  $trygoogle = <<<EOT
267 <div style="margin: 1.5em">$usegoogle<br />
268 <small>$outofdate</small>
269 </div>
270 <form method="get" action="//www.google.com/search" id="googlesearch">
271  <input type="hidden" name="domains" value="$server" />
272  <input type="hidden" name="num" value="50" />
273  <input type="hidden" name="ie" value="UTF-8" />
274  <input type="hidden" name="oe" value="UTF-8" />
275 
276  <input type="text" name="q" size="31" maxlength="255" value="$search" />
277  <input type="submit" name="btnG" value="$googlesearch" />
278  <p>
279  <label><input type="radio" name="sitesearch" value="$server" checked="checked" />$sitename</label>
280  <label><input type="radio" name="sitesearch" value="" />WWW</label>
281  </p>
282 </form>
283 EOT;
284 
285  return $trygoogle;
286  }
287 
291  private function fileCachedPage() {
292  $context = RequestContext::getMain();
293 
294  if ( $context->getOutput()->isDisabled() ) {
295  // Done already?
296  return '';
297  }
298 
299  if ( $context->getTitle() ) {
300  // Use the main context's title if we managed to set it
301  $t = $context->getTitle()->getPrefixedDBkey();
302  } else {
303  // Fallback to the raw title URL param. We can't use the Title
304  // class is it may hit the interwiki table and give a DB error.
305  // We may get a cache miss due to not sanitizing the title though.
306  $t = str_replace( ' ', '_', $context->getRequest()->getVal( 'title' ) );
307  if ( $t == '' ) { // fallback to main page
309  $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
310  }
311  }
312 
314  if ( $cache->isCached() ) {
315  return $cache->fetchText();
316  } else {
317  return '';
318  }
319  }
320 }
321 
325 class DBQueryError extends DBExpectedError {
326  public $error, $errno, $sql, $fname;
327 
336  $message = "A database error has occurred. Did you forget to run " .
337  "maintenance/update.php after upgrading? See: " .
338  "https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
339  "Query: $sql\n" .
340  "Function: $fname\n" .
341  "Error: $errno $error\n";
342  parent::__construct( $db, $message );
343 
344  $this->error = $error;
345  $this->errno = $errno;
346  $this->sql = $sql;
347  $this->fname = $fname;
348  }
349 
353  function isLoggable() {
354  // Don't send to the exception log, already in dberror log
355  return false;
356  }
357 
361  function getPageTitle() {
362  return $this->msg( 'databaseerror', 'Database error' );
363  }
364 
368  protected function getHTMLContent() {
369  $key = 'databaseerror-text';
370  $s = Html::element( 'p', array(), $this->msg( $key, $this->getFallbackMessage( $key ) ) );
371 
372  $details = $this->getTechnicalDetails();
373  if ( $details ) {
374  $s .= '<ul>';
375  foreach ( $details as $key => $detail ) {
376  $s .= str_replace(
377  '$1', call_user_func_array( 'Html::element', $detail ),
378  Html::element( 'li', array(),
379  $this->msg( $key, $this->getFallbackMessage( $key ) )
380  )
381  );
382  }
383  $s .= '</ul>';
384  }
385 
386  return $s;
387  }
388 
392  protected function getTextContent() {
393  $key = 'databaseerror-textcl';
394  $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
395 
396  foreach ( $this->getTechnicalDetails() as $key => $detail ) {
397  $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
398  }
399 
400  return $s;
401  }
402 
416  protected function getTechnicalDetails() {
417  global $wgShowHostnames, $wgShowSQLErrors;
418 
419  $attribs = array( 'dir' => 'ltr' );
420  $details = array();
421 
422  if ( $wgShowSQLErrors ) {
423  $details['databaseerror-query'] = array(
424  'div', array( 'class' => 'mw-code' ) + $attribs, $this->sql );
425  }
426 
427  if ( $wgShowHostnames || $wgShowSQLErrors ) {
428  $errorMessage = $this->errno . ' ' . $this->error;
429  $details['databaseerror-function'] = array( 'code', $attribs, $this->fname );
430  $details['databaseerror-error'] = array( 'samp', $attribs, $errorMessage );
431  }
432 
433  return $details;
434  }
435 
440  private function getFallbackMessage( $key ) {
441  $messages = array(
442  'databaseerror-text' => 'A database query error has occurred.
443 This may indicate a bug in the software.',
444  'databaseerror-textcl' => 'A database query error has occurred.',
445  'databaseerror-query' => 'Query: $1',
446  'databaseerror-function' => 'Function: $1',
447  'databaseerror-error' => 'Error: $1',
448  );
449 
450  return $messages[$key];
451  }
452 }
453 
457 class DBUnexpectedError extends DBError {
458 }
DBQueryError\$sql
$sql
Definition: DatabaseError.php:307
DBConnectionError\__construct
__construct(DatabaseBase $db=null, $error='unknown error')
Definition: DatabaseError.php:105
DBExpectedError\getText
getText()
Definition: DatabaseError.php:53
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$html
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:1530
DBQueryError\getTextContent
getTextContent()
Definition: DatabaseError.php:373
$fallback
$fallback
Definition: MessagesAb.php:12
DBQueryError\getHTMLContent
getHTMLContent()
Definition: DatabaseError.php:349
DBConnectionError\$error
string $error
Error text *.
Definition: DatabaseError.php:99
wfMsgReplaceArgs
wfMsgReplaceArgs( $message, $args)
Replace message parameter keys on the given formatted output.
Definition: GlobalFunctions.php:1590
DBConnectionError\msg
msg( $key, $fallback)
Definition: DatabaseError.php:133
$s
$s
Definition: mergeMessageFileList.php:156
DBExpectedError
Base class for the more common types of database errors.
Definition: DatabaseError.php:49
$messages
$messages
Definition: LogTests.i18n.php:8
DBQueryError\getTechnicalDetails
getTechnicalDetails()
Make a list of technical details that can be shown to the user.
Definition: DatabaseError.php:397
DBConnectionError\isLoggable
isLoggable()
Definition: DatabaseError.php:150
MWException\useMessageCache
useMessageCache()
Can the extension use the Message class/wfMessage to get i18n-ed messages?
Definition: MWException.php:54
DBError\__construct
__construct(DatabaseBase $db=null, $error)
Construct a database error.
Definition: DatabaseError.php:36
MWException
MediaWiki exception.
Definition: MWException.php:26
DBQueryError
Definition: DatabaseError.php:306
sql
SQLite shares the MySQL schema file at maintenance tables sql
Definition: sqlite.txt:1
DBQueryError\getPageTitle
getPageTitle()
Definition: DatabaseError.php:342
Html\element
static element( $element, $attribs=array(), $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:148
DBConnectionError\getTextContent
getTextContent()
Definition: DatabaseError.php:197
DBQueryError\isLoggable
isLoggable()
Definition: DatabaseError.php:334
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
form
null means default in associative array form
Definition: hooks.txt:1530
DBConnectionError
Definition: DatabaseError.php:98
DBConnectionError\getHTML
getHTML()
Definition: DatabaseError.php:158
DBUnexpectedError
Definition: DatabaseError.php:438
MessageCache\singleton
static singleton()
Get the signleton instance of this class.
Definition: MessageCache.php:101
DBError
Database error base class.
Definition: DatabaseError.php:28
DBQueryError\$error
$error
Definition: DatabaseError.php:307
action
action
Definition: parserTests.txt:378
DBQueryError\__construct
__construct(DatabaseBase $db, $error, $errno, $sql, $fname)
Definition: DatabaseError.php:316
DatabaseBase
Database abstraction object.
Definition: Database.php:219
MWException\msg
msg( $key, $fallback)
Get a message from i18n.
Definition: MWException.php:116
HTMLFileCache\newFromTitle
static newFromTitle( $title, $action)
Construct an ObjectFileCache from a Title and an action.
Definition: HTMLFileCache.php:39
DBQueryError\getFallbackMessage
getFallbackMessage( $key)
Definition: DatabaseError.php:421
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:420
DBConnectionError\searchForm
searchForm()
Definition: DatabaseError.php:246
DBConnectionError\fileCachedPage
fileCachedPage()
Definition: DatabaseError.php:289
$args
if( $line===false) $args
Definition: cdb.php:62
$wgLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition: design.txt:56
type
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
Definition: postgres.txt:22
$cache
$cache
Definition: mcc.php:32
DBQueryError\$fname
$fname
Definition: DatabaseError.php:307
DBConnectionError\useOutputPage
useOutputPage()
Definition: DatabaseError.php:121
DBError\$db
DatabaseBase $db
Definition: DatabaseError.php:29
as
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
Definition: distributors.txt:9
DBExpectedError\getHTMLContent
getHTMLContent()
Definition: DatabaseError.php:90
name
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at name
Definition: design.txt:12
$t
$t
Definition: testCompression.php:65
$error
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string as detected by MediaWiki Handlers will typically only apply for specific mime types object & $error
Definition: hooks.txt:2573
$e
if( $useReadline) $e
Definition: eval.php:66
DBExpectedError\getHTML
getHTML()
Definition: DatabaseError.php:68
DBConnectionError\reportHTML
reportHTML()
Output the exception report using HTML.
Definition: DatabaseError.php:212
$attribs
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:1530
DBExpectedError\getTextContent
getTextContent()
Definition: DatabaseError.php:83
DBQueryError\$errno
$errno
Definition: DatabaseError.php:307