Upgrader - Multiple fixes for new enableSimpleExtension()
authorTim Otten <totten@civicrm.org>
Mon, 18 Sep 2023 04:13:20 +0000 (21:13 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 18 Sep 2023 04:13:20 +0000 (21:13 -0700)
CRM/Upgrade/Incremental/Base.php

index e88b61dc9f3efad5fb1415ffb2317bd86484f0e0..d263842f936322a14cc17c64c06db3a775c4b70c 100644 (file)
@@ -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;
   }