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
This commit is contained in:
Alexandre Emsenhuber 2013-11-15 23:21:40 +01:00 committed by Nikerabbit
parent d6698354fe
commit a222905098
4 changed files with 33 additions and 2 deletions

View 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
*/

View 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

View 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
*/

View 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 ) {}