From: Tim Otten Date: Sun, 13 Sep 2015 04:18:10 +0000 (-0700) Subject: CRM-16373 - Simplify UpdateConfigBackend and doSiteMove() X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=4f240ac1294f2cd72ba4cff2b7319715929fae52;p=civicrm-core.git CRM-16373 - Simplify UpdateConfigBackend and doSiteMove() doSiteMove() has been performing a search-and-replace against data structures which haven't exist since at least 4.0; it seems quite unlikely that this works. Anecdotally, the workflow around this either incomplete or buggy/broken. Simplifying this is simply a reflection of the reality that it doesn't do what it was written to do. Instead, doSiteMove() now resets to a standard baseline/default. --- diff --git a/CRM/Admin/Form/Setting/UpdateConfigBackend.php b/CRM/Admin/Form/Setting/UpdateConfigBackend.php index 07d612a4e5..c793c6990e 100644 --- a/CRM/Admin/Form/Setting/UpdateConfigBackend.php +++ b/CRM/Admin/Form/Setting/UpdateConfigBackend.php @@ -35,9 +35,6 @@ * This class generates form components for Error Handling and Debugging */ class CRM_Admin_Form_Setting_UpdateConfigBackend extends CRM_Admin_Form_Setting { - protected $_oldBaseDir; - protected $_oldBaseURL; - protected $_oldSiteName; /** * Build the form object. @@ -45,61 +42,17 @@ class CRM_Admin_Form_Setting_UpdateConfigBackend extends CRM_Admin_Form_Setting public function buildQuickForm() { CRM_Utils_System::setTitle(ts('Settings - Cleanup Caches and Update Paths')); - list( - $this->_oldBaseURL, - $this->_oldBaseDir, - $this->_oldSiteName - ) = CRM_Core_BAO_ConfigSetting::getConfigSettings(); - - $this->assign('oldBaseURL', $this->_oldBaseURL); - $this->assign('oldBaseDir', $this->_oldBaseDir); - $this->assign('oldSiteName', $this->_oldSiteName); - $this->addElement( 'submit', $this->getButtonName('next', 'cleanup'), 'Cleanup Caches', array('class' => 'crm-form-submit', 'id' => 'cleanup-cache') ); - $this->add('text', 'newBaseURL', ts('New Base URL'), NULL, TRUE); - $this->add('text', 'newBaseDir', ts('New Base Directory'), NULL, TRUE); - if ($this->_oldSiteName) { - $this->add('text', 'newSiteName', ts('New Site Name'), NULL, TRUE); - } - $this->addFormRule(array('CRM_Admin_Form_Setting_UpdateConfigBackend', 'formRule')); - - parent::buildQuickForm(); - } - - public function setDefaultValues() { - if (!$this->_defaults) { - parent::setDefaultValues(); - - $config = CRM_Core_Config::singleton(); - list( - $this->_defaults['newBaseURL'], - $this->_defaults['newBaseDir'], - $this->_defaults['newSiteName'] - ) = CRM_Core_BAO_ConfigSetting::getBestGuessSettings(); - } + $this->addElement( + 'submit', $this->getButtonName('next', 'resetpaths'), 'Reset Paths', + array('class' => 'crm-form-submit', 'id' => 'resetpaths') + ); - return $this->_defaults; - } - - /** - * @param $fields - * - * @return array - */ - public static function formRule($fields) { - $tmpDir = trim($fields['newBaseDir']); - - $errors = array(); - if (!is_writable($tmpDir)) { - $errors['newBaseDir'] = ts('%1 directory does not exist or cannot be written by webserver', - array(1 => $tmpDir) - ); - } - return $errors; + //parent::buildQuickForm(); } public function postProcess() { @@ -119,49 +72,15 @@ class CRM_Admin_Form_Setting_UpdateConfigBackend extends CRM_Admin_Form_Setting CRM_Core_BAO_WordReplacement::rebuild(); CRM_Core_Session::setStatus(ts('Cache has been cleared and menu has been rebuilt successfully.'), ts("Success"), "success"); - return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/setting/updateConfigBackend', 'reset=1')); - } - - // redirect to admin page after saving - $session = CRM_Core_Session::singleton(); - $session->pushUserContext(CRM_Utils_System::url('civicrm/admin')); - - $params = $this->controller->exportValues($this->_name); - - //CRM-5679 - foreach ($params as $name => & $val) { - if ($val && in_array($name, array('newBaseDir', 'newSiteName'))) { - $val = CRM_Utils_File::addTrailingSlash($val); - } } - //CRM-15365 - Fix BaseURL to avoid wrong trailing slash on Windows installs - foreach ($params as $name => & $val) { - if ($val && in_array($name, array('newBaseURL'))) { - $val = CRM_Utils_File::addTrailingSlash($val, "/"); - } - } + if (!empty($_POST['_qf_UpdateConfigBackend_next_resetpaths'])) { + $msg = CRM_Core_BAO_ConfigSetting::doSiteMove(); - $from = array($this->_oldBaseURL, $this->_oldBaseDir); - $to = array( - trim($params['newBaseURL']), - trim($params['newBaseDir']), - ); - if ($this->_oldSiteName && - $params['newSiteName'] - ) { - $from[] = $this->_oldSiteName; - $to[] = $params['newSiteName']; + CRM_Core_Session::setStatus($msg, ts("Success"), "success"); } - $newValues = str_replace($from, - $to, - $this->_defaults - ); - - parent::commonProcess($newValues); - - parent::rebuildMenu(); + return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/setting/updateConfigBackend', 'reset=1')); } } diff --git a/CRM/Core/BAO/ConfigSetting.php b/CRM/Core/BAO/ConfigSetting.php index 9045c03c73..1d6f09c46d 100644 --- a/CRM/Core/BAO/ConfigSetting.php +++ b/CRM/Core/BAO/ConfigSetting.php @@ -220,156 +220,6 @@ class CRM_Core_BAO_ConfigSetting { } } - /** - * @return array - */ - public static function getConfigSettings() { - $config = CRM_Core_Config::singleton(); - - $url = $dir = $siteName = $siteRoot = NULL; - if ($config->userFramework == 'Joomla') { - $url = preg_replace( - '|administrator/components/com_civicrm/civicrm/|', - '', - $config->userFrameworkResourceURL - ); - - // lets use imageUploadDir since we dont mess around with its values - // in the config object, lets kep it a bit generic since folks - // might have different values etc - - //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths - if (DIRECTORY_SEPARATOR == '\\') { - $dir = preg_replace( - '|civicrm[/\\\\]templates_c[/\\\\].*$|', - '', - $config->templateCompileDir - ); - } - else { - $dir = preg_replace( - '|civicrm/templates_c/.*$|', - '', - $config->templateCompileDir - ); - } - - $siteRoot = preg_replace( - '|/media/civicrm/.*$|', - '', - $config->imageUploadDir - ); - } - elseif ($config->userFramework == 'WordPress') { - $url = preg_replace( - '|wp-content/plugins/civicrm/civicrm/|', - '', - $config->userFrameworkResourceURL - ); - - // lets use imageUploadDir since we dont mess around with its values - // in the config object, lets kep it a bit generic since folks - // might have different values etc - - //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths - if (DIRECTORY_SEPARATOR == '\\') { - $dir = preg_replace( - '|civicrm[/\\\\]templates_c[/\\\\].*$|', - '', - $config->templateCompileDir - ); - } - else { - $dir = preg_replace( - '|civicrm/templates_c/.*$|', - '', - $config->templateCompileDir - ); - } - - $siteRoot = preg_replace( - '|/wp-content/plugins/files/civicrm/.*$|', - '', - $config->imageUploadDir - ); - } - else { - $url = preg_replace( - '|sites/[\w\.\-\_]+/modules/civicrm/|', - '', - $config->userFrameworkResourceURL - ); - - // lets use imageUploadDir since we dont mess around with its values - // in the config object, lets kep it a bit generic since folks - // might have different values etc - - //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths - if (DIRECTORY_SEPARATOR == '\\') { - $dir = preg_replace( - '|[/\\\\]files[/\\\\]civicrm[/\\\\].*$|', - '\\\\files\\\\', - $config->imageUploadDir - ); - } - else { - $dir = preg_replace( - '|/files/civicrm/.*$|', - '/files/', - $config->imageUploadDir - ); - } - - $matches = array(); - if (preg_match( - '|/sites/([\w\.\-\_]+)/|', - $config->imageUploadDir, - $matches - )) { - $siteName = $matches[1]; - if ($siteName) { - $siteName = "/sites/$siteName/"; - $siteNamePos = strpos($dir, $siteName); - if ($siteNamePos !== FALSE) { - $siteRoot = substr($dir, 0, $siteNamePos); - } - } - } - } - - return array($url, $dir, $siteName, $siteRoot); - } - - /** - * Return likely default settings. - * @return array - * site settings - * - $url - * - $dir Base Directory - * - $siteName - * - $siteRoot - */ - public static function getBestGuessSettings() { - $config = CRM_Core_Config::singleton(); - - //CRM-15365 - Fix preg_replace to handle backslash for Windows File Paths - if (DIRECTORY_SEPARATOR == '\\') { - $needle = 'civicrm[/\\\\]templates_c[/\\\\].*$'; - } - else { - $needle = 'civicrm/templates_c/.*$'; - } - - $dir = preg_replace( - "|$needle|", - '', - $config->templateCompileDir - ); - - list($url, $siteName, $siteRoot) = $config->userSystem->getDefaultSiteSettings($dir); - return array($url, $dir, $siteName, $siteRoot); - } - /** * @param array $defaultValues * @@ -378,107 +228,27 @@ class CRM_Core_BAO_ConfigSetting { */ public static function doSiteMove($defaultValues = array()) { $moveStatus = ts('Beginning site move process...') . '
'; - // get the current and guessed values - list($oldURL, $oldDir, $oldSiteName, $oldSiteRoot) = self::getConfigSettings(); - list($newURL, $newDir, $newSiteName, $newSiteRoot) = self::getBestGuessSettings(); - - // retrieve these values from the argument list - $variables = array('URL', 'Dir', 'SiteName', 'SiteRoot', 'Val_1', 'Val_2', 'Val_3'); - $states = array('old', 'new'); - foreach ($variables as $varSuffix) { - foreach ($states as $state) { - $var = "{$state}{$varSuffix}"; - if (!isset($$var)) { - if (isset($defaultValues[$var])) { - $$var = $defaultValues[$var]; - } - else { - $$var = NULL; - } + $settings = Civi::settings(); + + foreach (array_merge(self::getPathSettings(), self::getUrlSettings()) as $key) { + $value = $settings->get($key); + if ($value && $value != $settings->getDefault($key)) { + if ($settings->getMandatory($key) === NULL) { + $settings->revert($key); + $moveStatus .= ts("WARNING: The setting (%1) has been reverted.", array( + 1 => $key, + )); + $moveStatus .= '
'; } - $$var = CRM_Utils_Request::retrieve($var, - 'String', - CRM_Core_DAO::$_nullArray, - FALSE, - $$var, - 'REQUEST' - ); - } - } - - $from = $to = array(); - foreach ($variables as $varSuffix) { - $oldVar = "old{$varSuffix}"; - $newVar = "new{$varSuffix}"; - //skip it if either is empty or both are exactly the same - if ($$oldVar && - $$newVar && - $$oldVar != $$newVar - ) { - $from[] = $$oldVar; - $to[] = $$newVar; - } - } - - $sql = " -SELECT config_backend -FROM civicrm_domain -WHERE id = %1 -"; - $params = array(1 => array(CRM_Core_Config::domainID(), 'Integer')); - $configBackend = CRM_Core_DAO::singleValueQuery($sql, $params); - if (!$configBackend) { - CRM_Core_Error::fatal(ts('Returning early due to unexpected error - civicrm_domain.config_backend column value is NULL. Try visiting CiviCRM Home page.')); - } - $configBackend = unserialize($configBackend); - - $configBackend = str_replace($from, - $to, - $configBackend - ); - - $configBackend = serialize($configBackend); - $sql = " -UPDATE civicrm_domain -SET config_backend = %2 -WHERE id = %1 -"; - $params[2] = array($configBackend, 'String'); - CRM_Core_DAO::executeQuery($sql, $params); - - // Apply the changes to civicrm_option_values - $optionGroups = array('url_preferences', 'directory_preferences'); - foreach ($optionGroups as $option) { - foreach ($variables as $varSuffix) { - $oldVar = "old{$varSuffix}"; - $newVar = "new{$varSuffix}"; - - $from = $$oldVar; - $to = $$newVar; - - if ($from && $to && $from != $to) { - $sql = ' -UPDATE civicrm_option_value -SET value = REPLACE(value, %1, %2) -WHERE option_group_id = ( - SELECT id - FROM civicrm_option_group - WHERE name = %3 ) -'; - $params = array( - 1 => array($from, 'String'), - 2 => array($to, 'String'), - 3 => array($option, 'String'), - ); - CRM_Core_DAO::executeQuery($sql, $params); + else { + $moveStatus .= ts("WARNING: The setting (%1) is overridden and could not be reverted.", array( + 1 => $key, + )); + $moveStatus .= '
'; } } } - $moveStatus .= - ts('Directory and Resource URLs have been updated in the moved database to reflect current site location.') . - '
'; - $config = CRM_Core_Config::singleton(); // clear the template_c and upload directory also @@ -636,4 +406,30 @@ WHERE option_group_id = ( return $params; } + /** + * @return array + */ + private static function getUrlSettings() { + return array( + 'userFrameworkResourceURL', + 'imageUploadURL', + 'customCSSURL', + 'extensionsURL', + ); + } + + /** + * @return array + */ + private static function getPathSettings() { + return array( + 'uploadDir', + 'imageUploadDir', + 'customFileUploadDir', + 'customTemplateDir', + 'customPHPPathDir', + 'extensionsDir', + ); + } + } diff --git a/templates/CRM/Admin/Form/Setting/UpdateConfigBackend.tpl b/templates/CRM/Admin/Form/Setting/UpdateConfigBackend.tpl index 126b7ea904..25148c4164 100644 --- a/templates/CRM/Admin/Form/Setting/UpdateConfigBackend.tpl +++ b/templates/CRM/Admin/Form/Setting/UpdateConfigBackend.tpl @@ -26,47 +26,24 @@

- {ts}Use this form if you need to reset the Base Directory Path and Base URL settings for your CiviCRM installation. These settings are stored in the database, and generally need adjusting after moving a CiviCRM installation to another location in the file system and/or to another URL.{/ts}

+ {ts}When migrating a site to a new server, the paths and URLs of your CiviCRM installation may change. {/ts} +

- {ts}CiviCRM will attempt to detect the new values that should be used. These are provided below as the default values for the New Base Directory and New Base URL fields.{/ts}

+ {capture assign="pathsURL"}{crmURL p="civicrm/admin/setting/path" q="reset=1"}{/capture} + {capture assign="urlsURL"}{crmURL p="civicrm/admin/setting/url" q="reset=1"}{/capture} + {ts 1=$pathsURL 2=$urlsURL}The old paths and URLs may be retained in some database records. Use this form to clear caches or to reset paths to their defaults. If you need further customizations, then update the Directories and Resource URLs.{/ts} +

{$form._qf_UpdateConfigBackend_next_cleanup.html} + + + {$form._qf_UpdateConfigBackend_next_resetpaths.html} +
- - - - - - - - - - - - - - - - - -{if $oldSiteName} - - - - - - - - -{/if} -
{ts}Old Base Directory{/ts}{$oldBaseDir}
{$form.newBaseDir.label}{$form.newBaseDir.html|crmAddClass:'huge'}
- {ts}For Drupal and WordPress installs, this is the absolute path to the location of the 'files' directory. For Joomla installs this is the absolute path to the location of the 'media' directory.{/ts}
{ts}Old Base URL{/ts}{$oldBaseURL}
{$form.newBaseURL.label}{$form.newBaseURL.html|crmAddClass:'huge'}
- {ts}This is the URL for your Drupal, Joomla or WordPress site (e.g. http://www.mysite.com/drupal/).{/ts}
{ts}Old Site Name{/ts}{$oldSiteName}
{$form.newSiteName.label}{$form.newSiteName.html|crmAddClass:'huge'}
- {ts}This is the your site name for a multisite install.{/ts}
{include file="CRM/common/formButtons.tpl" location="bottom"}