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