From eaba1cfdeb189d19a5f2d8faef8ede0e7a22cbde Mon Sep 17 00:00:00 2001 From: "[OXID-PS] Keywan Ghadami" Date: Wed, 30 Jan 2019 17:54:58 +0100 Subject: [PATCH 1/2] clean extensions that are not used by any module this helps to repair things that are not not be recognized to belong to an installed module. e.g. when deleting a module from file system or making big changes like renaming or introducing composer autoloader. --- Core/ModuleStateFixer.php | 60 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/Core/ModuleStateFixer.php b/Core/ModuleStateFixer.php index f132932..9e6f9c6 100644 --- a/Core/ModuleStateFixer.php +++ b/Core/ModuleStateFixer.php @@ -42,6 +42,8 @@ public function disableInitialCacheClear(){ */ protected $module = null; + protected $dryRun = false; + /** * Fix module states task runs version, extend, files, templates, blocks, * settings and events information fix tasks @@ -81,6 +83,56 @@ public function fix($module, $oConfig = null) */ public function cleanUp() { $this->cleanUpControllers(); + $this->cleanUpExtensions(); + } + + /** + * remove extensions that are not registered by any module + */ + public function cleanUpExtensions(){ + + //get all extions from all metadata + $oxModuleList = oxNew('oxModuleList'); + $oxModuleList->getModulesFromDir(\oxRegistry::getConfig()->getModulesDir()); + $aModules = $oxModuleList->getList(); + + //get extensions from metadata + $allExtensionsFromMetadata = []; + foreach ($aModules as $module) { + $extensions = $module->getExtensions(); + foreach ($extensions as $k => $v) { + $allExtensionsFromMetadata[$v] = 1; + } + } + + //get all extesions from db + $extensionChainDb = $this->getConfig()->getConfigParam('aModules'); + $extensionChainDb = $oxModuleList->parseModuleChains($extensionChainDb); + + $trash = []; + //calculate trash as extensions that are only in db + foreach ($extensionChainDb as $oxidClass => $arrayOfExtendingClasses) { + + foreach ($arrayOfExtendingClasses as $index => $extendingClass){ + if (!isset($allExtensionsFromMetadata[$extendingClass])) { + $trash[] = [$oxidClass, $extendingClass]; + } + } + } + + //remove diff + foreach ($trash as $item){ + list($oxidClass, $extendingClass) = $item; + $this->output->error("wrong extension found $extendingClass (registered for $oxidClass)"); + $key = array_search($extendingClass, $extensionChainDb[$oxidClass]); + unset($extensionChainDb[$oxidClass][$key]); + } + + if (!$dryRun) { + $extensionChainDb = $this->buildModuleChains($extensionChainDb); + $this->_saveToConfig('aModules', $extensionChainDb); + } + } /** @@ -399,9 +451,10 @@ private function getAllControllers() public function cleanUpControllers(){ $allFromDb = $this->getAllControllers(); + //? is aModuleVersions fixed already in that place $aVersions = (array) $this->getConfig()->getConfigParam('aModuleVersions'); $aVersions = array_change_key_case($aVersions,CASE_LOWER); - $cleaned = array_intersect_key($allFromDb,$aVersions); + $cleaned = array_intersect_key($allFromDb, $aVersions); if ($this->diff($allFromDb, $cleaned)) { $this->needCacheClear = true; $this->output->error(" cleaning up controllers"); @@ -558,9 +611,9 @@ protected function _addTemplateBlocks($moduleBlocks, $moduleId) protected function checkExtensions(\OxidEsales\Eshop\Core\Module\Module $module, &$aModulesDefault, &$aModules) { $aModulesDefault = $this->getConfig()->getConfigParam('aModules'); + //in case someone deleted values from the db using a empty array avoids php warnings + $aModulesDefault = is_null($aModulesDefault) ? [] : $aModulesDefault; $aModules = $this->getModulesWithExtendedClass(); - $aModules = $this->_removeNotUsedExtensions($aModules, $module); - if ($module->hasExtendClass()) { $this->validateMetadataExtendSection($module); @@ -600,4 +653,3 @@ public function getLogger(){ } } - From f6eecbf72cc918ebfef2302323508fc18063b2c9 Mon Sep 17 00:00:00 2001 From: "[OXID-PS] Keywan Ghadami" Date: Thu, 31 Jan 2019 11:29:34 +0100 Subject: [PATCH 2/2] Fix CodeReview findings --- Core/ModuleStateFixer.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Core/ModuleStateFixer.php b/Core/ModuleStateFixer.php index 9e6f9c6..db3983b 100644 --- a/Core/ModuleStateFixer.php +++ b/Core/ModuleStateFixer.php @@ -96,12 +96,12 @@ public function cleanUpExtensions(){ $oxModuleList->getModulesFromDir(\oxRegistry::getConfig()->getModulesDir()); $aModules = $oxModuleList->getList(); - //get extensions from metadata - $allExtensionsFromMetadata = []; + //get extensions from metadata file + $moduleClassesMf = []; foreach ($aModules as $module) { $extensions = $module->getExtensions(); - foreach ($extensions as $k => $v) { - $allExtensionsFromMetadata[$v] = 1; + foreach ($extensions as $moduleClass) { + $moduleClassesMf[$moduleClass] = 1; } } @@ -113,8 +113,8 @@ public function cleanUpExtensions(){ //calculate trash as extensions that are only in db foreach ($extensionChainDb as $oxidClass => $arrayOfExtendingClasses) { - foreach ($arrayOfExtendingClasses as $index => $extendingClass){ - if (!isset($allExtensionsFromMetadata[$extendingClass])) { + foreach ($arrayOfExtendingClasses as $extendingClass){ + if (!isset($moduleClassesMf[$extendingClass])) { $trash[] = [$oxidClass, $extendingClass]; } } @@ -128,7 +128,7 @@ public function cleanUpExtensions(){ unset($extensionChainDb[$oxidClass][$key]); } - if (!$dryRun) { + if (!$this->dryRun) { $extensionChainDb = $this->buildModuleChains($extensionChainDb); $this->_saveToConfig('aModules', $extensionChainDb); }