SpecialStatistics: Use Config instead of globals
[lhc/web/wiklou.git] / includes / db / Database.php
index 7d8fbe9..62d64eb 100644 (file)
@@ -600,7 +600,18 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @return bool
         */
        public function doneWrites() {
-               return $this->mDoneWrites;
+               return (bool)$this->mDoneWrites;
+       }
+
+       /**
+        * Returns the last time the connection may have been used for write queries.
+        * Should return a timestamp if unsure.
+        *
+        * @return int|float UNIX timestamp or false
+        * @since 1.24
+        */
+       public function lastDoneWrites() {
+               return $this->mDoneWrites ?: false;
        }
 
        /**
@@ -1017,13 +1028,13 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         *     for a successful read query, or false on failure if $tempIgnore set
         */
        public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) {
-               global $wgUser, $wgDebugDBTransactions;
+               global $wgUser, $wgDebugDBTransactions, $wgDebugDumpSqlLength;
 
                $this->mLastQuery = $sql;
-               if ( !$this->mDoneWrites && $this->isWriteQuery( $sql ) ) {
+               if ( $this->isWriteQuery( $sql ) ) {
                        # Set a flag indicating that writes have been done
                        wfDebug( __METHOD__ . ': Writes done: ' . DatabaseBase::generalizeSQL( $sql ) . "\n" );
-                       $this->mDoneWrites = true;
+                       $this->mDoneWrites = microtime( true );
                }
 
                # Add a comment for easy SHOW PROCESSLIST interpretation
@@ -1091,7 +1102,8 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                        static $cnt = 0;
 
                        $cnt++;
-                       $sqlx = substr( $commentedSql, 0, 500 );
+                       $sqlx = $wgDebugDumpSqlLength ? substr( $commentedSql, 0, $wgDebugDumpSqlLength )
+                               : $commentedSql;
                        $sqlx = strtr( $sqlx, "\t\n", '  ' );
 
                        $master = $isMaster ? 'master' : 'slave';
@@ -1122,7 +1134,8 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                        if ( $this->ping() ) {
                                global $wgRequestTime;
                                wfDebug( "Reconnected\n" );
-                               $sqlx = substr( $commentedSql, 0, 500 );
+                               $sqlx = $wgDebugDumpSqlLength ? substr( $commentedSql, 0, $wgDebugDumpSqlLength )
+                                       : $commentedSql;
                                $sqlx = strtr( $sqlx, "\t\n", '  ' );
                                $elapsed = round( microtime( true ) - $wgRequestTime, 3 );
                                if ( $elapsed < 300 ) {
@@ -1477,6 +1490,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @param array $options Query options
         * @param array $join_conds Join conditions
         *
+        *
         * @param string|array $table
         *
         * May be either an array of table names, or a single string holding a table
@@ -1769,9 +1783,10 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                # All newlines, tabs, etc replaced by single space
                $sql = preg_replace( '/\s+/', ' ', $sql );
 
-               # All numbers => N
+               # All numbers => N,
+               # except the ones surrounded by characters, e.g. l10n
                $sql = preg_replace( '/-?\d+(,-?\d+)+/s', 'N,...,N', $sql );
-               $sql = preg_replace( '/-?\d+/s', 'N', $sql );
+               $sql = preg_replace( '/(?<![a-zA-Z])-?\d+(?![a-zA-Z])/s', 'N', $sql );
 
                return $sql;
        }
@@ -1886,7 +1901,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         *   DatabaseBase::tableName().
         * @param array $a Array of rows to insert
         * @param string $fname Calling function name (use __METHOD__) for logs/profiling
-        * @param array $options of options
+        * @param array $options Array of options
         *
         * @return bool
         */
@@ -2081,11 +2096,11 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * Build a partial where clause from a 2-d array such as used for LinkBatch.
         * The keys on each level may be either integers or strings.
         *
-        * @param array $data organized as 2-d
+        * @param array $data Organized as 2-d
         *    array(baseKeyVal => array(subKeyVal => [ignored], ...), ...)
         * @param string $baseKey Field name to match the base-level keys to (eg 'pl_namespace')
         * @param string $subKey Field name to match the sub-level keys to (eg 'pl_title')
-        * @return string|bool string SQL fragment, or false if no items in array.
+        * @return string|bool SQL fragment, or false if no items in array
         */
        public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
                $conds = array();
@@ -2371,7 +2386,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
        /**
         * Gets an array of aliased table names
         *
-        * @param array $tables array( [alias] => table )
+        * @param array $tables Array( [alias] => table )
         * @return string[] See tableNameWithAlias()
         */
        public function tableNamesWithAlias( $tables ) {
@@ -2405,7 +2420,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
        /**
         * Gets an array of aliased field names
         *
-        * @param array $fields array( [alias] => field )
+        * @param array $fields Array( [alias] => field )
         * @return string[] See fieldNameWithAlias()
         */
        public function fieldNamesWithAlias( $fields ) {
@@ -2906,9 +2921,9 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * DELETE query wrapper.
         *
         * @param array $table Table name
-        * @param string|array $conds of conditions. See $conds in DatabaseBase::select()
+        * @param string|array $conds Array of conditions. See $conds in DatabaseBase::select()
         *   for the format. Use $conds == "*" to delete all rows
-        * @param string $fname name of the calling function
+        * @param string $fname Name of the calling function
         * @throws DBUnexpectedError
         * @return bool|ResultWrapper
         */
@@ -3197,7 +3212,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @param DBMasterPos $pos
         * @param int $timeout The maximum number of seconds to wait for
         *   synchronisation
-        * @return int Zzero if the slave was past that position already,
+        * @return int Zero if the slave was past that position already,
         *   greater than zero if we waited for some period of time, less than
         *   zero if we timed out.
         */
@@ -3274,7 +3289,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
        protected function runOnTransactionIdleCallbacks() {
                $autoTrx = $this->getFlag( DBO_TRX ); // automatic begin() enabled?
 
-               $e = null; // last exception
+               $e = $ePrior = null; // last exception
                do { // callbacks may add callbacks :)
                        $callbacks = $this->mTrxIdleCallbacks;
                        $this->mTrxIdleCallbacks = array(); // recursion guard
@@ -3285,6 +3300,10 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                                        call_user_func( $phpCallback );
                                        $this->setFlag( $autoTrx ? DBO_TRX : 0 ); // restore automatic begin()
                                } catch ( Exception $e ) {
+                                       if ( $ePrior ) {
+                                               MWExceptionHandler::logException( $ePrior );
+                                       }
+                                       $ePrior = $e;
                                }
                        }
                } while ( count( $this->mTrxIdleCallbacks ) );
@@ -3300,7 +3319,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
         * @since 1.22
         */
        protected function runOnTransactionPreCommitCallbacks() {
-               $e = null; // last exception
+               $e = $ePrior = null; // last exception
                do { // callbacks may add callbacks :)
                        $callbacks = $this->mTrxPreCommitCallbacks;
                        $this->mTrxPreCommitCallbacks = array(); // recursion guard
@@ -3309,6 +3328,10 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
                                        list( $phpCallback ) = $callback;
                                        call_user_func( $phpCallback );
                                } catch ( Exception $e ) {
+                                       if ( $ePrior ) {
+                                               MWExceptionHandler::logException( $ePrior );
+                                       }
+                                       $ePrior = $e;
                                }
                        }
                } while ( count( $this->mTrxPreCommitCallbacks ) );
@@ -4138,7 +4161,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType {
        /**
         * Encode an expiry time into the DBMS dependent format
         *
-        * @param string $expiry timestamp for expiry, or the 'infinity' string
+        * @param string $expiry Timestamp for expiry, or the 'infinity' string
         * @return string
         */
        public function encodeExpiry( $expiry ) {