Skip to content
71 changes: 48 additions & 23 deletions setup/extensionsmap.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ public function __construct()
$this->bVisible = true;
$this->aMissingDependencies = array();
}

public function IsUninstallable()
{
foreach ($this->aModuleInfo as $sModuleCode => $aModuleInfo) {
$bUninstallable = $aModuleInfo['uninstallable'] === 'yes';
if (!$bUninstallable) {
Comment on lines +121 to +122
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$bUninstallable = $aModuleInfo['uninstallable'] === 'yes';
if (!$bUninstallable) {
if (!filter_var($aModuleInfo['uninstallable'], FILTER_VALIDATE_BOOLEAN)) {

return false;
}
}
return true;
}
}

/**
Expand Down Expand Up @@ -253,6 +264,16 @@ protected function AddExtension(iTopExtension $oNewExtension)
$this->aExtensions[$oNewExtension->sCode.'/'.$oNewExtension->sVersion] = $oNewExtension;
}

public function Get($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)
*
Expand All @@ -277,8 +298,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');
Expand Down Expand Up @@ -317,20 +337,19 @@ protected function ReadDir($sSearchDir, $sSource, $sParentExtensionId = null)
// to this extension
$sModuleId = $aModuleInfo[1];
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[2]['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];
}
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
Expand Down Expand Up @@ -452,6 +471,17 @@ public function MarkAsChosen($sExtensionCode, $bMark = true)
}
}


public function MarkAsUninstallable($sExtensionCode, $bMark = true)
{
foreach($this->aExtensions as $oExtension) {
if ($oExtension->sCode == $sExtensionCode) {
$oExtension->bUninstallable = $bMark;
break;
}
}
}

/**
* Tells if a given extension(code) is marked as chosen
* @param string $sExtensionCode
Expand Down Expand Up @@ -530,6 +560,8 @@ public function LoadChoicesFromDatabase(Config $oConfig)
foreach($aInstalledExtensions as $aDBInfo)
{
$this->MarkAsChosen($aDBInfo['code']);
$sUninstallable = $aDBInfo['uninstallable'] ?? 'yes';
$this->MarkAsUninstallable($sUninstallable);
$this->SetInstalledVersion($aDBInfo['code'], $aDBInfo['version']);
}
return true;
Expand Down Expand Up @@ -572,46 +604,39 @@ 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'];
$oExtension->sSource = $sInSourceOnly;
$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);
}
Expand Down
3 changes: 2 additions & 1 deletion setup/moduleinstallation.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())));


Expand Down
3 changes: 3 additions & 0 deletions setup/runtimeenv.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down Expand Up @@ -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();
}

Expand All @@ -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->IsUninstallable() ? 'yes' : 'no');
$oInstallRec->Set('installed', $iInstallationTime);
$oInstallRec->DBInsertNoReload();
}
Expand Down
77 changes: 45 additions & 32 deletions setup/wizardsteps.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<fieldset>');
$oPage->add('<legend>Advanced parameters</legend>');
$oPage->p('<input id="force-uninstall" type="checkbox"'.$sChecked.' name="force-uninstall"><label for="force-uninstall">&nbsp;Disable uninstallation checks for extensions');
$oPage->add('</fieldset>');

$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
);
}
}


Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -1223,6 +1244,7 @@ public function Display(WebPage $oPage)
);

$this->AddUseSymlinksFlagOption($oPage);
$this->AddForceUninstallFlagOption($oPage);
}

public function AsyncAction(WebPage $oPage, $sCode, $aParameters)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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);

$bIsUninstallable = $this->oExtensionsMap->Get($aChoice['extension_code'])->IsUninstallable();
$bSelected = isset($aSelectedComponents[$sChoiceId]) && ($aSelectedComponents[$sChoiceId] == $sChoiceId);
$bMandatory = (isset($aChoice['mandatory']) && $aChoice['mandatory']) || ($this->bUpgrade && $bIsDefault);
$bDisabled = false;
if ($bMandatory) {
$oPage->add('<div class="choice" '.$sDataId.'><input id="'.$sId.'" checked disabled data-disabled="disabled" type="checkbox"'.$sAttributes.'/><input type="hidden" name="choice['.$sChoiceId.']" value="'.$sChoiceId.'">&nbsp;');
$bDisabled = true;
} else if ($bSelected) {
$oPage->add('<div class="choice" '.$sDataId.'><input class="wiz-choice" '.$sAllDisabled.'id="'.$sId.'" name="choice['.$sChoiceId.']" type="checkbox" checked value="'.$sChoiceId.'"/>&nbsp;');
} else {
$oPage->add('<div class="choice" '.$sDataId.'><input class="wiz-choice" '.$sAllDisabled.'id="'.$sId.'" name="choice['.$sChoiceId.']" type="checkbox" value="'.$sChoiceId.'"/>&nbsp;');
}
$this->DisplayChoice($oPage, $aChoice, $aSelectedComponents, $aDefaults, $sChoiceId, $bDisabled);
$bMandatory = (isset($aChoice['mandatory']) && $aChoice['mandatory']) || $this->bUpgrade && $bIsDefault && !$bIsUninstallable && !$bDisableUninstallCheck;;
$bDisabled = $bMandatory || $bAllDisabled;
$bChecked = $bMandatory || $bSelected;
$sChecked = $bChecked ? ' checked ' : '';
$sDisabled = $bDisabled ? ' disabled data-disabled="disabled" ' : '';
$sUnremovable = !$bIsUninstallable ? ' unremovable ' : '';
$sHiddenInput = $bDisabled && $bChecked ? '<input type="hidden" name="choice['.$sChoiceId.']" value="'.$sChoiceId.'"/>' : '';
$oPage->add('<div class="choice" '.$sDataId.'><input class="wiz-choice '.$sUnremovable.'" id="'.$sId.'" name="choice['.$sChoiceId.']" type="checkbox" value="'.$sChoiceId.'" '.$sDisabled.$sChecked.'/>'.$sHiddenInput.'&nbsp;');
$this->DisplayChoice($oPage, $aChoice, $aSelectedComponents, $aDefaults, $sChoiceId, $bDisabled, $bIsUninstallable);
$oPage->add('</div>');
$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
}
}
Expand Down Expand Up @@ -2111,24 +2124,24 @@ protected function DisplayOptions($oPage, $aStepInfo, $aSelectedComponents, $aDe
$sAttributes = ' checked ';
}
$sHidden = '';
if ($bMandatory && $bDisabled)
{
if ($bMandatory && $bDisabled) {
$sAttributes = ' checked ';
$sHidden = '<input type="hidden" name="choice['.$sChoiceName.']" value="'.$sChoiceId.'"/>';
}
$oPage->add('<div class="choice" '.$sDataId.'><input class="wiz-choice" id="'.$sId.'" name="choice['.$sChoiceName.']" type="radio"'.$sAttributes.' value="'.$sChoiceId.'"'.$sDisabled.'/>'.$sHidden.'&nbsp;');
$this->DisplayChoice($oPage, $aChoice, $aSelectedComponents, $aDefaults, $sChoiceId, $bDisabled && !$bSelected);
$oPage->add('</div>');
$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'] != '')) ? '<a class="setup--wizard-choice--more-info" target="_blank" href="'.$aChoice['more_info'].'">More information</a>' : '';
$sSourceLabel = isset($aChoice['source_label']) ? $aChoice['source_label'] : '';
$sId = utils::EscapeHtml($aChoice['extension_code']);
$oPage->add('<label class="setup--wizard-choice--label" for="'.$sId.'">'.$sSourceLabel.'<b>'.utils::EscapeHtml($aChoice['title']).'</b>'.'</label> '.$sMoreInfo);
$sUninstallationWarning = $bUninstallable ? '' : '<span style="color:orangered" title="Once this extension has been installed, it cannot be removed">(!)</span>';

$oPage->add('<label class="setup--wizard-choice--label" for="'.$sId.'">'.$sSourceLabel.'<b>'.utils::EscapeHtml($aChoice['title']).'</b>'.'</label>&nbsp;'.$sUninstallationWarning.' '.$sMoreInfo.'');
$sDescription = isset($aChoice['description']) ? utils::EscapeHtml($aChoice['description']) : '';
$oPage->add('<div class="setup--wizard-choice--description description">'.$sDescription.'<span id="sub_choices'.$sId.'">');
if (isset($aChoice['sub_options'])) {
Expand Down