Follow-up r104051: fix tests
[lhc/web/wiklou.git] / includes / db / DatabaseMysql.php
index 53adea1..22810ab 100644 (file)
  * @see Database
  */
 class DatabaseMysql extends DatabaseBase {
+
+       /**
+        * @return string
+        */
        function getType() {
                return 'mysql';
        }
@@ -110,22 +114,19 @@ class DatabaseMysql extends DatabaseBase {
                }
 
                if ( $success ) {
-                       $version = $this->getServerVersion();
-                       if ( version_compare( $version, '4.1' ) >= 0 ) {
-                               // Tell the server we're communicating with it in UTF-8.
-                               // This may engage various charset conversions.
-                               global $wgDBmysql5;
-                               if( $wgDBmysql5 ) {
-                                       $this->query( 'SET NAMES utf8', __METHOD__ );
-                               } else {
-                                       $this->query( 'SET NAMES binary', __METHOD__ );
-                               }
-                               // Set SQL mode, default is turning them all off, can be overridden or skipped with null
-                               global $wgSQLMode;
-                               if ( is_string( $wgSQLMode ) ) {
-                                       $mode = $this->addQuotes( $wgSQLMode );
-                                       $this->query( "SET sql_mode = $mode", __METHOD__ );
-                               }
+                       // Tell the server we're communicating with it in UTF-8.
+                       // This may engage various charset conversions.
+                       global $wgDBmysql5;
+                       if( $wgDBmysql5 ) {
+                               $this->query( 'SET NAMES utf8', __METHOD__ );
+                       } else {
+                               $this->query( 'SET NAMES binary', __METHOD__ );
+                       }
+                       // Set SQL mode, default is turning them all off, can be overridden or skipped with null
+                       global $wgSQLMode;
+                       if ( is_string( $wgSQLMode ) ) {
+                               $mode = $this->addQuotes( $wgSQLMode );
+                               $this->query( "SET sql_mode = $mode", __METHOD__ );
                        }
 
                        // Turn off strict mode if it is on
@@ -138,6 +139,9 @@ class DatabaseMysql extends DatabaseBase {
                return $success;
        }
 
+       /**
+        * @return bool
+        */
        function close() {
                $this->mOpened = false;
                if ( $this->mConn ) {
@@ -188,6 +192,11 @@ class DatabaseMysql extends DatabaseBase {
                return $row;
        }
 
+       /**
+        * @throws DBUnexpectedError
+        * @param $res
+        * @return int
+        */
        function numRows( $res ) {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
@@ -201,6 +210,10 @@ class DatabaseMysql extends DatabaseBase {
                return $n;
        }
 
+       /**
+        * @param $res
+        * @return int
+        */
        function numFields( $res ) {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
@@ -208,6 +221,11 @@ class DatabaseMysql extends DatabaseBase {
                return mysql_num_fields( $res );
        }
 
+       /**
+        * @param $res
+        * @param $n
+        * @return string
+        */
        function fieldName( $res, $n ) {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
@@ -215,7 +233,12 @@ class DatabaseMysql extends DatabaseBase {
                return mysql_field_name( $res, $n );
        }
 
-       function insertId() { return mysql_insert_id( $this->mConn ); }
+       /**
+        * @return int
+        */
+       function insertId() {
+               return mysql_insert_id( $this->mConn );
+       }
 
        function dataSeek( $res, $row ) {
                if ( $res instanceof ResultWrapper ) {
@@ -250,7 +273,12 @@ class DatabaseMysql extends DatabaseBase {
                return $error;
        }
 
-       function affectedRows() { return mysql_affected_rows( $this->mConn ); }
+       /**
+        * @return int
+        */
+       function affectedRows() {
+               return mysql_affected_rows( $this->mConn );
+       }
 
        function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseMysql::replace' ) {
                return $this->nativeReplace( $table, $rows, $fname );
@@ -260,6 +288,8 @@ class DatabaseMysql extends DatabaseBase {
         * Estimate rows in dataset
         * Returns estimated count, based on EXPLAIN output
         * Takes same arguments as Database::select()
+        *
+        * @return int
         */
        public function estimateRowCount( $table, $vars='*', $conds='', $fname = 'DatabaseMysql::estimateRowCount', $options = array() ) {
                $options['EXPLAIN'] = true;
@@ -278,6 +308,11 @@ class DatabaseMysql extends DatabaseBase {
                return $rows;
        }
 
+       /**
+        * @param $table string
+        * @param $field string
+        * @return bool|MySQLField
+        */
        function fieldInfo( $table, $field ) {
                $table = $this->tableName( $table );
                $res = $this->query( "SELECT * FROM $table LIMIT 1", __METHOD__, true );
@@ -297,6 +332,8 @@ class DatabaseMysql extends DatabaseBase {
        /**
         * Get information about an index into an object
         * Returns false if the index does not exist
+        *
+        * @return false|array
         */
        function indexInfo( $table, $index, $fname = 'DatabaseMysql::indexInfo' ) {
                # SHOW INDEX works in MySQL 3.23.58, but SHOW INDEXES does not.
@@ -322,11 +359,20 @@ class DatabaseMysql extends DatabaseBase {
                return empty( $result ) ? false : $result;
        }
 
+       /**
+        * @param $db
+        * @return bool
+        */
        function selectDB( $db ) {
                $this->mDBname = $db;
                return mysql_select_db( $db, $this->mConn );
        }
 
+       /**
+        * @param $s string
+        *
+        * @return string
+        */
        function strencode( $s ) {
                $sQuoted = mysql_real_escape_string( $s, $this->mConn );
 
@@ -339,15 +385,26 @@ class DatabaseMysql extends DatabaseBase {
 
        /**
         * MySQL uses `backticks` for identifier quoting instead of the sql standard "double quotes".
+        *
+        * @param $s string
+        *
+        * @return string
         */
        public function addIdentifierQuotes( $s ) {
                return "`" . $this->strencode( $s ) . "`";
        }
 
+       /**
+        * @param $name string
+        * @return bool
+        */
        public function isQuotedIdentifier( $name ) {
-               return strlen($name) && $name[0] == '`' && substr( $name, -1, 1 ) == '`';
+               return strlen( $name ) && $name[0] == '`' && substr( $name, -1, 1 ) == '`';
        }
 
+       /**
+        * @return bool
+        */
        function ping() {
                $ping = mysql_ping( $this->mConn );
                if ( $ping ) {
@@ -364,9 +421,7 @@ class DatabaseMysql extends DatabaseBase {
        /**
         * Returns slave lag.
         *
-        * On MySQL 4.1.9 and later, this will do a SHOW SLAVE STATUS. On earlier
-        * versions of MySQL, it uses SHOW PROCESSLIST, which requires the PROCESS
-        * privilege.
+        * On MySQL 4.1.9 and later, this will do a SHOW SLAVE STATUS
         *
         * @return int
         */
@@ -376,11 +431,7 @@ class DatabaseMysql extends DatabaseBase {
                        return $this->mFakeSlaveLag;
                }
 
-               /*if ( version_compare( $this->getServerVersion(), '4.1.9', '>=' ) ) {
-                       return $this->getLagFromSlaveStatus();
-               } else */{
-                       return $this->getLagFromProcesslist();
-               }
+               return $this->getLagFromSlaveStatus();
        }
 
        /**
@@ -403,6 +454,8 @@ class DatabaseMysql extends DatabaseBase {
        }
 
        /**
+        * @deprecated in 1.19, use getLagFromSlaveStatus
+        *
         * @return bool|int
         */
        function getLagFromProcesslist() {
@@ -516,18 +569,31 @@ class DatabaseMysql extends DatabaseBase {
                }
        }
 
+       /**
+        * @return string
+        */
        function getServerVersion() {
                return mysql_get_server_info( $this->mConn );
        }
 
+       /**
+        * @param $index
+        * @return string
+        */
        function useIndexClause( $index ) {
                return "FORCE INDEX (" . $this->indexName( $index ) . ")";
        }
 
+       /**
+        * @return string
+        */
        function lowPriorityOption() {
                return 'LOW_PRIORITY';
        }
 
+       /**
+        * @return string
+        */
        public static function getSoftwareLink() {
                return '[http://www.mysql.com/ MySQL]';
        }
@@ -630,6 +696,8 @@ class DatabaseMysql extends DatabaseBase {
 
        /**
         * Determines if the last failure was due to a deadlock
+        *
+        * @return bool
         */
        function wasDeadlock() {
                return $this->lastErrno() == 1213;
@@ -638,6 +706,8 @@ class DatabaseMysql extends DatabaseBase {
        /**
         * Determines if the last query error was something that should be dealt
         * with by pinging the connection and reissuing the query
+        *
+        * @return bool
         */
        function wasErrorReissuable() {
                return $this->lastErrno() == 2013 || $this->lastErrno() == 2006;
@@ -645,6 +715,8 @@ class DatabaseMysql extends DatabaseBase {
 
        /**
         * Determines if the last failure was due to the database being read-only.
+        *
+        * @return bool
         */
        function wasReadOnlyError() {
                return $this->lastErrno() == 1223 ||
@@ -653,29 +725,9 @@ class DatabaseMysql extends DatabaseBase {
 
        function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseMysql::duplicateTableStructure' ) {
                $tmp = $temporary ? 'TEMPORARY ' : '';
-               if ( strcmp( $this->getServerVersion(), '4.1' ) < 0 ) {
-                       # Hack for MySQL versions < 4.1, which don't support
-                       # "CREATE TABLE ... LIKE". Note that
-                       # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
-                       # would not create the indexes we need....
-                       #
-                       # Note that we don't bother changing around the prefixes here be-
-                       # cause we know we're using MySQL anyway.
-
-                       $res = $this->query( 'SHOW CREATE TABLE ' . $this->addIdentifierQuotes( $oldName ) );
-                       $row = $this->fetchRow( $res );
-                       $oldQuery = $row[1];
-                       $query = preg_replace( '/CREATE TABLE `(.*?)`/',
-                               "CREATE $tmp TABLE " . $this->addIdentifierQuotes( $newName ), $oldQuery );
-                       if ($oldQuery === $query) {
-                               # Couldn't do replacement
-                               throw new MWException( "could not create temporary table $newName" );
-                       }
-               } else {
-                       $newName = $this->addIdentifierQuotes( $newName );
-                       $oldName = $this->addIdentifierQuotes( $oldName );
-                       $query = "CREATE $tmp TABLE $newName (LIKE $oldName)";
-               }
+               $newName = $this->addIdentifierQuotes( $newName );
+               $oldName = $this->addIdentifierQuotes( $oldName );
+               $query = "CREATE $tmp TABLE $newName (LIKE $oldName)";
                $this->query( $query, $fname );
        }
 
@@ -702,16 +754,25 @@ class DatabaseMysql extends DatabaseBase {
                return $endArray;
        }
 
+       /**
+        * @param $tableName
+        * @param $fName string
+        * @return bool|ResultWrapper
+        */
        public function dropTable( $tableName, $fName = 'DatabaseMysql::dropTable' ) {
-               if( !$this->tableExists( $tableName ) ) {
+               if( !$this->tableExists( $tableName, $fName ) ) {
                        return false;
                }
                return $this->query( "DROP TABLE IF EXISTS " . $this->tableName( $tableName ), $fName );
        }
 
+       /**
+        * @return array
+        */
        protected function getDefaultSchemaVars() {
                $vars = parent::getDefaultSchemaVars();
-               $vars['wgDBTableOptions'] = $GLOBALS['wgDBTableOptions'];
+               $vars['wgDBTableOptions'] = str_replace( 'TYPE', 'ENGINE', $GLOBALS['wgDBTableOptions'] );
+               $vars['wgDBTableOptions'] = str_replace( 'CHARSET=mysql4', 'CHARSET=binary', $GLOBALS['wgDBTableOptions'] );
                return $vars;
        }
 
@@ -735,6 +796,8 @@ class DatabaseMysql extends DatabaseBase {
 
 /**
  * Legacy support: Database == DatabaseMysql
+ *
+ * @deprecated in 1.16
  */
 class Database extends DatabaseMysql {}
 
@@ -759,10 +822,16 @@ class MySQLField implements Field {
                $this->type = $info->type;
        }
 
+       /**
+        * @return string
+        */
        function name() {
                return $this->name;
        }
 
+       /**
+        * @return string
+        */
        function tableName() {
                return $this->tableName;
        }
@@ -771,6 +840,9 @@ class MySQLField implements Field {
                return $this->type;
        }
 
+       /**
+        * @return bool
+        */
        function isNullable() {
                return $this->nullable;
        }
@@ -779,10 +851,16 @@ class MySQLField implements Field {
                return $this->default;
        }
 
+       /**
+        * @return bool
+        */
        function isKey() {
                return $this->is_key;
        }
 
+       /**
+        * @return bool
+        */
        function isMultipleKey() {
                return $this->is_multiple;
        }