82e52decc3087d08736c3a7e134f22cfcce341c3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / ProcessedFileRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
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\Utility;
18
19 /**
20 * Repository for accessing files
21 * it also serves as the public API for the indexing part of files in general
22 *
23 * @author Benjamin Mack <benni@typo3.org>
24 * @author Ingmar Schlecht <ingmar@typo3.org>
25 */
26 class ProcessedFileRepository extends AbstractRepository {
27
28 /**
29 * The main object type of this class. In some cases (fileReference) this
30 * repository can also return FileReference objects, implementing the
31 * common FileInterface.
32 *
33 * @var string
34 */
35 protected $objectType = \TYPO3\CMS\Core\Resource\ProcessedFile::class;
36
37 /**
38 * Main File object storage table. Note that this repository also works on
39 * the sys_file_reference table when returning FileReference objects.
40 *
41 * @var string
42 */
43 protected $table = 'sys_file_processedfile';
44
45 /**
46 * @var ResourceFactory
47 */
48 protected $resourceFactory;
49
50 /**
51 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
52 */
53 protected $databaseConnection;
54
55 /**
56 * Creates this object.
57 */
58 public function __construct() {
59 $this->resourceFactory = Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
60 $this->databaseConnection = $GLOBALS['TYPO3_DB'];
61 }
62
63 /**
64 * Creates a ProcessedFile object from a file object and a processing configuration
65 *
66 * @param FileInterface $originalFile
67 * @param string $taskType
68 * @param array $configuration
69 * @return ProcessedFile
70 */
71 public function createNewProcessedFileObject(FileInterface $originalFile, $taskType, array $configuration) {
72 return Utility\GeneralUtility::makeInstance(
73 $this->objectType,
74 $originalFile,
75 $taskType,
76 $configuration
77 );
78 }
79
80 /**
81 * @param array $databaseRow
82 * @return ProcessedFile
83 */
84 protected function createDomainObject(array $databaseRow) {
85 $originalFile = $this->resourceFactory->getFileObject((int)$databaseRow['original']);
86 $originalFile->setStorage($this->resourceFactory->getStorageObject($originalFile->getProperty('storage')));
87 $taskType = $databaseRow['task_type'];
88 $configuration = unserialize($databaseRow['configuration']);
89
90 return Utility\GeneralUtility::makeInstance(
91 $this->objectType,
92 $originalFile,
93 $taskType,
94 $configuration,
95 $databaseRow
96 );
97 }
98
99 /**
100 * @param ResourceStorage $storage
101 * @param string $identifier
102 *
103 * @return null|ProcessedFile
104 */
105 public function findByStorageAndIdentifier(ResourceStorage $storage, $identifier) {
106 $processedFileObject = NULL;
107 if ($storage->hasFile($identifier)) {
108 $databaseRow = $this->databaseConnection->exec_SELECTgetSingleRow(
109 '*',
110 $this->table,
111 'storage = ' . (int)$storage->getUid() .
112 ' AND identifier = ' . $this->databaseConnection->fullQuoteStr($identifier, $this->table)
113 );
114 if ($databaseRow) {
115 $processedFileObject = $this->createDomainObject($databaseRow);
116 }
117 }
118 return $processedFileObject;
119 }
120 /**
121 * Adds a processedfile object in the database
122 *
123 * @param ProcessedFile $processedFile
124 * @return void
125 */
126 public function add($processedFile) {
127 if ($processedFile->isPersisted()) {
128 $this->update($processedFile);
129 } else {
130 $insertFields = $processedFile->toArray();
131 $insertFields['crdate'] = $insertFields['tstamp'] = time();
132 $insertFields = $this->cleanUnavailableColumns($insertFields);
133 $this->databaseConnection->exec_INSERTquery($this->table, $insertFields);
134 $uid = $this->databaseConnection->sql_insert_id();
135 $processedFile->updateProperties(array('uid' => $uid));
136 }
137 }
138
139 /**
140 * Updates an existing file object in the database
141 *
142 * @param ProcessedFile $processedFile
143 * @return void
144 */
145 public function update($processedFile) {
146 if ($processedFile->isPersisted()) {
147 $uid = (int)$processedFile->getUid();
148 $updateFields = $this->cleanUnavailableColumns($processedFile->toArray());
149 $updateFields['tstamp'] = time();
150 $this->databaseConnection->exec_UPDATEquery($this->table, 'uid=' . (int)$uid, $updateFields);
151 }
152 }
153
154 /**
155 * @param \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\FileInterface $file
156 * @param string $taskType The task that should be executed on the file
157 * @param array $configuration
158 *
159 * @return ProcessedFile
160 */
161 public function findOneByOriginalFileAndTaskTypeAndConfiguration(FileInterface $file, $taskType, array $configuration) {
162 $databaseRow = $this->databaseConnection->exec_SELECTgetSingleRow(
163 '*',
164 $this->table,
165 'original=' . (int)$file->getUid() .
166 ' AND task_type=' . $this->databaseConnection->fullQuoteStr($taskType, $this->table) .
167 ' AND configurationsha1=' . $this->databaseConnection->fullQuoteStr(sha1(serialize($configuration)), $this->table)
168 );
169
170 if (is_array($databaseRow)) {
171 $processedFile = $this->createDomainObject($databaseRow);
172 } else {
173 $processedFile = $this->createNewProcessedFileObject($file, $taskType, $configuration);
174 }
175 return $processedFile;
176 }
177
178 /**
179 * @param FileInterface $file
180 * @return ProcessedFile[]
181 * @throws \InvalidArgumentException
182 */
183 public function findAllByOriginalFile(FileInterface $file) {
184 if (!$file instanceof File) {
185 throw new \InvalidArgumentException('Parameter is no File object but got type "'
186 . (is_object($file) ? get_class($file) : gettype($file)) . '"', 1382006142);
187 }
188 $whereClause = 'original=' . (int)$file->getUid();
189 $rows = $this->databaseConnection->exec_SELECTgetRows('*', $this->table, $whereClause);
190
191 $itemList = array();
192 if ($rows !== NULL) {
193 foreach ($rows as $row) {
194 $itemList[] = $this->createDomainObject($row);
195 }
196 }
197 return $itemList;
198 }
199
200
201 /**
202 * Removes all array keys which cannot be persisted
203 *
204 * @param array $data
205 *
206 * @return array
207 */
208 protected function cleanUnavailableColumns(array $data) {
209 return array_intersect_key($data, $this->databaseConnection->admin_get_fields($this->table));
210 }
211
212 }