/
RemoveUnusedMediaCommand.php
192 lines (168 loc) · 6.7 KB
/
RemoveUnusedMediaCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace FIREGENTO\Magento\Command\Eav;
use N98\Magento\Command\AbstractMagentoCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Exception\InvalidArgumentException;
class RemoveUnusedMediaCommand extends AbstractCommand
{
protected function configure()
{
$this
->setName('eav:media:remove-unused')
->setDescription('Remove unused product images')
->addOption('dry-run')
->addOption(
'force',
null,
InputOption::VALUE_NONE,
'Don\'t ask any interactive questions (use this option with automated scripts or cronjobs)'
)
->addOption(
'format',
null,
InputOption::VALUE_OPTIONAL,
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
)
->addOption(
'backup',
null,
InputOption::VALUE_REQUIRED,
'Don\'t delete the product images, move them to a backup directory'
)
->addOption(
'touch',
null,
InputOption::VALUE_NONE,
'Modify the backup file date and time (use only with --backup)'
)
;
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$filesize = 0;
$countFiles = 0;
$isDryRun = $input->getOption('dry-run');
$force = $input->getOption('force');
if (!$isDryRun) {
$output->writeln('WARNING: this is not a dry run. If you want to do a dry-run, add --dry-run.');
$question = new ConfirmationQuestion('Are you sure you want to continue? [No] ', false);
$this->questionHelper = $this->getHelper('question');
if (!$force && !$this->questionHelper->ask($input, $output, $question)) {
return;
}
}
$backupDir = $input->getOption('backup');
if ($backupDir && !$this->isDirWritable($backupDir)) {
throw new InvalidArgumentException("The directory {$backupDir} does not exist or is not writable.");
}
$this->detectMagento($output);
if ($this->initMagento()) {
$table = array();
$imageDir = \Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product';
$resource = \Mage::getSingleton('core/resource');
$mediaGallery = $this->_prefixTable('catalog_product_entity_media_gallery');
$coreRead = $resource->getConnection('core_read');
$i=0;
$directoryIterator = new \RecursiveDirectoryIterator($imageDir);
foreach (new \RecursiveIteratorIterator($directoryIterator) as $file) {
$filePath = str_replace($imageDir, "", $file);
if (empty($filePath)) {
continue;
}
if (!$this->isCatalogProductImage($filePath) || is_dir($file)) {
continue;
}
$value = $coreRead->fetchOne('SELECT value FROM ' . $mediaGallery . ' WHERE BINARY value = ?', array($filePath));
if ($value == false) {
$row = array();
$row[] = $filePath;
$table[] = $row;
$filesize += filesize($file);
$countFiles++;
echo '## REMOVING: ' . $filePath . ' ##';
if ($backupDir) {
echo ' -- backup saved to ' . $backupDir . $filePath;
}
if (!$isDryRun) {
if ($backupDir) {
$this->backup($file, $backupDir, $filePath, $input->getOption('touch'));
} else {
unlink($file);
}
} else {
echo ' -- DRY RUN';
}
echo PHP_EOL;
$i++;
}
}
$headers = array();
$headers[] = 'filepath';
$this->getHelper('table')
->setHeaders($headers)
->renderByFormat($output, $table, $input->getOption('format'));
$output->writeln("Found " . number_format($filesize/1024/1024, '2') . " MB unused images in $countFiles files");
}
}
/**
* Move a file from origin to backupDir keeping the directory structure ($relativePath).
* If the parameter $touch is true, the backup file date & time will be updated.
*
* @param string $origin
* @param string $backupDir
* @param string $relativePath
* @param boolean $touch
*/
protected function backup($origin, $backupDir, $relativePath, $touch = false)
{
$filename = basename($origin);
$relativeDir = str_replace($filename, "", $relativePath);
$destinationDir = $backupDir . $relativeDir;
if (!is_dir($destinationDir)) {
mkdir($destinationDir, 0777, true);
}
if (!rename($origin, $backupDir . $relativePath)) {
throw new RuntimeException("Error moving {$filePath} to {$destinationDir}");
}
if ($touch === true) {
if (!touch($backupDir . $relativePath)) {
throw new RuntimeException("Error modifying date and time of {$relativePath}");
}
}
}
/**
* Return true if the file path matches the pattern of auto generated
* product images, e.g. /i/m/image.jpg
* The pattern matches a single alphanumeric character between slashes
* at the start of the relative file path, thus ignoring cache and
* custom directories in the catalog/product directory.
*
* @param string $file
* @return boolean
*/
public function isCatalogProductImage($filePath)
{
$pattern = '/^\/[a-zA-Z\d]{1}\//';
return (bool) preg_match($pattern, $filePath);
}
/**
* Check if a directory exists and is writable.
*
* @param string $dir
* @return boolean
*/
protected function isDirWritable($dir)
{
return is_dir($dir) && is_writable($dir);
}
}