diff --git a/CRM/Core/BAO/ConfigSetting.php b/CRM/Core/BAO/ConfigSetting.php index d912f974ea9cee5d37ff96bc97d252c598e0902a..b17b6bd326e872554125f371959e5360241d4796 100644 --- a/CRM/Core/BAO/ConfigSetting.php +++ b/CRM/Core/BAO/ConfigSetting.php @@ -53,6 +53,7 @@ class CRM_Core_BAO_ConfigSetting { self::add($params); $cache = CRM_Utils_Cache::singleton(); $cache->delete('CRM_Core_Config'); + $cache->delete('CRM_Core_Config' . CRM_Core_Config::domainID()); $config = CRM_Core_Config::singleton(TRUE, TRUE); } diff --git a/CRM/Core/BAO/Domain.php b/CRM/Core/BAO/Domain.php index e925d26d2d2be316fb40332eaa14181ac67eeca6..a728386d706f9965ff4560d66849bee2ee8ff768 100644 --- a/CRM/Core/BAO/Domain.php +++ b/CRM/Core/BAO/Domain.php @@ -101,6 +101,7 @@ class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { static function setDomain($domainID){ CRM_Core_Config::domainID($domainID); self::getDomain($domainID); + CRM_Core_Config::singleton(TRUE, TRUE); } /** @@ -114,6 +115,7 @@ class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain { static function resetDomain(){ CRM_Core_Config::domainID(null, true); self::getDomain(null, true); + CRM_Core_Config::singleton(TRUE, TRUE); } static function version( $skipUsingCache = false ) { diff --git a/CRM/Core/BAO/Setting.php b/CRM/Core/BAO/Setting.php index 068cf3e916293d6eeb25438add994b4b8d4343e8..0161abe92e6d3e4e6b7d79ccd1ba265c81a872e5 100644 --- a/CRM/Core/BAO/Setting.php +++ b/CRM/Core/BAO/Setting.php @@ -75,31 +75,45 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { * @static * @access public */ - static function inCache($group, + static function inCache( + $group, $name, $componentID = NULL, $contactID = NULL, $load = FALSE, - $domainID = NULL + $domainID = NULL, + $force = FALSE ) { if (!isset(self::$_cache)) { self::$_cache = array(); } $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}"; + if ($load && - !isset(self::$_cache[$cacheKey]) + ($force || !isset(self::$_cache[$cacheKey])) ) { + // check in civi cache if present (typically memcache) $globalCache = CRM_Utils_Cache::singleton(); $result = $globalCache->get($cacheKey); if ($result) { + self::$_cache[$cacheKey] = $result; } } return isset(self::$_cache[$cacheKey]) ? $cacheKey : NULL; } + /** + * Allow key o be cleared + * @param string $cacheKey + */ + static function flushCache($cacheKey){ + unset(self::$_cache[$cacheKey]); + $globalCache = CRM_Utils_Cache::singleton(); + $globalCache->delete($cacheKey); + } static function setCache($values, $group, @@ -200,7 +214,6 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $cacheKey = self::setCache($values, $group, $componentID, $contactID, $domainID); } - return $name ? CRM_Utils_Array::value($name, self::$_cache[$cacheKey], $defaultValue) : self::$_cache[$cacheKey]; } @@ -214,19 +227,32 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { * @access public */ static function getItems(&$params, $domains = null, $settingsToReturn) { + $originalDomain = CRM_Core_Config::domainID(); if (empty($domains)) { - $domains[] = CRM_Core_Config::domainID(); + $domains[] = $originalDomain; } if (!empty($settingsToReturn) && !is_array($settingsToReturn)) { $settingsToReturn = array($settingsToReturn); } - $config = CRM_Core_Config::singleton(); + $reloadConfig = FALSE; + $fields = $result = array(); $fieldsToGet = self::validateSettingsInput(array_flip($settingsToReturn), $fields, FALSE); - foreach ($domains as $domain) { - $result[$domain] = array(); + foreach ($domains as $domainID) { + if($domainID != CRM_Core_Config::domainID()){ + $reloadConfig = TRUE; + CRM_Core_BAO_Domain::setDomain($domainID); + } + $config = CRM_Core_Config::singleton($reloadConfig, $reloadConfig); + $result[$domainID] = array(); foreach ($fieldsToGet as $name => $value) { if(!empty($fields['values'][$name]['prefetch'])){ + if(isset($params['filters']) && isset($params['filters']['prefetch']) + && $params['filters']['prefetch'] == 0){ + // we are filtering out the prefetches from the return array + // so we will skip + continue; + } $configKey = CRM_Utils_Array::value('config_key', $fields['values'][$name], $name); if(isset($config->$configKey)){ $setting = $config->$configKey; @@ -238,17 +264,18 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $fields['values'][$name]['group_name'], $name, CRM_Utils_Array::value('component_id', $params), - CRM_Utils_Array::value('default_value', $params), + null, CRM_Utils_Array::value('contact_id', $params), - $domain + $domainID ); } if (!is_null($setting)) { // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones // e.g for revert of fill actions - $result[$domain][$name] = $setting; + $result[$domainID][$name] = $setting; } } + CRM_Core_BAO_Domain::resetDomain(); } return $result; } @@ -275,7 +302,6 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $createdID = NULL, $domainID = NULL ) { - if (empty($domainID)) { $domainID = CRM_Core_Config::domainID(); } @@ -338,9 +364,11 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { * @access public */ static function setItems(&$params, $domains = null) { + $originalDomain = CRM_Core_Config::domainID(); if (empty($domains)) { - $domains[] = CRM_Core_Config::domainID(); + $domains[] = $originalDomain; } + $reloadConfig = FALSE; $fields = $config_keys = array(); $fieldsToSet = self::validateSettingsInput($params, $fields); @@ -348,8 +376,12 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { self::validateSetting($settingValue, $fields['values'][$settingField]); } - foreach ($domains as $domain) { - $result[$domain] = array(); + foreach ($domains as $domainID) { + if($domainID != CRM_Core_Config::domainID()){ + $reloadConfig = TRUE; + CRM_Core_BAO_Domain::setDomain($domainID); + } + $result[$domainID] = array(); foreach ($fieldsToSet as $name => $value) { if(empty($fields['values'][$name]['config_only'])){ CRM_Core_BAO_Setting::setItem( @@ -359,7 +391,7 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { CRM_Utils_Array::value('component_id', $params), CRM_Utils_Array::value('contact_id', $params), CRM_Utils_Array::value('created_id', $params), - $domain + $domainID ); } if(!empty($fields['values'][$name]['prefetch'])){ @@ -368,9 +400,18 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { } $config_keys[$name] = $value; } - $result[$domain][$name] = $value; + $result[$domainID][$name] = $value; + } + if($reloadConfig){ + CRM_Core_Config::singleton($reloadConfig, $reloadConfig); + } + + if(!empty($config_keys)){ + CRM_Core_BAO_ConfigSetting::create($config_keys); + } + if($reloadConfig){ + CRM_Core_BAO_Domain::resetDomain(); } - CRM_Core_BAO_ConfigSetting::create($config_keys); } return $result; @@ -612,8 +653,10 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { $apiParams = array( 'version' => 3, 'domain_id' => 'all', + 'filters' => array('prefetch' => 0), ); $existing = civicrm_api('setting', 'get', $apiParams); + if (!empty($existing['values'])) { $allSettings = civicrm_api('setting', 'getfields', array('version' => 3)); foreach ($existing['values'] as $domainID => $domainSettings) { @@ -634,6 +677,8 @@ class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { * Note that where the key name is being changed the 'legacy_key' will give us the old name */ static function convertConfigToSetting($name, $domainID = null) { + // we have to force this here in case more than one domain is in play. + // whenever there is a possibility of more than one domain we must force it $config = CRM_Core_Config::singleton(); if (empty($domainID)) { $domainID= CRM_Core_Config::domainID(); diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index e91458dbf4b94dea3730cb24f873d6a48394ef71..3356f009743c654929c3ef5e4671ce633b8f552d 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -184,8 +184,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { // first, attempt to get configuration object from cache $cache = CRM_Utils_Cache::singleton(); - self::$_singleton = $cache->get('CRM_Core_Config'); - + self::$_singleton = $cache->get('CRM_Core_Config' . CRM_Core_Config::domainID()); // if not in cache, fire off config construction if (!self::$_singleton) { self::$_singleton = new CRM_Core_Config; @@ -199,7 +198,7 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { // retrieve and overwrite stuff from the settings file self::$_singleton->setCoreVariables(); } - $cache->set('CRM_Core_Config', self::$_singleton); + $cache->set('CRM_Core_Config' . CRM_Core_Config::domainID(), self::$_singleton); } else { // we retrieve the object from memcache, so we now initialize the objects diff --git a/api/v3/examples/Setting/CreateAllDomains.php b/api/v3/examples/Setting/CreateAllDomains.php index 9dc4d348c76fe05604e37d0da6fda6fce0bc7bd6..62de561da07a2ff692243695c8566fa13af8f244 100644 --- a/api/v3/examples/Setting/CreateAllDomains.php +++ b/api/v3/examples/Setting/CreateAllDomains.php @@ -8,7 +8,6 @@ $params = array( 'version' => 3, 'domain_id' => 'all', 'uniq_email_per_site' => 1, - 'debug' => 1, ); $result = civicrm_api( 'setting','create',$params ); @@ -36,11 +35,6 @@ function setting_create_expectedresult(){ 'uniq_email_per_site' => '1', ), ), - 'xdebug' => array( - 'peakMemory' => 127711392, - 'memory' => 123061848, - 'timeIndex' => '1357.0696148872', - ), ); return $expectedResult ; diff --git a/api/v3/examples/SettingGetfields.php b/api/v3/examples/SettingGetfields.php index 9d5a522449dadb8bddf4188a9800fdc9076f3ee1..869430aa520d474dadc4acafbf962db74c6a30c7 100644 --- a/api/v3/examples/SettingGetfields.php +++ b/api/v3/examples/SettingGetfields.php @@ -21,7 +21,7 @@ function setting_getfields_expectedresult(){ $expectedResult = array( 'is_error' => 0, 'version' => 3, - 'count' => 72, + 'count' => 73, 'values' => array( 'address_standardization_provider' => array( 'group_name' => 'Address Preferences', @@ -258,7 +258,7 @@ function setting_getfields_expectedresult(){ 'group' => 'core', 'name' => 'address_format', 'type' => 'String', - 'html_type' => 'Text', + 'html_type' => 'TextArea', 'default' => '{contact.address_name} {contact.street_address} {contact.supplemental_address_1} @@ -381,6 +381,9 @@ function setting_getfields_expectedresult(){ 'name' => 'contact_autocomplete_options', 'type' => 'String', 'html_type' => 'checkboxes', + 'pseudoconstant' => array( + 'optionGroupName' => 'contact_autocomplete_options', + ), 'default' => array( '0' => '1', '1' => '2', @@ -616,6 +619,27 @@ When enabled, statistics about your CiviCRM installation are reported anonymousl 'description' => '', 'help_text' => '', ), + 'communityMessagesUrl' => array( + 'group_name' => 'CiviCRM Preferences', + 'group' => 'core', + 'name' => 'communityMessagesUrl', + 'prefetch' => 0, + 'config_only' => 1, + 'type' => 'String', + 'quick_form_type' => 'Element', + 'html_type' => 'Text', + 'html_attributes' => array( + 'size' => 64, + 'maxlength' => 128, + ), + 'default' => '*default*', + 'add' => '4.3', + 'title' => 'Community Messages URL', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => 'Service providing CiviCRM community messages', + 'help_text' => 'Use "*default*" for the system default or override with a custom URL', + ), 'resCacheCode' => array( 'group_name' => 'CiviCRM Preferences', 'group' => 'core', diff --git a/tests/phpunit/CRM/Core/BAO/SettingTest.php b/tests/phpunit/CRM/Core/BAO/SettingTest.php index 7964d8a997ee958c9a7ad247fa9148a9ea492236..097dce012e6b7a535c24f4395883406ea27c1298 100644 --- a/tests/phpunit/CRM/Core/BAO/SettingTest.php +++ b/tests/phpunit/CRM/Core/BAO/SettingTest.php @@ -121,19 +121,23 @@ class CRM_Core_BAO_SettingTest extends CiviUnitTestCase { * **/ function testConvertAndFillSettings() { - $sql = " DELETE FROM civicrm_setting WHERE name = 'max_attachments'"; - CRM_Core_DAO::executeQuery($sql); - $settings = array('maxAttachments' => 6); CRM_Core_BAO_ConfigSetting::add($settings); $config = CRM_Core_Config::singleton(TRUE, TRUE); $this->assertEquals(6, $config->maxAttachments); - $checkSQL = "SELECT count(*) FROM civicrm_domain WHERE config_backend LIKE '%Max%' AND id = 1 + $checkSQL = "SELECT count(*) FROM civicrm_domain WHERE config_backend LIKE '%\"maxAttachments\";i:6%' AND id = 1 "; $checkresult = CRM_Core_DAO::singleValueQuery($checkSQL); $this->assertEquals(1, $checkresult, "Check that maxAttachments has been saved to database not just stored in config"); - CRM_Core_BAO_Setting::updateSettingsFromMetaData(); + $sql = " DELETE FROM civicrm_setting WHERE name = 'max_attachments'"; + CRM_Core_DAO::executeQuery($sql); + $currentDomain = CRM_Core_Config::domainID(); + // we are setting up an artificial situation here as we are trying to drive out + // previous memory of this setting so we need to flush it out + $cachekey = CRM_Core_BAO_Setting::inCache('CiviCRM Preferences', 'max_attachments', NULL, NULL, TRUE, $currentDomain); + CRM_Core_BAO_Setting::flushCache($cachekey); + CRM_Core_BAO_Setting::updateSettingsFromMetaData(); //check current domain $value = civicrm_api('setting', 'getvalue', array( 'version' => 3, diff --git a/tests/phpunit/api/v3/SettingTest.php b/tests/phpunit/api/v3/SettingTest.php index 48617a58c7f7bf5b0dd5600da902995bbce50c1a..dc78e6d63d2b72c6c88a2c2d35b9475b8d22337c 100644 --- a/tests/phpunit/api/v3/SettingTest.php +++ b/tests/phpunit/api/v3/SettingTest.php @@ -248,7 +248,6 @@ class api_v3_SettingTest extends CiviUnitTestCase { $params = array('version' => $this->_apiversion, 'domain_id' => 'all', 'uniq_email_per_site' => 1, - 'debug' =>1, ); $result = civicrm_api('setting', 'create', $params); $description = "shows setting a variable for all domains"; @@ -328,7 +327,9 @@ class api_v3_SettingTest extends CiviUnitTestCase { ); $result = civicrm_api('setting', 'create', $params); $this->assertAPISuccess($result, "in line " . __LINE__); - $config = CRM_Core_Config::singleton(); + CRM_Core_BAO_Domain::setDomain($this->_domainID2); + $config = CRM_Core_Config::singleton(TRUE, TRUE); + CRM_Core_BAO_Domain::resetDomain(); $this->assertTrue($config->debug == 1); // this should NOT be stored in the settings table now - only in config $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%debug%'"; @@ -343,12 +344,39 @@ class api_v3_SettingTest extends CiviUnitTestCase { $settings = civicrm_api('setting', 'get', array( 'name' => 'defaultCurrency', 'version' => $this->_apiversion, - 'sequential' => 1, - 'debug' => 1) + 'sequential' => 1,) ); $this->assertAPISuccess($settings); $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']); } + + /** + * setting api should set & fetch settings stored in config as well as those in settings table + */ + function testGetSetConfigSettingMultipleDomains() { + $settings = civicrm_api('setting', 'create', array( + 'defaultCurrency' => 'USD', + 'version' => $this->_apiversion, + 'domain_id' => $this->_currentDomain) + ); + $settings = civicrm_api('setting', 'create', array( + 'defaultCurrency' => 'CAD', + 'version' => $this->_apiversion, + 'domain_id' => $this->_domainID2) + ); + $this->assertAPISuccess($settings); + $settings = civicrm_api('setting', 'get', array( + 'return' => 'defaultCurrency', + 'version' => $this->_apiversion, + 'domain_id' => 'all', + ) + ); + $this->assertEquals('USD', $settings['values'][$this->_currentDomain]['defaultCurrency']); + $this->assertEquals('CAD', $settings['values'][$this->_domainID2]['defaultCurrency'], + "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD"); + + } + /* * Use getValue against a config setting */ @@ -483,6 +511,7 @@ class api_v3_SettingTest extends CiviUnitTestCase { $result = civicrm_api('setting', 'get', $params); $this->assertAPISuccess($result, "in line " . __LINE__); $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]); + $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]); $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']); } } diff --git a/tools/bin/scripts/memcache.php b/tools/bin/scripts/memcache.php index 9436fd3a3037f5196801c114f8c8c9d51e11ad7f..6a29cc494a9e5da3b6fe0d6d8ceabffaebc5b1ff 100644 --- a/tools/bin/scripts/memcache.php +++ b/tools/bin/scripts/memcache.php @@ -8,6 +8,6 @@ define('CIVICRM_USE_MEMCACHE', 1); $config = &CRM_Core_Config::singleton(); $cache = &CRM_Utils_Cache::singleton(); -$cache->set('CRM_Core_Config', $config); -CRM_Core_Error::debug('get', $cache->get('CRM_Core_Config')); +$cache->set('CRM_Core_Config' .CRM_Core_Config::domainID(), $config); +CRM_Core_Error::debug('get', $cache->get('CRM_Core_Config' . CRM_Core_Config::domainID()));