TYPO3 CMS  TYPO3_6-2
MetaDataRepository.php
Go to the documentation of this file.
1 <?php
2 
4 
23 
31 
35  protected $tableName = 'sys_file_metadata';
36 
42  protected $tableFields = array();
43 
49  protected function getDatabaseConnection() {
50  return $GLOBALS['TYPO3_DB'];
51  }
52 
59  public function findByFile(File $file) {
60  $record = $this->findByFileUid($file->getUid());
61 
62  // It could be possible that the meta information is freshly
63  // created and inserted into the database. If this is the case
64  // we have to take care about correct meta information for width and
65  // height in case of an image.
66  if (!empty($record['newlyCreated'])) {
67  if ($file->getType() === File::FILETYPE_IMAGE && $file->getStorage()->getDriverType() === 'Local') {
68  $fileNameAndPath = $file->getForLocalProcessing(FALSE);
69 
70  $imageInfo = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Type\\File\\ImageInfo', $fileNameAndPath);
71 
72  $additionalMetaInformation = array(
73  'width' => $imageInfo->getWidth(),
74  'height' => $imageInfo->getHeight(),
75  );
76 
77  $this->update($file->getUid(), $additionalMetaInformation);
78  }
79  $record = $this->findByFileUid($file->getUid());
80  }
81 
82  return $record;
83  }
84 
92  public function findByFileUid($uid) {
93  $uid = (int)$uid;
94  if ($uid <= 0) {
95  throw new InvalidUidException('Metadata can only be retrieved for indexed files. UID: "' . $uid . '"', 1381590731);
96  }
97  $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', $this->tableName, 'file = ' . $uid . $this->getGeneralWhereClause());
98 
99  if ($record === FALSE) {
100  $record = $this->createMetaDataRecord($uid);
101  }
102 
103  $passedData = new \ArrayObject($record);
104  $this->emitRecordPostRetrievalSignal($passedData);
105  return $passedData->getArrayCopy();
106  }
107 
113  protected function getGeneralWhereClause() {
114  return ' AND sys_language_uid IN (0,-1) AND pid=0';
115  }
116 
124  public function createMetaDataRecord($fileUid, array $additionalFields = array()) {
125  $emptyRecord = array(
126  'file' => (int)$fileUid,
127  'pid' => 0,
128  'crdate' => $GLOBALS['EXEC_TIME'],
129  'tstamp' => $GLOBALS['EXEC_TIME'],
130  'cruser_id' => isset($GLOBALS['BE_USER']->user['uid']) ? (int)$GLOBALS['BE_USER']->user['uid'] : 0
131  );
132  $emptyRecord = array_merge($emptyRecord, $additionalFields);
133  $this->getDatabaseConnection()->exec_INSERTquery($this->tableName, $emptyRecord);
134  $record = $emptyRecord;
135  $record['uid'] = $this->getDatabaseConnection()->sql_insert_id();
136  $record['newlyCreated'] = TRUE;
137 
138  $this->emitRecordCreatedSignal($record);
139 
140  return $record;
141  }
142 
151  public function update($fileUid, array $data) {
152  if (count($this->tableFields) === 0) {
153  $this->tableFields = $this->getDatabaseConnection()->admin_get_fields($this->tableName);
154  }
155  $updateRow = array_intersect_key($data, $this->tableFields);
156  if (array_key_exists('uid', $updateRow)) {
157  unset($updateRow['uid']);
158  }
159  $row = $this->findByFileUid($fileUid);
160  if (count($updateRow) > 0) {
161  $updateRow['tstamp'] = time();
162  $this->getDatabaseConnection()->exec_UPDATEquery($this->tableName, 'uid = ' . (int)$row['uid'], $updateRow);
163 
164  $this->emitRecordUpdatedSignal(array_merge($row, $updateRow));
165  }
166  }
167 
174  public function removeByFileUid($fileUid) {
175  $this->getDatabaseConnection()->exec_DELETEquery($this->tableName, 'file=' . (int)$fileUid);
176  $this->emitRecordDeletedSignal($fileUid);
177  }
178 
184  protected function getSignalSlotDispatcher() {
185  return $this->getObjectManager()->get('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
186  }
187 
193  protected function getObjectManager() {
194  return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
195  }
196 
205  protected function emitRecordPostRetrievalSignal(\ArrayObject $data) {
206  $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository', 'recordPostRetrieval', array($data));
207  }
208 
215  protected function emitRecordUpdatedSignal(array $data) {
216  $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository', 'recordUpdated', array($data));
217  }
218 
225  protected function emitRecordCreatedSignal(array $data) {
226  $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository', 'recordCreated', array($data));
227  }
228 
235  protected function emitRecordDeletedSignal($fileUid) {
236  $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository', 'recordDeleted', array($fileUid));
237  }
238 
242  public static function getInstance() {
243  return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\MetaDataRepository');
244  }
245 }
$uid
Definition: server.php:36
createMetaDataRecord($fileUid, array $additionalFields=array())
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]