Merge "Added result properties to action=paraminfo"
[lhc/web/wiklou.git] / includes / db / DatabaseError.php
index 430b2ca..53341c3 100644 (file)
@@ -1,25 +1,51 @@
 <?php
-/******************************************************************************
- * Error classes
- *****************************************************************************/
+/**
+ * This file contains database error classes.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Database
+ */
 
 /**
  * Database error base class
  * @ingroup Database
  */
 class DBError extends MWException {
+
+       /**
+        * @var DatabaseBase
+        */
        public $db;
 
        /**
         * Construct a database error
-        * @param $db Database object which threw the error
+        * @param $db DatabaseBase object which threw the error
         * @param $error String A simple error message to be used for debugging
         */
        function __construct( DatabaseBase &$db, $error ) {
-               $this->db =& $db;
+               $this->db = $db;
                parent::__construct( $error );
        }
 
+       /**
+        * @param $html string
+        * @return string
+        */
        protected function getContentMessage( $html ) {
                if ( $html ) {
                        return nl2br( htmlspecialchars( $this->getMessage() ) );
@@ -28,6 +54,9 @@ class DBError extends MWException {
                }
        }
 
+       /**
+        * @return string
+        */
        function getText() {
                global $wgShowDBErrorBacktrace;
 
@@ -40,6 +69,9 @@ class DBError extends MWException {
                return $s;
        }
 
+       /**
+        * @return string
+        */
        function getHTML() {
                global $wgShowDBErrorBacktrace;
 
@@ -71,11 +103,19 @@ class DBConnectionError extends DBError {
                parent::__construct( $db, $msg );
        }
 
+       /**
+        * @return bool
+        */
        function useOutputPage() {
                // Not likely to work
                return false;
        }
 
+       /**
+        * @param $key
+        * @param $fallback
+        * @return string
+        */
        function msg( $key, $fallback /*[, params...] */ ) {
                global $wgLang;
 
@@ -89,16 +129,25 @@ class DBConnectionError extends DBError {
                return wfMsgReplaceArgs( $message, $args );
        }
 
+       /**
+        * @return bool
+        */
        function getLogMessage() {
                # Don't send to the exception log
                return false;
        }
 
+       /**
+        * @return string
+        */
        function getPageTitle() {
                global $wgSitename;
                return htmlspecialchars( $this->msg( 'dberr-header', "$wgSitename has a problem" ) );
        }
 
+       /**
+        * @return string
+        */
        function getHTML() {
                global $wgShowDBErrorBacktrace;
 
@@ -154,26 +203,29 @@ class DBConnectionError extends DBError {
                }
 
                # We can't, cough and die in the usual fashion
-               return parent::reportHTML();
+               parent::reportHTML();
        }
 
+       /**
+        * @return string
+        */
        function searchForm() {
-               global $wgSitename, $wgServer;
+               global $wgSitename, $wgCanonicalServer, $wgRequest;
 
                $usegoogle = htmlspecialchars( $this->msg( 'dberr-usegoogle', 'You can try searching via Google in the meantime.' ) );
                $outofdate = htmlspecialchars( $this->msg( 'dberr-outofdate', 'Note that their indexes of our content may be out of date.' ) );
                $googlesearch = htmlspecialchars( $this->msg( 'searchbutton', 'Search' ) );
 
-               $search = htmlspecialchars( @$_REQUEST['search'] );
+               $search = htmlspecialchars( $wgRequest->getVal( 'search' ) );
 
-               $server = htmlspecialchars( $wgServer );
+               $server = htmlspecialchars( $wgCanonicalServer );
                $sitename = htmlspecialchars( $wgSitename );
 
                $trygoogle = <<<EOT
 <div style="margin: 1.5em">$usegoogle<br />
 <small>$outofdate</small></div>
 <!-- SiteSearch Google -->
-<form method="get" action="http://www.google.com/search" id="googlesearch">
+<form method="get" action="//www.google.com/search" id="googlesearch">
        <input type="hidden" name="domains" value="$server" />
        <input type="hidden" name="num" value="50" />
        <input type="hidden" name="ie" value="UTF-8" />
@@ -191,22 +243,32 @@ EOT;
                return $trygoogle;
        }
 
+       /**
+        * @return string
+        */
        private function fileCachedPage() {
-               global $wgTitle, $wgOut;
+               global $wgTitle, $wgOut, $wgRequest;
 
                if ( $wgOut->isDisabled() ) {
-                       return; // Done already?
+                       return ''; // Done already?
                }
 
-               if ( $wgTitle ) {
-                       $t =& $wgTitle;
+               if ( $wgTitle ) { // use $wgTitle if we managed to set it
+                       $t = $wgTitle->getPrefixedDBkey();
                } else {
-                       $t = Title::newFromText( $this->msg( 'mainpage', 'Main Page' ) );
+                       # Fallback to the raw title URL param. We can't use the Title
+                       # class is it may hit the interwiki table and give a DB error.
+                       # We may get a cache miss due to not sanitizing the title though.
+                       $t = str_replace( ' ', '_', $wgRequest->getVal( 'title' ) );
+                       if ( $t == '' ) { // fallback to main page
+                               $t = Title::newFromText(
+                                       $this->msg( 'mainpage', 'Main Page' ) )->getPrefixedDBkey();
+                       }
                }
 
-               $cache = new HTMLFileCache( $t );
-               if ( $cache->isFileCached() ) {
-                       return $cache->fetchPageText();
+               $cache = HTMLFileCache::newFromTitle( $t, 'view' );
+               if ( $cache->isCached() ) {
+                       return $cache->fetchText();
                } else {
                        return '';
                }
@@ -219,15 +281,18 @@ EOT;
 class DBQueryError extends DBError {
        public $error, $errno, $sql, $fname;
 
+       /**
+        * @param $db DatabaseBase
+        * @param $error string
+        * @param $errno int|string
+        * @param $sql string
+        * @param $fname string
+        */
        function __construct( DatabaseBase &$db, $error, $errno, $sql, $fname ) {
-               $message = "A database error has occurred.  Did you forget to run maintenance/update.php after upgrading?  See: http://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
+               $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" .
                  "Query: $sql\n" .
                  "Function: $fname\n" .
                  "Error: $errno $error\n";
-               global $wgShowDBErrorBacktrace;
-               if( $wgShowDBErrorBacktrace ) {
-                       $message .= $this->getTraceAsString();
-               }
                parent::__construct( $db, $message );
 
                $this->error = $error;
@@ -236,20 +301,32 @@ class DBQueryError extends DBError {
                $this->fname = $fname;
        }
 
+       /**
+        * @param $html string
+        * @return string
+        */
        function getContentMessage( $html ) {
                if ( $this->useMessageCache() ) {
-                       $msg = $html ? 'dberrortext' : 'dberrortextcl';
-                       $ret = wfMsg( $msg, $this->getSQL(),
-                               $this->fname, $this->errno, $this->error );
                        if ( $html ) {
-                               $ret = htmlspecialchars( $ret );
+                               $msg = 'dberrortext';
+                               $sql = htmlspecialchars( $this->getSQL() );
+                               $fname = htmlspecialchars( $this->fname );
+                               $error = htmlspecialchars( $this->error );
+                       } else {
+                               $msg = 'dberrortextcl';
+                               $sql = $this->getSQL();
+                               $fname = $this->fname;
+                               $error = $this->error;
                        }
-                       return $ret;
+                       return wfMsg( $msg, $sql, $fname, $this->errno, $error );
                } else {
                        return parent::getContentMessage( $html );
                }
        }
 
+       /**
+        * @return String
+        */
        function getSQL() {
                global $wgShowSQLErrors;
 
@@ -260,11 +337,17 @@ class DBQueryError extends DBError {
                }
        }
 
+       /**
+        * @return bool
+        */
        function getLogMessage() {
                # Don't send to the exception log
                return false;
        }
 
+       /**
+        * @return String
+        */
        function getPageTitle() {
                return $this->msg( 'databaseerror', 'Database error' );
        }