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