TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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 
24 use TYPO3\CMS\Core\Type\File as FileType;
28 
36 {
40  protected $tableName = 'sys_file_metadata';
41 
47  protected $tableFields = [];
48 
55  public function findByFile(File $file)
56  {
57  $record = $this->findByFileUid($file->getUid());
58 
59  // It could be possible that the meta information is freshly
60  // created and inserted into the database. If this is the case
61  // we have to take care about correct meta information for width and
62  // height in case of an image.
63  if (!empty($record['newlyCreated'])) {
64  if ($file->getType() === File::FILETYPE_IMAGE && $file->getStorage()->getDriverType() === 'Local') {
65  $fileNameAndPath = $file->getForLocalProcessing(false);
66 
67  $imageInfo = GeneralUtility::makeInstance(FileType\ImageInfo::class, $fileNameAndPath);
68 
69  $additionalMetaInformation = [
70  'width' => $imageInfo->getWidth(),
71  'height' => $imageInfo->getHeight(),
72  ];
73 
74  $this->update($file->getUid(), $additionalMetaInformation);
75  }
76  $record = $this->findByFileUid($file->getUid());
77  }
78 
79  return $record;
80  }
81 
89  public function findByFileUid($uid)
90  {
91  $uid = (int)$uid;
92  if ($uid <= 0) {
93  throw new InvalidUidException('Metadata can only be retrieved for indexed files. UID: "' . $uid . '"', 1381590731);
94  }
95 
96  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
97 
98  $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(RootLevelRestriction::class));
99 
100  $record = $queryBuilder
101  ->select('*')
102  ->from($this->tableName)
103  ->where(
104  $queryBuilder->expr()->eq('file', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)),
105  $queryBuilder->expr()->in('sys_language_uid', $queryBuilder->createNamedParameter([0, -1], Connection::PARAM_INT_ARRAY))
106  )
107  ->execute()
108  ->fetch();
109 
110  if (empty($record)) {
111  $record = $this->createMetaDataRecord($uid);
112  }
113 
114  $passedData = new \ArrayObject($record);
115 
116  $this->emitRecordPostRetrievalSignal($passedData);
117  return $passedData->getArrayCopy();
118  }
119 
127  public function createMetaDataRecord($fileUid, array $additionalFields = [])
128  {
129  $emptyRecord = [
130  'file' => (int)$fileUid,
131  'pid' => 0,
132  'crdate' => $GLOBALS['EXEC_TIME'],
133  'tstamp' => $GLOBALS['EXEC_TIME'],
134  'cruser_id' => isset($GLOBALS['BE_USER']->user['uid']) ? (int)$GLOBALS['BE_USER']->user['uid'] : 0,
135  'l10n_diffsource' => ''
136  ];
137  $emptyRecord = array_merge($emptyRecord, $additionalFields);
138 
139  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->tableName);
140  $connection->insert(
141  $this->tableName,
142  $emptyRecord
143  );
144 
145  $record = $emptyRecord;
146  $record['uid'] = $connection->lastInsertId($this->tableName);
147  $record['newlyCreated'] = true;
148 
149  $this->emitRecordCreatedSignal($record);
150 
151  return $record;
152  }
153 
162  public function update($fileUid, array $data)
163  {
164  if (empty($this->tableFields)) {
165  $this->tableFields = GeneralUtility::makeInstance(ConnectionPool::class)
166  ->getConnectionForTable($this->tableName)
167  ->getSchemaManager()
168  ->listTableColumns($this->tableName);
169  }
170  $updateRow = array_intersect_key($data, $this->tableFields);
171  if (array_key_exists('uid', $updateRow)) {
172  unset($updateRow['uid']);
173  }
174  $row = $this->findByFileUid($fileUid);
175  if (!empty($updateRow)) {
176  $updateRow['tstamp'] = time();
177  GeneralUtility::makeInstance(ConnectionPool::class)
178  ->getConnectionForTable($this->tableName)
179  ->update(
180  $this->tableName,
181  $updateRow,
182  [
183  'uid' => (int)$row['uid']
184  ]
185  );
186 
187  $this->emitRecordUpdatedSignal(array_merge($row, $updateRow));
188  }
189  }
190 
197  public function removeByFileUid($fileUid)
198  {
199  GeneralUtility::makeInstance(ConnectionPool::class)
200  ->getConnectionForTable($this->tableName)
201  ->delete(
202  $this->tableName,
203  [
204  'file' => (int)$fileUid
205  ]
206  );
207  $this->emitRecordDeletedSignal($fileUid);
208  }
209 
215  protected function getSignalSlotDispatcher()
216  {
217  return $this->getObjectManager()->get(Dispatcher::class);
218  }
219 
225  protected function getObjectManager()
226  {
227  return GeneralUtility::makeInstance(ObjectManager::class);
228  }
229 
238  protected function emitRecordPostRetrievalSignal(\ArrayObject $data)
239  {
240  $this->getSignalSlotDispatcher()->dispatch(self::class, 'recordPostRetrieval', [$data]);
241  }
242 
249  protected function emitRecordUpdatedSignal(array $data)
250  {
251  $this->getSignalSlotDispatcher()->dispatch(self::class, 'recordUpdated', [$data]);
252  }
253 
260  protected function emitRecordCreatedSignal(array $data)
261  {
262  $this->getSignalSlotDispatcher()->dispatch(self::class, 'recordCreated', [$data]);
263  }
264 
271  protected function emitRecordDeletedSignal($fileUid)
272  {
273  $this->getSignalSlotDispatcher()->dispatch(self::class, 'recordDeleted', [$fileUid]);
274  }
275 
279  public static function getInstance()
280  {
281  return GeneralUtility::makeInstance(self::class);
282  }
283 }
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
createMetaDataRecord($fileUid, array $additionalFields=[])