[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/db/ -> DatabaseError.php (source)

   1  <?php
   2  /**
   3   * This file contains database error classes.
   4   *
   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.
   9   *
  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.
  14   *
  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
  19   *
  20   * @file
  21   * @ingroup Database
  22   */
  23  
  24  /**
  25   * Database error base class
  26   * @ingroup Database
  27   */
  28  class DBError extends MWException {
  29      /** @var DatabaseBase */
  30      public $db;
  31  
  32      /**
  33       * Construct a database error
  34       * @param DatabaseBase $db Object which threw the error
  35       * @param string $error A simple error message to be used for debugging
  36       */
  37  	function __construct( DatabaseBase $db = null, $error ) {
  38          $this->db = $db;
  39          parent::__construct( $error );
  40      }
  41  }
  42  
  43  /**
  44   * Base class for the more common types of database errors. These are known to occur
  45   * frequently, so we try to give friendly error messages for them.
  46   *
  47   * @ingroup Database
  48   * @since 1.23
  49   */
  50  class DBExpectedError extends DBError {
  51      /**
  52       * @return string
  53       */
  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  
  66      /**
  67       * @return string
  68       */
  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  
  81      /**
  82       * @return string
  83       */
  84  	protected function getTextContent() {
  85          return $this->getMessage();
  86      }
  87  
  88      /**
  89       * @return string
  90       */
  91  	protected function getHTMLContent() {
  92          return '<p>' . nl2br( htmlspecialchars( $this->getTextContent() ) ) . '</p>';
  93      }
  94  }
  95  
  96  /**
  97   * @ingroup Database
  98   */
  99  class DBConnectionError extends DBExpectedError {
 100      /** @var string Error text */
 101      public $error;
 102  
 103      /**
 104       * @param DatabaseBase $db Object throwing the error
 105       * @param string $error Error text
 106       */
 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  
 120      /**
 121       * @return bool
 122       */
 123  	function useOutputPage() {
 124          // Not likely to work
 125          return false;
 126      }
 127  
 128      /**
 129       * @param string $key
 130       * @param string $fallback Unescaped alternative error text in case the
 131       *   message cache cannot be used. Can contain parameters as in regular
 132       *   messages, that should be passed as additional parameters.
 133       * @return string Unprocessed plain error text with parameters replaced
 134       */
 135  	function msg( $key, $fallback /*[, params...] */ ) {
 136          $args = array_slice( func_get_args(), 2 );
 137  
 138          if ( $this->useMessageCache() ) {
 139              return wfMessage( $key, $args )->useDatabase( false )->text();
 140          } else {
 141              return wfMsgReplaceArgs( $fallback, $args );
 142          }
 143      }
 144  
 145      /**
 146       * @return bool
 147       */
 148  	function isLoggable() {
 149          // Don't send to the exception log, already in dberror log
 150          return false;
 151      }
 152  
 153      /**
 154       * @return string Safe HTML
 155       */
 156  	function getHTML() {
 157          global $wgShowDBErrorBacktrace, $wgShowHostnames, $wgShowSQLErrors;
 158  
 159          $sorry = htmlspecialchars( $this->msg(
 160              'dberr-problems',
 161              'Sorry! This site is experiencing technical difficulties.'
 162          ) );
 163          $again = htmlspecialchars( $this->msg(
 164              'dberr-again',
 165              'Try waiting a few minutes and reloading.'
 166          ) );
 167  
 168          if ( $wgShowHostnames || $wgShowSQLErrors ) {
 169              $info = str_replace(
 170                  '$1', Html::element( 'span', array( 'dir' => 'ltr' ), $this->error ),
 171                  htmlspecialchars( $this->msg( 'dberr-info', '(Cannot contact the database server: $1)' ) )
 172              );
 173          } else {
 174              $info = htmlspecialchars( $this->msg(
 175                  'dberr-info-hidden',
 176                  '(Cannot contact the database server)'
 177              ) );
 178          }
 179  
 180          # No database access
 181          MessageCache::singleton()->disable();
 182  
 183          $html = "<h1>$sorry</h1><p>$again</p><p><small>$info</small></p>";
 184  
 185          if ( $wgShowDBErrorBacktrace ) {
 186              $html .= '<p>Backtrace:</p><pre>' . htmlspecialchars( $this->getTraceAsString() ) . '</pre>';
 187          }
 188  
 189          $html .= '<hr />';
 190          $html .= $this->searchForm();
 191  
 192          return $html;
 193      }
 194  
 195  	protected function getTextContent() {
 196          global $wgShowHostnames, $wgShowSQLErrors;
 197  
 198          if ( $wgShowHostnames || $wgShowSQLErrors ) {
 199              return $this->getMessage();
 200          } else {
 201              return 'DB connection error';
 202          }
 203      }
 204  
 205      /**
 206       * Output the exception report using HTML.
 207       *
 208       * @return void
 209       */
 210  	public function reportHTML() {
 211          global $wgUseFileCache;
 212  
 213          // Check whether we can serve a file-cached copy of the page with the error underneath
 214          if ( $wgUseFileCache ) {
 215              try {
 216                  $cache = $this->fileCachedPage();
 217                  // Cached version on file system?
 218                  if ( $cache !== null ) {
 219                      // Hack: extend the body for error messages
 220                      $cache = str_replace( array( '</html>', '</body>' ), '', $cache );
 221                      // Add cache notice...
 222                      $cache .= '<div style="border:1px solid #ffd0d0;padding:1em;">' .
 223                          htmlspecialchars( $this->msg( 'dberr-cachederror',
 224                              'This is a cached copy of the requested page, and may not be up to date.' ) ) .
 225                          '</div>';
 226  
 227                      // Output cached page with notices on bottom and re-close body
 228                      echo "{$cache}<hr />{$this->getHTML()}</body></html>";
 229  
 230                      return;
 231                  }
 232              } catch ( MWException $e ) {
 233                  // Do nothing, just use the default page
 234              }
 235          }
 236  
 237          // We can't, cough and die in the usual fashion
 238          parent::reportHTML();
 239      }
 240  
 241      /**
 242       * @return string
 243       */
 244  	function searchForm() {
 245          global $wgSitename, $wgCanonicalServer, $wgRequest;
 246  
 247          $usegoogle = htmlspecialchars( $this->msg(
 248              'dberr-usegoogle',
 249              'You can try searching via Google in the meantime.'
 250          ) );
 251          $outofdate = htmlspecialchars( $this->msg(
 252              'dberr-outofdate',
 253              'Note that their indexes of our content may be out of date.'
 254          ) );
 255          $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
 256  
 257          $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
 258  
 259          $server = htmlspecialchars( $wgCanonicalServer );
 260          $sitename = htmlspecialchars( $wgSitename );
 261  
 262          $trygoogle = <<<EOT
 263  <div style="margin: 1.5em">$usegoogle<br />
 264  <small>$outofdate</small>
 265  </div>
 266  <form method="get" action="//www.google.com/search" id="googlesearch">
 267      <input type="hidden" name="domains" value="$server" />
 268      <input type="hidden" name="num" value="50" />
 269      <input type="hidden" name="ie" value="UTF-8" />
 270      <input type="hidden" name="oe" value="UTF-8" />
 271  
 272      <input type="text" name="q" size="31" maxlength="255" value="$search" />
 273      <input type="submit" name="btnG" value="$googlesearch" />
 274      <p>
 275          <label><input type="radio" name="sitesearch" value="$server" checked="checked" />$sitename</label>
 276          <label><input type="radio" name="sitesearch" value="" />WWW</label>
 277      </p>
 278  </form>
 279  EOT;
 280  
 281          return $trygoogle;
 282      }
 283  
 284      /**
 285       * @return string
 286       */
 287  	private function fileCachedPage() {
 288          $context = RequestContext::getMain();
 289  
 290          if ( $context->getOutput()->isDisabled() ) {
 291              // Done already?
 292              return '';
 293          }
 294  
 295          if ( $context->getTitle() ) {
 296              // Use the main context's title if we managed to set it
 297              $t = $context->getTitle()->getPrefixedDBkey();
 298          } else {
 299              // Fallback to the raw title URL param. We can't use the Title
 300              // class is it may hit the interwiki table and give a DB error.
 301              // We may get a cache miss due to not sanitizing the title though.
 302              $t = str_replace( ' ', '_', $context->getRequest()->getVal( 'title' ) );
 303              if ( $t == '' ) { // fallback to main page
 304                  $t = Title::newFromText(
 305                      $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
 306              }
 307          }
 308  
 309          $cache = new HTMLFileCache( $t, 'view' );
 310          if ( $cache->isCached() ) {
 311              return $cache->fetchText();
 312          } else {
 313              return '';
 314          }
 315      }
 316  }
 317  
 318  /**
 319   * @ingroup Database
 320   */
 321  class DBQueryError extends DBExpectedError {
 322      public $error, $errno, $sql, $fname;
 323  
 324      /**
 325       * @param DatabaseBase $db
 326       * @param string $error
 327       * @param int|string $errno
 328       * @param string $sql
 329       * @param string $fname
 330       */
 331  	function __construct( DatabaseBase $db, $error, $errno, $sql, $fname ) {
 332          $message = "A database error has occurred. Did you forget to run " .
 333              "maintenance/update.php after upgrading?  See: " .
 334              "https://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
 335              "Query: $sql\n" .
 336              "Function: $fname\n" .
 337              "Error: $errno $error\n";
 338          parent::__construct( $db, $message );
 339  
 340          $this->error = $error;
 341          $this->errno = $errno;
 342          $this->sql = $sql;
 343          $this->fname = $fname;
 344      }
 345  
 346      /**
 347       * @return string
 348       */
 349  	function getPageTitle() {
 350          return $this->msg( 'databaseerror', 'Database error' );
 351      }
 352  
 353      /**
 354       * @return string
 355       */
 356  	protected function getHTMLContent() {
 357          $key = 'databaseerror-text';
 358          $s = Html::element( 'p', array(), $this->msg( $key, $this->getFallbackMessage( $key ) ) );
 359  
 360          $details = $this->getTechnicalDetails();
 361          if ( $details ) {
 362              $s .= '<ul>';
 363              foreach ( $details as $key => $detail ) {
 364                  $s .= str_replace(
 365                      '$1', call_user_func_array( 'Html::element', $detail ),
 366                      Html::element( 'li', array(),
 367                          $this->msg( $key, $this->getFallbackMessage( $key ) )
 368                      )
 369                  );
 370              }
 371              $s .= '</ul>';
 372          }
 373  
 374          return $s;
 375      }
 376  
 377      /**
 378       * @return string
 379       */
 380  	protected function getTextContent() {
 381          $key = 'databaseerror-textcl';
 382          $s = $this->msg( $key, $this->getFallbackMessage( $key ) ) . "\n";
 383  
 384          foreach ( $this->getTechnicalDetails() as $key => $detail ) {
 385              $s .= $this->msg( $key, $this->getFallbackMessage( $key ), $detail[2] ) . "\n";
 386          }
 387  
 388          return $s;
 389      }
 390  
 391      /**
 392       * Make a list of technical details that can be shown to the user. This information can
 393       * aid in debugging yet may be useful to an attacker trying to exploit a security weakness
 394       * in the software or server configuration.
 395       *
 396       * Thus no such details are shown by default, though if $wgShowHostnames is true, only the
 397       * full SQL query is hidden; in fact, the error message often does contain a hostname, and
 398       * sites using this option probably don't care much about "security by obscurity". Of course,
 399       * if $wgShowSQLErrors is true, the SQL query *is* shown.
 400       *
 401       * @return array Keys are message keys; values are arrays of arguments for Html::element().
 402       *   Array will be empty if users are not allowed to see any of these details at all.
 403       */
 404  	protected function getTechnicalDetails() {
 405          global $wgShowHostnames, $wgShowSQLErrors;
 406  
 407          $attribs = array( 'dir' => 'ltr' );
 408          $details = array();
 409  
 410          if ( $wgShowSQLErrors ) {
 411              $details['databaseerror-query'] = array(
 412                  'div', array( 'class' => 'mw-code' ) + $attribs, $this->sql );
 413          }
 414  
 415          if ( $wgShowHostnames || $wgShowSQLErrors ) {
 416              $errorMessage = $this->errno . ' ' . $this->error;
 417              $details['databaseerror-function'] = array( 'code', $attribs, $this->fname );
 418              $details['databaseerror-error'] = array( 'samp', $attribs, $errorMessage );
 419          }
 420  
 421          return $details;
 422      }
 423  
 424      /**
 425       * @param string $key Message key
 426       * @return string English message text
 427       */
 428  	private function getFallbackMessage( $key ) {
 429          $messages = array(
 430              'databaseerror-text' => 'A database query error has occurred.
 431  This may indicate a bug in the software.',
 432              'databaseerror-textcl' => 'A database query error has occurred.',
 433              'databaseerror-query' => 'Query: $1',
 434              'databaseerror-function' => 'Function: $1',
 435              'databaseerror-error' => 'Error: $1',
 436          );
 437  
 438          return $messages[$key];
 439      }
 440  }
 441  
 442  /**
 443   * @ingroup Database
 444   */
 445  class DBUnexpectedError extends DBError {
 446  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1