Never prefix table names with $wgSharedDB when used in foreign DB
[lhc/web/wiklou.git] / includes / db / Database.php
index 2142232..8b56ab0 100644 (file)
  * @ingroup Database
  */
 
-/** Number of times to re-try an operation in case of deadlock */
-define( 'DEADLOCK_TRIES', 4 );
-/** Minimum time to wait before retry, in microseconds */
-define( 'DEADLOCK_DELAY_MIN', 500000 );
-/** Maximum time to wait before retry */
-define( 'DEADLOCK_DELAY_MAX', 1500000 );
-
 /**
  * Base interface for all DBMS-specific code. At a bare minimum, all of the
  * following must be implemented to support MediaWiki
@@ -165,7 +158,7 @@ interface DatabaseType {
         * @param string $fname Calling function name
         * @return Mixed: Database-specific index description class or false if the index does not exist
         */
-       function indexInfo( $table, $index, $fname = 'Database::indexInfo' );
+       function indexInfo( $table, $index, $fname = __METHOD__ );
 
        /**
         * Get the number of rows affected by the last write query
@@ -216,6 +209,12 @@ interface DatabaseType {
  * @ingroup Database
  */
 abstract class DatabaseBase implements DatabaseType {
+       /** Number of times to re-try an operation in case of deadlock */
+       const DEADLOCK_TRIES = 4;
+       /** Minimum time to wait before retry, in microseconds */
+       const DEADLOCK_DELAY_MIN = 500000;
+       /** Maximum time to wait before retry */
+       const DEADLOCK_DELAY_MAX = 1500000;
 
 # ------------------------------------------------------------------------------
 # Variables
@@ -230,14 +229,14 @@ abstract class DatabaseBase implements DatabaseType {
        protected $mConn = null;
        protected $mOpened = false;
 
-       /**
-        * @since 1.20
-        * @var array of Closure
-        */
+       /** @var array of Closure */
        protected $mTrxIdleCallbacks = array();
+       /** @var array of Closure */
+       protected $mTrxPreCommitCallbacks = array();
 
        protected $mTablePrefix;
        protected $mFlags;
+       protected $mForeign;
        protected $mTrxLevel = 0;
        protected $mErrorCount = 0;
        protected $mLBInfo = array();
@@ -553,12 +552,14 @@ abstract class DatabaseBase implements DatabaseType {
 
        /**
         * Returns true if there is a transaction open with possible write
-        * queries or transaction idle callbacks waiting on it to finish.
+        * queries or transaction pre-commit/idle callbacks waiting on it to finish.
         *
         * @return bool
         */
        public function writesOrCallbacksPending() {
-               return $this->mTrxLevel && ( $this->mTrxDoneWrites || $this->mTrxIdleCallbacks );
+               return $this->mTrxLevel && (
+                       $this->mTrxDoneWrites || $this->mTrxIdleCallbacks || $this->mTrxPreCommitCallbacks
+               );
        }
 
        /**
@@ -584,7 +585,7 @@ abstract class DatabaseBase implements DatabaseType {
        public function setFlag( $flag ) {
                global $wgDebugDBTransactions;
                $this->mFlags |= $flag;
-               if ( ( $flag & DBO_TRX) & $wgDebugDBTransactions ) {
+               if ( ( $flag & DBO_TRX ) & $wgDebugDBTransactions ) {
                        wfDebug( "Implicit transactions are now  disabled.\n" );
                }
        }
@@ -660,9 +661,10 @@ abstract class DatabaseBase implements DatabaseType {
         * @param string $dbName database name
         * @param $flags
         * @param string $tablePrefix database table prefixes. By default use the prefix gave in LocalSettings.php
+        * @param bool $foreign disable some operations specific to local databases
         */
        function __construct( $server = false, $user = false, $password = false, $dbName = false,
-               $flags = 0, $tablePrefix = 'get from global'
+               $flags = 0, $tablePrefix = 'get from global', $foreign = false
        ) {
                global $wgDBprefix, $wgCommandLineMode, $wgDebugDBTransactions;
 
@@ -689,6 +691,8 @@ abstract class DatabaseBase implements DatabaseType {
                        $this->mTablePrefix = $tablePrefix;
                }
 
+               $this->mForeign = $foreign;
+
                if ( $user ) {
                        $this->open( $server, $user, $password, $dbName );
                }
@@ -731,14 +735,15 @@ abstract class DatabaseBase implements DatabaseType {
                $dbType = strtolower( $dbType );
                $class = 'Database' . ucfirst( $dbType );
 
-               if( in_array( $dbType, $canonicalDBTypes ) || ( class_exists( $class ) && is_subclass_of( $class, 'DatabaseBase' ) ) ) {
+               if ( in_array( $dbType, $canonicalDBTypes ) || ( class_exists( $class ) && is_subclass_of( $class, 'DatabaseBase' ) ) ) {
                        return new $class(
                                isset( $p['host'] ) ? $p['host'] : false,
                                isset( $p['user'] ) ? $p['user'] : false,
                                isset( $p['password'] ) ? $p['password'] : false,
                                isset( $p['dbname'] ) ? $p['dbname'] : false,
                                isset( $p['flags'] ) ? $p['flags'] : 0,
-                               isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : 'get from global'
+                               isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : 'get from global',
+                               isset( $p['foreign'] ) ? $p['foreign'] : false
                        );
                } else {
                        return null;
@@ -771,8 +776,9 @@ abstract class DatabaseBase implements DatabaseType {
        /**
         * @param $errno
         * @param $errstr
+        * @access private
         */
-       protected function connectionErrorHandler( $errno, $errstr ) {
+       public function connectionErrorHandler( $errno, $errstr ) {
                $this->mPHPError = $errstr;
        }
 
@@ -869,7 +875,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @return boolean|ResultWrapper. true for a successful write query, ResultWrapper object
         *     for a successful read query, or false on failure if $tempIgnore set
         */
-       public function query( $sql, $fname = '', $tempIgnore = false ) {
+       public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) {
                $isMaster = !is_null( $this->getLBInfo( 'master' ) );
                if ( !Profiler::instance()->isStub() ) {
                        # generalizeSQL will probably cut down the query to reasonable
@@ -944,14 +950,6 @@ abstract class DatabaseBase implements DatabaseType {
                        wfDebug( "Query {$this->mDBname} ($cnt) ($master): $sqlx\n" );
                }
 
-               if ( istainted( $sql ) & TC_MYSQL ) {
-                       if ( !Profiler::instance()->isStub() ) {
-                               wfProfileOut( $queryProf );
-                               wfProfileOut( $totalProf );
-                       }
-                       throw new MWException( 'Tainted query found' );
-               }
-
                $queryId = MWDebug::query( $sql, $fname, $isMaster );
 
                # Do the query and handle errors
@@ -964,6 +962,7 @@ abstract class DatabaseBase implements DatabaseType {
                        # Transaction is gone, like it or not
                        $this->mTrxLevel = 0;
                        $this->mTrxIdleCallbacks = array(); // cancel
+                       $this->mTrxPreCommitCallbacks = array(); // cancel
                        wfDebug( "Connection lost, reconnecting...\n" );
 
                        if ( $this->ping() ) {
@@ -1145,7 +1144,7 @@ abstract class DatabaseBase implements DatabaseType {
         *
         * @return bool|mixed The value from the field, or false on failure.
         */
-       public function selectField( $table, $var, $cond = '', $fname = 'DatabaseBase::selectField',
+       public function selectField( $table, $var, $cond = '', $fname = __METHOD__,
                $options = array()
        ) {
                if ( !is_array( $options ) ) {
@@ -1436,7 +1435,7 @@ abstract class DatabaseBase implements DatabaseType {
         *   DBQueryError exception will be thrown, except if the "ignore errors"
         *   option was set, in which case false will be returned.
         */
-       public function select( $table, $vars, $conds = '', $fname = 'DatabaseBase::select',
+       public function select( $table, $vars, $conds = '', $fname = __METHOD__,
                $options = array(), $join_conds = array() ) {
                $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
 
@@ -1459,7 +1458,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @return string SQL query string.
         * @see DatabaseBase::select()
         */
-       public function selectSQLText( $table, $vars, $conds = '', $fname = 'DatabaseBase::select',
+       public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__,
                $options = array(), $join_conds = array() )
        {
                if ( is_array( $vars ) ) {
@@ -1526,7 +1525,7 @@ abstract class DatabaseBase implements DatabaseType {
         *
         * @return object|bool
         */
-       public function selectRow( $table, $vars, $conds, $fname = 'DatabaseBase::selectRow',
+       public function selectRow( $table, $vars, $conds, $fname = __METHOD__,
                $options = array(), $join_conds = array() )
        {
                $options = (array)$options;
@@ -1567,7 +1566,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @return Integer: row count
         */
        public function estimateRowCount( $table, $vars = '*', $conds = '',
-               $fname = 'DatabaseBase::estimateRowCount', $options = array() )
+               $fname = __METHOD__, $options = array() )
        {
                $rows = 0;
                $res = $this->select( $table, array( 'rowcount' => 'COUNT(*)' ), $conds, $fname, $options );
@@ -1616,7 +1615,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @param string $fname calling function name (optional)
         * @return Boolean: whether $table has filed $field
         */
-       public function fieldExists( $table, $field, $fname = 'DatabaseBase::fieldExists' ) {
+       public function fieldExists( $table, $field, $fname = __METHOD__ ) {
                $info = $this->fieldInfo( $table, $field );
 
                return (bool)$info;
@@ -1633,8 +1632,8 @@ abstract class DatabaseBase implements DatabaseType {
         *
         * @return bool|null
         */
-       public function indexExists( $table, $index, $fname = 'DatabaseBase::indexExists' ) {
-               if( !$this->tableExists( $table ) ) {
+       public function indexExists( $table, $index, $fname = __METHOD__ ) {
+               if ( !$this->tableExists( $table ) ) {
                        return null;
                }
 
@@ -1738,7 +1737,7 @@ abstract class DatabaseBase implements DatabaseType {
         *
         * @return bool
         */
-       public function insert( $table, $a, $fname = 'DatabaseBase::insert', $options = array() ) {
+       public function insert( $table, $a, $fname = __METHOD__, $options = array() ) {
                # No rows to insert, easy just return now
                if ( !count( $a ) ) {
                        return true;
@@ -1837,7 +1836,7 @@ abstract class DatabaseBase implements DatabaseType {
         *                   - LOW_PRIORITY: MySQL-specific, see MySQL manual.
         * @return Boolean
         */
-       function update( $table, $values, $conds, $fname = 'DatabaseBase::update', $options = array() ) {
+       function update( $table, $values, $conds, $fname = __METHOD__, $options = array() ) {
                $table = $this->tableName( $table );
                $opts = $this->makeUpdateOptions( $options );
                $sql = "UPDATE $opts $table SET " . $this->makeList( $values, LIST_SET );
@@ -2074,6 +2073,7 @@ abstract class DatabaseBase implements DatabaseType {
                } else {
                        list( $table ) = $dbDetails;
                        if ( $wgSharedDB !== null # We have a shared database
+                               && $this->mForeign == false # We're not working on a foreign database
                                && !$this->isQuotedIdentifier( $table ) # Paranoia check to prevent shared tables listing '`table`'
                                && in_array( $table, $wgSharedTables ) # A shared table is selected
                        ) {
@@ -2283,9 +2283,9 @@ abstract class DatabaseBase implements DatabaseType {
        protected function indexName( $index ) {
                // Backwards-compatibility hack
                $renamed = array(
-                       'ar_usertext_timestamp' => 'usertext_timestamp',
-                       'un_user_id'            => 'user_id',
-                       'un_user_ip'            => 'user_ip',
+                       'ar_usertext_timestamp' => 'usertext_timestamp',
+                       'un_user_id' => 'user_id',
+                       'un_user_ip' => 'user_ip',
                );
 
                if ( isset( $renamed[$index] ) ) {
@@ -2454,7 +2454,7 @@ abstract class DatabaseBase implements DatabaseType {
         *    a field name or an array of field names
         * @param string $fname Calling function name (use __METHOD__) for logs/profiling
         */
-       public function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseBase::replace' ) {
+       public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
                $quotedTable = $this->tableName( $table );
 
                if ( count( $rows ) == 0 ) {
@@ -2466,7 +2466,7 @@ abstract class DatabaseBase implements DatabaseType {
                        $rows = array( $rows );
                }
 
-               foreach( $rows as $row ) {
+               foreach ( $rows as $row ) {
                        # Delete rows which collide
                        if ( $uniqueIndexes ) {
                                $sql = "DELETE FROM $quotedTable WHERE ";
@@ -2557,7 +2557,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @throws DBUnexpectedError
         */
        public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
-               $fname = 'DatabaseBase::deleteJoin' )
+               $fname = __METHOD__ )
        {
                if ( !$conds ) {
                        throw new DBUnexpectedError( $this,
@@ -2623,7 +2623,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @throws DBUnexpectedError
         * @return bool|ResultWrapper
         */
-       public function delete( $table, $conds, $fname = 'DatabaseBase::delete' ) {
+       public function delete( $table, $conds, $fname = __METHOD__ ) {
                if ( !$conds ) {
                        throw new DBUnexpectedError( $this, 'DatabaseBase::delete() called with no conditions' );
                }
@@ -2665,7 +2665,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @return ResultWrapper
         */
        public function insertSelect( $destTable, $srcTable, $varMap, $conds,
-               $fname = 'DatabaseBase::insertSelect',
+               $fname = __METHOD__,
                $insertOptions = array(), $selectOptions = array() )
        {
                $destTable = $this->tableName( $destTable );
@@ -2857,7 +2857,7 @@ abstract class DatabaseBase implements DatabaseType {
                $args = func_get_args();
                $function = array_shift( $args );
                $oldIgnore = $this->ignoreErrors( true );
-               $tries = DEADLOCK_TRIES;
+               $tries = self::DEADLOCK_TRIES;
 
                if ( is_array( $function ) ) {
                        $fname = $function[0];
@@ -2874,7 +2874,7 @@ abstract class DatabaseBase implements DatabaseType {
                        if ( $errno ) {
                                if ( $this->wasDeadlock() ) {
                                        # Retry
-                                       usleep( mt_rand( DEADLOCK_DELAY_MIN, DEADLOCK_DELAY_MAX ) );
+                                       usleep( mt_rand( self::DEADLOCK_DELAY_MIN, self::DEADLOCK_DELAY_MAX ) );
                                } else {
                                        $this->reportQueryError( $error, $errno, $sql, $fname );
                                }
@@ -2964,24 +2964,45 @@ abstract class DatabaseBase implements DatabaseType {
        /**
         * Run an anonymous function as soon as there is no transaction pending.
         * If there is a transaction and it is rolled back, then the callback is cancelled.
+        * Queries in the function will run in AUTO-COMMIT mode unless there are begin() calls.
         * Callbacks must commit any transactions that they begin.
         *
-        * This is useful for updates to different systems or separate transactions are needed.
+        * This is useful for updates to different systems or when separate transactions are needed.
+        * For example, one might want to enqueue jobs into a system outside the database, but only
+        * after the database is updated so that the jobs will see the data when they actually run.
+        * It can also be used for updates that easily cause deadlocks if locks are held too long.
         *
+        * @param Closure $callback
         * @since 1.20
+        */
+       final public function onTransactionIdle( Closure $callback ) {
+               $this->mTrxIdleCallbacks[] = $callback;
+               if ( !$this->mTrxLevel ) {
+                       $this->runOnTransactionIdleCallbacks();
+               }
+       }
+
+       /**
+        * Run an anonymous function before the current transaction commits or now if there is none.
+        * If there is a transaction and it is rolled back, then the callback is cancelled.
+        * Callbacks must not start nor commit any transactions.
+        *
+        * This is useful for updates that easily cause deadlocks if locks are held too long
+        * but where atomicity is strongly desired for these updates and some related updates.
         *
         * @param Closure $callback
+        * @since 1.22
         */
-       final public function onTransactionIdle( Closure $callback ) {
+       final public function onTransactionPreCommitOrIdle( Closure $callback ) {
                if ( $this->mTrxLevel ) {
-                       $this->mTrxIdleCallbacks[] = $callback;
+                       $this->mTrxPreCommitCallbacks[] = $callback;
                } else {
-                       $callback();
+                       $this->onTransactionIdle( $callback ); // this will trigger immediately
                }
        }
 
        /**
-        * Actually run the "on transaction idle" callbacks.
+        * Actually any "on transaction idle" callbacks.
         *
         * @since 1.20
         */
@@ -3007,6 +3028,28 @@ abstract class DatabaseBase implements DatabaseType {
                }
        }
 
+       /**
+        * Actually any "on transaction pre-commit" callbacks.
+        *
+        * @since 1.22
+        */
+       protected function runOnTransactionPreCommitCallbacks() {
+               $e = null; // last exception
+               do { // callbacks may add callbacks :)
+                       $callbacks = $this->mTrxPreCommitCallbacks;
+                       $this->mTrxPreCommitCallbacks = array(); // recursion guard
+                       foreach ( $callbacks as $callback ) {
+                               try {
+                                       $callback();
+                               } catch ( Exception $e ) {}
+                       }
+               } while ( count( $this->mTrxPreCommitCallbacks ) );
+
+               if ( $e instanceof Exception ) {
+                       throw $e; // re-throw any last exception
+               }
+       }
+
        /**
         * Begin a transaction. If a transaction is already in progress, that transaction will be committed before the
         * new transaction is started.
@@ -3019,7 +3062,7 @@ abstract class DatabaseBase implements DatabaseType {
         *
         * @param $fname string
         */
-       final public function begin( $fname = 'DatabaseBase::begin' ) {
+       final public function begin( $fname = __METHOD__ ) {
                global $wgDebugDBTransactions;
 
                if ( $this->mTrxLevel ) { // implicit commit
@@ -3040,6 +3083,7 @@ abstract class DatabaseBase implements DatabaseType {
                                }
                        }
 
+                       $this->runOnTransactionPreCommitCallbacks();
                        $this->doCommit( $fname );
                        $this->runOnTransactionIdleCallbacks();
                }
@@ -3073,21 +3117,22 @@ abstract class DatabaseBase implements DatabaseType {
         *        This will silently break any ongoing explicit transaction. Only set the flush flag if you are sure
         *        that it is safe to ignore these warnings in your context.
         */
-       final public function commit( $fname = 'DatabaseBase::commit', $flush = '' ) {
+       final public function commit( $fname = __METHOD__, $flush = '' ) {
                if ( $flush != 'flush' ) {
                        if ( !$this->mTrxLevel ) {
                                wfWarn( "$fname: No transaction to commit, something got out of sync!" );
-                       } elseif( $this->mTrxAutomatic ) {
+                       } elseif ( $this->mTrxAutomatic ) {
                                wfWarn( "$fname: Explicit commit of implicit transaction. Something may be out of sync!" );
                        }
                } else {
                        if ( !$this->mTrxLevel ) {
                                return; // nothing to do
-                       } elseif( !$this->mTrxAutomatic ) {
+                       } elseif ( !$this->mTrxAutomatic ) {
                                wfWarn( "$fname: Flushing an explicit transaction, getting out of sync!" );
                        }
                }
 
+               $this->runOnTransactionPreCommitCallbacks();
                $this->doCommit( $fname );
                $this->runOnTransactionIdleCallbacks();
        }
@@ -3113,12 +3158,13 @@ abstract class DatabaseBase implements DatabaseType {
         *
         * @param $fname string
         */
-       final public function rollback( $fname = 'DatabaseBase::rollback' ) {
+       final public function rollback( $fname = __METHOD__ ) {
                if ( !$this->mTrxLevel ) {
                        wfWarn( "$fname: No transaction to rollback, something got out of sync!" );
                }
                $this->doRollback( $fname );
                $this->mTrxIdleCallbacks = array(); // cancel
+               $this->mTrxPreCommitCallbacks = array(); // cancel
        }
 
        /**
@@ -3150,7 +3196,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @return Boolean: true if operation was successful
         */
        public function duplicateTableStructure( $oldName, $newName, $temporary = false,
-               $fname = 'DatabaseBase::duplicateTableStructure'
+               $fname = __METHOD__
        ) {
                throw new MWException(
                        'DatabaseBase::duplicateTableStructure is not implemented in descendant class' );
@@ -3163,7 +3209,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @param string $fname calling function name
         * @throws MWException
         */
-       function listTables( $prefix = null, $fname = 'DatabaseBase::listTables' ) {
+       function listTables( $prefix = null, $fname = __METHOD__ ) {
                throw new MWException( 'DatabaseBase::listTables is not implemented in descendant class' );
        }
 
@@ -3387,7 +3433,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @return bool|string
         */
        public function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
-               $fname = 'DatabaseBase::sourceStream', $inputCallback = false )
+               $fname = __METHOD__, $inputCallback = false )
        {
                $cmd = '';
 
@@ -3626,12 +3672,12 @@ abstract class DatabaseBase implements DatabaseType {
         * @return bool|ResultWrapper
         * @since 1.18
         */
-       public function dropTable( $tableName, $fName = 'DatabaseBase::dropTable' ) {
-               if( !$this->tableExists( $tableName, $fName ) ) {
+       public function dropTable( $tableName, $fName = __METHOD__ ) {
+               if ( !$this->tableExists( $tableName, $fName ) ) {
                        return false;
                }
                $sql = "DROP TABLE " . $this->tableName( $tableName );
-               if( $this->cascadingDeletes() ) {
+               if ( $this->cascadingDeletes() ) {
                        $sql .= " CASCADE";
                }
                return $this->query( $sql, $fName );
@@ -3704,8 +3750,8 @@ abstract class DatabaseBase implements DatabaseType {
        }
 
        public function __destruct() {
-               if ( count( $this->mTrxIdleCallbacks ) ) { // sanity
-                       trigger_error( "Transaction idle callbacks still pending." );
+               if ( count( $this->mTrxIdleCallbacks ) || count( $this->mTrxPreCommitCallbacks ) ) {
+                       trigger_error( "Transaction idle or pre-commit callbacks still pending." ); // sanity
                }
        }
 }