From 96f5c599dc033de1475a201f9598da94b3eb109b Mon Sep 17 00:00:00 2001 From: fabrizio Date: Thu, 26 Dec 2024 11:46:18 +0100 Subject: [PATCH 1/3] Added new option with a new sql query considering the relation between media and product; added new option to delete only the cache images --- Console/Command/RemoveUnusedMediaCommand.php | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Console/Command/RemoveUnusedMediaCommand.php b/Console/Command/RemoveUnusedMediaCommand.php index cc2a59b..9b17733 100644 --- a/Console/Command/RemoveUnusedMediaCommand.php +++ b/Console/Command/RemoveUnusedMediaCommand.php @@ -17,6 +17,11 @@ class RemoveUnusedMediaCommand extends Command { private const OPTION_DRY_RUN = 'dry-run'; private const OPTION_INCLUDING_CACHE = 'including-cache'; + + private const OPTION_ONLY_CACHE = 'only-cache'; + + private const OPTION_INCLUDING_RELATION_ENTITY = 'including-relation'; + private const OPTION_FORCE = 'force'; private const COMMAND_NAME_EAV_MEDIA_REMOVE_UNUSED = 'eav:media:remove-unused'; @@ -44,6 +49,11 @@ public function execute(InputInterface $input, OutputInterface $output): int $isForce = $input->getOption(self::OPTION_FORCE); $isDryRun = $input->getOption(self::OPTION_DRY_RUN); $deleteCacheAsWell = $input->getOption(self::OPTION_INCLUDING_CACHE); + $deleteOnlyCache = $input->getOption(self::OPTION_ONLY_CACHE); + if ($deleteOnlyCache) { + $deleteCacheAsWell=true; + } + $deleteNotInRelation = $input->getOption(self::OPTION_INCLUDING_RELATION_ENTITY); if (!$isDryRun && !$isForce) { if (!$input->isInteractive()) { @@ -69,6 +79,7 @@ public function execute(InputInterface $input, OutputInterface $output): int } $imageDir = $this->getImageDir(); + $connection = $this->resourceConnection->getConnection('core_read'); $mediaGalleryTable = $this->resourceConnection->getTableName('catalog_product_entity_media_gallery'); @@ -76,6 +87,14 @@ public function execute(InputInterface $input, OutputInterface $output): int $imagesToKeep = $connection->fetchCol('SELECT value FROM ' . $mediaGalleryTable); + // begin new code + if ($deleteNotInRelation) { + $mediaGalleryToEntityTable = $this->resourceConnection->getTableName('catalog_product_entity_media_gallery_value_to_entity'); + $sql='SELECT value FROM ' . $mediaGalleryTable. ' where value_id IN (SELECT value_id from '.$mediaGalleryToEntityTable.')'; + $imagesToKeep = $connection->fetchCol($sql); + } + // end + foreach (new RecursiveIteratorIterator($directoryIterator) as $file) { // Directory guard if (is_dir($file)) { @@ -87,6 +106,11 @@ public function execute(InputInterface $input, OutputInterface $output): int continue; } + // Original image guard if option --only-cache + if (!$this->isInCachePath($file) && $deleteOnlyCache) { + continue; + } + $filePath = str_replace($imageDir, "", $file); // Filepath guard if (empty($filePath)) { @@ -164,6 +188,21 @@ protected function configure(): void null, 'Also clear the ./cache/* entries for the corresponding images' ); + + $this->addOption( + self::OPTION_ONLY_CACHE, + 'k', + null, + 'Clear only the ./cache/* entries for the corresponding images nad not the corresponding images' + ); + + $this->addOption( + self::OPTION_INCLUDING_RELATION_ENTITY, + 'r', + null, + 'Also clear the media not in relation table "catalog_product_entity_media_gallery_value_to_entity"' + ); + $this->addOption( self::OPTION_DRY_RUN, 'd', From 3c6c140644b53da28c410845a8f4f17cd30210d8 Mon Sep 17 00:00:00 2001 From: fabrizio Date: Thu, 26 Dec 2024 11:52:38 +0100 Subject: [PATCH 2/3] Cleaned code --- Console/Command/RemoveUnusedMediaCommand.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Console/Command/RemoveUnusedMediaCommand.php b/Console/Command/RemoveUnusedMediaCommand.php index 9b17733..2fcdff6 100644 --- a/Console/Command/RemoveUnusedMediaCommand.php +++ b/Console/Command/RemoveUnusedMediaCommand.php @@ -17,11 +17,8 @@ class RemoveUnusedMediaCommand extends Command { private const OPTION_DRY_RUN = 'dry-run'; private const OPTION_INCLUDING_CACHE = 'including-cache'; - private const OPTION_ONLY_CACHE = 'only-cache'; - private const OPTION_INCLUDING_RELATION_ENTITY = 'including-relation'; - private const OPTION_FORCE = 'force'; private const COMMAND_NAME_EAV_MEDIA_REMOVE_UNUSED = 'eav:media:remove-unused'; @@ -79,7 +76,6 @@ public function execute(InputInterface $input, OutputInterface $output): int } $imageDir = $this->getImageDir(); - $connection = $this->resourceConnection->getConnection('core_read'); $mediaGalleryTable = $this->resourceConnection->getTableName('catalog_product_entity_media_gallery'); @@ -87,13 +83,11 @@ public function execute(InputInterface $input, OutputInterface $output): int $imagesToKeep = $connection->fetchCol('SELECT value FROM ' . $mediaGalleryTable); - // begin new code if ($deleteNotInRelation) { $mediaGalleryToEntityTable = $this->resourceConnection->getTableName('catalog_product_entity_media_gallery_value_to_entity'); $sql='SELECT value FROM ' . $mediaGalleryTable. ' where value_id IN (SELECT value_id from '.$mediaGalleryToEntityTable.')'; $imagesToKeep = $connection->fetchCol($sql); } - // end foreach (new RecursiveIteratorIterator($directoryIterator) as $file) { // Directory guard @@ -181,28 +175,24 @@ protected function configure(): void { $this->setName(self::COMMAND_NAME_EAV_MEDIA_REMOVE_UNUSED); $this->setDescription('Remove unused product images'); - $this->addOption( self::OPTION_INCLUDING_CACHE, 'c', null, 'Also clear the ./cache/* entries for the corresponding images' ); - $this->addOption( self::OPTION_ONLY_CACHE, 'k', null, 'Clear only the ./cache/* entries for the corresponding images nad not the corresponding images' ); - $this->addOption( self::OPTION_INCLUDING_RELATION_ENTITY, 'r', null, 'Also clear the media not in relation table "catalog_product_entity_media_gallery_value_to_entity"' ); - $this->addOption( self::OPTION_DRY_RUN, 'd', From 6f1c890ce14d026216de322683fd278e0dd8e0b4 Mon Sep 17 00:00:00 2001 From: fabrizio Date: Thu, 26 Dec 2024 11:55:04 +0100 Subject: [PATCH 3/3] Changed option description --- Console/Command/RemoveUnusedMediaCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Console/Command/RemoveUnusedMediaCommand.php b/Console/Command/RemoveUnusedMediaCommand.php index 2fcdff6..3e16368 100644 --- a/Console/Command/RemoveUnusedMediaCommand.php +++ b/Console/Command/RemoveUnusedMediaCommand.php @@ -185,7 +185,7 @@ protected function configure(): void self::OPTION_ONLY_CACHE, 'k', null, - 'Clear only the ./cache/* entries for the corresponding images nad not the corresponding images' + 'Clear only the ./cache/* entries for the corresponding images, but not the corresponding images' ); $this->addOption( self::OPTION_INCLUDING_RELATION_ENTITY,