[BUGFIX] Followup to remove PHP5.5 only syntax
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Index / MetaDataRepository.php
1 <?php
2
3 namespace TYPO3\CMS\Core\Resource\Index;
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 use TYPO3\CMS\Core\Resource\File;
19 use TYPO3\CMS\Core\SingletonInterface;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Type\File as FileType;
22
23 /**
24 * Repository Class as an abstraction layer to sys_file_metadata
25 *
26 * Every access to table sys_file_metadata which is not handled by TCEmain
27 * has to use this Repository class
28 */
29 class MetaDataRepository implements SingletonInterface {
30
31 /**
32 * @var string
33 */
34 protected $tableName = 'sys_file_metadata';
35
36 /**
37 * Internal storage for database table fields
38 *
39 * @var array
40 */
41 protected $tableFields = array();
42
43 /**
44 * Wrapper method for getting DatabaseConnection
45 *
46 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
47 */
48 protected function getDatabaseConnection() {
49 return $GLOBALS['TYPO3_DB'];
50 }
51
52 /**
53 * Returns array of meta-data properties
54 *
55 * @param File $file
56 * @return array
57 */
58 public function findByFile(File $file) {
59 $record = $this->findByFileUid($file->getUid());
60
61 // It could be possible that the meta information is freshly
62 // created and inserted into the database. If this is the case
63 // we have to take care about correct meta information for width and
64 // height in case of an image.
65 if (!empty($record['newlyCreated'])) {
66 if ($file->getType() === File::FILETYPE_IMAGE && $file->getStorage()->getDriverType() === 'Local') {
67 $fileNameAndPath = $file->getForLocalProcessing(FALSE);
68
69 $imageInfo = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Type\\File\\ImageInfo', $fileNameAndPath);
70
71 $additionalMetaInformation = array(
72 'width' => $imageInfo->getWidth(),
73 'height' => $imageInfo->getHeight(),
74 );
75
76 $this->update($file->getUid(), $additionalMetaInformation);
77 }
78 $record = $this->findByFileUid($file->getUid());
79 }
80
81 return $record;
82 }
83
84 /**
85 * Retrieves metadata for file
86 *
87 * @param int $uid
88 * @return array
89 * @throws \RuntimeException
90 */
91 public function findByFileUid($uid) {
92 $uid = (int)$uid;
93 if ($uid <= 0) {
94 throw new \RuntimeException('Metadata can only be retrieved for indexed files.', 1381590731);
95 }
96 $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', $this->tableName, 'file = ' . $uid . $this->getGeneralWhereClause());
97
98 if ($record === FALSE) {
99 $record = $this->createMetaDataRecord($uid);
100 }
101
102 $passedData = new \ArrayObject($record);
103 $this->emitRecordPostRetrievalSignal($passedData);
104 return $passedData->getArrayCopy();
105 }
106
107 /**
108 * General Where-Clause which is needed to fetch only language 0 and live record.
109 *
110 * @return string
111 */
112 protected function getGeneralWhereClause() {
113 return ' AND sys_language_uid IN (0,-1) AND pid=0';
114 }
115
116 /**
117 * Create empty
118 *
119 * @param int $fileUid
120 * @param array $additionalFields
121 * @return array
122 */
123 public function createMetaDataRecord($fileUid, array $additionalFields = array()) {
124 $emptyRecord = array(
125 'file' => (int)$fileUid,
126 'pid' => 0,
127 'crdate' => $GLOBALS['EXEC_TIME'],
128 'tstamp' => $GLOBALS['EXEC_TIME'],
129 'cruser_id' => isset($GLOBALS['BE_USER']->user['uid']) ? (int)$GLOBALS['BE_USER']->user['uid'] : 0
130 );
131 $emptyRecord = array_merge($emptyRecord, $additionalFields);
132 $this->getDatabaseConnection()->exec_INSERTquery($this->tableName, $emptyRecord);
133 $record = $emptyRecord;
134 $record['uid'] = $this->getDatabaseConnection()->sql_insert_id();
135 $record['newlyCreated'] = TRUE;
136
137 $this->emitRecordCreatedSignal($record);
138
139 return $record;
140 }
141
142 /**
143 * Updates the metadata record in the database
144 *
145 * @param int $fileUid the file uid to update
146 * @param array $data Data to update
147 * @return void
148 * @internal
149 */
150 public function update($fileUid, array $data) {
151 if (count($this->tableFields) === 0) {
152 $this->tableFields = $this->getDatabaseConnection()->admin_get_fields($this->tableName);
153 }
154 $updateRow = array_intersect_key($data, $this->tableFields);
155 if (array_key_exists('uid', $updateRow)) {
156 unset($updateRow['uid']);
157 }
158 $row = $this->findByFileUid($fileUid);
159 if (count($updateRow) > 0) {
160 $updateRow['tstamp'] = time();
161 $this->getDatabaseConnection()->exec_UPDATEquery($this->tableName, 'uid = ' . (int)$row['uid'], $updateRow);
162
163 $this->emitRecordUpdatedSignal(array_merge($row, $updateRow));
164 }
165 }
166
167 /**
168 * Remove all metadata records for a certain file from the database
169 *
170 * @param integer $fileUid
171 * @return void
172 */
173 public function removeByFileUid($fileUid) {
174 $this->getDatabaseConnection()->exec_DELETEquery($this->tableName, 'file=' . (int)$fileUid);
175 $this->emitRecordDeletedSignal($fileUid);
176 }
177
178 /**
179 * Get the SignalSlot dispatcher
180 *
181 * @return \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
182 */
183 protected function getSignalSlotDispatcher() {
184 return $this->getObjectManager()->get('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
185 }
186
187 /**
188 * Get the ObjectManager
189 *
190 * @return \TYPO3\CMS\Extbase\Object\ObjectManager
191 */
192 protected function getObjectManager() {
193 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
194 }
195
196 /**
197 * Signal that is called after a record has been loaded from database
198 * Allows other places to do extension of metadata at runtime or
199 * for example translation and workspace overlay
200 *
201 * @param \ArrayObject $data
202 * @signal
203 */
204 protected function emitRecordPostRetrievalSignal(\ArrayObject $data) {
205 $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository', 'recordPostRetrieval', array($data));
206 }
207
208 /**
209 * Signal that is called after an IndexRecord is updated
210 *
211 * @param array $data
212 * @signal
213 */
214 protected function emitRecordUpdatedSignal(array $data) {
215 $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository', 'recordUpdated', array($data));
216 }
217
218 /**
219 * Signal that is called after an IndexRecord is created
220 *
221 * @param array $data
222 * @signal
223 */
224 protected function emitRecordCreatedSignal(array $data) {
225 $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository', 'recordCreated', array($data));
226 }
227
228 /**
229 * Signal that is called after an IndexRecord is deleted
230 *
231 * @param integer $fileUid
232 * @signal
233 */
234 protected function emitRecordDeletedSignal($fileUid) {
235 $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository', 'recordDeleted', array($fileUid));
236 }
237
238 /**
239 * @return \TYPO3\CMS\Core\Resource\Index\MetaDataRepository
240 */
241 public static function getInstance() {
242 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository');
243 }
244 }