Skip to content

Commit

Permalink
performance: avoid unnecessary writes to oxconfigdisplay in case noth…
Browse files Browse the repository at this point in the history
…ing has changed

+ do not import/export theme configuration of not active themes
+ remove performance optimization that compares config value with the stored value to avoid unnecessary writes because that is now handled in oxid console
+ warn on export if config values are in theme and in general namespace and optionally fix it ( because that values in general namesapace may cause trouble and are useless in the export because the, normally they should be overwritten by the theme config anyway)
  • Loading branch information
keywan-ghadami-oxid committed Jul 21, 2018
1 parent e07b413 commit f147e45
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 37 deletions.
22 changes: 20 additions & 2 deletions Core/ConfigExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace OxidProfessionalServices\ModulesConfig\Core;

use OxidEsales\Eshop\Core\DatabaseProvider;
use Symfony\Component\Yaml\Yaml;

/**
Expand Down Expand Up @@ -252,10 +253,27 @@ protected function withoutDefaults(&$aGroupedValues)
if (array_key_exists('theme', $aShopConfig)) {
$aCurrentThemeConfigs = &$aShopConfig['theme'];
$aDefaultThemeConfigs = $this->aDefaultConfig['theme'];

$currentParentTheme = $aGeneralConfig['sTheme'];
$currentTheme = $aGeneralConfig['sCustomTheme'];

foreach ($aCurrentThemeConfigs as $sTheme => &$aThemeConfig) {
if ($sTheme != $currentParentTheme && $currentTheme != $sTheme) {
unset($aCurrentThemeConfigs[$sTheme]);
continue;
}

$aDefaultThemeConfig = $aDefaultThemeConfigs[$sTheme];
if ($aDefaultThemeConfig != null) {
foreach ($aThemeConfig as $sVarName => $mCurrentValue) {
foreach ($aThemeConfig as $sVarName => $mCurrentValue) {
if (array_key_exists($sVarName,$aGeneralConfig)) {
$this->output->writeln("config '$sVarName' is in theme and in gerneral namespace use --force-cleanup to repair");
if ($this->input->getOption('force-cleanup') ) {
$sSql = "DELETE FROM oxconfig WHERE oxmodule = '' AND oxvarname = ? AND oxshopid = ?";
DatabaseProvider::getDb()->execute($sSql,[$sVarName,$sShopId]);
}
unset($aGeneralConfig[$sVarName]);
}
if ($aDefaultThemeConfig != null) {
$mDefaultValue = $aDefaultThemeConfig[$sVarName];
if ($mCurrentValue === $mDefaultValue) {
unset($aThemeConfig[$sVarName]);
Expand Down
89 changes: 54 additions & 35 deletions Core/ConfigImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace OxidProfessionalServices\ModulesConfig\Core;

use OxidEsales\Eshop\Core\DatabaseProvider;
use OxidProfessionalServices\OxidConsole\Core\Module\ModuleStateFixer;
use OxidProfessionalServices\OxidConsole\Core\ShopConfig;
use OxidEsales\Eshop\Core\Registry;
Expand All @@ -38,7 +39,7 @@ class ConfigImport extends CommandBase
{

/**
* @var \oxConfig $oConfig
* @var ShopConfig $oConfig
*/
protected $oConfig;

Expand All @@ -55,6 +56,17 @@ class ConfigImport extends CommandBase
*/
protected $storedVarTypes = [];

protected $storedDisplayConfigHash = [];


public function init(){
parent::init();
$db = DatabaseProvider::getDb();
$hashValues = $db->getCol("SELECT md5(CONCAT(oxcfgmodule,'#', oxcfgvarname,'#', oxgrouping,'#', oxvarconstraint,'#', oxpos)) FROM oxconfigdisplay");
$this->storedDisplayConfigHash = array_fill_keys($hashValues, true);

}

/*
* executes all functionality which is necessary for a call of OXID console config:import
*
Expand Down Expand Up @@ -336,7 +348,7 @@ protected function importConfigValues($aConfigValues)
}
}
}
$oConfig->saveShopConfVar('arr','aDisabledModules',$aDisabledModules);
$this->saveShopVar('aDisabledModules', $aDisabledModules, '','arr');

foreach ($aModuleVersions as $sModuleId => $sVersion) {
if (!$oModule->load($sModuleId)) {
Expand Down Expand Up @@ -532,42 +544,39 @@ protected function getStoredVarTypes()
}
return $map;
}

public function getShopConfType($sVarName,$sSectionModule)
{
return $this->storedVarTypes[$sVarName.'+'.$sSectionModule];
}

protected function saveShopVar($sVarName, $mVarValue, $sSectionModule, $sVarType = null)

protected function saveShopVarWithTypeInfo($sVarName, $mVarValue, $sSectionModule){
list($sVarType, $mVarValue) = $this->getTypeAndValue($sVarName, $mVarValue);
$this->saveShopVar($sVarName, $mVarValue, $sSectionModule, $sVarType);
}


protected function saveShopVar($sVarName, $mVarValue, $sSectionModule, $sVarType)
{
$sShopId = $this->sShopId;
$oConfig = $this->oConfig;

$value = $oConfig->getShopConfVar($sVarName, $sShopId, $sSectionModule);
$type = $this->getShopConfType($sVarName,$sSectionModule);

if ($sVarType === null) {
list($sVarType, $mVarValue) = $this->getTypeAndValue($sVarName, $mVarValue);
if ($sVarType == 'bool') {
//internal representation of bool is 1 or ''
$value = $value ? '1' : '';
}
} else {
if ($sVarType == 'bool') {
//cleanup for some modules that have all kinds of bool representations in metadata.php
//so we can compare that value
$mVarValue = (($mVarValue == 'true' || $mVarValue) && $mVarValue && strcasecmp($mVarValue, "false"));
if ($sShopId != 1) {
$aOnlyMainShopVars = array_fill_keys(['blMallUsers', 'aSerials', 'IMD', 'IMA', 'IMS'],true);
if ($aOnlyMainShopVars[$sVarName]){
return;
}
}

if ($mVarValue !== $value || $sVarType !== $type) {
$oConfig->saveShopConfVar(
$sVarType,
$sVarName,
$mVarValue,
$sShopId,
$sSectionModule
);
}
$oConfig->saveShopConfVar(
$sVarType,
$sVarName,
$mVarValue,
$sShopId,
$sSectionModule
);

if(strpos($sSectionModule,'module') === 0) {
if($existsAlsoInGlobalNameSpace = $this->getShopConfType($sVarName,'')) {
$db = \oxDb::getDb();
Expand All @@ -594,15 +603,21 @@ protected function importThemeConfig($aThemes)
if ($aThemes == null) {
return;
}
$parentTheme = $this->oConfig->getConfigParam('sTheme');
$theme = $this->oConfig->getConfigParam('sCustomTheme');
foreach ($aThemes as $sThemeId => $aSettings) {
if ($sThemeId != $parentTheme && $theme != $sThemeId) {
$this->output->writeln("Theme $sThemeId from import from config file ignored because it is not active");
return;
}
$sSectionModule = "theme:$sThemeId";
foreach ($aSettings as $sVarName => $mVarValue) {
if(isset($mVarValue['value'])) {
$this->saveShopVar($sVarName, $mVarValue['value'], $sSectionModule);
$this->saveShopVarWithTypeInfo($sVarName, $mVarValue['value'], $sSectionModule);
$this->saveThemeDisplayVars($sVarName, $mVarValue, $sSectionModule);
}
else {
$this->saveShopVar($sVarName, $mVarValue, $sSectionModule);
$this->saveShopVarWithTypeInfo($sVarName, $mVarValue, $sSectionModule);
}
}
}
Expand Down Expand Up @@ -641,32 +656,36 @@ protected function restoreGeneralShopSettings($aConfigValues)
} elseif ($sVarName == 'aModuleVersions') {
$aModuleVersions = $mVarValue;
}
$this->saveShopVar($sVarName, $mTypedVarValue, $sSectionModule);
$this->saveShopVarWithTypeInfo($sVarName, $mTypedVarValue, $sSectionModule);
}
return $aModuleVersions;
}

protected function saveThemeDisplayVars($sVarName, $mVarValue, $sModule)
{
$oConfig = $this->oConfig;

$oDb = \oxDb::getDb();
$sModuleQuoted = $oDb->quote($sModule);
$sVarNameQuoted = $oDb->quote($sVarName);
$sVarConstraintsQuoted = isset($mVarValue['constraints']) ? $oDb->quote($mVarValue['constraints']) : '\'\'';
$sVarGroupingQuoted = isset($mVarValue['grouping']) ? $oDb->quote($mVarValue['grouping']) : '\'\'';
$sVarPosQuoted = isset($mVarValue['pos']) ? $oDb->quote($mVarValue['pos']) : '\'\'';
$constraints = $mVarValue['constraints'];
$sVarConstraintsQuoted = isset($constraints) ? $oDb->quote($constraints) : '\'\'';
$grouping = $mVarValue['grouping'];
$sVarGroupingQuoted = isset($grouping) ? $oDb->quote($grouping) : '\'\'';
$pos = $mVarValue['pos'];
$sVarPosQuoted = isset($pos) ? $oDb->quote($pos) : '\'\'';

$sNewOXIDdQuoted = $oDb->quote(\oxUtilsObject::getInstance()->generateUID());

if (isset($this->storedDisplayConfigHash[md5($sModule . '#' . $sVarName . '#' . $grouping. '#'. $constraints .'#'. $pos)])){
return;
}

$sQ = "delete from oxconfigdisplay WHERE OXCFGVARNAME = $sVarNameQuoted and OXCFGMODULE = $sModuleQuoted";
$oDb->execute($sQ);

$sQ = "insert into oxconfigdisplay (oxid, oxcfgmodule, oxcfgvarname, oxgrouping, oxvarconstraint, oxpos )
values($sNewOXIDdQuoted, $sModuleQuoted, $sVarNameQuoted, $sVarGroupingQuoted, $sVarConstraintsQuoted, $sVarPosQuoted)";
$oDb->execute($sQ);

$oConfig->executeDependencyEvent($sVarName);

}

Expand Down

0 comments on commit f147e45

Please sign in to comment.