Feature #272 - Adding code in the Database class for improved internationalization. The new code is latent, it will be used after the first release of Chamilo.
* Converts an encoding identificator to MySQL-specific encoding identifictor,
* i.e. 'UTF-8' --> 'utf8'.
* @param string $encoding The conventional encoding identificator.
* @return string Returns the corresponding MySQL-specific encoding identificator if any, otherwise returns NULL.
* @author Ivan Tcholakov
*/
public static function to_db_encoding($encoding) {
static $result = array();
if (!isset($result[$encoding])) {
$result[$encoding] = null;
$encoding_map = & self::get_db_encoding_map();
foreach ($encoding_map as $key => $value) {
if (api_equal_encodings($encoding, $key)) {
// Check whether the encoding is supported by the server.
if (self::num_rows(self::query("SHOW CHARACTER SET WHERE `Charset` = '".$value."';", __FILE__, __LINE__)) > 0) {
$result[$encoding] = $value;
}
break;
}
}
}
return $result[$encoding];
}
/**
* Converts a MySQL-specific encoding identifictor to conventional encoding identificator,
* i.e. 'utf8' --> 'UTF-8'.
* @param string $encoding The MySQL-specific encoding identificator.
* @return string Returns the corresponding conventional encoding identificator if any, otherwise returns NULL.
* @author Ivan Tcholakov
*/
public static function from_db_encoding($db_encoding) {
static $result = array();
if (!isset($result[$db_encoding])) {
$result[$db_encoding] = null;
$encoding_map = & self::get_db_encoding_map();
foreach ($encoding_map as $key => $value) {
if (strtolower($db_encoding) == $value) {
$result[$db_encoding] = $key;
break;
}
}
}
return $result[$db_encoding];
}
/**
* This private function encapsulates a table with relations between
* conventional and MuSQL-specific encoding identificators.
* @author Ivan Tcholakov
*/
private static function & get_db_encoding_map() {
static $encoding_map = array(
'ARMSCII-8' => 'armscii8',
'BIG5' => 'big5',
'BINARY' => 'binary',
'CP866' => 'cp866',
'EUC-JP' => 'ujis',
'EUC-KR' => 'euckr',
'GB2312' => 'gb2312',
'GBK' => 'gbk',
'ISO-8859-1' => 'latin1',
'ISO-8859-2' => 'latin2',
'ISO-8859-7' => 'greek',
'ISO-8859-8' => 'hebrew',
'ISO-8859-9' => 'latin5',
'ISO-8859-13' => 'latin7',
'ISO-8859-15' => 'latin1',
'KOI8-R' => 'koi8r',
'KOI8-U' => 'koi8u',
'SHIFT-JIS' => 'sjis',
'TIS-620' => 'tis620',
'US-ASCII' => 'ascii',
'UTF-8' => 'utf8',
'WINDOWS-1250' => 'cp1250',
'WINDOWS-1251' => 'cp1251',
'WINDOWS-1252' => 'latin1',
'WINDOWS-1256' => 'cp1256',
'WINDOWS-1257' => 'cp1257'
);
return $encoding_map;
}
/**
* Chooses the default MySQL-specific collation from given encoding and language.
* @param string $encoding A conventional encoding id, i.e. 'UTF-8'
* @param string $language (optional) A conventional for the system language id, i.e. 'bulgarian'. If it is empty, the chosen collation is the default server value corresponding to the given encoding.
* @return string Returns a suitable default collation, for example 'utf8_general_ci', or NULL if collation was not found.
* @author Ivan Tcholakov
*/
public static function to_db_collation($encoding, $language = null) {