Skip to content

Commit 0434f2f

Browse files
Merge pull request #94 from OXIDprojects/keywan-ghadami-oxid-patch-2
less invasive method of fixing the module blocks
2 parents 8d01624 + 6abcde4 commit 0434f2f

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

Core/ModuleStateFixer.php

+34-10
Original file line numberDiff line numberDiff line change
@@ -568,14 +568,13 @@ protected function _addExtensions(\OxidEsales\Eshop\Core\Module\Module $module)
568568
}
569569
}
570570

571-
572-
573571
/**
574572
* Add module templates to database.
575573
*
576-
* @deprecated please use setTemplateBlocks this method will be removed because
577-
* the combination of deleting and adding does unnessery writes and so it does not scale
578-
* also it's more likely to get race conditions (in the moment the blocks are deleted)
574+
* this method sets template blocks with less conflicting writes as possible
575+
* to reduce race conditions (in the moment the blocks are deleted)
576+
* it will only report major changes even if id may be changed to ensure consistency of the data
577+
*
579578
*
580579
* @param array $moduleBlocks Module blocks array
581580
* @param string $moduleId Module id
@@ -586,16 +585,41 @@ protected function _addTemplateBlocks($moduleBlocks, $moduleId)
586585
$moduleBlocks = array();
587586
}
588587
$shopId = Registry::getConfig()->getShopId();
589-
$db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb();
588+
$db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
590589
$knownBlocks = ['dummy']; // Start with a dummy value to prevent having an empty list in the NOT IN statement.
591590
$rowsEffected = 0;
591+
$select_sql = "SELECT `OXID`, `OXTHEME` as `theme`, `OXTEMPLATE` as `template`, `OXBLOCKNAME` as `block`, `OXPOS` as `position`, `OXFILE` as `file`
592+
FROM `oxtplblocks`
593+
WHERE `OXMODULE` = ? AND `OXSHOPID` = ?";
594+
$existingBlocks = $db->getAll($select_sql,[$moduleId,$shopId]);
595+
596+
foreach ($existingBlocks as $block) {
597+
$id = $block['OXID'];
598+
$block['position'] = (int) $block['position'];
599+
600+
unset($block['OXID']);
601+
ksort($block);
602+
$str1 = $moduleId . json_encode($block) . $shopId;
603+
$wellGeneratedId = md5($str1);
604+
if ($id !== $wellGeneratedId) {
605+
$sql = "UPDATE IGNORE `oxtplblocks` SET OXID = ? WHERE OXID = ?";
606+
$this->output->debug("$sql, $wellGeneratedId, $id");
607+
$db->execute($sql, [$wellGeneratedId, $id]);
608+
}
609+
}
610+
611+
592612
foreach ($moduleBlocks as $moduleBlock) {
593-
$blockId = md5($moduleId . json_encode($moduleBlock) . $shopId);
613+
$moduleBlock['theme'] = $moduleBlock['theme'] ?? '';
614+
$moduleBlock['position'] = $position = isset($moduleBlock['position']) && is_numeric($moduleBlock['position']) ?
615+
(int) $moduleBlock['position'] : 1;
616+
ksort($moduleBlock);
617+
618+
$str = $moduleId . json_encode($moduleBlock) . $shopId;
619+
$blockId = md5($str);
594620
$knownBlocks[] = $blockId;
595621

596622
$template = $moduleBlock["template"];
597-
$position = isset($moduleBlock['position']) && is_numeric($moduleBlock['position']) ?
598-
intval($moduleBlock['position']) : 1;
599623

600624
$block = $moduleBlock["block"];
601625
$filePath = $moduleBlock["file"];
@@ -638,7 +662,7 @@ protected function _addTemplateBlocks($moduleBlocks, $moduleId)
638662
$this->output->info("fixed template blocks for module " . $moduleId);
639663
}
640664
}
641-
665+
642666
/**
643667
* @param Module $module
644668
* @param $aModulesDefault

0 commit comments

Comments
 (0)