From 01a82cf35f3a113e87047d8008dca2a41ade0f6a Mon Sep 17 00:00:00 2001 From: "Phil E. Taylor" Date: Sat, 9 Jan 2021 15:49:22 +0000 Subject: [PATCH 1/5] Add dry-run and verbose/debug modes to update:joomla:remove-old-files cmd Signed-off-by: Phil E. Taylor --- administrator/components/com_admin/script.php | 65 ++++++++++++++-- .../src/Console/RemoveOldFilesCommand.php | 77 +++++++++++++++++-- 2 files changed, 129 insertions(+), 13 deletions(-) diff --git a/administrator/components/com_admin/script.php b/administrator/components/com_admin/script.php index c77ec7db65d51..28931e6f281fc 100644 --- a/administrator/components/com_admin/script.php +++ b/administrator/components/com_admin/script.php @@ -467,10 +467,24 @@ protected function updateManifestCaches() /** * Delete files that should not exist * - * @return void + * @param bool $dryRun If set to true, will not actually delete files, but just report their status for use in CLI + * @param bool $supressOutput Set to true to supress echoing any errors, and just return the $status array + * + * @return array */ - public function deleteUnexistingFiles() + public function deleteUnexistingFiles($dryRun = false, $supressOutput = false) { + $status = [ + 'files_exist' =>[], + 'folders_exist' =>[], + 'files_deleted' =>[], + 'folders_deleted' =>[], + 'files_errors' =>[], + 'folders_errors' =>[], + 'folders_checked' =>[], + 'files_checked' =>[], + ]; + $files = array( // Joomla 4.0 Beta 1 '/administrator/components/com_actionlogs/actionlogs.php', @@ -5026,7 +5040,6 @@ public function deleteUnexistingFiles() '/templates/cassiopeia/scss/vendor/bootstrap/_card.scss', ); - // TODO There is an issue while deleting folders using the ftp mode $folders = array( // Joomla 4.0 Beta 1 '/templates/system/images', @@ -6215,21 +6228,59 @@ public function deleteUnexistingFiles() '/plugins/content/imagelazyload', ); + $status['files_checked'] = $files; + $status['folders_checked'] = $folders; + foreach ($files as $file) { - if (File::exists(JPATH_ROOT . $file) && !File::delete(JPATH_ROOT . $file)) + if ($fileExists = File::exists(JPATH_ROOT . $file)) { - echo Text::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $file) . '
'; + $status['files_exist'][] = $file; + + if ($dryRun === false){ + if (File::delete(JPATH_ROOT . $file)) + { + $status['files_deleted'][] = $file; + } + else + { + $status['files_errors'][] = Text::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $file) ; + } + } } } foreach ($folders as $folder) { - if (Folder::exists(JPATH_ROOT . $folder) && !Folder::delete(JPATH_ROOT . $folder)) + if ($folderExists = Folder::exists(JPATH_ROOT . $folder)) { - echo Text::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $folder) . '
'; + $status['folders_exist'][] = $folder; + + if ($dryRun === false){ + // TODO There is an issue while deleting folders using the ftp mode + if (Folder::delete(JPATH_ROOT . $folder)) + { + $status['folders_deleted'][] = $folder; + } + else + { + $status['folders_errors'][] = Text::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $folder) ; + } + } } } + + if ($supressOutput === false && \count($status['folders_errors'])) + { + echo implode('
', $status['folders_errors']); + } + + if ($supressOutput === false && \count($status['files_errors'])) + { + echo implode('
', $status['files_errors']); + } + + return $status; } /** diff --git a/libraries/src/Console/RemoveOldFilesCommand.php b/libraries/src/Console/RemoveOldFilesCommand.php index 98ba8633511ea..3de342cec0b14 100644 --- a/libraries/src/Console/RemoveOldFilesCommand.php +++ b/libraries/src/Console/RemoveOldFilesCommand.php @@ -11,7 +11,9 @@ \defined('JPATH_PLATFORM') or die; use Joomla\Console\Command\AbstractCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -33,8 +35,8 @@ class RemoveOldFilesCommand extends AbstractCommand /** * Internal function to execute the command. * - * @param InputInterface $input The input to inject into the command. - * @param OutputInterface $output The output to inject into the command. + * @param InputInterface $input The input to inject into the command. + * @param OutputInterface $output The output to inject into the command. * * @return integer The command exit code * @@ -44,16 +46,78 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in { $symfonyStyle = new SymfonyStyle($input, $output); - $symfonyStyle->title('Removing Old Files'); + $dryRun = $input->getOption('dry-run'); + + $symfonyStyle->title('Removing Unneeded Files & Folders' . ($dryRun ? ' - Dry Run' : '')); // We need the update script \JLoader::register('JoomlaInstallerScript', JPATH_ADMINISTRATOR . '/components/com_admin/script.php'); - (new \JoomlaInstallerScript)->deleteUnexistingFiles(); + $status = (new \JoomlaInstallerScript)->deleteUnexistingFiles($dryRun, true); + + if ($output->isVeryVerbose()||$output->isDebug()) + { + foreach ($status['files_checked'] as $file) + { + $exists = in_array($file, array_values($status['files_exist'])); + if ($exists) + { + $symfonyStyle->writeln('File Checked & Exists - ' . $file, OutputInterface::VERBOSITY_VERY_VERBOSE); + } + else + { + $symfonyStyle->writeln('File Checked & Doesn\'t Exist - ' . $file, OutputInterface::VERBOSITY_DEBUG); + } + } + + foreach ($status['folders_checked'] as $folder) + { + $exists = in_array($file, array_values($status['folders_exist'])); + if ($exists) + { + $symfonyStyle->writeln('Folder Checked & Exists - ' . $folder, OutputInterface::VERBOSITY_VERY_VERBOSE); + } + else + { + $symfonyStyle->writeln('Folder Checked & Doesn\'t Exist - ' . $folder, OutputInterface::VERBOSITY_DEBUG); + } + } + } + + if ($dryRun===false) + { + foreach ($status['files_deleted'] as $file) { + $symfonyStyle->writeln('File Deleted = ' . $file . '', OutputInterface::VERBOSITY_VERBOSE); + } + + foreach ($status['files_errors'] as $error) { + $symfonyStyle->error($error); + } + + foreach ($status['folders_deleted'] as $folder) { + $symfonyStyle->writeln('Folder Deleted = ' . $folder . '', OutputInterface::VERBOSITY_VERBOSE); + } + + foreach ($status['folders_errors'] as $error) { + $symfonyStyle->error($error); + } + } + + $symfonyStyle->success(sprintf( + $dryRun ? '%s Files checked and %s would be deleted' : '%s Files checked and %s deleted', + \count($status['files_checked']), + ($dryRun ? \count($status['files_exist']) : \count($status['files_deleted']) + ) + )); - $symfonyStyle->success('Files removed'); + $symfonyStyle->success(sprintf( + $dryRun ? '%s Folders checked and %s would be deleted' : '%s Folders checked and %s deleted', + \count($status['folders_checked']), + ($dryRun ? \count($status['folders_exist']) : \count($status['folders_deleted']) + ) + )); - return 0; + return Command::SUCCESS; } /** @@ -69,6 +133,7 @@ protected function configure(): void \nUsage: php %command.full_name%"; $this->setDescription('Remove old system files'); + $this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Executes a dry run without deleting anything'); $this->setHelp($help); } } From 113ba90e05a12fabf97d43678e397e6f220a8802 Mon Sep 17 00:00:00 2001 From: "Phil E. Taylor" Date: Sat, 9 Jan 2021 15:56:15 +0000 Subject: [PATCH 2/5] typo Signed-off-by: Phil E. Taylor --- libraries/src/Console/RemoveOldFilesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/src/Console/RemoveOldFilesCommand.php b/libraries/src/Console/RemoveOldFilesCommand.php index 3de342cec0b14..5a253b5b8f429 100644 --- a/libraries/src/Console/RemoveOldFilesCommand.php +++ b/libraries/src/Console/RemoveOldFilesCommand.php @@ -72,7 +72,7 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in foreach ($status['folders_checked'] as $folder) { - $exists = in_array($file, array_values($status['folders_exist'])); + $exists = in_array($folder, array_values($status['folders_exist'])); if ($exists) { $symfonyStyle->writeln('Folder Checked & Exists - ' . $folder, OutputInterface::VERBOSITY_VERY_VERBOSE); From 0a2266dd3843a93e00e1c363d51435512d544444 Mon Sep 17 00:00:00 2001 From: "Phil E. Taylor" Date: Sat, 9 Jan 2021 16:05:10 +0000 Subject: [PATCH 3/5] typo Signed-off-by: Phil E. Taylor --- administrator/components/com_admin/script.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/administrator/components/com_admin/script.php b/administrator/components/com_admin/script.php index 28931e6f281fc..d81d4d36b5ee9 100644 --- a/administrator/components/com_admin/script.php +++ b/administrator/components/com_admin/script.php @@ -468,11 +468,11 @@ protected function updateManifestCaches() * Delete files that should not exist * * @param bool $dryRun If set to true, will not actually delete files, but just report their status for use in CLI - * @param bool $supressOutput Set to true to supress echoing any errors, and just return the $status array + * @param bool $suppressOutput Set to true to supress echoing any errors, and just return the $status array * * @return array */ - public function deleteUnexistingFiles($dryRun = false, $supressOutput = false) + public function deleteUnexistingFiles($dryRun = false, $suppressOutput = false) { $status = [ 'files_exist' =>[], From e2f6bd6c7d7a6c5907f5e7102b1dc58972e518a4 Mon Sep 17 00:00:00 2001 From: "Phil E. Taylor" Date: Sat, 9 Jan 2021 16:14:04 +0000 Subject: [PATCH 4/5] cs Signed-off-by: Phil E. Taylor cs multi line Signed-off-by: Phil E. Taylor tabs to spaces Signed-off-by: Phil E. Taylor spaces to tabs - even though cs said it wanted spaces grrr Signed-off-by: Phil E. Taylor adding more spaces for the fun of it Signed-off-by: Phil E. Taylor Tabs must be used to indent lines; spaces are not allowed - apparently. Signed-off-by: Phil E. Taylor cs - last chance! Signed-off-by: Phil E. Taylor --- administrator/components/com_admin/script.php | 26 ++++++------ .../src/Console/RemoveOldFilesCommand.php | 42 +++++++++++-------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/administrator/components/com_admin/script.php b/administrator/components/com_admin/script.php index d81d4d36b5ee9..432e3d4238121 100644 --- a/administrator/components/com_admin/script.php +++ b/administrator/components/com_admin/script.php @@ -475,14 +475,14 @@ protected function updateManifestCaches() public function deleteUnexistingFiles($dryRun = false, $suppressOutput = false) { $status = [ - 'files_exist' =>[], - 'folders_exist' =>[], - 'files_deleted' =>[], - 'folders_deleted' =>[], - 'files_errors' =>[], - 'folders_errors' =>[], - 'folders_checked' =>[], - 'files_checked' =>[], + 'files_exist' => [], + 'folders_exist' => [], + 'files_deleted' => [], + 'folders_deleted' => [], + 'files_errors' => [], + 'folders_errors' => [], + 'folders_checked' => [], + 'files_checked' => [], ]; $files = array( @@ -6237,14 +6237,15 @@ public function deleteUnexistingFiles($dryRun = false, $suppressOutput = false) { $status['files_exist'][] = $file; - if ($dryRun === false){ + if ($dryRun === false) + { if (File::delete(JPATH_ROOT . $file)) { $status['files_deleted'][] = $file; } else { - $status['files_errors'][] = Text::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $file) ; + $status['files_errors'][] = Text::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $file); } } } @@ -6256,7 +6257,8 @@ public function deleteUnexistingFiles($dryRun = false, $suppressOutput = false) { $status['folders_exist'][] = $folder; - if ($dryRun === false){ + if ($dryRun === false) + { // TODO There is an issue while deleting folders using the ftp mode if (Folder::delete(JPATH_ROOT . $folder)) { @@ -6264,7 +6266,7 @@ public function deleteUnexistingFiles($dryRun = false, $suppressOutput = false) } else { - $status['folders_errors'][] = Text::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $folder) ; + $status['folders_errors'][] = Text::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $folder); } } } diff --git a/libraries/src/Console/RemoveOldFilesCommand.php b/libraries/src/Console/RemoveOldFilesCommand.php index 5a253b5b8f429..ca245a2065b6d 100644 --- a/libraries/src/Console/RemoveOldFilesCommand.php +++ b/libraries/src/Console/RemoveOldFilesCommand.php @@ -35,8 +35,8 @@ class RemoveOldFilesCommand extends AbstractCommand /** * Internal function to execute the command. * - * @param InputInterface $input The input to inject into the command. - * @param OutputInterface $output The output to inject into the command. + * @param InputInterface $input The input to inject into the command. + * @param OutputInterface $output The output to inject into the command. * * @return integer The command exit code * @@ -60,6 +60,7 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in foreach ($status['files_checked'] as $file) { $exists = in_array($file, array_values($status['files_exist'])); + if ($exists) { $symfonyStyle->writeln('File Checked & Exists - ' . $file, OutputInterface::VERBOSITY_VERY_VERBOSE); @@ -73,6 +74,7 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in foreach ($status['folders_checked'] as $folder) { $exists = in_array($folder, array_values($status['folders_exist'])); + if ($exists) { $symfonyStyle->writeln('Folder Checked & Exists - ' . $folder, OutputInterface::VERBOSITY_VERY_VERBOSE); @@ -84,38 +86,44 @@ protected function doExecute(InputInterface $input, OutputInterface $output): in } } - if ($dryRun===false) + if ($dryRun === false) { - foreach ($status['files_deleted'] as $file) { + foreach ($status['files_deleted'] as $file) + { $symfonyStyle->writeln('File Deleted = ' . $file . '', OutputInterface::VERBOSITY_VERBOSE); } - foreach ($status['files_errors'] as $error) { + foreach ($status['files_errors'] as $error) + { $symfonyStyle->error($error); } - foreach ($status['folders_deleted'] as $folder) { + foreach ($status['folders_deleted'] as $folder) + { $symfonyStyle->writeln('Folder Deleted = ' . $folder . '', OutputInterface::VERBOSITY_VERBOSE); } - foreach ($status['folders_errors'] as $error) { + foreach ($status['folders_errors'] as $error) + { $symfonyStyle->error($error); } } - $symfonyStyle->success(sprintf( - $dryRun ? '%s Files checked and %s would be deleted' : '%s Files checked and %s deleted', - \count($status['files_checked']), - ($dryRun ? \count($status['files_exist']) : \count($status['files_deleted']) + $symfonyStyle->success( + sprintf( + $dryRun ? '%s Files checked and %s would be deleted' : '%s Files checked and %s deleted', + \count($status['files_checked']), + ($dryRun ? \count($status['files_exist']) : \count($status['files_deleted'])) ) - )); + ); - $symfonyStyle->success(sprintf( - $dryRun ? '%s Folders checked and %s would be deleted' : '%s Folders checked and %s deleted', - \count($status['folders_checked']), - ($dryRun ? \count($status['folders_exist']) : \count($status['folders_deleted']) + $symfonyStyle->success( + sprintf( + $dryRun ? '%s Folders checked and %s would be deleted' : '%s Folders checked and %s deleted', + \count($status['folders_checked']), + ($dryRun ? \count($status['folders_exist']) : \count($status['folders_deleted'])) ) - )); + ); return Command::SUCCESS; } From 3f9d3ce002d90c533b1fa2d5ceba80de6a6a6d41 Mon Sep 17 00:00:00 2001 From: "Phil E. Taylor" Date: Sat, 9 Jan 2021 16:47:08 +0000 Subject: [PATCH 5/5] s/supressOutput/suppressOutput Signed-off-by: Phil E. Taylor --- administrator/components/com_admin/script.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/administrator/components/com_admin/script.php b/administrator/components/com_admin/script.php index 432e3d4238121..64d21d2e43766 100644 --- a/administrator/components/com_admin/script.php +++ b/administrator/components/com_admin/script.php @@ -6272,12 +6272,12 @@ public function deleteUnexistingFiles($dryRun = false, $suppressOutput = false) } } - if ($supressOutput === false && \count($status['folders_errors'])) + if ($suppressOutput === false && \count($status['folders_errors'])) { echo implode('
', $status['folders_errors']); } - if ($supressOutput === false && \count($status['files_errors'])) + if ($suppressOutput === false && \count($status['files_errors'])) { echo implode('
', $status['files_errors']); }