From 9b3fdc4a979731d35597a27f94dca18fbe692676 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 17 Sep 2023 21:13:20 -0700 Subject: [PATCH] Upgrader - Multiple fixes for new enableSimpleExtension() --- CRM/Upgrade/Incremental/Base.php | 58 +++++++++++++++++--------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/CRM/Upgrade/Incremental/Base.php b/CRM/Upgrade/Incremental/Base.php index e88b61dc9f..d263842f93 100644 --- a/CRM/Upgrade/Incremental/Base.php +++ b/CRM/Upgrade/Incremental/Base.php @@ -250,36 +250,35 @@ class CRM_Upgrade_Incremental_Base { */ public static function enableSimpleExtension(CRM_Queue_TaskContext $ctx, $keys): bool { $keys = (array) $keys; - $select = CRM_Utils_SQL_Select::from('civicrm_extension') - ->select(['full_name, is_active']) - ->where('full_name IN (@keys)', ['@keys' => $keys]) - ->toSQL(); - $all = CRM_Core_DAO::executeQuery($select, [], TRUE, NULL, FALSE, FALSE) - ->fetchMap('full_name', 'is_active'); - $toEnable = array_filter($all, function($isActive) { - return !$isActive; - }); + + // Find out current situation $system = CRM_Extension_System::singleton(); + $statuses = CRM_Utils_SQL_Select::from('civicrm_extension') + ->select(['full_name, is_active']) + ->execute(NULL, FALSE) + ->fetchAll(); + $byStatus = CRM_Utils_Array::index(['is_active', 'full_name'], $statuses); + $disabled = array_intersect($keys, array_keys($byStatus[0] ?? [])); + $uninstalled = array_diff($keys, array_keys($byStatus[0] ?? []), array_keys($byStatus[1] ?? [])); + + // Make a plan + $toUpdate = $disabled; $toInsert = []; - foreach ($keys as $key) { - if (empty($all[$key])) { - $info = $system->getMapper()->keyToInfo($key); - $path = $system->getMapper()->keyToPath($key); - $system->getClassLoader()->installExtension($info, $path); - } - if (!isset($all[$key])) { - $toInsert[] = [ - 'full_name' => $info->key, - 'type' => $info->type, - 'name' => $info->name, - 'label' => $info->label, - 'file' => $info->file, - 'is_active' => 1, - ]; - } + foreach ($uninstalled as $key) { + $info = $system->getMapper()->keyToInfo($key); + $toInsert[] = [ + 'full_name' => $info->key, + 'type' => $info->type, + 'name' => $info->name, + 'label' => $info->label, + 'file' => $info->file, + 'is_active' => 1, + ]; } - if ($toEnable) { - $updateSql = 'UPDATE civicrm_extension SET is_active = 1 WHERE full_name IN ("' . implode('", "', array_keys($toEnable)) . '")'; + + // Execute the plan + if ($toUpdate) { + $updateSql = 'UPDATE civicrm_extension SET is_active = 1 WHERE full_name IN ("' . implode('", "', array_keys($toUpdate)) . '")'; CRM_Core_DAO::executeQuery($updateSql, [], TRUE, NULL, FALSE, FALSE); } if ($toInsert) { @@ -288,6 +287,11 @@ class CRM_Upgrade_Incremental_Base { ->toSQL(); CRM_Core_DAO::executeQuery($insertSql, [], TRUE, NULL, FALSE, FALSE); } + foreach (array_merge($disabled, $uninstalled) as $key) { + $info = $system->getMapper()->keyToInfo($key); + $path = $system->getMapper()->keyToPath($key); + $system->getClassLoader()->installExtension($info, dirname($path)); + } return TRUE; } -- 2.25.1