Use the native set_charset() method if available instead of SET NAMES
authorAlexandre Emsenhuber <ialex.wiki@gmail.com>
Fri, 15 Nov 2013 22:21:40 +0000 (23:21 +0100)
committerNikerabbit <niklas.laxstrom@gmail.com>
Sat, 16 Nov 2013 07:57:52 +0000 (07:57 +0000)
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

includes/db/DatabaseMysql.php
includes/db/DatabaseMysqlBase.php
includes/db/DatabaseMysqli.php
tests/phpunit/includes/db/DatabaseMysqlBaseTest.php

index 956bb69..1314b12 100644 (file)
@@ -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
         */
index cdfa769..5be460f 100644 (file)
@@ -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
index 7761abe..5b2e11d 100644 (file)
@@ -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
         */
index 10a9227..2162a02 100644 (file)
@@ -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 ) {}