On second thoughts, remove it altogether...if asking the user is really so hard,...
[lhc/web/wiklou.git] / includes / Database.php
index 65ccd58..eb1ee13 100644 (file)
@@ -5,13 +5,6 @@
  * @package MediaWiki
  */
 
-/** See Database::makeList() */
-define( 'LIST_COMMA', 0 );
-define( 'LIST_AND', 1 );
-define( 'LIST_SET', 2 );
-define( 'LIST_NAMES', 3);
-define( 'LIST_OR', 4);
-
 /** Number of times to re-try an operation in case of deadlock */
 define( 'DEADLOCK_TRIES', 4 );
 /** Minimum time to wait before retry, in microseconds */
@@ -86,14 +79,19 @@ class DBConnectionError extends DBError {
                return $this->getMessage() . "\n";
        }
 
+       function getLogMessage() {
+               # Don't send to the exception log
+               return false;
+       }
+
        function getPageTitle() {
                global $wgSitename;
                return "$wgSitename has a problem";
        }
 
        function getHTML() {
-               global $wgTitle, $wgUseFileCache, $title, $wgInputEncoding, $wgOutputEncoding;
-               global $wgSitename, $wgServer, $wgMessageCache, $wgLogo;
+               global $wgTitle, $wgUseFileCache, $title, $wgInputEncoding;
+               global $wgSitename, $wgServer, $wgMessageCache;
 
                # I give up, Brion is right. Getting the message cache to work when there is no DB is tricky.
                # Hard coding strings instead.
@@ -154,7 +152,7 @@ border=\"0\" ALT=\"Google\"></A>
                                }
                        }
 
-                       $cache = new CacheManager( $t );
+                       $cache = new HTMLFileCache( $t );
                        if( $cache->isFileCached() ) {
                                $msg = '<p style="color: red"><b>'.$msg."<br />\n" .
                                        $cachederror . "</b></p>\n";
@@ -205,6 +203,11 @@ class DBQueryError extends DBError {
                }
        }
        
+       function getLogMessage() {
+               # Don't send to the exception log
+               return false;
+       }
+
        function getPageTitle() {
                return $this->msg( 'databaseerror', 'Database error' );
        }
@@ -244,6 +247,9 @@ class Database {
        protected $mTrxLevel = 0;
        protected $mErrorCount = 0;
        protected $mLBInfo = array();
+       protected $mCascadingDeletes = false;
+       protected $mCleanupTriggers = false;
+       protected $mStrictIPs = false;
 
 #------------------------------------------------------------------------------
 # Accessors
@@ -289,7 +295,7 @@ class Database {
         * Turns on (false) or off (true) the automatic generation and sending
         * of a "we're sorry, but there has been a database error" page on
         * database errors. Default is on (false). When turned off, the
-        * code should use wfLastErrno() and wfLastError() to handle the
+        * code should use lastErrno() and lastError() to handle the
         * situation as appropriate.
         */
        function ignoreErrors( $ignoreErrors = NULL ) {
@@ -334,6 +340,42 @@ class Database {
                }
        }
 
+       /**
+        * Returns true if this database supports (and uses) cascading deletes
+        */
+       function cascadingDeletes() {
+               return $this->mCascadingDeletes;
+       }
+
+       /**
+        * Returns true if this database supports (and uses) triggers (e.g. on the page table)
+        */
+       function cleanupTriggers() {
+               return $this->mCleanupTriggers;
+       }
+
+       /**
+        * Returns true if this database is strict about what can be put into an IP field.
+        * Specifically, it uses a NULL value instead of an empty string.
+        */
+       function strictIPs() {
+               return $this->mStrictIPs;
+       }
+
+       /**
+        * Returns true if this database uses timestamps rather than integers
+       */
+       function realTimestamps() {
+               return false;
+       }
+
+       /**
+        * Returns true if this database does an implicit sort when doing GROUP BY
+        */
+       function implicitGroupby() {
+               return true;
+       }
+
        /**#@+
         * Get function
         */
@@ -433,6 +475,7 @@ class Database {
         */
        function open( $server, $user, $password, $dbName ) {
                global $wguname;
+               wfProfileIn( __METHOD__ );
 
                # Test for missing mysql.so
                # First try to load it
@@ -454,12 +497,28 @@ class Database {
 
                $success = false;
 
-               if ( $this->mFlags & DBO_PERSISTENT ) {
-                       @/**/$this->mConn = mysql_pconnect( $server, $user, $password );
-               } else {
-                       # Create a new connection...
-                       @/**/$this->mConn = mysql_connect( $server, $user, $password, true );
+               wfProfileIn("dbconnect-$server");
+               
+               # LIVE PATCH by Tim, ask Domas for why: retry loop
+               $this->mConn = false;
+               $max = 3;
+               for ( $i = 0; $i < $max && !$this->mConn; $i++ ) {
+                       if ( $i > 1 ) {
+                               usleep( 1000 );
+                       }
+                       if ( $this->mFlags & DBO_PERSISTENT ) {
+                               @/**/$this->mConn = mysql_pconnect( $server, $user, $password );
+                       } else {
+                               # Create a new connection...
+                               @/**/$this->mConn = mysql_connect( $server, $user, $password, true );
+                       }
+                       if ($this->mConn === false) {
+                               $iplus = $i + 1;
+                               wfLogDBError("Connect loop error $iplus of $max ($server): " . mysql_errno() . " - " . mysql_error()."\n"); 
+                       }
                }
+               
+               wfProfileOut("dbconnect-$server");
 
                if ( $dbName != '' ) {
                        if ( $this->mConn !== false ) {
@@ -467,6 +526,7 @@ class Database {
                                if ( !$success ) {
                                        $error = "Error selecting database $dbName on server {$this->mServer} " .
                                                "from client host {$wguname['nodename']}\n";
+                                       wfLogDBError(" Error selecting database $dbName on server {$this->mServer} \n");
                                        wfDebug( $error );
                                }
                        } else {
@@ -480,18 +540,19 @@ class Database {
                        $success = (bool)$this->mConn;
                }
 
-               if ( !$success ) {
+               if ( $success ) {
+                       global $wgDBmysql5;
+                       if( $wgDBmysql5 ) {
+                               // Tell the server we're communicating with it in UTF-8.
+                               // This may engage various charset conversions.
+                               $this->query( 'SET NAMES utf8' );
+                       }
+               } else {
                        $this->reportConnectionError();
                }
 
-               global $wgDBmysql5;
-               if( $wgDBmysql5 ) {
-                       // Tell the server we're communicating with it in UTF-8.
-                       // This may engage various charset conversions.
-                       $this->query( 'SET NAMES utf8' );
-               }
-
                $this->mOpened = $success;
+               wfProfileOut( __METHOD__ );
                return $success;
        }
        /**@}}*/
@@ -566,7 +627,7 @@ class Database {
 
                # Add a comment for easy SHOW PROCESSLIST interpretation
                if ( $fname ) {
-                       $commentedSql = preg_replace("/\s/", " /* $fname */ ", $sql, 1);
+                       $commentedSql = preg_replace('/\s/', " /* $fname */ ", $sql, 1);
                } else {
                        $commentedSql = $sql;
                }
@@ -632,7 +693,7 @@ class Database {
         * @param bool $tempIgnore
         */
        function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
-               global $wgCommandLineMode, $wgFullyInitialised, $wgColorErrors;
+               global $wgCommandLineMode;
                # Ignore errors during error handling to avoid infinite recursion
                $ignore = $this->ignoreErrors( true );
                ++$this->mErrorCount;
@@ -731,7 +792,7 @@ class Database {
                        case '\\!': return '!';
                        case '\\&': return '&';
                }
-               list( $n, $arg ) = each( $this->preparedArgs );
+               list( /* $n */ , $arg ) = each( $this->preparedArgs );
                switch( $matches[1] ) {
                        case '?': return $this->addQuotes( $arg );
                        case '!': return $arg;
@@ -760,8 +821,8 @@ class Database {
         */
        function fetchObject( $res ) {
                @/**/$row = mysql_fetch_object( $res );
-               if( mysql_errno() ) {
-                       throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( mysql_error() ) );
+               if( $this->lastErrno() ) {
+                       throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) );
                }
                return $row;
        }
@@ -772,8 +833,8 @@ class Database {
         */
        function fetchRow( $res ) {
                @/**/$row = mysql_fetch_array( $res );
-               if (mysql_errno() ) {
-                       throw new DBUnexpectedError( $this, 'Error in fetchRow(): ' . htmlspecialchars( mysql_error() ) );
+               if ( $this->lastErrno() ) {
+                       throw new DBUnexpectedError( $this, 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() ) );
                }
                return $row;
        }
@@ -783,8 +844,8 @@ class Database {
         */
        function numRows( $res ) {
                @/**/$n = mysql_num_rows( $res );
-               if( mysql_errno() ) {
-                       throw new DBUnexpectedError( $this, 'Error in numRows(): ' . htmlspecialchars( mysql_error() ) );
+               if( $this->lastErrno() ) {
+                       throw new DBUnexpectedError( $this, 'Error in numRows(): ' . htmlspecialchars( $this->lastError() ) );
                }
                return $n;
        }
@@ -934,6 +995,7 @@ class Database {
                if ( isset( $noKeyOptions['DISTINCT'] ) && isset( $noKeyOptions['DISTINCTROW'] ) ) $startOpts .= 'DISTINCT';
 
                # Various MySQL extensions
+               if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) $startOpts .= ' /*! STRAIGHT_JOIN */';
                if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) $startOpts .= ' HIGH_PRIORITY';
                if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) $startOpts .= ' SQL_BIG_RESULT';
                if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) $startOpts .= ' SQL_BUFFER_RESULT';
@@ -953,6 +1015,14 @@ class Database {
 
        /**
         * SELECT wrapper
+        *
+        * @param mixed  $table   Array or string, table name(s) (prefix auto-added)
+        * @param mixed  $vars    Array or string, field name(s) to be retrieved
+        * @param mixed  $conds   Array or string, condition(s) for WHERE
+        * @param string $fname   Calling function name (use __METHOD__) for logs/profiling
+        * @param array  $options Associative array of options (e.g. array('GROUP BY' => 'page_title')),
+        *                        see Database::makeSelectOptions code for list of supported stuff
+        * @return mixed Database result resource (feed to Database::fetchObject or whatever), or false on failure
         */
        function select( $table, $vars, $conds='', $fname = 'Database::select', $options = array() )
        {
@@ -963,7 +1033,7 @@ class Database {
                        $options = array( $options );
                }
                if( is_array( $table ) ) {
-                       if ( @is_array( $options['USE INDEX'] ) )
+                       if ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
                                $from = ' FROM ' . $this->tableNamesWithUseIndex( $table, $options['USE INDEX'] );
                        else
                                $from = ' FROM ' . implode( ',', array_map( array( &$this, 'tableName' ), $table ) );
@@ -1035,7 +1105,7 @@ class Database {
                $sql = preg_replace ('/".*"/s', "'X'", $sql);
 
                # All newlines, tabs, etc replaced by single space
-               $sql = preg_replace ( "/\s+/", ' ', $sql);
+               $sql = preg_replace ( '/\s+/', ' ', $sql);
 
                # All numbers => N
                $sql = preg_replace ('/-?[0-9]+/s', 'N', $sql);
@@ -1096,12 +1166,15 @@ class Database {
                        return NULL;
                }
 
+               $result = array();
                while ( $row = $this->fetchObject( $res ) ) {
                        if ( $row->Key_name == $index ) {
-                               return $row;
+                               $result[] = $row;
                        }
                }
-               return false;
+               $this->freeResult($res);
+               
+               return empty($result) ? false : $result;
        }
 
        /**
@@ -1155,7 +1228,7 @@ class Database {
                if ( !$indexInfo ) {
                        return NULL;
                }
-               return !$indexInfo->Non_unique;
+               return !$indexInfo[0]->Non_unique;
        }
 
        /**
@@ -1245,7 +1318,7 @@ class Database {
        }
 
        /**
-        * Makes a wfStrencoded list from an array
+        * Makes an encoded list of strings from an array
         * $mode:
         *        LIST_COMMA         - comma separated, no field names
         *        LIST_AND           - ANDed WHERE clause (without the WHERE)
@@ -1274,6 +1347,8 @@ class Database {
                        }
                        if ( ($mode == LIST_AND || $mode == LIST_OR) && is_numeric( $field ) ) {
                                $list .= "($value)";
+                       } elseif ( ($mode == LIST_SET) && is_numeric( $field ) ) {
+                               $list .= "$value";
                        } elseif ( ($mode == LIST_AND || $mode == LIST_OR) && is_array ($value) ) {
                                $list .= $field." IN (".$this->makeList($value).") ";
                        } else {
@@ -1332,7 +1407,7 @@ class Database {
         * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
         *         WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
         */
-       function tableNames() {
+       public function tableNames() {
                $inArray = func_get_args();
                $retVal = array();
                foreach ( $inArray as $name ) {
@@ -1340,6 +1415,24 @@ class Database {
                }
                return $retVal;
        }
+       
+       /**
+        * @desc: Fetch a number of table names into an zero-indexed numerical array
+        * This is handy when you need to construct SQL for joins
+        *
+        * Example:
+        * list( $user, $watchlist ) = $dbr->tableNames('user','watchlist');
+        * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
+        *         WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
+        */
+       public function tableNamesN() {
+               $inArray = func_get_args();
+               $retVal = array();
+               foreach ( $inArray as $name ) {
+                       $retVal[] = $this->tableName( $name );
+               }
+               return $retVal;
+       }
 
        /**
         * @private
@@ -1481,7 +1574,8 @@ class Database {
                $row = $this->fetchObject( $res );
                $this->freeResult( $res );
 
-               if ( preg_match( "/\((.*)\)/", $row->Type, $m ) ) {
+               $m = array();
+               if ( preg_match( '/\((.*)\)/', $row->Type, $m ) ) {
                        $size = $m[1];
                } else {
                        $size = -1;
@@ -1782,7 +1876,7 @@ class Database {
         * @return string Version information from the database
         */
        function getServerVersion() {
-               return mysql_get_server_info();
+               return mysql_get_server_info( $this->mConn );
        }
 
        /**
@@ -1805,7 +1899,6 @@ class Database {
                $res = $this->query( 'SHOW PROCESSLIST' );
                # Find slave SQL thread. Assumed to be the second one running, which is a bit
                # dubious, but unfortunately there's no easy rigorous way
-               $slaveThreads = 0;
                while ( $row = $this->fetchObject( $res ) ) {
                        /* This should work for most situations - when default db 
                         * for thread is not specified, it had no events executed, 
@@ -1854,6 +1947,10 @@ class Database {
                return $b;
        }
 
+       function decodeBlob($b) {
+               return $b;
+       }
+
        /**
         * Read and execute SQL commands from a file.
         * Returns true on success, error string on failure
@@ -1861,7 +1958,7 @@ class Database {
        function sourceFile( $filename ) {
                $fp = fopen( $filename, 'r' );
                if ( false === $fp ) {
-                       return "Could not open \"{$fname}\".\n";
+                       return "Could not open \"{$filename}\".\n";
                }
 
                $cmd = "";