Merge "Remove unused 'XMPGetInfo' and 'XMPGetResults' hooks"
[lhc/web/wiklou.git] / includes / db / Database.php
index 511edf8..97073dd 100644 (file)
@@ -69,6 +69,8 @@ abstract class DatabaseBase implements IDatabase {
        protected $mLBInfo = array();
        protected $mDefaultBigSelects = null;
        protected $mSchemaVars = false;
+       /** @var array */
+       protected $mSessionVars = array();
 
        protected $preparedArgs;
 
@@ -141,6 +143,13 @@ abstract class DatabaseBase implements IDatabase {
         */
        private $mTrxAutomaticAtomic = false;
 
+       /**
+        * Track the seconds spent in write queries for the current transaction
+        *
+        * @var float
+        */
+       private $mTrxWriteDuration = 0.0;
+
        /**
         * @since 1.21
         * @var resource File handle for upgrade
@@ -473,6 +482,18 @@ abstract class DatabaseBase implements IDatabase {
                );
        }
 
+       /**
+        * Get the time spend running write queries for this
+        *
+        * High times could be due to scanning, updates, locking, and such
+        *
+        * @return float|bool Returns false if not transaction is active
+        * @since 1.26
+        */
+       public function pendingWriteQueryDuration() {
+               return $this->mTrxLevel ? $this->mTrxWriteDuration : false;
+       }
+
        /**
         * Is a connection to the database open?
         * @return bool
@@ -794,15 +815,17 @@ abstract class DatabaseBase implements IDatabase {
                        }
                }
 
+               $this->mSessionVars = $params['variables'];
+
                /** Get the default table prefix*/
-               if ( $tablePrefix == 'get from global' ) {
+               if ( $tablePrefix === 'get from global' ) {
                        $this->mTablePrefix = $wgDBprefix;
                } else {
                        $this->mTablePrefix = $tablePrefix;
                }
 
                /** Get the database schema*/
-               if ( $schema == 'get from global' ) {
+               if ( $schema === 'get from global' ) {
                        $this->mSchema = $wgDBmwschema;
                } else {
                        $this->mSchema = $schema;
@@ -907,6 +930,7 @@ abstract class DatabaseBase implements IDatabase {
                        $p['password'] = isset( $p['password'] ) ? $p['password'] : false;
                        $p['dbname'] = isset( $p['dbname'] ) ? $p['dbname'] : false;
                        $p['flags'] = isset( $p['flags'] ) ? $p['flags'] : 0;
+                       $p['variables'] = isset( $p['variables'] ) ? $p['variables'] : array();
                        $p['tablePrefix'] = isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : 'get from global';
                        $p['schema'] = isset( $p['schema'] ) ? $p['schema'] : $defaultSchemas[$dbType];
                        $p['foreign'] = isset( $p['foreign'] ) ? $p['foreign'] : false;
@@ -1160,6 +1184,7 @@ abstract class DatabaseBase implements IDatabase {
                # Do the query and handle errors
                $startTime = microtime( true );
                $ret = $this->doQuery( $commentedSql );
+               $queryRuntime = microtime( true ) - $startTime;
                # Log the query time and feed it into the DB trx profiler
                $this->getTransactionProfiler()->recordQueryCompletion(
                        $queryProf, $startTime, $isWriteQuery, $this->affectedRows() );
@@ -1191,6 +1216,7 @@ abstract class DatabaseBase implements IDatabase {
                                        # Should be safe to silently retry (no trx and thus no callbacks)
                                        $startTime = microtime( true );
                                        $ret = $this->doQuery( $commentedSql );
+                                       $queryRuntime = microtime( true ) - $startTime;
                                        # Log the query time and feed it into the DB trx profiler
                                        $this->getTransactionProfiler()->recordQueryCompletion(
                                                $queryProf, $startTime, $isWriteQuery, $this->affectedRows() );
@@ -1211,6 +1237,10 @@ abstract class DatabaseBase implements IDatabase {
                $queryProfSection = false;
                $totalProfSection = false;
 
+               if ( $isWriteQuery && $this->mTrxLevel ) {
+                       $this->mTrxWriteDuration += $queryRuntime;
+               }
+
                return $res;
        }
 
@@ -2455,7 +2485,7 @@ abstract class DatabaseBase implements IDatabase {
                }
 
                # Quote $schema and merge it with the table name if needed
-               if ( $schema !== null ) {
+               if ( strlen( $schema ) ) {
                        if ( $format == 'quoted' && !$this->isQuotedIdentifier( $schema ) ) {
                                $schema = $this->addIdentifierQuotes( $schema );
                        }
@@ -3609,11 +3639,12 @@ abstract class DatabaseBase implements IDatabase {
                        }
 
                        $this->runOnTransactionPreCommitCallbacks();
+                       $writeTime = $this->pendingWriteQueryDuration();
                        $this->doCommit( $fname );
                        if ( $this->mTrxDoneWrites ) {
                                $this->mDoneWrites = microtime( true );
                                $this->getTransactionProfiler()->transactionWritingOut(
-                                       $this->mServer, $this->mDBname, $this->mTrxShortId );
+                                       $this->mServer, $this->mDBname, $this->mTrxShortId, $writeTime );
                        }
                        $this->runOnTransactionIdleCallbacks();
                }
@@ -3633,6 +3664,7 @@ abstract class DatabaseBase implements IDatabase {
                $this->mTrxIdleCallbacks = array();
                $this->mTrxPreCommitCallbacks = array();
                $this->mTrxShortId = wfRandomString( 12 );
+               $this->mTrxWriteDuration = 0.0;
        }
 
        /**
@@ -3690,11 +3722,12 @@ abstract class DatabaseBase implements IDatabase {
                }
 
                $this->runOnTransactionPreCommitCallbacks();
+               $writeTime = $this->pendingWriteQueryDuration();
                $this->doCommit( $fname );
                if ( $this->mTrxDoneWrites ) {
                        $this->mDoneWrites = microtime( true );
                        $this->getTransactionProfiler()->transactionWritingOut(
-                               $this->mServer, $this->mDBname, $this->mTrxShortId );
+                               $this->mServer, $this->mDBname, $this->mTrxShortId, $writeTime );
                }
                $this->runOnTransactionIdleCallbacks();
        }
@@ -4209,7 +4242,7 @@ abstract class DatabaseBase implements IDatabase {
        }
 
        /**
-        * Check to see if a named lock is available. This is non-blocking.
+        * Check to see if a named lock is available (non-blocking)
         *
         * @param string $lockName Name of lock to poll
         * @param string $method Name of method calling us
@@ -4223,8 +4256,7 @@ abstract class DatabaseBase implements IDatabase {
        /**
         * Acquire a named lock
         *
-        * Abstracted from Filestore::lock() so child classes can implement for
-        * their own needs.
+        * Named locks are not related to transactions
         *
         * @param string $lockName Name of lock to aquire
         * @param string $method Name of method calling us
@@ -4236,7 +4268,9 @@ abstract class DatabaseBase implements IDatabase {
        }
 
        /**
-        * Release a lock.
+        * Release a lock
+        *
+        * Named locks are not related to transactions
         *
         * @param string $lockName Name of lock to release
         * @param string $method Name of method calling us
@@ -4249,6 +4283,16 @@ abstract class DatabaseBase implements IDatabase {
                return true;
        }
 
+       /**
+        * Check to see if a named lock used by lock() use blocking queues
+        *
+        * @return bool
+        * @since 1.26
+        */
+       public function namedLocksEnqueue() {
+               return false;
+       }
+
        /**
         * Lock specific tables
         *
@@ -4332,7 +4376,7 @@ abstract class DatabaseBase implements IDatabase {
         * @return string
         */
        public function decodeExpiry( $expiry, $format = TS_MW ) {
-               return ( $expiry == '' || $expiry == $this->getInfinity() )
+               return ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() )
                        ? 'infinity'
                        : wfTimestamp( $format, $expiry );
        }