Issue #306: Reworks about the initialization of the multibyte string library.

skala
Ivan Tcholakov 16 years ago
parent dd23b4423f
commit c3c4275f33
  1. 11
      main/inc/global.inc.php
  2. 166
      main/inc/lib/multibyte_string_functions.lib.php
  3. 62
      main/inc/lib/multibyte_string_functions_internal.lib.php
  4. 8
      main/install/index.php
  5. 7
      tests/main/inc/lib/multibyte_string_functions.lib.test.php

@ -260,8 +260,11 @@ if (empty($charset)) {
}
// Preserving the value of the global variable $charset.
$charset_initial_value = $charset;
// Initialization of the default encoding that will be used by the string routines.
api_set_default_encoding($charset);
// Initialization of the multibyte string library.
api_initialize_string_library();
// Initialization of the default encoding that will be used by the multibyte string routines.
api_set_string_library_default_encoding($charset);
/*
--------------------------------------------
@ -570,8 +573,8 @@ if ($_course['language'])
// to use it within the function get_lang(...).
$language_interface_initial_value = $language_interface;
// Initialization the default ICU locale id based in the current interface language.
api_set_default_locale(api_get_locale_from_language($language_interface));
// Initialization of the default language for the multibyte string library.
api_set_string_library_default_language($language_interface);
/*
* Include all necessary language files

@ -45,6 +45,58 @@
*/
/**
* ----------------------------------------------------------------------------
* Initialization
* ----------------------------------------------------------------------------
*/
/**
* Initialization of some internal default valies of the multibyte string library.
* @return void
* Note: This function should be called once in the initialization script.
*/
function api_initialize_string_library() {
if (MBSTRING_INSTALLED) {
@ini_set('mbstring.func_overload', 0);
@ini_set('mbstring.encoding_translation', 0);
@ini_set('mbstring.http_input', 'pass');
@ini_set('mbstring.http_output', 'pass');
@ini_set('mbstring.language', 'neutral');
}
api_set_string_library_default_encoding('ISO-8859-15');
api_set_string_library_default_language('english');
}
/**
* Sets the internal default encoding for the multi-byte string functions.
* @param string $encoding The specified default encoding.
* @return string Returns the old value of the default encoding.
*/
function api_set_string_library_default_encoding($encoding) {
$encoding = api_refine_encoding_id($encoding);
$result = _api_mb_internal_encoding();
_api_mb_internal_encoding($encoding);
_api_mb_regex_encoding($encoding);
_api_iconv_set_encoding('iconv_internal_encoding', $encoding);
return $result;
}
/**
* Sets the internal default language value.
* If the intl extension is present, given language identificator modifies string comparison and sorting functionaliry.
* @param string $language The specified default language.
* @return string Returns the old value of the default language.
*/
function api_set_string_library_default_language($language) {
static $old_language = 'english';
_api_set_default_locale(_api_get_locale_from_language($language));
$result = $old_language;
$old_language = $language;
return $result;
}
/**
* ----------------------------------------------------------------------------
* A safe way to calculate binary lenght of a string (as number of bytes)
@ -2303,26 +2355,6 @@ function api_in_array_nocase($needle, $haystack, $strict = false, $encoding = nu
* ----------------------------------------------------------------------------
*/
/**
* Returns the most-probably used non-UTF-8 encoding for the given language.
* @param string $language (optional) The specified language, the default value is the user intrface language.
* @return string The correspondent encoding to the specified language.
*/
function api_get_non_utf8_encoding($language = null) {
if (empty($language)) {
$language = api_get_interface_language();
}
$language = api_refine_language_id($language);
$encodings = & _api_non_utf8_encodings();
if (is_array($encodings[$language])) {
if (!empty($encodings[$language][0])) {
return $encodings[$language][0];
}
return 'ISO-8859-15';
}
return 'ISO-8859-15';
}
/**
* This function unifies the encoding identificators, so they could be compared.
* @param string/array $encoding The specified encoding.
@ -2442,26 +2474,6 @@ function api_get_file_system_encoding() {
return $file_system_encoding;
}
/**
* Sets all internal default encodings of the multi-byte functions to the given value.
* @param string $encoding The specified default encoding.
* @return void
* Note: This function should be called once the initialization. Please, avoid further manipulation of the internal default encodings.
*/
function api_set_default_encoding($encoding) {
if (MBSTRING_INSTALLED) { // This fragment of initialization code should find a better place in the future.
// Additional mbstring-related initialization operations.
@ini_set('mbstring.func_overload', 0);
@ini_set('mbstring.encoding_translation', 0);
@ini_set('mbstring.http_input', 'pass');
@ini_set('mbstring.http_output', 'pass');
@ini_set('mbstring.language', 'neutral');
}
_api_mb_internal_encoding($encoding);
_api_mb_regex_encoding($encoding);
_api_iconv_set_encoding('iconv_internal_encoding', $encoding);
}
/**
* Checks whether a specified encoding is supported by this API.
* @param string $encoding The specified encoding.
@ -2476,6 +2488,26 @@ function api_is_encoding_supported($encoding) {
}
/**
* Returns the most-probably used non-UTF-8 encoding for the given language.
* @param string $language (optional) The specified language, the default value is the user intrface language.
* @return string The correspondent encoding to the specified language.
*/
function api_get_non_utf8_encoding($language = null) {
if (empty($language)) {
$language = api_get_interface_language();
}
$language = api_refine_language_id($language);
$encodings = & _api_non_utf8_encodings();
if (is_array($encodings[$language])) {
if (!empty($encodings[$language][0])) {
return $encodings[$language][0];
}
return 'ISO-8859-15';
}
return 'ISO-8859-15';
}
/**
* ----------------------------------------------------------------------------
* String validation functions concerning some encodings
@ -2694,60 +2726,6 @@ function api_is_latin1_compatible($language) {
}
/**
* ----------------------------------------------------------------------------
* ICU locales (accessible through intl extension).
* ----------------------------------------------------------------------------
*/
/**
* Returns isocode (see api_get_language_isocode()) which is purified accordingly to
* be used by the php intl extension (ICU library).
* @param string $language (optional) This is the name of the folder containing translations for the corresponding language.
* If $language is omitted, interface language is assumed then.
* @return string The found language locale id or null on error. Examples: bg, en, pt_BR, ...
*/
function api_get_locale_from_language($language = null) {
static $locale = array();
if (!isset($locale[$language])) {
$locale[$language] = Database::get_language_isocode($language);
if (!is_null($locale[$language])) {
$locale[$language] = str_replace('-', '_', $locale[$language]);
}
}
return $locale[$language];
}
/**
* Sets/gets the default internal value of the locale id (for the intl extension, ICU).
* @param string $locale (optional) The locale id to be set. When it is omitted, the function returns (gets, reads) the default internal value.
* @return mixed When the function sets the default value, it returns TRUE on success or FALSE on error. Otherwise the function returns as string the current default value.
*/
function api_set_default_locale($locale = null) {
static $default_locale = 'en';
if (!empty($locale)) {
$default_locale = $locale;
if (INTL_INSTALLED) {
return @locale_set_default($locale);
}
return true;
} else {
if (INTL_INSTALLED) {
$default_locale = @locale_get_default();
}
}
return $default_locale;
}
/**
* Gets the default internal value of the locale id (for the intl extension, ICU).
* @return string Returns as string the current default value.
*/
function api_get_default_locale() {
return api_set_default_locale();
}
/**
* ----------------------------------------------------------------------------
* Functions for internal use behind this API.

@ -467,7 +467,7 @@ function _api_array_utf8_decode($variable) {
function _api_get_collator($language = null) {
static $collator = array();
if (!isset($collator[$language])) {
$locale = api_get_locale_from_language($language);
$locale = _api_get_locale_from_language($language);
$collator[$language] = collator_create($locale);
if (is_object($collator[$language])) {
collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
@ -484,7 +484,7 @@ function _api_get_collator($language = null) {
function _api_get_alpha_numerical_collator($language = null) {
static $collator = array();
if (!isset($collator[$language])) {
$locale = api_get_locale_from_language($language);
$locale = _api_get_locale_from_language($language);
$collator[$language] = collator_create($locale);
if (is_object($collator[$language])) {
collator_set_attribute($collator[$language], Collator::CASE_FIRST, Collator::UPPER_FIRST);
@ -579,6 +579,64 @@ function _api_get_collator_sort_flag($sort_flag = SORT_REGULAR) {
}
/**
* ----------------------------------------------------------------------------
* ICU locales (accessible through intl extension).
* ----------------------------------------------------------------------------
*/
/**
* Returns isocode (see api_get_language_isocode()) which is purified accordingly to
* be used by the php intl extension (ICU library).
* @param string $language (optional) This is the name of the folder containing translations for the corresponding language.
* If $language is omitted, interface language is assumed then.
* @return string The found language locale id or null on error. Examples: bg, en, pt_BR, ...
*/
function _api_get_locale_from_language($language = null) {
static $locale = array();
if (!isset($locale[$language])) {
if (class_exists('Database')) {
$locale[$language] = Database::get_language_isocode($language);
} else {
return 'en';
}
if (!is_null($locale[$language])) {
$locale[$language] = str_replace('-', '_', $locale[$language]);
}
}
return $locale[$language];
}
/**
* Sets/gets the default internal value of the locale id (for the intl extension, ICU).
* @param string $locale (optional) The locale id to be set. When it is omitted, the function returns (gets, reads) the default internal value.
* @return mixed When the function sets the default value, it returns TRUE on success or FALSE on error. Otherwise the function returns as string the current default value.
*/
function _api_set_default_locale($locale = null) {
static $default_locale = 'en';
if (!empty($locale)) {
$default_locale = $locale;
if (INTL_INSTALLED) {
return @locale_set_default($locale);
}
return true;
} else {
if (INTL_INSTALLED) {
$default_locale = @locale_get_default();
}
}
return $default_locale;
}
/**
* Gets the default internal value of the locale id (for the intl extension, ICU).
* @return string Returns as string the current default value.
*/
function api_get_default_locale() {
return _api_set_default_locale();
}
/**
* ----------------------------------------------------------------------------
* Appendix to "Encoding management functions"

@ -156,7 +156,13 @@ if (isset($install_language)) {
}
}
header('Content-Type: text/html; charset='. $charset);
api_set_default_encoding($charset); // Initialization of the default encoding that will be used by the string routines.
// Initialization of the multibyte string library.
api_initialize_string_library();
// Initialization of the default encoding that will be used by the multibyte string routines.
api_set_string_library_default_encoding($charset);
// Initialization of the default language for the multibyte string library.
api_set_string_library_default_language($install_language);
require_once 'install_upgrade.lib.php'; //also defines constants
require_once 'install_functions.inc.php';

@ -683,7 +683,8 @@ class TestMultibyte_String_Functions extends UnitTestCase {
public function testApiSetDefaultEncoding(){
$encoding ='UTF-8';
$res = api_set_default_encoding($encoding);
$res = api_set_string_library_default_encoding($encoding);
api_set_string_library_default_encoding($res); // This is for restoring the original internal value.
if(!is_null($res)) :
$this->assertTrue($res);
endif;
@ -798,7 +799,7 @@ class TestMultibyte_String_Functions extends UnitTestCase {
public function testapi_get_locale_from_language(){
$language = 'EN';
$res = api_get_locale_from_language($language);
$res = _api_get_locale_from_language($language);
if(!is_null($res)):
$this->assertTrue($res);
$this->assertTrue(is_string);
@ -808,7 +809,7 @@ class TestMultibyte_String_Functions extends UnitTestCase {
public function testapi_set_default_locale(){
$locale = null;
$res = api_set_default_locale($locale);
$res = _api_set_default_locale($locale);
if(!is_string($res)) :
$this->assertTrue(is_bool($res));
$this->assertTrue($res === true || $res === false);

Loading…
Cancel
Save