Never prefix table names with $wgSharedDB when used in foreign DB
[lhc/web/wiklou.git] / includes / db / Database.php
index 63c9cfe..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
@@ -237,6 +236,7 @@ abstract class DatabaseBase implements DatabaseType {
 
        protected $mTablePrefix;
        protected $mFlags;
+       protected $mForeign;
        protected $mTrxLevel = 0;
        protected $mErrorCount = 0;
        protected $mLBInfo = array();
@@ -585,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" );
                }
        }
@@ -661,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;
 
@@ -690,6 +691,8 @@ abstract class DatabaseBase implements DatabaseType {
                        $this->mTablePrefix = $tablePrefix;
                }
 
+               $this->mForeign = $foreign;
+
                if ( $user ) {
                        $this->open( $server, $user, $password, $dbName );
                }
@@ -739,7 +742,8 @@ abstract class DatabaseBase implements DatabaseType {
                                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;
@@ -772,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;
        }
 
@@ -870,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
@@ -945,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
@@ -1147,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 ) ) {
@@ -1438,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 );
 
@@ -1461,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 ) ) {
@@ -1528,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;
@@ -1569,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 );
@@ -1618,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;
@@ -1635,7 +1632,7 @@ abstract class DatabaseBase implements DatabaseType {
         *
         * @return bool|null
         */
-       public function indexExists( $table, $index, $fname = 'DatabaseBase::indexExists' ) {
+       public function indexExists( $table, $index, $fname = __METHOD__ ) {
                if ( !$this->tableExists( $table ) ) {
                        return null;
                }
@@ -1740,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;
@@ -1839,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 );
@@ -2076,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
                        ) {
@@ -2456,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 ) {
@@ -2559,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,
@@ -2625,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' );
                }
@@ -2667,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 );
@@ -2859,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];
@@ -2876,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 );
                                }
@@ -2966,6 +2964,7 @@ 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 when separate transactions are needed.
@@ -3063,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
@@ -3118,7 +3117,7 @@ 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!" );
@@ -3159,7 +3158,7 @@ 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!" );
                }
@@ -3197,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' );
@@ -3210,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' );
        }
 
@@ -3434,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 = '';
 
@@ -3673,7 +3672,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @return bool|ResultWrapper
         * @since 1.18
         */
-       public function dropTable( $tableName, $fName = 'DatabaseBase::dropTable' ) {
+       public function dropTable( $tableName, $fName = __METHOD__ ) {
                if ( !$this->tableExists( $tableName, $fName ) ) {
                        return false;
                }