1
|
|
|
<?php |
2
|
|
|
namespace Fab\Media\Command; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the Fab/Media project under GPLv2 or later. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please read the |
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use TYPO3\CMS\Core\Resource\StorageRepository; |
12
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
13
|
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Command Controller which handles actions related to File Index. |
17
|
|
|
*/ |
18
|
|
|
class MissingFilesCommandController extends CommandController |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $message = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $missingFiles = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $deletedFiles = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \TYPO3\CMS\Core\Mail\MailMessage |
38
|
|
|
*/ |
39
|
|
|
protected $mailMessage; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Check whether the Index is Ok. In case not, display a message on the console. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function analyseCommand() |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
foreach ($this->getStorageRepository()->findAll() as $storage) { |
50
|
|
|
|
51
|
|
|
// For the CLI cause. |
52
|
|
|
$storage->setEvaluatePermissions(false); |
53
|
|
|
|
54
|
|
|
$this->printOut(); |
55
|
|
|
$this->printOut(sprintf('%s (%s)', $storage->getName(), $storage->getUid())); |
56
|
|
|
$this->printOut('--------------------------------------------'); |
57
|
|
|
|
58
|
|
|
if ($storage->isOnline()) { |
59
|
|
|
|
60
|
|
|
$missingFiles = $this->getIndexAnalyser()->searchForMissingFiles($storage); |
61
|
|
|
if (empty($missingFiles)) { |
62
|
|
|
$this->printOut(); |
63
|
|
|
$this->printOut('Looks good, no missing files!'); |
64
|
|
|
} else { |
65
|
|
|
// Missing files... |
66
|
|
|
$this->printOut(); |
67
|
|
|
$this->printOut('Missing resources:'); |
68
|
|
|
$this->missingFiles[$storage->getUid()] = $missingFiles; // Store missing files. |
69
|
|
|
|
70
|
|
|
/** @var \TYPO3\CMS\Core\Resource\File $missingFile */ |
71
|
|
|
foreach ($missingFiles as $missingFile) { |
72
|
|
|
$message = sprintf('* Missing file "%s" with identifier "%s".', |
73
|
|
|
$missingFile->getUid(), |
74
|
|
|
$missingFile->getIdentifier() |
75
|
|
|
); |
76
|
|
|
$this->printOut($message); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} else { |
81
|
|
|
$this->outputLine('Storage is offline!'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$to = $this->getTo(); |
86
|
|
|
if (!empty($to)) { |
87
|
|
|
$this->sendReport(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Delete the missing files which have no file references |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function deleteCommand() |
97
|
|
|
{ |
98
|
|
|
|
99
|
|
|
foreach ($this->getStorageRepository()->findAll() as $storage) { |
100
|
|
|
|
101
|
|
|
// For the CLI cause. |
102
|
|
|
$storage->setEvaluatePermissions(false); |
103
|
|
|
|
104
|
|
|
$this->printOut(); |
105
|
|
|
$this->printOut(sprintf('%s (%s)', $storage->getName(), $storage->getUid())); |
106
|
|
|
$this->printOut('--------------------------------------------'); |
107
|
|
|
|
108
|
|
|
if ($storage->isOnline()) { |
109
|
|
|
|
110
|
|
|
$deletedFiles = $this->getIndexAnalyser()->deleteMissingFiles($storage); |
111
|
|
|
if (empty($deletedFiles)) { |
112
|
|
|
$this->printOut(); |
113
|
|
|
$this->printOut('No files deleted!'); |
114
|
|
|
} else { |
115
|
|
|
// Missing files... |
116
|
|
|
$this->printOut(); |
117
|
|
|
$this->printOut('Deleted Files:'); |
118
|
|
|
/** @var \TYPO3\CMS\Core\Resource\File $deletedFile */ |
119
|
|
|
foreach ($deletedFiles as $deletedFileUid => $deletedFileIdentifier) { |
120
|
|
|
$message = sprintf('* Deleted file "%s" with identifier "%s".', |
121
|
|
|
$deletedFileUid, |
122
|
|
|
$deletedFileIdentifier |
123
|
|
|
); |
124
|
|
|
$this->printOut($message); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} else { |
129
|
|
|
$this->outputLine('Storage is offline!'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Print a message and store its content in a variable for the email report. |
136
|
|
|
* |
137
|
|
|
* @param string $message |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
protected function printOut($message = '') |
141
|
|
|
{ |
142
|
|
|
$this->message[] = $message; |
143
|
|
|
$this->outputLine($message); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Send a possible report to an admin. |
148
|
|
|
* |
149
|
|
|
* @throws \Exception |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
protected function sendReport() |
153
|
|
|
{ |
154
|
|
|
if ($this->hasReport()) { |
155
|
|
|
|
156
|
|
|
// Prepare email. |
157
|
|
|
$this->getMailMessage()->setTo($this->getTo()) |
158
|
|
|
->setFrom($this->getFrom()) |
159
|
|
|
->setSubject('Missing files detected!') |
160
|
|
|
->setBody(implode("\n", $this->message)); |
161
|
|
|
|
162
|
|
|
$isSent = $this->getMailMessage()->send(); |
163
|
|
|
|
164
|
|
|
if (!$isSent) { |
165
|
|
|
throw new \Exception('I could not send a message', 1408343882); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$to = $this->getTo(); |
169
|
|
|
$this->outputLine(); |
170
|
|
|
$message = sprintf('Report was sent to %s', key($to)); |
171
|
|
|
$this->outputLine($message); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Send a report |
177
|
|
|
* |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
protected function hasReport() |
181
|
|
|
{ |
182
|
|
|
return !empty($this->missingFiles); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
protected function getTo() |
189
|
|
|
{ |
190
|
|
|
|
191
|
|
|
$to = []; |
192
|
|
|
|
193
|
|
|
// @todo make me more flexible! |
194
|
|
|
if (!empty($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'])) { |
195
|
|
|
$emailAddress = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']; |
196
|
|
|
$name = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']; |
197
|
|
|
$to[$emailAddress] = $name; |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
return $to; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
protected function getFrom() |
207
|
|
|
{ |
208
|
|
|
$from = []; |
209
|
|
|
|
210
|
|
|
// @todo make me more flexible! |
211
|
|
|
if (!empty($GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'])) { |
212
|
|
|
$emailAddress = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']; |
213
|
|
|
$name = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']; |
214
|
|
|
$from[$emailAddress] = $name; |
215
|
|
|
} |
216
|
|
|
return $from; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return StorageRepository|object |
221
|
|
|
*/ |
222
|
|
|
protected function getStorageRepository() |
223
|
|
|
{ |
224
|
|
|
return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return \TYPO3\CMS\Core\Mail\MailMessage |
229
|
|
|
*/ |
230
|
|
|
public function getMailMessage() |
231
|
|
|
{ |
232
|
|
|
if (is_null($this->mailMessage)) { |
233
|
|
|
$this->mailMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class); |
234
|
|
|
} |
235
|
|
|
return $this->mailMessage; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Return a pointer to the database. |
240
|
|
|
* |
241
|
|
|
* @return \Fab\Media\Index\IndexAnalyser|object |
242
|
|
|
*/ |
243
|
|
|
protected function getIndexAnalyser() |
244
|
|
|
{ |
245
|
|
|
return GeneralUtility::makeInstance(\Fab\Media\Index\IndexAnalyser::class); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
} |
249
|
|
|
|