Merge "API: Handle exceptions from ApiBeforeMain hook in a user-friendly manner"
[lhc/web/wiklou.git] / includes / db / Database.php
index 6e87c40..91ab0ca 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+
 /**
  * @defgroup Database Database
  *
@@ -245,11 +246,11 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
        protected $mTrxPreCommitCallbacks = array();
 
        protected $mTablePrefix;
+       protected $mSchema;
        protected $mFlags;
        protected $mForeign;
        protected $mErrorCount = 0;
        protected $mLBInfo = array();
-       protected $mFakeSlaveLag = null, $mFakeMaster = false;
        protected $mDefaultBigSelects = null;
        protected $mSchemaVars = false;
 
@@ -429,6 +430,15 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                return wfSetVar( $this->mTablePrefix, $prefix );
        }
 
+       /**
+        * Get/set the db schema.
+        * @param string $schema The database schema to set, or omitted to leave it unchanged.
+        * @return string The previous db schema.
+        */
+       public function dbSchema( $schema = null ) {
+               return wfSetVar( $this->mSchema, $schema );
+       }
+
        /**
         * Set the filehandle to copy write statements to.
         *
@@ -478,10 +488,11 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
        /**
         * Set lag time in seconds for a fake slave
         *
-        * @param int $lag
+        * @param mixed $lag Valid values for this parameter are determined by the
+        *   subclass, but should be a PHP scalar or array that would be sensible
+        *   as part of $wgLBFactoryConf.
         */
        public function setFakeSlaveLag( $lag ) {
-               $this->mFakeSlaveLag = $lag;
        }
 
        /**
@@ -490,7 +501,6 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @param bool $enabled
         */
        public function setFakeMaster( $enabled = true ) {
-               $this->mFakeMaster = $enabled;
        }
 
        /**
@@ -712,7 +722,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @param array $params Parameters passed from DatabaseBase::factory()
         */
        function __construct( $params = null ) {
-               global $wgDBprefix, $wgCommandLineMode, $wgDebugDBTransactions;
+               global $wgDBprefix, $wgDBmwschema, $wgCommandLineMode, $wgDebugDBTransactions;
 
                $this->mTrxAtomicLevels = new SplStack;
 
@@ -723,6 +733,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                        $dbName = $params['dbname'];
                        $flags = $params['flags'];
                        $tablePrefix = $params['tablePrefix'];
+                       $schema = $params['schema'];
                        $foreign = $params['foreign'];
                } else { // legacy calling pattern
                        wfDeprecated( __METHOD__ . " method called without parameter array.", "1.23" );
@@ -733,6 +744,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                        $dbName = isset( $args[3] ) ? $args[3] : false;
                        $flags = isset( $args[4] ) ? $args[4] : 0;
                        $tablePrefix = isset( $args[5] ) ? $args[5] : 'get from global';
+                       $schema = 'get from global';
                        $foreign = isset( $args[6] ) ? $args[6] : false;
                }
 
@@ -758,6 +770,13 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                        $this->mTablePrefix = $tablePrefix;
                }
 
+               /** Get the database schema*/
+               if ( $schema == 'get from global' ) {
+                       $this->mSchema = $wgDBmwschema;
+               } else {
+                       $this->mSchema = $schema;
+               }
+
                $this->mForeign = $foreign;
 
                if ( $user ) {
@@ -793,7 +812,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         *
         * @param string $dbType A possible DB type
         * @param array $p An array of options to pass to the constructor.
-        *    Valid options are: host, user, password, dbname, flags, tablePrefix, driver
+        *    Valid options are: host, user, password, dbname, flags, tablePrefix, schema, driver
         * @throws MWException If the database driver or extension cannot be found
         * @return DatabaseBase|null DatabaseBase subclass or null
         */
@@ -833,6 +852,18 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                                " no viable database extension found for type '$dbType'" );
                }
 
+               // Determine schema defaults. Currently Microsoft SQL Server uses $wgDBmwschema,
+               // and everything else doesn't use a schema (e.g. null)
+               // Although postgres and oracle support schemas, we don't use them (yet)
+               // to maintain backwards compatibility
+               $defaultSchemas = array(
+                       'mysql' => null,
+                       'postgres' => null,
+                       'sqlite' => null,
+                       'oracle' => null,
+                       'mssql' => 'get from global',
+               );
+
                $class = 'Database' . ucfirst( $driver );
                if ( class_exists( $class ) && is_subclass_of( $class, 'DatabaseBase' ) ) {
                        $params = array(
@@ -842,6 +873,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                                'dbname' => isset( $p['dbname'] ) ? $p['dbname'] : false,
                                'flags' => isset( $p['flags'] ) ? $p['flags'] : 0,
                                'tablePrefix' => isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : 'get from global',
+                               'schema' => isset( $p['schema'] ) ? $p['schema'] : $defaultSchemas[$dbType],
                                'foreign' => isset( $p['foreign'] ) ? $p['foreign'] : false
                        );
 
@@ -1890,12 +1922,12 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
        }
 
        /**
-        * Make UPDATE options for the DatabaseBase::update function
+        * Make UPDATE options array for DatabaseBase::makeUpdateOptions
         *
-        * @param array $options The options passed to DatabaseBase::update
-        * @return string
+        * @param array $options
+        * @return array
         */
-       protected function makeUpdateOptions( $options ) {
+       protected function makeUpdateOptionsArray( $options ) {
                if ( !is_array( $options ) ) {
                        $options = array( $options );
                }
@@ -1910,6 +1942,18 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                        $opts[] = 'IGNORE';
                }
 
+               return $opts;
+       }
+
+       /**
+        * Make UPDATE options for the DatabaseBase::update function
+        *
+        * @param array $options The options passed to DatabaseBase::update
+        * @return string
+        */
+       protected function makeUpdateOptions( $options ) {
+               $opts = $this->makeUpdateOptionsArray( $options );
+
                return implode( ' ', $opts );
        }
 
@@ -2163,7 +2207,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @return string Full database name
         */
        public function tableName( $name, $format = 'quoted' ) {
-               global $wgSharedDB, $wgSharedPrefix, $wgSharedTables;
+               global $wgSharedDB, $wgSharedPrefix, $wgSharedTables, $wgSharedSchema;
                # Skip the entire process when we have a string quoted on both ends.
                # Note that we check the end so that we will still quote any use of
                # use of `database`.table. But won't break things if someone wants
@@ -2187,10 +2231,17 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                # We reverse the explode so that database.table and table both output
                # the correct table.
                $dbDetails = explode( '.', $name, 2 );
-               if ( count( $dbDetails ) == 2 ) {
+               if ( count( $dbDetails ) == 3 ) {
+                       list( $database, $schema, $table ) = $dbDetails;
+                       # We don't want any prefix added in this case
+                       $prefix = '';
+               } elseif ( count( $dbDetails ) == 2 ) {
                        list( $database, $table ) = $dbDetails;
                        # We don't want any prefix added in this case
+                       # In dbs that support it, $database may actually be the schema
+                       # but that doesn't affect any of the functionality here
                        $prefix = '';
+                       $schema = null;
                } else {
                        list( $table ) = $dbDetails;
                        if ( $wgSharedDB !== null # We have a shared database
@@ -2199,19 +2250,30 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                                && in_array( $table, $wgSharedTables ) # A shared table is selected
                        ) {
                                $database = $wgSharedDB;
+                               $schema = $wgSharedSchema === null ? $this->mSchema : $wgSharedSchema;
                                $prefix = $wgSharedPrefix === null ? $this->mTablePrefix : $wgSharedPrefix;
                        } else {
                                $database = null;
+                               $schema = $this->mSchema; # Default schema
                                $prefix = $this->mTablePrefix; # Default prefix
                        }
                }
 
                # Quote $table and apply the prefix if not quoted.
+               # $tableName might be empty if this is called from Database::replaceVars()
                $tableName = "{$prefix}{$table}";
-               if ( $format == 'quoted' && !$this->isQuotedIdentifier( $tableName ) ) {
+               if ( $format == 'quoted' && !$this->isQuotedIdentifier( $tableName ) && $tableName !== '' ) {
                        $tableName = $this->addIdentifierQuotes( $tableName );
                }
 
+               # Quote $schema and merge it with the table name if needed
+               if ( $schema !== null ) {
+                       if ( $format == 'quoted' && !$this->isQuotedIdentifier( $schema ) ) {
+                               $schema = $this->addIdentifierQuotes( $schema );
+                       }
+                       $tableName = $schema . '.' . $tableName;
+               }
+
                # Quote $database and merge it with the table name if needed
                if ( $database !== null ) {
                        if ( $format == 'quoted' && !$this->isQuotedIdentifier( $database ) ) {
@@ -2374,11 +2436,12 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                                }
 
                                $retJOIN[] = $tableClause;
-                       // Is there an INDEX clause for this table?
                        } elseif ( isset( $use_index[$alias] ) ) {
+                               // Is there an INDEX clause for this table?
                                $tableClause = $this->tableNameWithAlias( $table, $alias );
                                $tableClause .= ' ' . $this->useIndexClause(
-                                       implode( ',', (array)$use_index[$alias] ) );
+                                       implode( ',', (array)$use_index[$alias] )
+                               );
 
                                $ret[] = $tableClause;
                        } else {
@@ -2697,7 +2760,10 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                if ( !count( $rows ) ) {
                        return true; // nothing to do
                }
-               $rows = is_array( reset( $rows ) ) ? $rows : array( $rows );
+
+               if ( !is_array( reset( $rows ) ) ) {
+                       $rows = array( $rows );
+               }
 
                if ( count( $uniqueIndexes ) ) {
                        $clauses = array(); // list WHERE clauses that each identify a single row
@@ -2756,7 +2822,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @param string $delTable The table to delete from.
         * @param string $joinTable The other table.
         * @param string $delVar The variable to join on, in the first table.
-        * @param string$joinVar The variable to join on, in the second table.
+        * @param string $joinVar The variable to join on, in the second table.
         * @param array $conds Condition array of field names mapped to variables,
         *   ANDed together in the WHERE clause
         * @param string $fname Calling function name (use __METHOD__) for logs/profiling
@@ -3117,32 +3183,6 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         *   zero if we timed out.
         */
        public function masterPosWait( DBMasterPos $pos, $timeout ) {
-               wfProfileIn( __METHOD__ );
-
-               if ( !is_null( $this->mFakeSlaveLag ) ) {
-                       $wait = intval( ( $pos->pos - microtime( true ) + $this->mFakeSlaveLag ) * 1e6 );
-
-                       if ( $wait > $timeout * 1e6 ) {
-                               wfDebug( "Fake slave timed out waiting for $pos ($wait us)\n" );
-                               wfProfileOut( __METHOD__ );
-
-                               return -1;
-                       } elseif ( $wait > 0 ) {
-                               wfDebug( "Fake slave waiting $wait us\n" );
-                               usleep( $wait );
-                               wfProfileOut( __METHOD__ );
-
-                               return 1;
-                       } else {
-                               wfDebug( "Fake slave up to date ($wait us)\n" );
-                               wfProfileOut( __METHOD__ );
-
-                               return 0;
-                       }
-               }
-
-               wfProfileOut( __METHOD__ );
-
                # Real waits are implemented in the subclass.
                return 0;
        }
@@ -3153,15 +3193,8 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @return DBMasterPos|bool False if this is not a slave.
         */
        public function getSlavePos() {
-               if ( !is_null( $this->mFakeSlaveLag ) ) {
-                       $pos = new MySQLMasterPos( 'fake', microtime( true ) - $this->mFakeSlaveLag );
-                       wfDebug( __METHOD__ . ": fake slave pos = $pos\n" );
-
-                       return $pos;
-               } else {
-                       # Stub
-                       return false;
-               }
+               # Stub
+               return false;
        }
 
        /**
@@ -3170,11 +3203,8 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @return DBMasterPos|bool False if this is not a master
         */
        public function getMasterPos() {
-               if ( $this->mFakeMaster ) {
-                       return new MySQLMasterPos( 'fake', microtime( true ) );
-               } else {
-                       return false;
-               }
+               # Stub
+               return false;
        }
 
        /**
@@ -3642,7 +3672,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @return int Database replication lag in seconds
         */
        public function getLag() {
-               return intval( $this->mFakeSlaveLag );
+               return 0;
        }
 
        /**