With mysqli, avoid setting the charset twice
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 27 Dec 2013 22:40:15 +0000 (14:40 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Mon, 30 Dec 2013 23:16:59 +0000 (23:16 +0000)
* This uses MYSQLI_SET_CHARSET_NAME to set the charset to utf8/binary
* This replaces a live WMF hack to skip the extra round trip

Change-Id: I1718e013fcdc95163d111d460f0dd6d2190a99b1

includes/db/DatabaseMysqlBase.php
includes/db/DatabaseMysqli.php

index 3125325..08fb2c9 100644 (file)
@@ -49,7 +49,7 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         * @throws DBConnectionError
         */
        function open( $server, $user, $password, $dbName ) {
-               global $wgAllDBsAreLocalhost, $wgDBmysql5, $wgSQLMode;
+               global $wgAllDBsAreLocalhost, $wgSQLMode;
                wfProfileIn( __METHOD__ );
 
                # Debugging hack -- fake cluster
@@ -113,13 +113,11 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                        }
                }
 
-               // Tell the server we're communicating with it in UTF-8.
-               // This may engage various charset conversions.
-               if ( $wgDBmysql5 ) {
-                       $this->mysqlSetCharset( 'utf8' );
-               } else {
-                       $this->mysqlSetCharset( 'binary' );
+               // Tell the server what we're communicating with
+               if ( !$this->connectInitCharset() ) {
+                       return $this->reportConnectionError( "Error setting character set" );
                }
+
                // Set SQL mode, default is turning them all off, can be overridden or skipped with null
                if ( is_string( $wgSQLMode ) ) {
                        $mode = $this->addQuotes( $wgSQLMode );
@@ -138,6 +136,22 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                return true;
        }
 
+       /**
+        * Set the character set information right after connection
+        * @return bool
+        */
+       protected function connectInitCharset() {
+               global $wgDBmysql5;
+
+               if ( $wgDBmysql5 ) {
+                       // Tell the server we're communicating with it in UTF-8.
+                       // This may engage various charset conversions.
+                       return $this->mysqlSetCharset( 'utf8' );
+               } else {
+                       return $this->mysqlSetCharset( 'binary' );
+               }
+       }
+
        /**
         * Open a connection to a MySQL server
         *
index 6c83adb..d0c19fa 100644 (file)
@@ -44,6 +44,7 @@ class DatabaseMysqli extends DatabaseMysqlBase {
        }
 
        protected function mysqlConnect( $realServer ) {
+               global $wgDBmysql5;
                # Fail now
                # Otherwise we get a suppressed fatal error, which is very hard to track down
                if ( !function_exists( 'mysqli_init' ) ) {
@@ -63,8 +64,15 @@ class DatabaseMysqli extends DatabaseMysqlBase {
                }
 
                $mysqli = mysqli_init();
-               $numAttempts = 2;
+               if ( $wgDBmysql5 ) {
+                       // Tell the server we're communicating with it in UTF-8.
+                       // This may engage various charset conversions.
+                       $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'utf8' );
+               } else {
+                       $mysqli->options( MYSQLI_SET_CHARSET_NAME, 'binary' );
+               }
 
+               $numAttempts = 2;
                for ( $i = 0; $i < $numAttempts; $i++ ) {
                        if ( $i > 1 ) {
                                usleep( 1000 );
@@ -79,6 +87,11 @@ class DatabaseMysqli extends DatabaseMysqlBase {
                return false;
        }
 
+       protected function connectInitCharset() {
+               // already done in mysqlConnect()
+               return true;
+       }
+
        /**
         * @return bool
         */