Selaa lähdekoodia

#239 Issue with character set (impacting searches with accents)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@746 a333f486-631f-4898-b8df-5754b55c2be0
romainq 14 vuotta sitten
vanhempi
commit
435c88baaa

+ 15 - 0
core/cmdbsource.class.inc.php

@@ -69,6 +69,21 @@ class CMDBSource
 		}
 		}
 	}
 	}
 
 
+	public static function SetCharacterSet($sCharset = 'utf8', $sCollation = 'utf8_general_ci')
+	{
+		if (strlen($sCharset) > 0)
+		{
+			if (strlen($sCollation) > 0)
+			{
+				self::Query("SET NAMES '$sCharset' COLLATE '$sCollation'");
+			}
+			else
+			{
+				self::Query("SET NAMES '$sCharset'");
+			}
+		}
+	}
+
 	public static function ListDB()
 	public static function ListDB()
 	{
 	{
 		$aDBs = self::QueryToCol('SHOW DATABASES', 'Database');
 		$aDBs = self::QueryToCol('SHOW DATABASES', 'Database');

+ 32 - 0
core/config.class.inc.php

@@ -33,6 +33,9 @@ class ConfigException extends CoreException
 {
 {
 }
 }
 
 
+define ('DEFAULT_CHARACTER_SET', 'utf8');
+define ('DEFAULT_COLLATION', 'utf8_general_ci');
+
 define ('DEFAULT_LOG_GLOBAL', true);
 define ('DEFAULT_LOG_GLOBAL', true);
 define ('DEFAULT_LOG_NOTIFICATION', true);
 define ('DEFAULT_LOG_NOTIFICATION', true);
 define ('DEFAULT_LOG_ISSUE', true);
 define ('DEFAULT_LOG_ISSUE', true);
@@ -73,6 +76,8 @@ class Config
 	protected $m_sDBPwd;
 	protected $m_sDBPwd;
 	protected $m_sDBName;
 	protected $m_sDBName;
 	protected $m_sDBSubname;
 	protected $m_sDBSubname;
+	protected $m_sDBCharacterSet;
+	protected $m_sDBCollation;
 
 
 	/**
 	/**
 	 * @var integer Event log options (see LOG_... definition)
 	 * @var integer Event log options (see LOG_... definition)
@@ -182,6 +187,8 @@ class Config
 		$this->m_sDBPwd = '';
 		$this->m_sDBPwd = '';
 		$this->m_sDBName = '';
 		$this->m_sDBName = '';
 		$this->m_sDBSubname = '';
 		$this->m_sDBSubname = '';
+		$this->m_sDBCharacterSet = DEFAULT_CHARACTER_SET;
+		$this->m_sDBCollation = DEFAULT_COLLATION;
 		$this->m_bLogGlobal = DEFAULT_LOG_GLOBAL;
 		$this->m_bLogGlobal = DEFAULT_LOG_GLOBAL;
 		$this->m_bLogNotification = DEFAULT_LOG_NOTIFICATION;
 		$this->m_bLogNotification = DEFAULT_LOG_NOTIFICATION;
 		$this->m_bLogIssue = DEFAULT_LOG_ISSUE;
 		$this->m_bLogIssue = DEFAULT_LOG_ISSUE;
@@ -284,6 +291,9 @@ class Config
 		$this->m_sDBName = trim($MySettings['db_name']);
 		$this->m_sDBName = trim($MySettings['db_name']);
 		$this->m_sDBSubname = trim($MySettings['db_subname']);
 		$this->m_sDBSubname = trim($MySettings['db_subname']);
 
 
+		$this->m_sDBCharacterSet = isset($MySettings['db_character_set']) ? trim($MySettings['db_character_set']) : DEFAULT_CHARACTER_SET;
+		$this->m_sDBCollation = isset($MySettings['db_collation']) ? trim($MySettings['db_collation']) : DEFAULT_COLLATION;
+
 		$this->m_bLogGlobal = isset($MySettings['log_global']) ? (bool) trim($MySettings['log_global']) : DEFAULT_LOG_GLOBAL;
 		$this->m_bLogGlobal = isset($MySettings['log_global']) ? (bool) trim($MySettings['log_global']) : DEFAULT_LOG_GLOBAL;
 		$this->m_bLogNotification = isset($MySettings['log_notification']) ? (bool) trim($MySettings['log_notification']) : DEFAULT_LOG_NOTIFICATION;
 		$this->m_bLogNotification = isset($MySettings['log_notification']) ? (bool) trim($MySettings['log_notification']) : DEFAULT_LOG_NOTIFICATION;
 		$this->m_bLogIssue = isset($MySettings['log_issue']) ? (bool) trim($MySettings['log_issue']) : DEFAULT_LOG_ISSUE;
 		$this->m_bLogIssue = isset($MySettings['log_issue']) ? (bool) trim($MySettings['log_issue']) : DEFAULT_LOG_ISSUE;
@@ -391,6 +401,16 @@ class Config
 		return $this->m_sDBSubname;
 		return $this->m_sDBSubname;
 	}
 	}
 
 
+	public function GetDBCharacterSet()
+	{
+		return $this->m_sDBCharacterSet;
+	}
+
+	public function GetDBCollation()
+	{
+		return $this->m_sDBCollation;
+	}
+
 	public function GetDBUser()
 	public function GetDBUser()
 	{
 	{
 		return $this->m_sDBUser;
 		return $this->m_sDBUser;
@@ -501,6 +521,16 @@ class Config
 		$this->m_sDBSubname = $sDBSubName;
 		$this->m_sDBSubname = $sDBSubName;
 	}
 	}
 
 
+	public function SetDBCharacterSet($sDBCharacterSet)
+	{
+		$this->m_sDBCharacterSet = $sDBCharacterSet;
+	}
+
+	public function SetDBCollation($sDBCollation)
+	{
+		$this->m_sDBCollation = $sDBCollation;
+	}
+
 	public function SetDBUser($sUser)
 	public function SetDBUser($sUser)
 	{
 	{
 		$this->m_sDBUser = $sUser;
 		$this->m_sDBUser = $sUser;
@@ -626,6 +656,8 @@ class Config
 			fwrite($hFile, "\t'db_pwd' => '".addslashes($this->m_sDBPwd)."',\n");
 			fwrite($hFile, "\t'db_pwd' => '".addslashes($this->m_sDBPwd)."',\n");
 			fwrite($hFile, "\t'db_name' => '{$this->m_sDBName}',\n");
 			fwrite($hFile, "\t'db_name' => '{$this->m_sDBName}',\n");
 			fwrite($hFile, "\t'db_subname' => '{$this->m_sDBSubname}',\n");
 			fwrite($hFile, "\t'db_subname' => '{$this->m_sDBSubname}',\n");
+			fwrite($hFile, "\t'db_character_set' => '{$this->m_sDBCharacterSet}',\n");
+			fwrite($hFile, "\t'db_collation' => '{$this->m_sDBCollation}',\n");
 			fwrite($hFile, "\n");
 			fwrite($hFile, "\n");
 			fwrite($hFile, "\t'log_global' => {$this->m_bLogGlobal},\n");
 			fwrite($hFile, "\t'log_global' => {$this->m_bLogGlobal},\n");
 			fwrite($hFile, "\t'log_notification' => {$this->m_bLogNotification},\n");
 			fwrite($hFile, "\t'log_notification' => {$this->m_bLogNotification},\n");

+ 3 - 0
core/metamodel.class.php

@@ -3242,6 +3242,8 @@ abstract class MetaModel
 		$sPwd = self::$m_oConfig->GetDBPwd();
 		$sPwd = self::$m_oConfig->GetDBPwd();
 		$sSource = self::$m_oConfig->GetDBName();
 		$sSource = self::$m_oConfig->GetDBName();
 		$sTablePrefix = self::$m_oConfig->GetDBSubname();
 		$sTablePrefix = self::$m_oConfig->GetDBSubname();
+		$sCharacterSet = self::$m_oConfig->GetDBCharacterSet();
+		$sCollation = self::$m_oConfig->GetDBCollation();
 
 
 		$oKPI = new ExecutionKPI();
 		$oKPI = new ExecutionKPI();
 
 
@@ -3255,6 +3257,7 @@ abstract class MetaModel
 		self::$m_sTablePrefix = $sTablePrefix;
 		self::$m_sTablePrefix = $sTablePrefix;
 
 
 		CMDBSource::Init($sServer, $sUser, $sPwd); // do not select the DB (could not exist)
 		CMDBSource::Init($sServer, $sUser, $sPwd); // do not select the DB (could not exist)
+		CMDBSource::SetCharacterSet($sCharacterSet, $sCollation);
 	}
 	}
 
 
 	public static function GetModuleSetting($sModule, $sProperty, $defaultvalue = null)
 	public static function GetModuleSetting($sModule, $sProperty, $defaultvalue = null)

+ 1 - 0
core/test.class.inc.php

@@ -361,6 +361,7 @@ abstract class TestScenarioOnDB extends TestHandler
 		$sDBName = $this->GetDBName();
 		$sDBName = $this->GetDBName();
 
 
 		CMDBSource::Init($sDBHost, $sDBUser, $sDBPwd);
 		CMDBSource::Init($sDBHost, $sDBUser, $sDBPwd);
+		CMDBSource::SetCharacterSet();
 		if (CMDBSource::IsDB($sDBName))
 		if (CMDBSource::IsDB($sDBName))
 		{
 		{
 			CMDBSource::DropDB($sDBName);
 			CMDBSource::DropDB($sDBName);