Merge "Add class around diff-empty and add it as notice"
[lhc/web/wiklou.git] / includes / db / Database.php
index a86d6be..27e6dad 100644 (file)
@@ -204,11 +204,17 @@ interface DatabaseType {
        function getServerInfo();
 }
 
+/**
+ * Interface for classes that implement or wrap DatabaseBase
+ * @ingroup Database
+ */
+interface IDatabase {}
+
 /**
  * Database abstraction object
  * @ingroup Database
  */
-abstract class DatabaseBase implements DatabaseType {
+abstract class DatabaseBase implements IDatabase, 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 */
@@ -236,6 +242,7 @@ abstract class DatabaseBase implements DatabaseType {
 
        protected $mTablePrefix;
        protected $mFlags;
+       protected $mForeign;
        protected $mTrxLevel = 0;
        protected $mErrorCount = 0;
        protected $mLBInfo = array();
@@ -660,9 +667,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 +697,8 @@ abstract class DatabaseBase implements DatabaseType {
                        $this->mTablePrefix = $tablePrefix;
                }
 
+               $this->mForeign = $foreign;
+
                if ( $user ) {
                        $this->open( $server, $user, $password, $dbName );
                }
@@ -738,7 +748,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;
@@ -871,22 +882,7 @@ abstract class DatabaseBase implements DatabaseType {
         *     for a successful read query, or false on failure if $tempIgnore set
         */
        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
-                       # logging size most of the time. The substr is really just a sanity check.
-
-                       if ( $isMaster ) {
-                               $queryProf = 'query-m: ' . substr( DatabaseBase::generalizeSQL( $sql ), 0, 255 );
-                               $totalProf = 'DatabaseBase::query-master';
-                       } else {
-                               $queryProf = 'query: ' . substr( DatabaseBase::generalizeSQL( $sql ), 0, 255 );
-                               $totalProf = 'DatabaseBase::query';
-                       }
-
-                       wfProfileIn( $totalProf );
-                       wfProfileIn( $queryProf );
-               }
+               global $wgUser, $wgDebugDBTransactions;
 
                $this->mLastQuery = $sql;
                if ( !$this->mDoneWrites && $this->isWriteQuery( $sql ) ) {
@@ -896,7 +892,6 @@ abstract class DatabaseBase implements DatabaseType {
                }
 
                # Add a comment for easy SHOW PROCESSLIST interpretation
-               global $wgUser;
                if ( is_object( $wgUser ) && $wgUser->isItemLoaded( 'name' ) ) {
                        $userName = $wgUser->getName();
                        if ( mb_strlen( $userName ) > 15 ) {
@@ -920,7 +915,6 @@ abstract class DatabaseBase implements DatabaseType {
                        # is really used by application
                        $sqlstart = substr( $sql, 0, 10 ); // very much worth it, benchmark certified(tm)
                        if ( strpos( $sqlstart, "SHOW " ) !== 0 && strpos( $sqlstart, "SET " ) !== 0 ) {
-                               global $wgDebugDBTransactions;
                                if ( $wgDebugDBTransactions ) {
                                        wfDebug( "Implicit transaction start.\n" );
                                }
@@ -932,6 +926,22 @@ abstract class DatabaseBase implements DatabaseType {
                # Keep track of whether the transaction has write queries pending
                if ( $this->mTrxLevel && !$this->mTrxDoneWrites && $this->isWriteQuery( $sql ) ) {
                        $this->mTrxDoneWrites = true;
+                       Profiler::instance()->transactionWritingIn( $this->mServer, $this->mDBname );
+               }
+
+               $isMaster = !is_null( $this->getLBInfo( 'master' ) );
+               if ( !Profiler::instance()->isStub() ) {
+                       # generalizeSQL will probably cut down the query to reasonable
+                       # logging size most of the time. The substr is really just a sanity check.
+                       if ( $isMaster ) {
+                               $queryProf = 'query-m: ' . substr( DatabaseBase::generalizeSQL( $sql ), 0, 255 );
+                               $totalProf = 'DatabaseBase::query-master';
+                       } else {
+                               $queryProf = 'query: ' . substr( DatabaseBase::generalizeSQL( $sql ), 0, 255 );
+                               $totalProf = 'DatabaseBase::query';
+                       }
+                       wfProfileIn( $totalProf );
+                       wfProfileIn( $queryProf );
                }
 
                if ( $this->debug() ) {
@@ -1595,7 +1605,8 @@ abstract class DatabaseBase implements DatabaseType {
                $sql = preg_replace( '/\s+/', ' ', $sql );
 
                # All numbers => N
-               $sql = preg_replace( '/-?[0-9]+/s', 'N', $sql );
+               $sql = preg_replace( '/-?\d+(,-?\d+)+/s', 'N,...,N', $sql );
+               $sql = preg_replace( '/-?\d+/s', 'N', $sql );
 
                return $sql;
        }
@@ -2066,6 +2077,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
                        ) {
@@ -3097,10 +3109,9 @@ abstract class DatabaseBase implements DatabaseType {
                        foreach ( $callbacks as $callback ) {
                                try {
                                        $this->clearFlag( DBO_TRX ); // make each query its own transaction
-                                       $callback();
+                                       call_user_func( $callback );
                                        $this->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore automatic begin()
-                               } catch ( Exception $e ) {
-                               }
+                               } catch ( Exception $e ) {}
                        }
                } while ( count( $this->mTrxIdleCallbacks ) );
 
@@ -3121,7 +3132,7 @@ abstract class DatabaseBase implements DatabaseType {
                        $this->mTrxPreCommitCallbacks = array(); // recursion guard
                        foreach ( $callbacks as $callback ) {
                                try {
-                                       $callback();
+                                       call_user_func( $callback );
                                } catch ( Exception $e ) {}
                        }
                } while ( count( $this->mTrxPreCommitCallbacks ) );
@@ -3166,6 +3177,9 @@ abstract class DatabaseBase implements DatabaseType {
 
                        $this->runOnTransactionPreCommitCallbacks();
                        $this->doCommit( $fname );
+                       if ( $this->mTrxDoneWrites ) {
+                               Profiler::instance()->transactionWritingOut( $this->mServer, $this->mDBname );
+                       }
                        $this->runOnTransactionIdleCallbacks();
                }
 
@@ -3215,6 +3229,9 @@ abstract class DatabaseBase implements DatabaseType {
 
                $this->runOnTransactionPreCommitCallbacks();
                $this->doCommit( $fname );
+               if ( $this->mTrxDoneWrites ) {
+                       Profiler::instance()->transactionWritingOut( $this->mServer, $this->mDBname );
+               }
                $this->runOnTransactionIdleCallbacks();
        }
 
@@ -3246,6 +3263,9 @@ abstract class DatabaseBase implements DatabaseType {
                $this->doRollback( $fname );
                $this->mTrxIdleCallbacks = array(); // cancel
                $this->mTrxPreCommitCallbacks = array(); // cancel
+               if ( $this->mTrxDoneWrites ) {
+                       Profiler::instance()->transactionWritingOut( $this->mServer, $this->mDBname );
+               }
        }
 
        /**