1af97133486b37922b28122f672f9027be9908d8
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Processing / FileDeletionAspect.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Processing;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Frans Saris <franssaris@gmail.com>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Core\Resource\File;
31 use TYPO3\CMS\Core\Resource\ProcessedFile;
32 use TYPO3\CMS\Core\Resource\FileInterface;
33 use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35 /**
36 * Class FileDeletionAspect
37 *
38 * We do not have AOP in TYPO3 for now, thus the aspect which
39 * deals with deleted files is a slot which reacts on a signal
40 * on file deletion.
41 *
42 * The aspect cleans up database records, processed files and filereferences
43 */
44 class FileDeletionAspect {
45
46 /**
47 * Return a file index repository
48 *
49 * @return \TYPO3\CMS\Core\Resource\Index\FileIndexRepository
50 */
51 protected function getFileIndexRepository() {
52 return GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\FileIndexRepository');
53 }
54
55 /**
56 * Return a metadata repository
57 *
58 * @return \TYPO3\CMS\Core\Resource\Index\MetaDataRepository
59 */
60 protected function getMetaDataRepository() {
61 return GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository');
62 }
63
64 /**
65 * Return a processed file repository
66 *
67 * @return \TYPO3\CMS\Core\Resource\ProcessedFileRepository
68 */
69 protected function getProcessedFileRepository() {
70 return GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ProcessedFileRepository');
71 }
72
73 /**
74 * Wrapper method for getting DatabaseConnection
75 *
76 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
77 */
78 protected function getDatabase() {
79 return $GLOBALS['TYPO3_DB'];
80 }
81
82 /**
83 * Cleanup database record for a deleted file
84 *
85 * @param FileInterface $fileObject
86 * @return void
87 */
88 public function removeFromRepository(FileInterface $fileObject) {
89 // remove file from repository
90 if ($fileObject instanceof File) {
91 $this->getFileIndexRepository()->remove($fileObject->getUid());
92 $this->getMetaDataRepository()->removeByFileUid($fileObject->getUid());
93 // remove all references
94 $this->getDatabase()->exec_DELETEquery(
95 'sys_file_reference',
96 'uid_local=' . intval($fileObject->getUid()) . ' AND table_local = \'sys_file\''
97 );
98 $this->cleanupProcessedFiles($fileObject);
99 } elseif ($fileObject instanceof ProcessedFile) {
100 $this->getDatabase()->exec_DELETEquery('sys_file_processedfile', 'uid=' . intval($fileObject->getUid()));
101 }
102 }
103
104 /**
105 * Remove all processed files that belong to the given File object
106 *
107 * @param FileInterface $fileObject
108 * @return void
109 */
110 public function cleanupProcessedFiles(FileInterface $fileObject) {
111
112 // only delete processed files of File objects
113 if (!$fileObject instanceof File) {
114 return;
115 }
116
117 /** @var $processedFile \TYPO3\CMS\Core\Resource\ProcessedFile */
118 foreach ($this->getProcessedFileRepository()->findAllByOriginalFile($fileObject) as $processedFile) {
119 $processedFile->delete(TRUE);
120 }
121 }
122 }