From: Alexandre Emsenhuber Date: Fri, 15 Nov 2013 22:21:40 +0000 (+0100) Subject: Use the native set_charset() method if available instead of SET NAMES X-Git-Tag: 1.31.0-rc.0~18096^2 X-Git-Url: http://git.cyclocoop.org/%24image?a=commitdiff_plain;h=a22290509881705a1ab54599a07a1c14077c2bde;p=lhc%2Fweb%2Fwiklou.git Use the native set_charset() method if available instead of SET NAMES According to the PHP manual, it is the recommended way to set the charset. - mysql extension has it for MySQL >= 5.0.7 - mysqli extension has it for MySQL >= 5.0.6 or if using mysqlnd. Change-Id: I8cd2f97fcad4b045c6f99ff894254847b13c6878 --- diff --git a/includes/db/DatabaseMysql.php b/includes/db/DatabaseMysql.php index 956bb694e2..1314b122fb 100644 --- a/includes/db/DatabaseMysql.php +++ b/includes/db/DatabaseMysql.php @@ -80,6 +80,17 @@ class DatabaseMysql extends DatabaseMysqlBase { return $conn; } + /** + * @return bool + */ + protected function mysqlSetCharset( $charset ) { + if ( function_exists( 'mysql_set_charset' ) ) { + return mysql_set_charset( $charset, $this->mConn ); + } else { + return $this->query( 'SET NAMES ' . $charset, __METHOD__ ); + } + } + /** * @return bool */ diff --git a/includes/db/DatabaseMysqlBase.php b/includes/db/DatabaseMysqlBase.php index cdfa76902a..5be460f429 100644 --- a/includes/db/DatabaseMysqlBase.php +++ b/includes/db/DatabaseMysqlBase.php @@ -114,9 +114,9 @@ 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->query( 'SET NAMES utf8', __METHOD__ ); + $this->mysqlSetCharset( 'utf8' ); } else { - $this->query( 'SET NAMES binary', __METHOD__ ); + $this->mysqlSetCharset( 'binary' ); } // Set SQL mode, default is turning them all off, can be overridden or skipped with null if ( is_string( $wgSQLMode ) ) { @@ -138,6 +138,14 @@ abstract class DatabaseMysqlBase extends DatabaseBase { */ abstract protected function mysqlConnect( $realServer ); + /** + * Set the character set of the MySQL link + * + * @param string $charset + * @return bool + */ + abstract protected function mysqlSetCharset( $charset ); + /** * @param $res ResultWrapper * @throws DBUnexpectedError diff --git a/includes/db/DatabaseMysqli.php b/includes/db/DatabaseMysqli.php index 7761abe923..5b2e11d960 100644 --- a/includes/db/DatabaseMysqli.php +++ b/includes/db/DatabaseMysqli.php @@ -79,6 +79,17 @@ class DatabaseMysqli extends DatabaseMysqlBase { return false; } + /** + * @return bool + */ + protected function mysqlSetCharset( $charset ) { + if ( method_exists( $this->mConn, 'set_charset' ) ) { + return $this->mConn->set_charset( $charset ); + } else { + return $this->query( 'SET NAMES ' . $charset, __METHOD__ ); + } + } + /** * @return bool */ diff --git a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php index 10a922710f..2162a02a18 100644 --- a/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php +++ b/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php @@ -37,6 +37,7 @@ class FakeDatabaseMysqlBase extends DatabaseMysqlBase { // From DatabaseMysql protected function mysqlConnect( $realServer ) {} + protected function mysqlSetCharset( $charset ) {} protected function mysqlFreeResult( $res ) {} protected function mysqlFetchObject( $res ) {} protected function mysqlFetchArray( $res ) {}