From df7a19882229dd88ac11ed932b0da9c05da92d39 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 26 Sep 2019 15:34:53 -0400 Subject: [PATCH] Completely uninstall obsolete extensions during upgrade --- CRM/Extension/Manager.php | 15 ++++++++------ CRM/Upgrade/Form.php | 40 ++++++++++++++++++++++++++++-------- extension-compatibility.json | 20 +++++++++++++----- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/CRM/Extension/Manager.php b/CRM/Extension/Manager.php index 845c66f0be..152f54df21 100644 --- a/CRM/Extension/Manager.php +++ b/CRM/Extension/Manager.php @@ -215,11 +215,12 @@ class CRM_Extension_Manager { /** * Add records of the extension to the database -- and enable it * - * @param array $keys - * List of extension keys. + * @param string|array $keys + * One or more extension keys. * @throws CRM_Extension_Exception */ public function install($keys) { + $keys = (array) $keys; $origStatuses = $this->getStatuses(); // TODO: to mitigate the risk of crashing during installation, scan @@ -322,11 +323,12 @@ class CRM_Extension_Manager { /** * Disable extension without removing record from db. * - * @param array $keys - * List of extension keys. + * @param string|array $keys + * One or more extension keys. * @throws CRM_Extension_Exception */ public function disable($keys) { + $keys = (array) $keys; $origStatuses = $this->getStatuses(); // TODO: to mitigate the risk of crashing during installation, scan @@ -378,11 +380,12 @@ class CRM_Extension_Manager { /** * Remove all database references to an extension. * - * @param array $keys - * List of extension keys. + * @param string|array $keys + * One or more extension keys. * @throws CRM_Extension_Exception */ public function uninstall($keys) { + $keys = (array) $keys; $origStatuses = $this->getStatuses(); // TODO: to mitigate the risk of crashing during installation, scan diff --git a/CRM/Upgrade/Form.php b/CRM/Upgrade/Form.php index f37f682246..5880076991 100644 --- a/CRM/Upgrade/Form.php +++ b/CRM/Upgrade/Form.php @@ -637,25 +637,47 @@ SET version = '$version' } /** - * Disable any extensions not compatible with this new version. + * Disable/uninstall any extensions not compatible with this new version. * * @param \CRM_Queue_TaskContext $ctx * @param string $postUpgradeMessageFile * @return bool */ public static function disableOldExtensions(CRM_Queue_TaskContext $ctx, $postUpgradeMessageFile) { - $compatInfo = CRM_Extension_System::getCompatibilityInfo(); - $disabled = []; + $messages = []; $manager = CRM_Extension_System::singleton()->getManager(); - foreach ($compatInfo as $key => $ext) { - if (!empty($ext['obsolete']) && in_array($manager->getStatus($key), [$manager::STATUS_INSTALLED, $manager::STATUS_INSTALLED_MISSING])) { - $disabled[$key] = sprintf("
  • %s
  • ", ts('The extension %1 is now obsolete and has been disabled.', [1 => $key])); + foreach ($manager->getStatuses() as $key => $status) { + $obsolete = $manager->isIncompatible($key); + if ($obsolete) { + if (!empty($obsolete['disable']) && in_array($status, [$manager::STATUS_INSTALLED, $manager::STATUS_INSTALLED_MISSING])) { + try { + $manager->disable($key); + // Update the status for the sake of uninstall below. + $status = $status == $manager::STATUS_INSTALLED ? $manager::STATUS_DISABLED : $manager::STATUS_DISABLED_MISSING; + // This message is intentionally overwritten by uninstall below as it would be redundant + $messages[$key] = ts('The extension %1 is now obsolete and has been disabled.', [1 => $key]); + } + catch (CRM_Extension_Exception $e) { + $messages[] = ts('The obsolete extension %1 could not be removed due to an error. It is recommended to remove this extension manually.', [1 => $key]); + } + } + if (!empty($obsolete['uninstall']) && in_array($status, [$manager::STATUS_DISABLED, $manager::STATUS_DISABLED_MISSING])) { + try { + $manager->uninstall($key); + $messages[$key] = ts('The extension %1 is now obsolete and has been uninstalled.', [1 => $key]); + if ($status == $manager::STATUS_DISABLED) { + $messages[$key] .= ' ' . ts('You can remove it from your extensions directory.'); + } + } + catch (CRM_Extension_Exception $e) { + $messages[] = ts('The obsolete extension %1 could not be removed due to an error. It is recommended to remove this extension manually.', [1 => $key]); + } + } } } - if ($disabled) { - $manager->disable(array_keys($disabled)); + if ($messages) { file_put_contents($postUpgradeMessageFile, - '

    ', + '

    ', FILE_APPEND ); } diff --git a/extension-compatibility.json b/extension-compatibility.json index 1633610fa2..6b7c9c814e 100644 --- a/extension-compatibility.json +++ b/extension-compatibility.json @@ -1,17 +1,27 @@ { "org.civicrm.api4": { - "obsolete": "5.19" + "obsolete": "5.19", + "disable": true, + "uninstall": true }, "uk.squiffle.kam": { - "obsolete": "5.12" + "obsolete": "5.12", + "disable": true, + "uninstall": true }, "com.aghstrategies.slicknav": { - "obsolete": "5.12" + "obsolete": "5.12", + "disable": true, + "uninstall": true }, "de.systopia.recentitems": { - "obsolete": "5.12" + "obsolete": "5.12", + "disable": true, + "uninstall": true }, "com.ixiam.modules.quicksearch": { - "obsolete": "5.8" + "obsolete": "5.8", + "disable": true, + "uninstall": true } } -- 2.25.1