Skip to content

Commit

Permalink
Remove unused const and irrelevant consecutive calls to UpgradeContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Dec 20, 2024
1 parent 918ec6f commit fcfab4f
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 52 deletions.
10 changes: 0 additions & 10 deletions classes/Parameters/UpgradeFileNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ class UpgradeFileNames
*/
const CONFIG_FILENAME = 'config.var';

/**
* during upgradeFiles process,
* this files contains the list of queries left to upgrade in a serialized array.
* (this file is deleted in init() method if you reload the page).
*
* @var string
*/
const QUERIES_TO_UPGRADE_LIST = 'queriesToUpgrade.list';

/**
* during upgradeFiles process,
* this files contains the list of files left to upgrade in a serialized array.
Expand Down Expand Up @@ -151,7 +142,6 @@ class UpgradeFileNames
*/
public static $update_tmp_files = [
'STATE_FILENAME' => self::STATE_UPDATE_FILENAME,
'QUERIES_TO_UPGRADE_LIST' => self::QUERIES_TO_UPGRADE_LIST, // used ?
'FILES_TO_UPGRADE_LIST' => self::FILES_TO_UPGRADE_LIST,
'DB_TABLES_TO_CLEAN_LIST' => self::DB_TABLES_TO_CLEAN_LIST,
'FILES_TO_REMOVE_LIST' => self::FILES_TO_REMOVE_LIST,
Expand Down
4 changes: 4 additions & 0 deletions classes/State/BackupState.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ protected function getFileNameForPersistentStorage(): string

public function initDefault(string $currentVersion): void
{
$this->disableSave = true;
$rand = dechex(mt_rand(0, min(0xffffffff, mt_getrandmax())));
$date = date('Ymd-His');
$backupName = 'V' . $currentVersion . '_' . $date . '-' . $rand;
Expand All @@ -79,6 +80,9 @@ public function initDefault(string $currentVersion): void
$this->setBackupTable(null);
$this->setBackupLoopLimit(null);
$this->setDbStep(0);

$this->disableSave = false;
$this->save();
}

public function getBackupName(): string
Expand Down
5 changes: 0 additions & 5 deletions classes/Task/AbstractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,6 @@ protected function setupEnvironment(): void
$this->container->getUpdateState()->initDefault($this->container->getProperty(UpgradeContainer::PS_VERSION), $this->container->getUpgrader()->getDestinationVersion());
$this->logger->debug($this->translator->trans('Successfully initialized update state.'));
}

if ($this::TASK_TYPE === TaskType::TASK_TYPE_BACKUP && !$this->container->getBackupState()->isInitialized()) {
$this->container->getBackupState()->initDefault($this->container->getProperty(UpgradeContainer::PS_VERSION));
$this->logger->debug($this->translator->trans('Successfully initialized backup state.'));
}
}

abstract public function run(): int;
Expand Down
7 changes: 4 additions & 3 deletions classes/Task/Backup/BackupFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ class BackupFiles extends AbstractTask
*/
public function run(): int
{
$state = $this->container->getBackupState();
$this->stepDone = false;
$backupFilesFilename = $this->container->getBackupState()->getBackupFilesFilename();
$backupFilesFilename = $state->getBackupFilesFilename();
if (empty($backupFilesFilename)) {
$this->next = TaskName::TASK_ERROR;
$this->setErrorFlag();
Expand All @@ -57,7 +58,7 @@ public function run(): int
}

if (!$this->container->getFileConfigurationStorage()->exists(UpgradeFileNames::FILES_TO_BACKUP_LIST)) {
$this->container->getBackupState()->setProgressPercentage(
$state->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

Expand Down Expand Up @@ -93,7 +94,7 @@ public function run(): int
return ExitCode::FAIL;
}
$this->container->getFileConfigurationStorage()->save($backlog->dump(), UpgradeFileNames::FILES_TO_BACKUP_LIST);
$this->container->getBackupState()->setProgressPercentage(
$state->setProgressPercentage(
$this->container->getCompletionCalculator()->computePercentage($backlog, self::class, BackupDatabase::class)
);
} else {
Expand Down
18 changes: 10 additions & 8 deletions classes/Task/Restore/Restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ class Restore extends AbstractTask
*/
public function run(): int
{
$this->container->getRestoreState()->setProgressPercentage(
$state = $this->container->getRestoreState();

$state->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

// 1st, need to analyse what was wrong.
$restoreName = $this->container->getRestoreState()->getRestoreName();
$this->container->getRestoreState()->setRestoreFilesFilename($restoreName);
$restoreDbFilenames = $this->container->getRestoreState()->getRestoreDbFilenames();
$restoreName = $state->getRestoreName();
$state->setRestoreFilesFilename($restoreName);
$restoreDbFilenames = $state->getRestoreDbFilenames();

if (empty($restoreName)) {
$this->next = TaskName::TASK_RESTORE_EMPTY;
Expand All @@ -68,14 +70,14 @@ public function run(): int
// find backup filenames, and be sure they exists
foreach ($files as $file) {
if (preg_match('#' . preg_quote(BackupFinder::BACKUP_ZIP_NAME_PREFIX . $restoreName) . '#', $file)) {
$this->container->getRestoreState()->setRestoreFilesFilename($file);
$state->setRestoreFilesFilename($file);
break;
}
}
if (!is_file($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $this->container->getRestoreState()->getRestoreFilesFilename())) {
if (!is_file($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $state->getRestoreFilesFilename())) {
$this->next = TaskName::TASK_ERROR;
$this->setErrorFlag();
$this->logger->error($this->translator->trans('File %s is missing: unable to restore files. Operation aborted.', [$this->container->getRestoreState()->getRestoreFilesFilename()]));
$this->logger->error($this->translator->trans('File %s is missing: unable to restore files. Operation aborted.', [$state->getRestoreFilesFilename()]));

return ExitCode::FAIL;
}
Expand All @@ -88,7 +90,7 @@ public function run(): int

// order files is important !
sort($restoreDbFilenames);
$this->container->getRestoreState()->setRestoreDbFilenames($restoreDbFilenames);
$state->setRestoreDbFilenames($restoreDbFilenames);
if (count($restoreDbFilenames) == 0) {
$this->next = TaskName::TASK_ERROR;
$this->setErrorFlag();
Expand Down
21 changes: 11 additions & 10 deletions classes/Task/Restore/RestoreDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class RestoreDatabase extends AbstractTask
*/
public function run(): int
{
$this->container->getRestoreState()->setProgressPercentage(
$state = $this->container->getRestoreState();
$state->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

Expand All @@ -70,19 +71,19 @@ public function run(): int
}

// deal with the next files stored in restoreDbFilenames
$restoreDbFilenames = $this->container->getRestoreState()->getRestoreDbFilenames();
$restoreDbFilenames = $state->getRestoreDbFilenames();
if ((!isset($backlog) || !$backlog->getRemainingTotal()) && count($restoreDbFilenames) > 0) {
$currentDbFilename = array_shift($restoreDbFilenames);
$this->container->getRestoreState()->setRestoreDbFilenames($restoreDbFilenames);
$state->setRestoreDbFilenames($restoreDbFilenames);
if (!preg_match('#' . BackupFinder::BACKUP_DB_FOLDER_NAME_PREFIX . '([0-9]{6})_#', $currentDbFilename, $match)) {
$this->next = TaskName::TASK_ERROR;
$this->setErrorFlag();
$this->logger->error($this->translator->trans('%s: File format does not match.', [$currentDbFilename]));

return ExitCode::FAIL;
}
$this->container->getRestoreState()->setDbStep((int) $match[1]);
$backupdb_path = $this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $this->container->getRestoreState()->getRestoreName();
$state->setDbStep((int) $match[1]);
$backupdb_path = $this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $state->getRestoreName();

$dot_pos = strrpos($currentDbFilename, '.');
$fileext = substr($currentDbFilename, $dot_pos + 1);
Expand Down Expand Up @@ -140,7 +141,7 @@ public function run(): int
unset($content);

// Get tables before backup
if ($this->container->getRestoreState()->getDbStep() == '1') {
if ($state->getDbStep() == '1') {
$tables_after_restore = [];
foreach ($listQuery as $q) {
if (preg_match('/`(?<table>' . _DB_PREFIX_ . '[a-zA-Z0-9_-]+)`/', $q, $matches)) {
Expand Down Expand Up @@ -173,17 +174,17 @@ public function run(): int
unlink($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::QUERIES_TO_RESTORE_LIST);
}

$restoreDbFilenamesCount = count($this->container->getRestoreState()->getRestoreDbFilenames());
$restoreDbFilenamesCount = count($state->getRestoreDbFilenames());
if ($restoreDbFilenamesCount) {
$this->logger->info($this->translator->trans(
'Database restoration file %filename% done. %filescount% file(s) left...',
[
'%filename%' => $this->container->getRestoreState()->getDbStep(),
'%filename%' => $state->getDbStep(),
'%filescount%' => $restoreDbFilenamesCount,
]
));
} else {
$this->logger->info($this->translator->trans('Database restoration file %1$s done.', [$this->container->getRestoreState()->getDbStep()]));
$this->logger->info($this->translator->trans('Database restoration file %1$s done.', [$state->getDbStep()]));
}

$this->stepDone = true;
Expand Down Expand Up @@ -230,7 +231,7 @@ public function run(): int
'%numberqueries% queries left for file %filename%...',
[
'%numberqueries%' => $queries_left,
'%filename%' => $this->container->getRestoreState()->getDbStep(),
'%filename%' => $state->getDbStep(),
]
));
} else {
Expand Down
7 changes: 4 additions & 3 deletions classes/Task/Restore/RestoreFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class RestoreFiles extends AbstractTask
*/
public function run(): int
{
$this->container->getRestoreState()->setProgressPercentage(
$state = $this->container->getRestoreState();
$state->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

Expand All @@ -57,7 +58,7 @@ public function run(): int
if (!file_exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_FROM_ARCHIVE_LIST)
|| !file_exists($this->container->getProperty(UpgradeContainer::WORKSPACE_PATH) . DIRECTORY_SEPARATOR . UpgradeFileNames::FILES_TO_REMOVE_LIST)) {
// cleanup current PS tree
$fromArchive = $this->container->getZipAction()->listContent($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $this->container->getRestoreState()->getRestoreFilesFilename());
$fromArchive = $this->container->getZipAction()->listContent($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $state->getRestoreFilesFilename());
foreach ($fromArchive as $k => $v) {
$fromArchive[DIRECTORY_SEPARATOR . $v] = DIRECTORY_SEPARATOR . $v;
}
Expand Down Expand Up @@ -98,7 +99,7 @@ public function run(): int
}

if (!empty($fromArchive)) {
$filepath = $this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $this->container->getRestoreState()->getRestoreFilesFilename();
$filepath = $this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $state->getRestoreFilesFilename();
$destExtract = $this->container->getProperty(UpgradeContainer::PS_ROOT_PATH);

$res = $this->container->getZipAction()->extract($filepath, $destExtract);
Expand Down
7 changes: 4 additions & 3 deletions classes/Task/Update/UpdateComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ class UpdateComplete extends AbstractTask
*/
public function run(): int
{
$this->container->getUpdateState()->setProgressPercentage(
$state = $this->container->getUpdateState();
$state->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

$destinationVersion = $this->container->getUpdateState()->getDestinationVersion();
$destinationVersion = $state->getDestinationVersion();

$this->logger->info($this->container->getUpdateState()->getWarningExists() ?
$this->logger->info($state->getWarningExists() ?
$this->translator->trans('Shop updated to %s, but some warnings have been found.', [$destinationVersion]) :
$this->translator->trans('Shop updated to %s. Congratulations! You can now reactivate your shop.', [$destinationVersion])
);
Expand Down
15 changes: 8 additions & 7 deletions classes/Task/Update/UpdateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ class UpdateDatabase extends AbstractTask

public function run(): int
{
$state = $this->container->getUpdateState();
try {
if (!$this->container->getFileConfigurationStorage()->exists(UpgradeFileNames::SQL_TO_EXECUTE_LIST)) {
$this->warmUp();
$currentVersion = $this->container->getUpdateState()->getCurrentVersion();
$currentVersion = $state->getCurrentVersion();
$sqlContentList = $this->getCoreUpgrader()->getSqlContentList($currentVersion);
$backlog = new Backlog(array_reverse($sqlContentList), count($sqlContentList));
} else {
Expand All @@ -66,7 +67,7 @@ public function run(): int

$this->updateDatabase($backlog);

$this->container->getUpdateState()->setProgressPercentage(
$state->setProgressPercentage(
$this->container->getCompletionCalculator()->computePercentage($backlog, self::class, UpdateModules::class)
);

Expand Down Expand Up @@ -101,9 +102,9 @@ public function getCoreUpgrader(): CoreUpgrader
return $this->coreUpgrader;
}

if (version_compare($this->container->getUpdateState()->getDestinationVersion(), '8', '<')) {
if (version_compare($state->getDestinationVersion(), '8', '<')) {
$this->coreUpgrader = new CoreUpgrader17($this->container, $this->logger);
} elseif (version_compare($this->container->getUpdateState()->getDestinationVersion(), '8.1', '<')) {
} elseif (version_compare($state->getDestinationVersion(), '8.1', '<')) {
$this->coreUpgrader = new CoreUpgrader80($this->container, $this->logger);
} else {
$this->coreUpgrader = new CoreUpgrader81($this->container, $this->logger);
Expand Down Expand Up @@ -134,7 +135,7 @@ protected function warmUp(): int
{
$this->logger->info($this->container->getTranslator()->trans('Updating database data and structure'));

$this->container->getUpdateState()->setProgressPercentage(
$state->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

Expand Down Expand Up @@ -164,8 +165,8 @@ protected function warmUp(): int
*/
protected function checkVersionIsNewer(): void
{
$currentVersion = VersionUtils::normalizePrestaShopVersion($this->container->getUpdateState()->getCurrentVersion());
$destinationVersion = VersionUtils::normalizePrestaShopVersion($this->container->getUpdateState()->getDestinationVersion());
$currentVersion = VersionUtils::normalizePrestaShopVersion($state->getCurrentVersion());
$destinationVersion = VersionUtils::normalizePrestaShopVersion($state->getDestinationVersion());

$versionCompare = version_compare($destinationVersion, $currentVersion);

Expand Down
7 changes: 4 additions & 3 deletions classes/Task/Update/UpdateFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ public function upgradeThisFile($orig): bool
*/
protected function warmUp(): int
{
$this->container->getUpdateState()->setProgressPercentage(
$state = $this->container->getUpdateState();
$state->setProgressPercentage(
$this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class)
);

Expand All @@ -224,8 +225,8 @@ protected function warmUp(): int
rename($newReleasePath . DIRECTORY_SEPARATOR . 'install-dev', $newReleasePath . DIRECTORY_SEPARATOR . 'install');
}

$destinationVersion = $this->container->getUpdateState()->getDestinationVersion();
$originVersion = $this->container->getUpdateState()->getCurrentVersion();
$destinationVersion = $state->getDestinationVersion();
$originVersion = $state->getCurrentVersion();
$this->logger->debug($this->translator->trans('Generate diff file list between %s and %s.', [$originVersion, $destinationVersion]));
$diffFileList = $this->container->getChecksumCompare()->getFilesDiffBetweenVersions($originVersion, $destinationVersion);
if (!is_array($diffFileList)) {
Expand Down
2 changes: 2 additions & 0 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,8 @@ public function getUpdateState(): UpdateState

/**
* @param TaskType::TASK_TYPE_* $taskType
*
* @throws InvalidArgumentException
*/
public function getStateFromTaskType($taskType): AbstractState
{
Expand Down

0 comments on commit fcfab4f

Please sign in to comment.