From fbe350750b79b5c0ec2fbf9b0fd1554399bb1c05 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 27 Dec 2013 14:40:15 -0800 Subject: [PATCH] With mysqli, avoid setting the charset twice * 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 | 28 +++++++++++++++++++++------- includes/db/DatabaseMysqli.php | 15 ++++++++++++++- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/includes/db/DatabaseMysqlBase.php b/includes/db/DatabaseMysqlBase.php index 31253259f7..08fb2c9c71 100644 --- a/includes/db/DatabaseMysqlBase.php +++ b/includes/db/DatabaseMysqlBase.php @@ -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 * diff --git a/includes/db/DatabaseMysqli.php b/includes/db/DatabaseMysqli.php index 6c83adb732..d0c19fa1e2 100644 --- a/includes/db/DatabaseMysqli.php +++ b/includes/db/DatabaseMysqli.php @@ -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 */ -- 2.20.1