Merge "(bug 47070) check content model namespace on import."
[lhc/web/wiklou.git] / includes / db / DatabaseMysqlBase.php
index 14369bf..647a42a 100644 (file)
@@ -33,6 +33,11 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /** @var MysqlMasterPos */
        protected $lastKnownSlavePos;
 
+       /** @var null|int */
+       protected $mFakeSlaveLag = null;
+
+       protected $mFakeMaster = false;
+
        /**
         * @return string
         */
@@ -41,12 +46,12 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        }
 
        /**
-        * @param $server string
-        * @param $user string
-        * @param $password string
-        * @param $dbName string
+        * @param string $server
+        * @param string $user
+        * @param string $password
+        * @param string $dbName
+        * @throws Exception|DBConnectionError
         * @return bool
-        * @throws DBConnectionError
         */
        function open( $server, $user, $password, $dbName ) {
                global $wgAllDBsAreLocalhost, $wgSQLMode;
@@ -155,7 +160,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Open a connection to a MySQL server
         *
-        * @param $realServer string
+        * @param string $realServer
         * @return mixed Raw connection
         * @throws DBConnectionError
         */
@@ -170,7 +175,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        abstract protected function mysqlSetCharset( $charset );
 
        /**
-        * @param $res ResultWrapper
+        * @param ResultWrapper|resource $res
         * @throws DBUnexpectedError
         */
        function freeResult( $res ) {
@@ -188,14 +193,14 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Free result memory
         *
-        * @param $res Raw result
+        * @param resource $res Raw result
         * @return bool
         */
        abstract protected function mysqlFreeResult( $res );
 
        /**
-        * @param $res ResultWrapper
-        * @return object|bool
+        * @param ResultWrapper|resource $res
+        * @return stdClass|bool
         * @throws DBUnexpectedError
         */
        function fetchObject( $res ) {
@@ -224,13 +229,13 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Fetch a result row as an object
         *
-        * @param $res Raw result
+        * @param resource $res Raw result
         * @return stdClass
         */
        abstract protected function mysqlFetchObject( $res );
 
        /**
-        * @param $res ResultWrapper
+        * @param ResultWrapper|resource $res
         * @return array|bool
         * @throws DBUnexpectedError
         */
@@ -260,14 +265,14 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Fetch a result row as an associative and numeric array
         *
-        * @param $res Raw result
+        * @param resource $res Raw result
         * @return array
         */
        abstract protected function mysqlFetchArray( $res );
 
        /**
         * @throws DBUnexpectedError
-        * @param $res ResultWrapper
+        * @param ResultWrapper|resource $res
         * @return int
         */
        function numRows( $res ) {
@@ -289,13 +294,13 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Get number of rows in result
         *
-        * @param $res Raw result
+        * @param resource $res Raw result
         * @return int
         */
        abstract protected function mysqlNumRows( $res );
 
        /**
-        * @param $res ResultWrapper
+        * @param ResultWrapper|resource $res
         * @return int
         */
        function numFields( $res ) {
@@ -309,13 +314,13 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Get number of fields in result
         *
-        * @param $res Raw result
+        * @param resource $res Raw result
         * @return int
         */
        abstract protected function mysqlNumFields( $res );
 
        /**
-        * @param $res ResultWrapper
+        * @param ResultWrapper|resource $res
         * @param $n int
         * @return string
         */
@@ -330,7 +335,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Get the name of the specified field in a result
         *
-        * @param $res Raw result
+        * @param ResultWrapper|resource $res
         * @param $n int
         * @return string
         */
@@ -338,7 +343,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
 
        /**
         * mysql_field_type() wrapper
-        * @param $res
+        * @param ResultWrapper|resource $res
         * @param $n int
         * @return string
         */
@@ -353,15 +358,15 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Get the type of the specified field in a result
         *
-        * @param $res Raw result
-        * @param $n int
+        * @param ResultWrapper|resource $res
+        * @param int $n
         * @return string
         */
        abstract protected function mysqlFieldType( $res, $n );
 
        /**
-        * @param $res ResultWrapper
-        * @param $row
+        * @param ResultWrapper|resource $res
+        * @param int $row
         * @return bool
         */
        function dataSeek( $res, $row ) {
@@ -375,8 +380,8 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Move internal result pointer
         *
-        * @param $res Raw result
-        * @param $row int
+        * @param ResultWrapper|resource $res
+        * @param int $row
         * @return bool
         */
        abstract protected function mysqlDataSeek( $res, $row );
@@ -406,16 +411,16 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Returns the text of the error message from previous MySQL operation
         *
-        * @param $conn Raw connection
+        * @param resource $conn Raw connection
         * @return string
         */
        abstract protected function mysqlError( $conn = null );
 
        /**
-        * @param $table string
-        * @param $uniqueIndexes
-        * @param $rows array
-        * @param $fname string
+        * @param string $table
+        * @param array $uniqueIndexes
+        * @param array $rows
+        * @param string $fname
         * @return ResultWrapper
         */
        function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
@@ -427,12 +432,12 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         * Returns estimated count, based on EXPLAIN output
         * Takes same arguments as Database::select()
         *
-        * @param $table string|array
-        * @param $vars string|array
-        * @param $conds string|array
-        * @param $fname string
-        * @param $options string|array
-        * @return int
+        * @param string|array $table
+        * @param string|array $vars
+        * @param string|array $conds
+        * @param string $fname
+        * @param string|array $options
+        * @return bool|int
         */
        public function estimateRowCount( $table, $vars = '*', $conds = '',
                $fname = __METHOD__, $options = array()
@@ -455,8 +460,8 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        }
 
        /**
-        * @param $table string
-        * @param $field string
+        * @param string $table
+        * @param string $field
         * @return bool|MySQLField
         */
        function fieldInfo( $table, $field ) {
@@ -479,8 +484,8 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Get column information from a result
         *
-        * @param $res Raw result
-        * @param $n int
+        * @param resource $res Raw result
+        * @param int $n
         * @return stdClass
         */
        abstract protected function mysqlFetchField( $res, $n );
@@ -489,9 +494,9 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         * Get information about an index into an object
         * Returns false if the index does not exist
         *
-        * @param $table string
-        * @param $index string
-        * @param $fname string
+        * @param string $table
+        * @param string $index
+        * @param string $fname
         * @return bool|array|null False or null on failure
         */
        function indexInfo( $table, $index, $fname = __METHOD__ ) {
@@ -520,8 +525,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        }
 
        /**
-        * @param $s string
-        *
+        * @param string $s
         * @return string
         */
        function strencode( $s ) {
@@ -538,8 +542,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * MySQL uses `backticks` for identifier quoting instead of the sql standard "double quotes".
         *
-        * @param $s string
-        *
+        * @param string $s
         * @return string
         */
        public function addIdentifierQuotes( $s ) {
@@ -549,7 +552,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        }
 
        /**
-        * @param $name string
+        * @param string $name
         * @return bool
         */
        public function isQuotedIdentifier( $name ) {
@@ -580,6 +583,24 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         */
        abstract protected function mysqlPing();
 
+       /**
+        * Set lag time in seconds for a fake slave
+        *
+        * @param int $lag
+        */
+       public function setFakeSlaveLag( $lag ) {
+               $this->mFakeSlaveLag = $lag;
+       }
+
+       /**
+        * Make this connection a fake master
+        *
+        * @param bool $enabled
+        */
+       public function setFakeMaster( $enabled = true ) {
+               $this->mFakeMaster = $enabled;
+       }
+
        /**
         * Returns slave lag.
         *
@@ -659,11 +680,13 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
 
        /**
         * Wait for the slave to catch up to a given master position.
-        * @TODO: return values for this and base class are rubbish
+        * @todo Return values for this and base class are rubbish
         *
-        * @param $pos DBMasterPos object
-        * @param $timeout Integer: the maximum number of seconds to wait for synchronisation
-        * @return bool|string
+        * @param DBMasterPos|MySQLMasterPos $pos
+        * @param int $timeout The maximum number of seconds to wait for synchronisation
+        * @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.
         */
        function masterPosWait( DBMasterPos $pos, $timeout ) {
                if ( $this->lastKnownSlavePos && $this->lastKnownSlavePos->hasReached( $pos ) ) {
@@ -675,10 +698,25 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                $this->commit( __METHOD__, 'flush' );
 
                if ( !is_null( $this->mFakeSlaveLag ) ) {
-                       $status = parent::masterPosWait( $pos, $timeout );
-                       wfProfileOut( __METHOD__ );
+                       $wait = intval( ( $pos->pos - microtime( true ) + $this->mFakeSlaveLag ) * 1e6 );
+
+                       if ( $wait > $timeout * 1e6 ) {
+                               wfDebug( "Fake slave timed out waiting for $pos ($wait us)\n" );
+                               wfProfileOut( __METHOD__ );
+
+                               return -1;
+                       } elseif ( $wait > 0 ) {
+                               wfDebug( "Fake slave waiting $wait us\n" );
+                               usleep( $wait );
+                               wfProfileOut( __METHOD__ );
 
-                       return $status;
+                               return 1;
+                       } else {
+                               wfDebug( "Fake slave up to date ($wait us)\n" );
+                               wfProfileOut( __METHOD__ );
+
+                               return 0;
+                       }
                }
 
                # Call doQuery() directly, to avoid opening a transaction if DBO_TRX is set
@@ -707,7 +745,10 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         */
        function getSlavePos() {
                if ( !is_null( $this->mFakeSlaveLag ) ) {
-                       return parent::getSlavePos();
+                       $pos = new MySQLMasterPos( 'fake', microtime( true ) - $this->mFakeSlaveLag );
+                       wfDebug( __METHOD__ . ": fake slave pos = $pos\n" );
+
+                       return $pos;
                }
 
                $res = $this->query( 'SHOW SLAVE STATUS', 'DatabaseBase::getSlavePos' );
@@ -731,7 +772,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         */
        function getMasterPos() {
                if ( $this->mFakeMaster ) {
-                       return parent::getMasterPos();
+                       return new MySQLMasterPos( 'fake', microtime( true ) );
                }
 
                $res = $this->query( 'SHOW MASTER STATUS', 'DatabaseBase::getMasterPos' );
@@ -745,7 +786,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        }
 
        /**
-        * @param $index
+        * @param string $index
         * @return string
         */
        function useIndexClause( $index ) {
@@ -763,11 +804,18 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         * @return string
         */
        public function getSoftwareLink() {
-               return '[http://www.mysql.com/ MySQL]';
+               $version = $this->getServerVersion();
+               if ( strpos( $version, 'MariaDB' ) !== false ) {
+                       return '[{{int:version-db-mariadb-url}} MariaDB]';
+               } elseif ( strpos( $version, 'percona' ) !== false ) {
+                       return '[{{int:version-db-percona-url}} Percona Server]';
+               } else {
+                       return '[{{int:version-db-mysql-url}} MySQL]';
+               }
        }
 
        /**
-        * @param $options array
+        * @param array $options
         */
        public function setSessionOptions( array $options ) {
                if ( isset( $options['connTimeout'] ) ) {
@@ -777,6 +825,11 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                }
        }
 
+       /**
+        * @param string $sql
+        * @param string $newLine
+        * @return bool
+        */
        public function streamStatementEnd( &$sql, &$newLine ) {
                if ( strtoupper( substr( $newLine, 0, 9 ) ) == 'DELIMITER' ) {
                        preg_match( '/^DELIMITER\s+(\S+)/', $newLine, $m );
@@ -792,7 +845,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         *
         * @param string $lockName name of lock to poll
         * @param string $method name of method calling us
-        * @return Boolean
+        * @return bool
         * @since 1.20
         */
        public function lockIsFree( $lockName, $method ) {
@@ -804,9 +857,9 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        }
 
        /**
-        * @param $lockName string
-        * @param $method string
-        * @param $timeout int
+        * @param string $lockName
+        * @param string $method
+        * @param int $timeout
         * @return bool
         */
        public function lock( $lockName, $method, $timeout = 5 ) {
@@ -826,8 +879,8 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * FROM MYSQL DOCS:
         * http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock
-        * @param $lockName string
-        * @param $method string
+        * @param string $lockName
+        * @param string $method
         * @return bool
         */
        public function unlock( $lockName, $method ) {
@@ -839,10 +892,10 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        }
 
        /**
-        * @param $read array
-        * @param $write array
-        * @param $method string
-        * @param $lowPriority bool
+        * @param array $read
+        * @param array $write
+        * @param string $method
+        * @param bool $lowPriority
         * @return bool
         */
        public function lockTables( $read, $write, $method, $lowPriority = true ) {
@@ -864,7 +917,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        }
 
        /**
-        * @param $method string
+        * @param string $method
         * @return bool
         */
        public function unlockTables( $method ) {
@@ -877,7 +930,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         * Get search engine class. All subclasses of this
         * need to implement this if they wish to use searching.
         *
-        * @return String
+        * @return string
         */
        public function getSearchEngine() {
                return 'SearchMySQL';
@@ -885,7 +938,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
 
        /**
         * @param bool $value
-        * @return mixed
+        * @return mixed null|bool|ResultWrapper
         */
        public function setBigSelects( $value = true ) {
                if ( $value === 'default' ) {
@@ -935,11 +988,10 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         * @param array $uniqueIndexes
         * @param array $set
         * @param string $fname
-        * @param array $options
         * @return bool
         */
-       public function upsert(
-               $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
+       public function upsert( $table, array $rows, array $uniqueIndexes,
+               array $set, $fname = __METHOD__
        ) {
                if ( !count( $rows ) ) {
                        return true; // nothing to do
@@ -1010,24 +1062,26 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        }
 
        /**
-        * @param $oldName
-        * @param $newName
-        * @param $temporary bool
-        * @param $fname string
+        * @param string $oldName
+        * @param string $newName
+        * @param bool $temporary
+        * @param string $fname
+        * @return bool
         */
        function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = __METHOD__ ) {
                $tmp = $temporary ? 'TEMPORARY ' : '';
                $newName = $this->addIdentifierQuotes( $newName );
                $oldName = $this->addIdentifierQuotes( $oldName );
                $query = "CREATE $tmp TABLE $newName (LIKE $oldName)";
-               $this->query( $query, $fname );
+
+               return $this->query( $query, $fname );
        }
 
        /**
         * List all tables on the database
         *
         * @param string $prefix Only show tables with this prefix, e.g. mw_
-        * @param string $fname calling function name
+        * @param string $fname Calling function name
         * @return array
         */
        function listTables( $prefix = null, $fname = __METHOD__ ) {
@@ -1078,7 +1132,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Get status information from SHOW STATUS in an associative array
         *
-        * @param $which string
+        * @param string $which
         * @return array
         */
        function getMysqlStatus( $which = "%" ) {
@@ -1134,7 +1188,8 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        /**
         * Differentiates between a TABLE and a VIEW.
         *
-        * @param $name string: Name of the TABLE/VIEW to test
+        * @param string $name Name of the TABLE/VIEW to test
+        * @param string $prefix
         * @return bool
         * @since 1.22
         */
@@ -1217,7 +1272,11 @@ class MySQLField implements Field {
 }
 
 class MySQLMasterPos implements DBMasterPos {
-       var $file, $pos;
+       /** @var string */
+       public $file;
+
+       /** @var int timestamp */
+       public $pos;
 
        function __construct( $file, $pos ) {
                $this->file = $file;