Revert "Replace wfMsgReplaceArgs by RawMessage"
[lhc/web/wiklou.git] / includes / db / Database.php
index 42c94f0..29642d0 100644 (file)
@@ -272,9 +272,20 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * Either a short hexidecimal string if a transaction is active or ""
         *
         * @var string
+        * @see DatabaseBase::mTrxLevel
         */
        protected $mTrxShortId = '';
 
+       /**
+        * The UNIX time that the transaction started. Callers can assume that if
+        * snapshot isolation is used, then the data is *at least* up to date to that
+        * point (possibly more up-to-date since the first SELECT defines the snapshot).
+        *
+        * @var float|null
+        * @see DatabaseBase::mTrxLevel
+        */
+       private $mTrxTimestamp = null;
+
        /**
         * Remembers the function name given for starting the most recent transaction via begin().
         * Used to provide additional context for error reporting.
@@ -419,6 +430,19 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                return $this->mTrxLevel;
        }
 
+       /**
+        * Get the UNIX timestamp of the time that the transaction was established
+        *
+        * This can be used to reason about the staleness of SELECT data
+        * in REPEATABLE-READ transaction isolation level.
+        *
+        * @return float|null Returns null if there is not active transaction
+        * @since 1.25
+        */
+       public function trxTimestamp() {
+               return $this->mTrxLevel ? $this->mTrxTimestamp : null;
+       }
+
        /**
         * Get/set the number of errors logged. Only useful when errors are ignored
         * @param int $count The count to set, or omitted to leave it unchanged.
@@ -679,8 +703,6 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         *   - DBO_DEBUG: output some debug info (same as debug())
         *   - DBO_NOBUFFER: don't buffer results (inverse of bufferResults())
         *   - DBO_TRX: automatically start transactions
-        *   - DBO_DEFAULT: automatically sets DBO_TRX if not in command line mode
-        *       and removes it in command line mode
         *   - DBO_PERSISTENT: use persistant database connection
         * @return bool
         */
@@ -710,19 +732,42 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
        }
 
        /**
-        * Return a path to the DBMS-specific schema file, otherwise default to tables.sql
+        * Return a path to the DBMS-specific SQL file if it exists,
+        * otherwise default SQL file
         *
+        * @param string $filename
         * @return string
         */
-       public function getSchemaPath() {
+       private function getSqlFilePath( $filename ) {
                global $IP;
-               if ( file_exists( "$IP/maintenance/" . $this->getType() . "/tables.sql" ) ) {
-                       return "$IP/maintenance/" . $this->getType() . "/tables.sql";
+               $dbmsSpecificFilePath = "$IP/maintenance/" . $this->getType() . "/$filename";
+               if ( file_exists( $dbmsSpecificFilePath ) ) {
+                       return $dbmsSpecificFilePath;
                } else {
-                       return "$IP/maintenance/tables.sql";
+                       return "$IP/maintenance/$filename";
                }
        }
 
+       /**
+        * Return a path to the DBMS-specific schema file,
+        * otherwise default to tables.sql
+        *
+        * @return string
+        */
+       public function getSchemaPath() {
+               return $this->getSqlFilePath( 'tables.sql' );
+       }
+
+       /**
+        * Return a path to the DBMS-specific update key file,
+        * otherwise default to update-keys.sql
+        *
+        * @return string
+        */
+       public function getUpdateKeysPath() {
+               return $this->getSqlFilePath( 'update-keys.sql' );
+       }
+
 # ------------------------------------------------------------------------------
 # Other functions
 # ------------------------------------------------------------------------------
@@ -1731,7 +1776,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
        }
 
        /**
-        * Estimate rows in dataset.
+        * Estimate the number of rows in dataset
         *
         * MySQL allows you to estimate the number of rows that would be returned
         * by a SELECT query, using EXPLAIN SELECT. The estimate is provided using
@@ -1750,8 +1795,8 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @param array $options Options for select
         * @return int Row count
         */
-       public function estimateRowCount( $table, $vars = '*', $conds = '',
-               $fname = __METHOD__, $options = array()
+       public function estimateRowCount(
+               $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = array()
        ) {
                $rows = 0;
                $res = $this->select( $table, array( 'rowcount' => 'COUNT(*)' ), $conds, $fname, $options );
@@ -1764,6 +1809,36 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                return $rows;
        }
 
+       /**
+        * Get the number of rows in dataset
+        *
+        * This is useful when trying to do COUNT(*) but with a LIMIT for performance.
+        *
+        * Takes the same arguments as DatabaseBase::select().
+        *
+        * @param string $table Table name
+        * @param string $vars Unused
+        * @param array|string $conds Filters on the table
+        * @param string $fname Function name for profiling
+        * @param array $options Options for select
+        * @return int Row count
+        * @since 1.24
+        */
+       public function selectRowCount(
+               $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = array()
+       ) {
+               $rows = 0;
+               $sql = $this->selectSQLText( $table, '1', $conds, $fname, $options );
+               $res = $this->query( "SELECT COUNT(*) AS rowcount FROM ($sql) tmp_count" );
+
+               if ( $res ) {
+                       $row = $this->fetchRow( $res );
+                       $rows = ( isset( $row['rowcount'] ) ) ? $row['rowcount'] : 0;
+               }
+
+               return $rows;
+       }
+
        /**
         * Removes most variables from an SQL query and replaces them with X or N for numbers.
         * It's only slightly flawed. Don't use for anything important.
@@ -3461,6 +3536,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                }
 
                $this->doBegin( $fname );
+               $this->mTrxTimestamp = microtime( true );
                $this->mTrxFname = $fname;
                $this->mTrxDoneWrites = false;
                $this->mTrxAutomatic = false;