diff --git a/setup/extensionsmap.class.inc.php b/setup/extensionsmap.class.inc.php index d0cf51851f..0e8fca1ebd 100644 --- a/setup/extensionsmap.class.inc.php +++ b/setup/extensionsmap.class.inc.php @@ -110,6 +110,21 @@ public function __construct() $this->bVisible = true; $this->aMissingDependencies = array(); } + + /** + * @since 3.3.0 + * @return bool + */ + public function CanBeUninstalled() + { + foreach ($this->aModuleInfo as $sModuleCode => $aModuleInfo) { + $bUninstallable = $aModuleInfo['uninstallable'] === 'yes'; + if (!$bUninstallable) { + return false; + } + } + return true; + } } /** @@ -253,6 +268,22 @@ protected function AddExtension(iTopExtension $oNewExtension) $this->aExtensions[$oNewExtension->sCode.'/'.$oNewExtension->sVersion] = $oNewExtension; } + /** + * @since 3.3.0 + * @param string $sExtensionCode + * + * @return \iTopExtension|null + */ + public function Get(string $sExtensionCode):?iTopExtension + { + foreach($this->aExtensions as $oExtension) { + if ($oExtension->sCode === $sExtensionCode) { + return $oExtension; + } + } + return null; + } + /** * Read (recursively) a directory to find if it contains extensions (or modules) * @@ -277,8 +308,7 @@ protected function ReadDir($sSearchDir, $sSource, $sParentExtensionId = null) $aSubDirectories = array(); // First check if there is an extension.xml file in this directory - if (is_readable($sSearchDir.'/extension.xml')) - { + if (is_readable($sSearchDir.'/extension.xml')) { $oXml = new XMLParameters($sSearchDir.'/extension.xml'); $oExtension = new iTopExtension(); $oExtension->sCode = $oXml->Get('extension_code'); @@ -315,28 +345,27 @@ protected function ReadDir($sSearchDir, $sSource, $sParentExtensionId = null) // If we are not already inside a formal extension, then the module itself is considered // as an extension, otherwise, the module is just added to the list of modules belonging // to this extension - $sModuleId = $aModuleInfo[1]; + $sModuleId = $aModuleInfo[ModuleFileReader::MODULE_INFO_ID]; list($sModuleName, $sModuleVersion) = ModuleDiscovery::GetModuleName($sModuleId); - if ($sModuleVersion == '') - { + if ($sModuleVersion == '') { // Provide a default module version since version is mandatory when recording ExtensionInstallation $sModuleVersion = '0.0.1'; } + $aModuleInfo[ModuleFileReader::MODULE_INFO_CONFIG]['uninstallable'] ??= 'yes'; if (($sParentExtensionId !== null) && (array_key_exists($sParentExtensionId, $this->aExtensions)) && ($this->aExtensions[$sParentExtensionId] instanceof iTopExtension)) { // Already inside an extension, let's add this module the list of modules belonging to this extension $this->aExtensions[$sParentExtensionId]->aModules[] = $sModuleName; $this->aExtensions[$sParentExtensionId]->aModuleVersion[$sModuleName] = $sModuleVersion; - $this->aExtensions[$sParentExtensionId]->aModuleInfo[$sModuleName] = $aModuleInfo[2]; + $this->aExtensions[$sParentExtensionId]->aModuleInfo[$sModuleName] = $aModuleInfo[ModuleFileReader::MODULE_INFO_CONFIG]; } - else - { + else { // Not already inside an folder containing an 'extension.xml' file // Ignore non-visible modules and auto-select ones, since these are never prompted // as a choice to the end-user $bVisible = true; - if (!$aModuleInfo[2]['visible'] || isset($aModuleInfo[2]['auto_select'])) + if (!$aModuleInfo[ModuleFileReader::MODULE_INFO_CONFIG]['visible'] || isset($aModuleInfo[ModuleFileReader::MODULE_INFO_CONFIG]['auto_select'])) { $bVisible = false; } @@ -344,15 +373,15 @@ protected function ReadDir($sSearchDir, $sSource, $sParentExtensionId = null) // Let's create a "fake" extension from this module (containing just this module) for backwards compatibility $oExtension = new iTopExtension(); $oExtension->sCode = $sModuleName; - $oExtension->sLabel = $aModuleInfo[2]['label']; + $oExtension->sLabel = $aModuleInfo[ModuleFileReader::MODULE_INFO_CONFIG]['label']; $oExtension->sDescription = ''; $oExtension->sVersion = $sModuleVersion; $oExtension->sSource = $sSource; - $oExtension->bMandatory = $aModuleInfo[2]['mandatory']; - $oExtension->sMoreInfoUrl = $aModuleInfo[2]['doc.more_information']; + $oExtension->bMandatory = $aModuleInfo[ModuleFileReader::MODULE_INFO_CONFIG]['mandatory']; + $oExtension->sMoreInfoUrl = $aModuleInfo[ModuleFileReader::MODULE_INFO_CONFIG]['doc.more_information']; $oExtension->aModules = array($sModuleName); $oExtension->aModuleVersion[$sModuleName] = $sModuleVersion; - $oExtension->aModuleInfo[$sModuleName] = $aModuleInfo[2]; + $oExtension->aModuleInfo[$sModuleName] = $aModuleInfo[ModuleFileReader::MODULE_INFO_CONFIG]; $oExtension->sSourceDir = $sSearchDir; $oExtension->bVisible = $bVisible; $this->AddExtension($oExtension); @@ -452,6 +481,7 @@ public function MarkAsChosen($sExtensionCode, $bMark = true) } } + /** * Tells if a given extension(code) is marked as chosen * @param string $sExtensionCode @@ -572,30 +602,24 @@ public function IsExtensionObsoletedByAnother(iTopExtension $oExtension) public function NormalizeOldExtensions($sInSourceOnly = iTopExtension::SOURCE_MANUAL) { $aSignatures = $this->GetOldExtensionsSignatures(); - foreach($aSignatures as $sExtensionCode => $aExtensionSignatures) - { + foreach($aSignatures as $sExtensionCode => $aExtensionSignatures) { $bFound = false; - foreach($aExtensionSignatures['versions'] as $sVersion => $aModules) - { + foreach($aExtensionSignatures['versions'] as $sVersion => $aModules) { $bInstalled = true; - foreach($aModules as $sModuleId) - { - if(!$this->ModuleIsPresent($sModuleId, $sInSourceOnly)) - { + foreach($aModules as $sModuleId) { + if(!$this->ModuleIsPresent($sModuleId, $sInSourceOnly)) { $bFound = false; break; // One missing module is enough to determine that the extension/version is not present } - else - { - $bInstalled = $bInstalled && (!$this->ModuleIsInstalled($sModuleId, $sInSourceOnly)); + else { + $bInstalled = $bInstalled && $this->ModuleIsInstalled($sModuleId, $sInSourceOnly); $bFound = true; } } if ($bFound) break; // The current version matches the signature } - if ($bFound) - { + if ($bFound) { $oExtension = new iTopExtension(); $oExtension->sCode = $sExtensionCode; $oExtension->sLabel = $aExtensionSignatures['label']; @@ -603,15 +627,14 @@ public function NormalizeOldExtensions($sInSourceOnly = iTopExtension::SOURCE_MA $oExtension->sDescription = $aExtensionSignatures['description']; $oExtension->sVersion = $sVersion; $oExtension->aModules = array(); - if ($bInstalled) - { + if ($bInstalled) { $oExtension->sInstalledVersion = $sVersion; $oExtension->bMarkedAsChosen = true; } - foreach($aModules as $sModuleId) - { + foreach($aModules as $sModuleId) { list($sModuleName, $sModuleVersion) = ModuleDiscovery::GetModuleName($sModuleId); $oExtension->aModules[] = $sModuleName; + $oExtension->aModuleInfo[$sModuleName] = $this->aExtensions[$sModuleId]->aModuleInfo[$sModuleName]; } $this->ReplaceModulesByNormalizedExtension($aExtensionSignatures['versions'][$sVersion], $oExtension); } diff --git a/setup/modulediscovery.class.inc.php b/setup/modulediscovery.class.inc.php index 6340f6654c..89cc864401 100644 --- a/setup/modulediscovery.class.inc.php +++ b/setup/modulediscovery.class.inc.php @@ -520,7 +520,7 @@ protected static function ListModuleFiles($sRelDir, $sRootDir) $sModuleFilePath = $sDirectory.'/'.$sFile; try { $aModuleInfo = ModuleFileReader::GetInstance()->ReadModuleFileInformation($sDirectory.'/'.$sFile); - SetupWebPage::AddModule($sModuleFilePath, $aModuleInfo[1], $aModuleInfo[2]); + SetupWebPage::AddModule($sModuleFilePath, $aModuleInfo[ModuleFileReader::MODULE_INFO_ID], $aModuleInfo[ModuleFileReader::MODULE_INFO_CONFIG]); } catch(ModuleFileReaderException $e){ continue; } diff --git a/setup/modulediscovery/ModuleFileReader.php b/setup/modulediscovery/ModuleFileReader.php index c5a7412a30..b09d3ef3e7 100644 --- a/setup/modulediscovery/ModuleFileReader.php +++ b/setup/modulediscovery/ModuleFileReader.php @@ -32,6 +32,10 @@ class ModuleFileReader { "method_exists" ]; + const MODULE_INFO_PATH = 0; + const MODULE_INFO_ID = 1; + const MODULE_INFO_CONFIG = 2; + const STATIC_CALLWHITELIST=[ "utils::GetItopVersionWikiSyntax" ]; @@ -168,7 +172,7 @@ public function ReadModuleFileInformationUnsafe(string $sModuleFilePath) : array private function CompleteModuleInfoWithFilePath(array &$aModuleInfo) { if (count($aModuleInfo)==3) { - $aModuleInfo[2]['module_file_path'] = $aModuleInfo[0]; + $aModuleInfo[static::MODULE_INFO_CONFIG]['module_file_path'] = $aModuleInfo[static::MODULE_INFO_PATH]; } } @@ -255,9 +259,9 @@ private function GetModuleInformationFromAddModuleCall(string $sModuleFilePath, } return [ - $sModuleFilePath, - $sModuleId, - $aModuleConfig, + static::MODULE_INFO_PATH => $sModuleFilePath, + static::MODULE_INFO_ID => $sModuleId, + static::MODULE_INFO_CONFIG => $aModuleConfig, ]; } diff --git a/setup/moduleinstallation.class.inc.php b/setup/moduleinstallation.class.inc.php index 535f22daf3..823d0283c0 100644 --- a/setup/moduleinstallation.class.inc.php +++ b/setup/moduleinstallation.class.inc.php @@ -48,7 +48,7 @@ public static function Init() MetaModel::Init_AddAttribute(new AttributeDateTime("installed", array("allowed_values" => null, "sql" => "installed", "default_value" => null, "is_null_allowed" => true, "depends_on" => array()))); MetaModel::Init_AddAttribute(new AttributeText("comment", array("allowed_values" => null, "sql" => "comment", "default_value" => null, "is_null_allowed" => true, "depends_on" => array()))); MetaModel::Init_AddAttribute(new AttributeExternalKey("parent_id", array("targetclass" => "ModuleInstallation", "jointype" => "", "allowed_values" => null, "sql" => "parent_id", "is_null_allowed" => true, "on_target_delete" => DEL_MANUAL, "depends_on" => array()))); - + MetaModel::Init_AddAttribute(new AttributeEnum("uninstallable", array("allowed_values"=>new ValueSetEnum('yes,no,maybe'), "sql"=>"uninstallable", "default_value"=>'yes', "is_null_allowed"=>false, "depends_on"=>array()))); // Display lists MetaModel::Init_SetZListItems('details', array('name', 'version', 'installed', 'comment', 'parent_id')); // Attributes to be displayed for the complete details @@ -87,6 +87,7 @@ public static function Init() MetaModel::Init_AddAttribute(new AttributeString("label", array("allowed_values"=>null, "sql"=>"label", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array()))); MetaModel::Init_AddAttribute(new AttributeString("version", array("allowed_values"=>null, "sql"=>"version", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array()))); MetaModel::Init_AddAttribute(new AttributeString("source", array("allowed_values"=>null, "sql"=>"source", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array()))); + MetaModel::Init_AddAttribute(new AttributeEnum("uninstallable", array("allowed_values"=>new ValueSetEnum('yes,no,maybe'), "sql"=>"uninstallable", "default_value"=>'yes', "is_null_allowed"=>false, "depends_on"=>array()))); MetaModel::Init_AddAttribute(new AttributeDateTime("installed", array("allowed_values"=>null, "sql"=>"installed", "default_value"=>'NOW()', "is_null_allowed"=>false, "depends_on"=>array()))); diff --git a/setup/runtimeenv.class.inc.php b/setup/runtimeenv.class.inc.php index 0051ddaaaa..e8052c434a 100644 --- a/setup/runtimeenv.class.inc.php +++ b/setup/runtimeenv.class.inc.php @@ -756,6 +756,7 @@ public function RecordInstallation(Config $oConfig, $sDataModelVersion, $aSelect $aModuleData = $aAvailableModules[$sModuleId]; $sName = $sModuleId; $sVersion = $aModuleData['version_code']; + $sUninstallable = $aModuleData['uninstallable'] ?? 'yes'; $aComments = array(); $aComments[] = $sShortComment; if ($aModuleData['mandatory']) { @@ -783,6 +784,7 @@ public function RecordInstallation(Config $oConfig, $sDataModelVersion, $aSelect $oInstallRec->Set('comment', $sComment); $oInstallRec->Set('parent_id', $iMainItopRecord); $oInstallRec->Set('installed', $iInstallationTime); + $oInstallRec->Set('uninstallable', $sUninstallable); $oInstallRec->DBInsertNoReload(); } @@ -805,6 +807,7 @@ public function RecordInstallation(Config $oConfig, $sDataModelVersion, $aSelect $oInstallRec->Set('label', $oExtension->sLabel); $oInstallRec->Set('version', $oExtension->sVersion); $oInstallRec->Set('source', $oExtension->sSource); + $oInstallRec->Set('uninstallable', $oExtension->CanBeUninstalled() ? 'yes' : 'no'); $oInstallRec->Set('installed', $iInstallationTime); $oInstallRec->DBInsertNoReload(); } diff --git a/setup/wizardsteps.class.inc.php b/setup/wizardsteps.class.inc.php index d954d4ecd9..88c290c113 100644 --- a/setup/wizardsteps.class.inc.php +++ b/setup/wizardsteps.class.inc.php @@ -997,6 +997,26 @@ final protected function AddUseSymlinksFlagOption(WebPage $oPage): void ); } } + + final protected function AddForceUninstallFlagOption(WebPage $oPage): void + { + $sChecked = $this->oWizard->GetParameter('force-uninstall', false) ? ' checked ' : ''; + $oPage->add('
'); + $oPage->add('Advanced parameters'); + $oPage->p('
'); + + $oPage->add_ready_script(<<<'JS' +$("#force-uninstall").on("click", function() { + let $this = $(this); + let bForceUninstall = $this.prop("checked"); + if( bForceUninstall && !confirm('Beware, uninstalling extensions flagged as non uninstallable may result in data corruption and application crashes. Are you sure you want to continue ?')){ + $this.prop("checked",false); + } +}); +JS + ); + } } @@ -1181,6 +1201,7 @@ public function ProcessParams($bMoveForward = true) { $this->oWizard->SaveParameter('application_url', ''); $this->oWizard->SaveParameter('graphviz_path', ''); + $this->oWizard->SaveParameter('force-uninstall', false); return array('class' => 'WizStepModulesChoice', 'state' => 'start_upgrade'); } @@ -1223,6 +1244,7 @@ public function Display(WebPage $oPage) ); $this->AddUseSymlinksFlagOption($oPage); + $this->AddForceUninstallFlagOption($oPage); } public function AsyncAction(WebPage $oPage, $sCode, $aParameters) @@ -1436,6 +1458,7 @@ protected function DisplayStep($oPage) $oPage->add_style("div.choice a { text-decoration:none; font-weight: bold; color: #1C94C4 }"); $oPage->add_style("div.description { margin-left: 2em; }"); $oPage->add_style(".choice-disabled { color: #999; }"); + $oPage->add_style("input.unremovable { accent-color: orangered;}"); $aModules = SetupUtils::AnalyzeInstallation($this->oWizard); $sManualInstallError = SetupUtils::CheckManualInstallDirEmpty($aModules, @@ -1931,7 +1954,7 @@ protected function GetStepInfo($idx = null) if (@file_exists($this->GetSourceFilePath())) { - // Found an "installation.xml" file, let's us tis definition for the wizard + // Found an "installation.xml" file, let's use this definition for the wizard $aParams = new XMLParameters($this->GetSourceFilePath()); $aSteps = $aParams->Get('steps', array()); @@ -2031,55 +2054,45 @@ protected function DisplayOptions($oPage, $aStepInfo, $aSelectedComponents, $aDe { $aOptions = isset($aStepInfo['options']) ? $aStepInfo['options'] : array(); $aAlternatives = isset($aStepInfo['alternatives']) ? $aStepInfo['alternatives'] : array(); - $index = 0; - $sAllDisabled = ''; - if ($bAllDisabled) { - $sAllDisabled = 'disabled data-disabled="disabled" '; - } + $bDisableUninstallCheck = (bool)$this->oWizard->GetParameter('force-uninstall', false); foreach ($aOptions as $index => $aChoice) { - $sAttributes = ''; $sChoiceId = $sParentId.self::$SEP.$index; $sDataId = 'data-id="'.utils::EscapeHtml($aChoice['extension_code']).'"'; $sId = utils::EscapeHtml($aChoice['extension_code']); $bIsDefault = array_key_exists($sChoiceId, $aDefaults); + + $bCanBeUninstalled = $this->oExtensionsMap->Get($aChoice['extension_code'])->CanBeUninstalled(); $bSelected = isset($aSelectedComponents[$sChoiceId]) && ($aSelectedComponents[$sChoiceId] == $sChoiceId); - $bMandatory = (isset($aChoice['mandatory']) && $aChoice['mandatory']) || ($this->bUpgrade && $bIsDefault); - $bDisabled = false; - if ($bMandatory) { - $oPage->add('
 '); - $bDisabled = true; - } else if ($bSelected) { - $oPage->add('
 '); - } else { - $oPage->add('
 '); - } - $this->DisplayChoice($oPage, $aChoice, $aSelectedComponents, $aDefaults, $sChoiceId, $bDisabled); + $bMandatory = (isset($aChoice['mandatory']) && $aChoice['mandatory']) || $this->bUpgrade && $bIsDefault && !$bCanBeUninstalled && !$bDisableUninstallCheck;; + $bDisabled = $bMandatory || $bAllDisabled; + $bChecked = $bMandatory || $bSelected; + $sChecked = $bChecked ? ' checked ' : ''; + $sDisabled = $bDisabled ? ' disabled data-disabled="disabled" ' : ''; + $sUnremovable = !$bCanBeUninstalled ? ' unremovable ' : ''; + $sHiddenInput = $bDisabled && $bChecked ? '' : ''; + $oPage->add('
'.$sHiddenInput.' '); + $this->DisplayChoice($oPage, $aChoice, $aSelectedComponents, $aDefaults, $sChoiceId, $bDisabled, $bCanBeUninstalled); $oPage->add('
'); - $index++; } $sChoiceName = null; $sDisabled = ''; $bDisabled = false; $sChoiceIdNone = null; - foreach($aAlternatives as $index => $aChoice) - { + foreach($aAlternatives as $index => $aChoice) { $sChoiceId = $sParentId.self::$SEP.$index; - if ($sChoiceName == null) - { + if ($sChoiceName == null) { $sChoiceName = $sChoiceId; // All radios share the same name } $bIsDefault = array_key_exists($sChoiceName, $aDefaults) && ($aDefaults[$sChoiceName] == $sChoiceId); $bMandatory = (isset($aChoice['mandatory']) && $aChoice['mandatory']) || ($this->bUpgrade && $bIsDefault); - if ($bMandatory || $bAllDisabled) - { + if ($bMandatory || $bAllDisabled) { // One choice is mandatory, all alternatives are disabled $sDisabled = ' disabled data-disabled="disabled"'; $bDisabled = true; } - if ( (!isset($aChoice['sub_options']) || (count($aChoice['sub_options']) == 0)) && (!isset($aChoice['modules']) || (count($aChoice['modules']) == 0)) ) - { + if ( (!isset($aChoice['sub_options']) || (count($aChoice['sub_options']) == 0)) && (!isset($aChoice['modules']) || (count($aChoice['modules']) == 0)) ) { $sChoiceIdNone = $sChoiceId; // the "None" / empty choice } } @@ -2111,24 +2124,24 @@ protected function DisplayOptions($oPage, $aStepInfo, $aSelectedComponents, $aDe $sAttributes = ' checked '; } $sHidden = ''; - if ($bMandatory && $bDisabled) - { + if ($bMandatory && $bDisabled) { $sAttributes = ' checked '; $sHidden = ''; } $oPage->add('
'.$sHidden.' '); $this->DisplayChoice($oPage, $aChoice, $aSelectedComponents, $aDefaults, $sChoiceId, $bDisabled && !$bSelected); $oPage->add('
'); - $index++; } } - protected function DisplayChoice($oPage, $aChoice, $aSelectedComponents, $aDefaults, $sChoiceId, $bDisabled = false) + protected function DisplayChoice($oPage, $aChoice, $aSelectedComponents, $aDefaults, $sChoiceId, $bDisabled = false, $bUninstallable = true) { $sMoreInfo = (isset($aChoice['more_info']) && ($aChoice['more_info'] != '')) ? 'More information' : ''; $sSourceLabel = isset($aChoice['source_label']) ? $aChoice['source_label'] : ''; $sId = utils::EscapeHtml($aChoice['extension_code']); - $oPage->add(' '.$sMoreInfo); + $sUninstallationWarning = $bUninstallable ? '' : '(!)'; + + $oPage->add(' '.$sUninstallationWarning.' '.$sMoreInfo.''); $sDescription = isset($aChoice['description']) ? utils::EscapeHtml($aChoice['description']) : ''; $oPage->add('
'.$sDescription.''); if (isset($aChoice['sub_options'])) {