Skip to content

Commit c25adf7

Browse files
committed
N°8763 refactor
1 parent e5829aa commit c25adf7

File tree

2 files changed

+57
-34
lines changed

2 files changed

+57
-34
lines changed

setup/extensionsmap.class.inc.php

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class iTopExtension
6060
* @var bool
6161
*/
6262
public $bMarkedAsChosen;
63+
public ?bool $bUninstallable = null;
6364

6465
/**
6566
* @var bool
@@ -95,6 +96,10 @@ class iTopExtension
9596
* @var true
9697
*/
9798
public bool $bInstalled = false;
99+
/**
100+
* @var true
101+
*/
102+
public bool $bRemovedFromDisk = false;
98103

99104
public function __construct()
100105
{
@@ -119,8 +124,11 @@ public function __construct()
119124
* @since 3.3.0
120125
* @return bool
121126
*/
122-
public function CanBeUninstalled()
127+
public function CanBeUninstalled(): bool
123128
{
129+
if (!is_null($this->bUninstallable)) {
130+
return $this->bUninstallable;
131+
}
124132
foreach ($this->aModuleInfo as $sModuleCode => $aModuleInfo) {
125133
$bUninstallable = $aModuleInfo['uninstallable'] === 'yes';
126134
if (!$bUninstallable) {
@@ -143,6 +151,7 @@ class iTopExtensionsMap
143151
* @return void
144152
*/
145153
protected $aExtensions;
154+
protected ?array $aInstalledExtensions = null;
146155

147156
/**
148157
* The list of directories browsed using the ReadDir method when building the map
@@ -263,7 +272,7 @@ protected function AddExtension(iTopExtension $oNewExtension)
263272
*
264273
* @return \iTopExtension|null
265274
*/
266-
public function Get(string $sExtensionCode): ?iTopExtension
275+
public function GetFromExtensionCode(string $sExtensionCode): ?iTopExtension
267276
{
268277
foreach ($this->aExtensions as $oExtension) {
269278
if ($oExtension->sCode === $sExtensionCode) {
@@ -434,6 +443,11 @@ public function GetAllExtensions()
434443
return $this->aExtensions;
435444
}
436445

446+
public function GetAllExtensionsWithPreviouslyInstalled()
447+
{
448+
return array_merge($this->aExtensions, $this->aInstalledExtensions ?? []);
449+
}
450+
437451
/**
438452
* Mark the given extension as chosen
439453
* @param string $sExtensionCode The code of the extension (code without version number)
@@ -503,24 +517,52 @@ public function GetChoices()
503517
*/
504518
public function LoadChoicesFromDatabase(Config $oConfig)
505519
{
506-
507-
$aInstalledExtensions = $this->GetInstalledExtensionsFromDatabase($oConfig);
508-
509-
foreach ($aInstalledExtensions as $aDBInfo) {
510-
$this->MarkAsChosen($aDBInfo['code']);
511-
$this->SetInstalledVersion($aDBInfo['code'], $aDBInfo['version']);
520+
foreach ($this->LoadInstalledExtensionsFromDatabase($oConfig) as $oExtension) {
521+
$this->MarkAsChosen($oExtension->sCode);
522+
$this->SetInstalledVersion($oExtension->sCode, $oExtension->sVersion);
512523
}
513524
return true;
514525
}
515526

516-
public function GetInstalledExtensionsFromDatabase(Config $oConfig): array|false
527+
public function LoadInstalledExtensionsFromDatabase(Config $oConfig): array|false
517528
{
529+
if (is_array($this->aInstalledExtensions)) {
530+
return $this->aInstalledExtensions;
531+
}
518532
try {
519533
if (CMDBSource::DBName() === null) {
520534
CMDBSource::InitFromConfig($oConfig);
521535
}
522536
$sLatestInstallationDate = CMDBSource::QueryToScalar("SELECT max(installed) FROM ".$oConfig->Get('db_subname')."priv_extension_install");
523-
return CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->Get('db_subname')."priv_extension_install WHERE installed = '".$sLatestInstallationDate."'");
537+
$aDBInfo = CMDBSource::QueryToArray("SELECT * FROM ".$oConfig->Get('db_subname')."priv_extension_install WHERE installed = '".$sLatestInstallationDate."'");
538+
539+
$this->aInstalledExtensions = [];
540+
foreach ($aDBInfo as $aExtensionInfo) {
541+
$oExtension = new iTopExtension();
542+
$oExtension->sCode = $aExtensionInfo['code'];
543+
$oExtension->sLabel = $aExtensionInfo['label'];
544+
$oExtension->sDescription = $aExtensionInfo['description'] ?? '';
545+
$oExtension->sVersion = $aExtensionInfo['version'];
546+
$oExtension->sSource = $aExtensionInfo['source'];
547+
$oExtension->bMandatory = false;
548+
$oExtension->sMoreInfoUrl = '';
549+
$oExtension->aModules = [];
550+
$oExtension->aModuleVersion = [];
551+
$oExtension->aModuleInfo = [];
552+
$oExtension->sSourceDir = '';
553+
$oExtension->bVisible = true;
554+
$oExtension->bInstalled = true;
555+
$oExtension->bUninstallable = !isset($aExtensionInfo['uninstallable']) || $aExtensionInfo['uninstallable'] === 'yes';
556+
if ($oChoice = $this->GetFromExtensionCode($oExtension->sCode)) {
557+
$oChoice->bInstalled = true;
558+
} else {
559+
$oExtension->bRemovedFromDisk = true;
560+
}
561+
562+
$this->aInstalledExtensions[$oExtension->sCode.'/'.$oExtension->sVersion] = $oExtension;
563+
}
564+
565+
return $this->aInstalledExtensions;
524566
} catch (MySQLException $e) {
525567
// No database or erroneous information
526568
return false;

setup/wizardsteps.class.inc.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,7 @@ protected function GetStepIndex()
18481848
}
18491849
return $index;
18501850
}
1851+
18511852
protected function GetStepInfo($idx = null)
18521853
{
18531854
$aStepInfo = null;
@@ -1873,7 +1874,7 @@ protected function GetStepInfo($idx = null)
18731874
'options' => [],
18741875
];
18751876

1876-
foreach ($this->oExtensionsMap->GetAllExtensions() as $oExtension) {
1877+
foreach ($this->oExtensionsMap->GetAllExtensionsWithPreviouslyInstalled() as $oExtension) {
18771878
if (($oExtension->sSource !== iTopExtension::SOURCE_WIZARD) && ($oExtension->bVisible) && (count($oExtension->aMissingDependencies) == 0)) {
18781879
$aStepDefinition['options'][] = [
18791880
'extension_code' => $oExtension->sCode,
@@ -1885,31 +1886,11 @@ protected function GetStepInfo($idx = null)
18851886
'mandatory' => $oExtension->bMandatory || ($oExtension->sSource === iTopExtension::SOURCE_REMOTE),
18861887
'source_label' => $this->GetExtensionSourceLabel($oExtension->sSource),
18871888
'uninstallable' => $oExtension->CanBeUninstalled(),
1889+
'missing' => $oExtension->bRemovedFromDisk,
18881890
];
18891891
}
18901892
}
1891-
//Listing previously installed extension missing from disk
1892-
if (!is_null($this->oConfig) && ($aPreviouslyInstalled = $this->oExtensionsMap->GetInstalledExtensionsFromDatabase($this->oConfig))) {
1893-
foreach ($aPreviouslyInstalled as $aExtension) {
1894-
if (!$this->oExtensionsMap->Get($aExtension['code'])) {
1895-
$bUninstallable = $aExtension['uninstallable'] === 'yes';
1896-
$aStepDefinition['options'][] = [
1897-
'extension_code' => $aExtension['code'],
1898-
'title' => $aExtension['label'],
1899-
'description' => $aExtension['description'] ?? '',
1900-
'more_info' => '',
1901-
'default' => true, // by default offer to install all modules
1902-
'modules' => [],
1903-
'mandatory' => false,
1904-
'source_label' => $this->GetExtensionSourceLabel($aExtension['source']),
1905-
'uninstallable' => $bUninstallable,
1906-
'missing' => true,
1907-
];
1908-
} else {
1909-
$this->oExtensionsMap->Get($aExtension['code'])->bInstalled = true;
1910-
}
1911-
}
1912-
}
1893+
19131894
// Display this step of the wizard only if there is something to display
19141895
if (count($aStepDefinition['options']) !== 0) {
19151896
$aSteps[] = $aStepDefinition;
@@ -1983,7 +1964,7 @@ protected function DisplayOptions($oPage, $aStepInfo, $aSelectedComponents, $aDe
19831964
$sId = utils::EscapeHtml($aChoice['extension_code']);
19841965
$bIsDefault = array_key_exists($sChoiceId, $aDefaults);
19851966

1986-
$oITopExtension = $this->oExtensionsMap->Get($aChoice['extension_code']);
1967+
$oITopExtension = $this->oExtensionsMap->GetFromExtensionCode($aChoice['extension_code']);
19871968
$bCanBeUninstalled = isset($aChoice['uninstallable']) ? $aChoice['uninstallable'] : $oITopExtension->CanBeUninstalled();
19881969
$bSelected = isset($aSelectedComponents[$sChoiceId]) && ($aSelectedComponents[$sChoiceId] == $sChoiceId);
19891970
$bMandatory = (isset($aChoice['mandatory']) && $aChoice['mandatory']) || $this->bUpgrade && $bIsDefault && !$bCanBeUninstalled && !$bDisableUninstallCheck;

0 commit comments

Comments
 (0)