Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 60 additions & 7 deletions administrator/components/com_admin/script.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 $suppressOutput Set to true to supress echoing any errors, and just return the $status array
*
* @return array
*/
public function deleteUnexistingFiles()
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 = array(
// Joomla 4.0 Beta 1
'/administrator/components/com_actionlogs/actionlogs.php',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -6215,21 +6228,61 @@ 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) . '<br>';
$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) . '<br>';
$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 ($suppressOutput === false && \count($status['folders_errors']))
{
echo implode('<br/>', $status['folders_errors']);
}

if ($suppressOutput === false && \count($status['files_errors']))
{
echo implode('<br/>', $status['files_errors']);
}

return $status;
}

/**
Expand Down
85 changes: 79 additions & 6 deletions libraries/src/Console/RemoveOldFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
*
Expand All @@ -44,16 +46,86 @@ 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('<error>File Checked & Exists</error> - ' . $file, OutputInterface::VERBOSITY_VERY_VERBOSE);
}
else
{
$symfonyStyle->writeln('<info>File Checked & Doesn\'t Exist</info> - ' . $file, OutputInterface::VERBOSITY_DEBUG);
}
}

foreach ($status['folders_checked'] as $folder)
{
$exists = in_array($folder, array_values($status['folders_exist']));

if ($exists)
{
$symfonyStyle->writeln('<error>Folder Checked & Exists</error> - ' . $folder, OutputInterface::VERBOSITY_VERY_VERBOSE);
}
else
{
$symfonyStyle->writeln('<info>Folder Checked & Doesn\'t Exist</info> - ' . $folder, OutputInterface::VERBOSITY_DEBUG);
}
}
}

if ($dryRun === false)
{
foreach ($status['files_deleted'] as $file)
{
$symfonyStyle->writeln('<comment>File Deleted = ' . $file . '</comment>', OutputInterface::VERBOSITY_VERBOSE);
}

foreach ($status['files_errors'] as $error)
{
$symfonyStyle->error($error);
}

foreach ($status['folders_deleted'] as $folder)
{
$symfonyStyle->writeln('<comment>Folder Deleted = ' . $folder . '</comment>', 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;
}

/**
Expand All @@ -69,6 +141,7 @@ protected function configure(): void
\nUsage: <info>php %command.full_name%</info>";

$this->setDescription('Remove old system files');
$this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Executes a dry run without deleting anything');
$this->setHelp($help);
}
}