Skip to content

Commit 96f5c59

Browse files
author
fabrizio
committed
Added new option with a new sql query considering the relation between media and product; added new option to delete only the cache images
1 parent 447e799 commit 96f5c59

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Console/Command/RemoveUnusedMediaCommand.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class RemoveUnusedMediaCommand extends Command
1717
{
1818
private const OPTION_DRY_RUN = 'dry-run';
1919
private const OPTION_INCLUDING_CACHE = 'including-cache';
20+
21+
private const OPTION_ONLY_CACHE = 'only-cache';
22+
23+
private const OPTION_INCLUDING_RELATION_ENTITY = 'including-relation';
24+
2025
private const OPTION_FORCE = 'force';
2126
private const COMMAND_NAME_EAV_MEDIA_REMOVE_UNUSED = 'eav:media:remove-unused';
2227

@@ -44,6 +49,11 @@ public function execute(InputInterface $input, OutputInterface $output): int
4449
$isForce = $input->getOption(self::OPTION_FORCE);
4550
$isDryRun = $input->getOption(self::OPTION_DRY_RUN);
4651
$deleteCacheAsWell = $input->getOption(self::OPTION_INCLUDING_CACHE);
52+
$deleteOnlyCache = $input->getOption(self::OPTION_ONLY_CACHE);
53+
if ($deleteOnlyCache) {
54+
$deleteCacheAsWell=true;
55+
}
56+
$deleteNotInRelation = $input->getOption(self::OPTION_INCLUDING_RELATION_ENTITY);
4757

4858
if (!$isDryRun && !$isForce) {
4959
if (!$input->isInteractive()) {
@@ -69,13 +79,22 @@ public function execute(InputInterface $input, OutputInterface $output): int
6979
}
7080

7181
$imageDir = $this->getImageDir();
82+
7283
$connection = $this->resourceConnection->getConnection('core_read');
7384
$mediaGalleryTable = $this->resourceConnection->getTableName('catalog_product_entity_media_gallery');
7485

7586
$directoryIterator = new RecursiveDirectoryIterator($imageDir);
7687

7788
$imagesToKeep = $connection->fetchCol('SELECT value FROM ' . $mediaGalleryTable);
7889

90+
// begin new code
91+
if ($deleteNotInRelation) {
92+
$mediaGalleryToEntityTable = $this->resourceConnection->getTableName('catalog_product_entity_media_gallery_value_to_entity');
93+
$sql='SELECT value FROM ' . $mediaGalleryTable. ' where value_id IN (SELECT value_id from '.$mediaGalleryToEntityTable.')';
94+
$imagesToKeep = $connection->fetchCol($sql);
95+
}
96+
// end
97+
7998
foreach (new RecursiveIteratorIterator($directoryIterator) as $file) {
8099
// Directory guard
81100
if (is_dir($file)) {
@@ -87,6 +106,11 @@ public function execute(InputInterface $input, OutputInterface $output): int
87106
continue;
88107
}
89108

109+
// Original image guard if option --only-cache
110+
if (!$this->isInCachePath($file) && $deleteOnlyCache) {
111+
continue;
112+
}
113+
90114
$filePath = str_replace($imageDir, "", $file);
91115
// Filepath guard
92116
if (empty($filePath)) {
@@ -164,6 +188,21 @@ protected function configure(): void
164188
null,
165189
'Also clear the ./cache/* entries for the corresponding images'
166190
);
191+
192+
$this->addOption(
193+
self::OPTION_ONLY_CACHE,
194+
'k',
195+
null,
196+
'Clear only the ./cache/* entries for the corresponding images nad not the corresponding images'
197+
);
198+
199+
$this->addOption(
200+
self::OPTION_INCLUDING_RELATION_ENTITY,
201+
'r',
202+
null,
203+
'Also clear the media not in relation table "catalog_product_entity_media_gallery_value_to_entity"'
204+
);
205+
167206
$this->addOption(
168207
self::OPTION_DRY_RUN,
169208
'd',

0 commit comments

Comments
 (0)