TYPO3  7.6
MetaDataRepository.php
Go to the documentation of this file.
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 
21 use TYPO3\CMS\Core\Resource\Exception\InvalidUidException;
22 use TYPO3\CMS\Core\Type\File as FileType;
23 
31 {
35  protected $tableName = 'sys_file_metadata';
36 
42  protected $tableFields = array();
43 
49  protected function getDatabaseConnection()
50  {
51  return $GLOBALS['TYPO3_DB'];
52  }
53 
60  public function findByFile(File $file)
61  {
62  $record = $this->findByFileUid($file->getUid());
63 
64  // It could be possible that the meta information is freshly
65  // created and inserted into the database. If this is the case
66  // we have to take care about correct meta information for width and
67  // height in case of an image.
68  if (!empty($record['newlyCreated'])) {
69  if ($file->getType() === File::FILETYPE_IMAGE && $file->getStorage()->getDriverType() === 'Local') {
70  $fileNameAndPath = $file->getForLocalProcessing(false);
71 
72  $imageInfo = GeneralUtility::makeInstance(FileType\ImageInfo::class, $fileNameAndPath);
73 
74  $additionalMetaInformation = array(
75  'width' => $imageInfo->getWidth(),
76  'height' => $imageInfo->getHeight(),
77  );
78 
79  $this->update($file->getUid(), $additionalMetaInformation);
80  }
81  $record = $this->findByFileUid($file->getUid());
82  }
83 
84  return $record;
85  }
86 
94  public function findByFileUid($uid)
95  {
96  $uid = (int)$uid;
97  if ($uid <= 0) {
98  throw new InvalidUidException('Metadata can only be retrieved for indexed files. UID: "' . $uid . '"', 1381590731);
99  }
100  $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', $this->tableName, 'file = ' . $uid . $this->getGeneralWhereClause());
101 
102  if ($record === false) {
103  $record = $this->createMetaDataRecord($uid);
104  }
105 
106  $passedData = new \ArrayObject($record);
107  $this->emitRecordPostRetrievalSignal($passedData);
108  return $passedData->getArrayCopy();
109  }
110 
116  protected function getGeneralWhereClause()
117  {
118  return ' AND sys_language_uid IN (0,-1) AND pid=0';
119  }
120 
128  public function createMetaDataRecord($fileUid, array $additionalFields = array())
129  {
130  $emptyRecord = array(
131  'file' => (int)$fileUid,
132  'pid' => 0,
133  'crdate' => $GLOBALS['EXEC_TIME'],
134  'tstamp' => $GLOBALS['EXEC_TIME'],
135  'cruser_id' => isset($GLOBALS['BE_USER']->user['uid']) ? (int)$GLOBALS['BE_USER']->user['uid'] : 0,
136  'l10n_diffsource' => ''
137  );
138  $emptyRecord = array_merge($emptyRecord, $additionalFields);
139  $this->getDatabaseConnection()->exec_INSERTquery($this->tableName, $emptyRecord);
140  $record = $emptyRecord;
141  $record['uid'] = $this->getDatabaseConnection()->sql_insert_id();
142  $record['newlyCreated'] = true;
143 
144  $this->emitRecordCreatedSignal($record);
145 
146  return $record;
147  }
148 
157  public function update($fileUid, array $data)
158  {
159  if (empty($this->tableFields)) {
160  $this->tableFields = $this->getDatabaseConnection()->admin_get_fields($this->tableName);
161  }
162  $updateRow = array_intersect_key($data, $this->tableFields);
163  if (array_key_exists('uid', $updateRow)) {
164  unset($updateRow['uid']);
165  }
166  $row = $this->findByFileUid($fileUid);
167  if (!empty($updateRow)) {
168  $updateRow['tstamp'] = time();
169  $this->getDatabaseConnection()->exec_UPDATEquery($this->tableName, 'uid = ' . (int)$row['uid'], $updateRow);
170 
171  $this->emitRecordUpdatedSignal(array_merge($row, $updateRow));
172  }
173  }
174 
181  public function removeByFileUid($fileUid)
182  {
183  $this->getDatabaseConnection()->exec_DELETEquery($this->tableName, 'file=' . (int)$fileUid);
184  $this->emitRecordDeletedSignal($fileUid);
185  }
186 
192  protected function getSignalSlotDispatcher()
193  {
194  return $this->getObjectManager()->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
195  }
196 
202  protected function getObjectManager()
203  {
204  return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
205  }
206 
215  protected function emitRecordPostRetrievalSignal(\ArrayObject $data)
216  {
217  $this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class, 'recordPostRetrieval', array($data));
218  }
219 
226  protected function emitRecordUpdatedSignal(array $data)
227  {
228  $this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class, 'recordUpdated', array($data));
229  }
230 
237  protected function emitRecordCreatedSignal(array $data)
238  {
239  $this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class, 'recordCreated', array($data));
240  }
241 
248  protected function emitRecordDeletedSignal($fileUid)
249  {
250  $this->getSignalSlotDispatcher()->dispatch(\TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class, 'recordDeleted', array($fileUid));
251  }
252 
256  public static function getInstance()
257  {
258  return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class);
259  }
260 }