N°1260 Config : migrate DB* variables to the Get() model, create CMDBSource::InitFromConfig

SVN:trunk[5308]
This commit is contained in:
Pierre Goiffon
2018-02-08 14:21:25 +00:00
parent 5a25e44177
commit d2f0deec9c
13 changed files with 426 additions and 366 deletions

View File

@@ -1,5 +1,5 @@
<?php
// Copyright (C) 2010-2017 Combodo SARL
// Copyright (C) 2010-2018 Combodo SARL
//
// This file is part of iTop.
//
@@ -20,7 +20,7 @@
/**
* Class iTopWebPage
*
* @copyright Copyright (C) 2010-2017 Combodo SARL
* @copyright Copyright (C) 2010-2018 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
@@ -796,7 +796,7 @@ EOF
if ($iBreadCrumbMaxCount > 1)
{
$oConfig = MetaModel::GetConfig();
$siTopInstanceId = json_encode(utils::GetAbsoluteUrlAppRoot().'==='.$oConfig->GetDBHost().'/'.$oConfig->GetDBName().'/'.$oConfig->GetDBSubname());
$siTopInstanceId = json_encode(utils::GetAbsoluteUrlAppRoot().'==='.$oConfig->Get('db_host').'/'.$oConfig->Get('db_name').'/'.$oConfig->Get('db_subname'));
if ($this->bBreadCrumbEnabled)
{
if (is_null($this->sBreadCrumbEntryId))

View File

@@ -1,5 +1,5 @@
<?php
// Copyright (C) 2010-2015 Combodo SARL
// Copyright (C) 2010-2018 Combodo SARL
//
// This file is part of iTop.
//
@@ -20,7 +20,7 @@
/**
* DB Server abstraction
*
* @copyright Copyright (C) 2010-2015 Combodo SARL
* @copyright Copyright (C) 2010-2018 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
@@ -92,9 +92,34 @@ class CMDBSource
protected static $m_sDBSSLCert;
protected static $m_sDBSSLCA;
protected static $m_sDBSSLCipher;
/** @var mysqli */
/** @var mysqli $m_oMysqli */
protected static $m_oMysqli;
/**
* @param Config $oConfig
*
* @throws \MySQLException
* @uses \CMDBSource::Init()
* @uses \CMDBSource::SetCharacterSet()
*/
public static function InitFromConfig($oConfig)
{
$sServer = $oConfig->Get('db_host');
$sUser = $oConfig->Get('db_user');
$sPwd = $oConfig->Get('db_pwd');
$sSource = $oConfig->Get('db_name');
$sSSLKey = $oConfig->Get('db_ssl.key');
$sSSLCert = $oConfig->Get('db_ssl.cert');
$sSSLCA = $oConfig->Get('db_ssl.ca');
$sSSLCipher = $oConfig->Get('db_ssl.cipher');
self::Init($sServer, $sUser, $sPwd, $sSource, $sSSLKey, $sSSLCert, $sSSLCA, $sSSLCipher);
$sCharacterSet = $oConfig->Get('db_character_set');
$sCollation = $oConfig->Get('db_collation');
self::SetCharacterSet($sCharacterSet, $sCollation);
}
/**
* @param string $sServer
* @param string $sUser

File diff suppressed because it is too large Load Diff

View File

@@ -182,7 +182,7 @@ abstract class MetaModel
var_dump(get_class_vars(__CLASS__));
}
/** @var Config */
/** @var Config m_oConfig */
private static $m_oConfig = null;
/** @var array */
protected static $m_aModulesParameters = array();
@@ -5801,17 +5801,8 @@ abstract class MetaModel
self::IncludeModule($sToInclude, 'addons');
}
$sServer = self::$m_oConfig->GetDBHost();
$sUser = self::$m_oConfig->GetDBUser();
$sPwd = self::$m_oConfig->GetDBPwd();
$sSource = self::$m_oConfig->GetDBName();
$sSSLKey = self::$m_oConfig->GetDBSSLKey();
$sSSLCert = self::$m_oConfig->GetDBSSLCert();
$sSSLCA = self::$m_oConfig->GetDBSSLCA();
$sSSLCipher = self::$m_oConfig->GetDBSSLCipher();
$sTablePrefix = self::$m_oConfig->GetDBSubname();
$sCharacterSet = self::$m_oConfig->GetDBCharacterSet();
$sCollation = self::$m_oConfig->GetDBCollation();
$sSource = self::$m_oConfig->Get('db_name');
$sTablePrefix = self::$m_oConfig->Get('db_subname');
if (self::$m_bUseAPCCache)
{
@@ -5888,8 +5879,7 @@ abstract class MetaModel
self::$m_sDBName = $sSource;
self::$m_sTablePrefix = $sTablePrefix;
CMDBSource::Init($sServer, $sUser, $sPwd, '', $sSSLKey, $sSSLCert, $sSSLCA, $sSSLCipher); // do not select the DB (could not exist)
CMDBSource::SetCharacterSet($sCharacterSet, $sCollation);
CMDBSource::InitFromConfig(self::$m_oConfig);
// Later when timezone implementation is correctly done: CMDBSource::SetTimezone($sDBTimezone);
}

View File

@@ -1,5 +1,5 @@
<?php
// Copyright (C) 2013-2017 Combodo SARL
// Copyright (C) 2013-2018 Combodo SARL
//
// This file is part of iTop.
//
@@ -24,7 +24,7 @@
* Relies on MySQL locks because the API sem_get is not always present in the
* installed PHP.
*
* @copyright Copyright (C) 2013-2017 Combodo SARL
* @copyright Copyright (C) 2013-2018 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
class iTopMutex
@@ -47,12 +47,15 @@ class iTopMutex
{
$oConfig = utils::GetConfig(); // Will return an empty config when called during the setup
}
$sDBName = $oConfig->GetDBName();
$sDBSubname = $oConfig->GetDBSubname();
$this->sDBSSLKey = $oConfig->GetDBSSLKey();
$this->sDBSSLCert = $oConfig->GetDBSSLCert();
$this->sDBSSLCA = $oConfig->GetDBSSLCA();
$this->sDBSSLCipher = $oConfig->GetDBSSLCipher();
$sDBHost = is_null($sDBHost) ? $oConfig->Get('db_host') : $sDBHost;
$sDBUser = is_null($sDBUser) ? $oConfig->Get('db_user') : $sDBUser;
$sDBPwd = is_null($sDBPwd) ? $oConfig->Get('db_pwd') : $sDBPwd;
$sDBName = $oConfig->Get('db_name');
$sDBSubname = $oConfig->Get('db_subname');
$this->sDBSSLKey = $oConfig->Get('db_ssl.key');
$this->sDBSSLCert = $oConfig->Get('db_ssl.cert');
$this->sDBSSLCA = $oConfig->Get('db_ssl.ca');
$this->sDBSSLCipher = $oConfig->Get('db_ssl.cipher');
$this->sName = 'itop.'.$sName;
$this->sName = $sName;
if (substr($sName, -strlen($sDBName.$sDBSubname)) != $sDBName.$sDBSubname)
@@ -75,9 +78,6 @@ class iTopMutex
// It is a MUST to create a dedicated session each time a lock is required, because
// using GET_LOCK anytime on the same session will RELEASE the current and unique session lock (known issue)
$sDBHost = is_null($sDBHost) ? $oConfig->GetDBHost() : $sDBHost;
$sDBUser = is_null($sDBUser) ? $oConfig->GetDBUser() : $sDBUser;
$sDBPwd = is_null($sDBPwd) ? $oConfig->GetDBPwd() : $sDBPwd;
$this->InitMySQLSession($sDBHost, $sDBUser, $sDBPwd);
}

View File

@@ -86,10 +86,10 @@ function MakeArchiveFileName($iRefTime = null)
$sBackupFile = utils::ReadParam('backup_file', $sDefaultBackupFileName, true, 'raw_data');
$oConfig = new Config(APPCONF.'production/config-itop.php');
$sBackupFile = str_replace('__HOST__', $oConfig->GetDBHost(), $sBackupFile);
$sBackupFile = str_replace('__DB__', $oConfig->GetDBName(), $sBackupFile);
$sBackupFile = str_replace('__SUBNAME__', $oConfig->GetDBSubName(), $sBackupFile);
$sBackupFile = str_replace('__HOST__', $oConfig->Get('db_host'), $sBackupFile);
$sBackupFile = str_replace('__DB__', $oConfig->Get('db_name'), $sBackupFile);
$sBackupFile = str_replace('__SUBNAME__', $oConfig->Get('db_subname'), $sBackupFile);
if (is_null($iRefTime))
{

View File

@@ -1,5 +1,5 @@
<?php
// Copyright (C) 2016 Combodo SARL
// Copyright (C) 2016-2018 Combodo SARL
//
// This file is part of iTop.
//
@@ -20,7 +20,7 @@
/**
* Monitor the backup
*
* @copyright Copyright (C) 2016 Combodo SARL
* @copyright Copyright (C) 2016-2018 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
@@ -296,11 +296,11 @@ try
$sRestoreDone = addslashes(Dict::S('bkp-success-restore'));
$sMySQLBinDir = addslashes(MetaModel::GetConfig()->GetModuleSetting('itop-backup', 'mysql_bindir', ''));
$sDBHost = addslashes(MetaModel::GetConfig()->GetDBHost());
$sDBUser = addslashes(MetaModel::GetConfig()->GetDBUser());
$sDBPwd = addslashes(MetaModel::GetConfig()->GetDBPwd());
$sDBName = addslashes(MetaModel::GetConfig()->GetDBName());
$sDBSubName = addslashes(MetaModel::GetConfig()->GetDBSubName());
$sDBHost = addslashes(MetaModel::GetConfig()->Get('db_host'));
$sDBUser = addslashes(MetaModel::GetConfig()->Get('db_user'));
$sDBPwd = addslashes(MetaModel::GetConfig()->Get('db_pwd'));
$sDBName = addslashes(MetaModel::GetConfig()->Get('db_name'));
$sDBSubName = addslashes(MetaModel::GetConfig()->Get('db_subname'));
$sEnvironment = addslashes(utils::GetCurrentEnvironment());

View File

@@ -189,9 +189,9 @@ function collect_configuration()
// iTop modules
$oConfig = MetaModel::GetConfig();
$sLatestInstallationDate = CMDBSource::QueryToScalar("SELECT max(installed) FROM ".$oConfig->GetDBSubname()."priv_module_install");
$sLatestInstallationDate = CMDBSource::QueryToScalar("SELECT max(installed) FROM ".$oConfig->Get('db_subname')."priv_module_install");
// Get the latest installed modules, without the "root" ones (iTop version and datamodel version)
$aInstalledModules = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->GetDBSubname()."priv_module_install WHERE installed = '".$sLatestInstallationDate."' AND parent_id != 0");
$aInstalledModules = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->Get('db_subname')."priv_module_install WHERE installed = '".$sLatestInstallationDate."' AND parent_id != 0");
foreach ($aInstalledModules as $aDBInfo)
{

View File

@@ -1,5 +1,5 @@
<?php
// Copyright (C) 2010-2017 Combodo SARL
// Copyright (C) 2010-2018 Combodo SARL
//
// This file is part of iTop.
//
@@ -194,11 +194,11 @@ if (class_exists('ZipArchive')) // The setup must be able to start even if the "
if (is_null($sDBHost))
{
// Defaulting to the current config
$sDBHost = MetaModel::GetConfig()->GetDBHost();
$sDBUser = MetaModel::GetConfig()->GetDBUser();
$sDBPwd = MetaModel::GetConfig()->GetDBPwd();
$sDBName = MetaModel::GetConfig()->GetDBName();
$sDBSubName = MetaModel::GetConfig()->GetDBSubName();
$sDBHost = MetaModel::GetConfig()->Get('db_host');
$sDBUser = MetaModel::GetConfig()->Get('db_user');
$sDBPwd = MetaModel::GetConfig()->Get('db_pwd');
$sDBName = MetaModel::GetConfig()->Get('db_name');
$sDBSubName = MetaModel::GetConfig()->Get('db_subname');
}
// Compute the port (if present in the host name)

View File

@@ -547,11 +547,10 @@ class iTopExtensionsMap
{
if (CMDBSource::DBName() === null)
{
CMDBSource::Init($oConfig->GetDBHost(), $oConfig->GetDBUser(), $oConfig->GetDBPwd(), $oConfig->GetDBName());
CMDBSource::SetCharacterSet($oConfig->GetDBCharacterSet(), $oConfig->GetDBCollation());
CMDBSource::InitFromConfig($oConfig);
}
$sLatestInstallationDate = CMDBSource::QueryToScalar("SELECT max(installed) FROM ".$oConfig->GetDBSubname()."priv_extension_install");
$aInstalledExtensions = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->GetDBSubname()."priv_extension_install WHERE installed = '".$sLatestInstallationDate."'");
$sLatestInstallationDate = CMDBSource::QueryToScalar("SELECT max(installed) FROM ".$oConfig->Get('db_subname')."priv_extension_install");
$aInstalledExtensions = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->Get('db_subname')."priv_extension_install WHERE installed = '".$sLatestInstallationDate."'");
}
catch (MySQLException $e)
{

View File

@@ -1,5 +1,5 @@
<?php
// Copyright (C) 2010-2017 Combodo SARL
// Copyright (C) 2010-2018 Combodo SARL
//
// This file is part of iTop.
//
@@ -20,7 +20,7 @@
/**
* Manage a runtime environment
*
* @copyright Copyright (C) 2010-2017 Combodo SARL
* @copyright Copyright (C) 2010-2018 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
@@ -243,9 +243,8 @@ class RunTimeEnvironment
try
{
require_once(APPROOT.'/core/cmdbsource.class.inc.php');
CMDBSource::Init($oConfig->GetDBHost(), $oConfig->GetDBUser(), $oConfig->GetDBPwd(), $oConfig->GetDBName(), $oConfig->GetDBSSLKey(), $oConfig->GetDBSSLCert(), $oConfig->GetDBSSLCA(), $oConfig->GetDBSSLCipher());
CMDBSource::SetCharacterSet($oConfig->GetDBCharacterSet(), $oConfig->GetDBCollation());
$aSelectInstall = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->GetDBSubname()."priv_module_install");
CMDBSource::InitFromConfig($oConfig);
$aSelectInstall = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->Get('db_subname')."priv_module_install");
}
catch (MySQLException $e)
{
@@ -577,13 +576,13 @@ class RunTimeEnvironment
*/
public function CreateDatabaseStructure(Config $oConfig, $sMode)
{
if (strlen($oConfig->GetDBSubname()) > 0)
if (strlen($oConfig->Get('db_subname')) > 0)
{
$this->log_info("Creating the structure in '".$oConfig->GetDBName()."' (table names prefixed by '".$oConfig->GetDBSubname()."').");
$this->log_info("Creating the structure in '".$oConfig->Get('db_name')."' (table names prefixed by '".$oConfig->Get('db_subname')."').");
}
else
{
$this->log_info("Creating the structure in '".$oConfig->GetDBSubname()."'.");
$this->log_info("Creating the structure in '".$oConfig->Get('db_subname')."'.");
}
//MetaModel::CheckDefinitions();
@@ -596,13 +595,13 @@ class RunTimeEnvironment
}
else
{
if (strlen($oConfig->GetDBSubname()) > 0)
if (strlen($oConfig->Get('db_subname')) > 0)
{
throw new Exception("Error: found iTop tables into the database '".$oConfig->GetDBName()."' (prefix: '".$oConfig->GetDBSubname()."'). Please, try selecting another database instance or specify another prefix to prevent conflicting table names.");
throw new Exception("Error: found iTop tables into the database '".$oConfig->Get('db_name')."' (prefix: '".$oConfig->Get('db_subname')."'). Please, try selecting another database instance or specify another prefix to prevent conflicting table names.");
}
else
{
throw new Exception("Error: found iTop tables into the database '".$oConfig->GetDBName()."'. Please, try selecting another database instance or specify a prefix to prevent conflicting table names.");
throw new Exception("Error: found iTop tables into the database '".$oConfig->Get('db_name')."'. Please, try selecting another database instance or specify a prefix to prevent conflicting table names.");
}
}
}
@@ -641,13 +640,13 @@ class RunTimeEnvironment
}
else
{
if (strlen($oConfig->GetDBSubname()) > 0)
if (strlen($oConfig->Get('db_subname')) > 0)
{
throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."' (prefix: '".$oConfig->GetDBSubname()."'). Please, try selecting another database instance.");
throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->Get('db_name')."' (prefix: '".$oConfig->Get('db_subname')."'). Please, try selecting another database instance.");
}
else
{
throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->GetDBName()."'. Please, try selecting another database instance.");
throw new Exception("Error: No previous instance of iTop found into the database '".$oConfig->Get('db_name')."'. Please, try selecting another database instance.");
}
}
}
@@ -724,8 +723,7 @@ class RunTimeEnvironment
if (CMDBSource::DBName() == '')
{
// In case this has not yet been done
CMDBSource::Init($oConfig->GetDBHost(), $oConfig->GetDBUser(), $oConfig->GetDBPwd(), $oConfig->GetDBName());
CMDBSource::SetCharacterSet($oConfig->GetDBCharacterSet(), $oConfig->GetDBCollation());
CMDBSource::InitFromConfig($oConfig);
}
if ($sShortComment === null)
@@ -836,15 +834,14 @@ class RunTimeEnvironment
try
{
require_once(APPROOT.'/core/cmdbsource.class.inc.php');
CMDBSource::Init($oConfig->GetDBHost(), $oConfig->GetDBUser(), $oConfig->GetDBPwd(), $oConfig->GetDBName(), $oConfig->GetDBSSLKey(), $oConfig->GetDBSSLCert(), $oConfig->GetDBSSLCA(), $oConfig->GetDBSSLCipher());
CMDBSource::SetCharacterSet($oConfig->GetDBCharacterSet(), $oConfig->GetDBCollation());
$sSQLQuery = "SELECT * FROM ".$oConfig->GetDBSubname()."priv_module_install";
CMDBSource::InitFromConfig($oConfig);
$sSQLQuery = "SELECT * FROM ".$oConfig->Get('db_subname')."priv_module_install";
$aSelectInstall = CMDBSource::QueryToArray($sSQLQuery);
}
catch (MySQLException $e)
{
// No database or erroneous information
$this->log_error('Can not connect to the database: host: '.$oConfig->GetDBHost().', user:'.$oConfig->GetDBUser().', pwd:'.$oConfig->GetDBPwd().', db name:'.$oConfig->GetDBName());
$this->log_error('Can not connect to the database: host: '.$oConfig->Get('db_host').', user:'.$oConfig->Get('db_user').', pwd:'.$oConfig->Get('db_pwd').', db name:'.$oConfig->Get('db_name'));
$this->log_error('Exception '.$e->getMessage());
return false;
}

View File

@@ -1,5 +1,5 @@
<?php
// Copyright (C) 2010-2017 Combodo SARL
// Copyright (C) 2010-2018 Combodo SARL
//
// This file is part of iTop.
//
@@ -18,7 +18,8 @@
/**
* The standardized result of any pass/fail check performed by the setup
* @copyright Copyright (C) 2010-2017 Combodo SARL
*
* @copyright Copyright (C) 2010-2018 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
class CheckResult
@@ -821,11 +822,11 @@ class SetupUtils
'source_dir' => $sSourceDir,
'source_environment' => $sSourceEnvironment,
'configuration_file' => $sConfigFile,
'db_server' => $oPrevConf->GetDBHost(),
'db_user' => $oPrevConf->GetDBUser(),
'db_pwd' => $oPrevConf->GetDBPwd(),
'db_name' => $oPrevConf->GetDBName(),
'db_prefix' => $oPrevConf->GetDBSubname(),
'db_server' => $oPrevConf->Get('db_host'),
'db_user' => $oPrevConf->Get('db_user'),
'db_pwd' => $oPrevConf->Get('db_pwd'),
'db_name' => $oPrevConf->Get('db_name'),
'db_prefix' => $oPrevConf->Get('db_subname'),
'graphviz_path' => $oPrevConf->Get('graphviz_path'),
);
}

View File

@@ -753,7 +753,7 @@ EOF
{
$sDBTableName = preg_replace('/[^A-za-z0-9_]/', '_', $sDBTableName); // Remove forbidden characters from the table name
}
$sPrefix = MetaModel::GetConfig()->GetDBSubName()."synchro_data_";
$sPrefix = MetaModel::GetConfig()->Get('db_subname')."synchro_data_";
if (strpos($sDBTableName, $sPrefix) !== 0)
{
$sDBTableName = $sPrefix.$sDBTableName;
@@ -972,7 +972,7 @@ EOF
}
}
$sDBName = MetaModel::GetConfig()->GetDBName();
$sDBName = MetaModel::GetConfig()->Get('db_name');
try
{
// Note: as per the MySQL documentation, using information_schema behaves exactly like SHOW TRIGGERS (user privileges)