[TASK] Use null coalescing operator where possible
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Processing / FileDeletionAspect.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Processing;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Resource\File;
19 use TYPO3\CMS\Core\Resource\FileInterface;
20 use TYPO3\CMS\Core\Resource\ProcessedFile;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Class FileDeletionAspect
25 *
26 * We do not have AOP in TYPO3 for now, thus the aspect which
27 * deals with deleted files is a slot which reacts on a signal
28 * on file deletion.
29 *
30 * The aspect cleans up database records, processed files and filereferences
31 */
32 class FileDeletionAspect
33 {
34 /**
35 * Return a file index repository
36 *
37 * @return \TYPO3\CMS\Core\Resource\Index\FileIndexRepository
38 */
39 protected function getFileIndexRepository()
40 {
41 return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Index\FileIndexRepository::class);
42 }
43
44 /**
45 * Return a metadata repository
46 *
47 * @return \TYPO3\CMS\Core\Resource\Index\MetaDataRepository
48 */
49 protected function getMetaDataRepository()
50 {
51 return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class);
52 }
53
54 /**
55 * Return a processed file repository
56 *
57 * @return \TYPO3\CMS\Core\Resource\ProcessedFileRepository
58 */
59 protected function getProcessedFileRepository()
60 {
61 return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ProcessedFileRepository::class);
62 }
63
64 /**
65 * Cleanup database record for a deleted file
66 *
67 * @param FileInterface $fileObject
68 */
69 public function removeFromRepository(FileInterface $fileObject)
70 {
71 // remove file from repository
72 if ($fileObject instanceof File) {
73 $this->cleanupProcessedFiles($fileObject);
74 $this->cleanupCategoryReferences($fileObject);
75 $this->getFileIndexRepository()->remove($fileObject->getUid());
76 $this->getMetaDataRepository()->removeByFileUid($fileObject->getUid());
77
78 // remove all references
79 GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_reference')
80 ->delete(
81 'sys_file_reference',
82 [
83 'uid_local' => (int)$fileObject->getUid(),
84 'table_local' => 'sys_file'
85 ]
86 );
87 } elseif ($fileObject instanceof ProcessedFile) {
88 GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_processedfile')
89 ->delete(
90 'sys_file_processedfile',
91 [
92 'uid' => (int)$fileObject->getUid()
93 ]
94 );
95 }
96 }
97
98 /**
99 * Remove all processed files on SIGNAL_PostFileAdd
100 *
101 * @param FileInterface $file
102 * @param string $targetFolder
103 */
104 public function cleanupProcessedFilesPostFileAdd(FileInterface $file, $targetFolder)
105 {
106 $this->cleanupProcessedFiles($file);
107 }
108
109 /**
110 * Remove all processed files on SIGNAL_PostFileReplace
111 *
112 * @param FileInterface $file
113 * @param string $localFilePath
114 */
115 public function cleanupProcessedFilesPostFileReplace(FileInterface $file, $localFilePath)
116 {
117 $this->cleanupProcessedFiles($file);
118 }
119
120 /**
121 * Remove all category references of the deleted file.
122 *
123 * @param File $fileObject
124 */
125 protected function cleanupCategoryReferences(File $fileObject)
126 {
127 // Retrieve the file metadata uid which is different from the file uid.
128 $metadataProperties = $fileObject->_getMetaData();
129 $metaDataUid = $metadataProperties['_ORIG_uid'] ?? $metadataProperties['uid'];
130
131 GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_category_record_mm')
132 ->delete(
133 'sys_category_record_mm',
134 [
135 'uid_foreign' => (int)$metaDataUid,
136 'tablenames' => 'sys_file_metadata'
137 ]
138 );
139 }
140
141 /**
142 * Remove all processed files that belong to the given File object
143 *
144 * @param FileInterface $fileObject
145 */
146 protected function cleanupProcessedFiles(FileInterface $fileObject)
147 {
148 // only delete processed files of File objects
149 if (!$fileObject instanceof File) {
150 return;
151 }
152
153 /** @var $processedFile \TYPO3\CMS\Core\Resource\ProcessedFile */
154 foreach ($this->getProcessedFileRepository()->findAllByOriginalFile($fileObject) as $processedFile) {
155 if ($processedFile->exists()) {
156 $processedFile->delete(true);
157 }
158 $this->removeFromRepository($processedFile);
159 }
160 }
161 }