‪TYPO3CMS  9.5
MetaDataRepository.php
Go to the documentation of this file.
1 <?php
2 
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 Doctrine\DBAL\Platforms\SQLServerPlatform;
29 
37 {
41  protected ‪$tableName = 'sys_file_metadata';
42 
48  protected ‪$tableFields = [];
49 
56  public function ‪findByFile(‪File $file)
57  {
58  $record = $this->‪findByFileUid($file->‪getUid());
59 
60  // It could be possible that the meta information is freshly
61  // created and inserted into the database. If this is the case
62  // we have to take care about correct meta information for width and
63  // height in case of an image.
64  if (!empty($record['newlyCreated'])) {
65  if ($file->‪getType() === ‪File::FILETYPE_IMAGE && $file->‪getStorage()->‪getDriverType() === 'Local') {
66  $fileNameAndPath = $file->‪getForLocalProcessing(false);
67 
68  $imageInfo = GeneralUtility::makeInstance(‪FileType\ImageInfo::class, $fileNameAndPath);
69 
70  $additionalMetaInformation = [
71  'width' => $imageInfo->getWidth(),
72  'height' => $imageInfo->getHeight(),
73  ];
74 
75  $this->‪update($file->‪getUid(), $additionalMetaInformation);
76  }
77  $record = $this->‪findByFileUid($file->‪getUid());
78  }
79 
80  return $record;
81  }
82 
90  public function ‪findByFileUid($uid)
91  {
92  $uid = (int)$uid;
93  if ($uid <= 0) {
94  throw new ‪InvalidUidException('Metadata can only be retrieved for indexed files. UID: "' . $uid . '"', 1381590731);
95  }
96 
97  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
98 
99  $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(RootLevelRestriction::class));
100 
101  $record = $queryBuilder
102  ->select('*')
103  ->from($this->tableName)
104  ->where(
105  $queryBuilder->expr()->eq('file', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)),
106  $queryBuilder->expr()->in('sys_language_uid', $queryBuilder->createNamedParameter([0, -1], Connection::PARAM_INT_ARRAY))
107  )
108  ->execute()
109  ->fetch();
110 
111  if (empty($record)) {
112  $record = $this->‪createMetaDataRecord($uid);
113  }
114 
115  $passedData = new \ArrayObject($record);
116 
117  $this->‪emitRecordPostRetrievalSignal($passedData);
118  return $passedData->getArrayCopy();
119  }
120 
128  public function ‪createMetaDataRecord($fileUid, array $additionalFields = [])
129  {
130  $emptyRecord = [
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 
140  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->tableName);
141  $connection->insert(
142  $this->tableName,
143  $emptyRecord,
144  ['l10n_diffsource' => ‪Connection::PARAM_LOB]
145  );
146 
147  $record = $emptyRecord;
148  $record['uid'] = $connection->lastInsertId($this->tableName);
149  $record['newlyCreated'] = true;
150 
151  $this->‪emitRecordCreatedSignal($record);
152 
153  return $record;
154  }
155 
163  public function ‪update($fileUid, array $data)
164  {
165  if (empty($this->tableFields)) {
166  $this->tableFields = GeneralUtility::makeInstance(ConnectionPool::class)
167  ->getConnectionForTable($this->tableName)
168  ->getSchemaManager()
169  ->listTableColumns($this->tableName);
170  }
171  $updateRow = array_intersect_key($data, $this->tableFields);
172  if (array_key_exists('uid', $updateRow)) {
173  unset($updateRow['uid']);
174  }
175  $row = $this->‪findByFileUid($fileUid);
176  if (!empty($updateRow)) {
177  $updateRow['tstamp'] = time();
178  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->tableName);
179  $types = [];
180  if ($connection->getDatabasePlatform() instanceof SQLServerPlatform) {
181  // mssql needs to set proper PARAM_LOB and others to update fields
182  $tableDetails = $connection->getSchemaManager()->listTableDetails($this->tableName);
183  foreach ($updateRow as $columnName => $columnValue) {
184  $types[$columnName] = $tableDetails->getColumn($columnName)->getType()->getBindingType();
185  }
186  }
187  $connection->update(
188  $this->tableName,
189  $updateRow,
190  [
191  'uid' => (int)$row['uid']
192  ],
193  $types
194  );
195 
196  $this->‪emitRecordUpdatedSignal(array_merge($row, $updateRow));
197  }
198  }
199 
205  public function ‪removeByFileUid($fileUid)
206  {
207  GeneralUtility::makeInstance(ConnectionPool::class)
208  ->getConnectionForTable($this->tableName)
209  ->delete(
210  $this->tableName,
211  [
212  'file' => (int)$fileUid
213  ]
214  );
215  $this->‪emitRecordDeletedSignal($fileUid);
216  }
217 
223  protected function ‪getSignalSlotDispatcher()
224  {
225  return $this->‪getObjectManager()->‪get(Dispatcher::class);
226  }
227 
233  protected function ‪getObjectManager()
234  {
235  return GeneralUtility::makeInstance(ObjectManager::class);
236  }
237 
245  protected function ‪emitRecordPostRetrievalSignal(\ArrayObject $data)
246  {
247  $this->‪getSignalSlotDispatcher()->‪dispatch(self::class, 'recordPostRetrieval', [$data]);
248  }
249 
255  protected function ‪emitRecordUpdatedSignal(array $data)
256  {
257  $this->‪getSignalSlotDispatcher()->‪dispatch(self::class, 'recordUpdated', [$data]);
258  }
259 
265  protected function ‪emitRecordCreatedSignal(array $data)
266  {
267  $this->‪getSignalSlotDispatcher()->‪dispatch(self::class, 'recordCreated', [$data]);
268  }
269 
275  protected function ‪emitRecordDeletedSignal($fileUid)
276  {
277  $this->‪getSignalSlotDispatcher()->‪dispatch(self::class, 'recordDeleted', [$fileUid]);
278  }
279 
283  public static function ‪getInstance()
284  {
285  return GeneralUtility::makeInstance(self::class);
286  }
287 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:37
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:284
‪TYPO3\CMS\Core\Type\File
Definition: FileInfo.php:2
‪TYPO3\CMS\Core\Resource\AbstractFile\getForLocalProcessing
‪string getForLocalProcessing($writable=true)
Definition: AbstractFile.php:548
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\findByFileUid
‪array findByFileUid($uid)
Definition: MetaDataRepository.php:88
‪TYPO3\CMS\Core\Resource\ResourceStorage\getDriverType
‪string getDriverType()
Definition: ResourceStorage.php:3133
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\getSignalSlotDispatcher
‪Dispatcher getSignalSlotDispatcher()
Definition: MetaDataRepository.php:221
‪TYPO3\CMS\Core\Resource\Exception\InvalidUidException
Definition: InvalidUidException.php:21
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\findByFile
‪array findByFile(File $file)
Definition: MetaDataRepository.php:54
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\removeByFileUid
‪removeByFileUid($fileUid)
Definition: MetaDataRepository.php:203
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_IMAGE
‪const FILETYPE_IMAGE
Definition: AbstractFile.php:76
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\emitRecordPostRetrievalSignal
‪emitRecordPostRetrievalSignal(\ArrayObject $data)
Definition: MetaDataRepository.php:243
‪TYPO3\CMS\Core\Database\Query\Restriction\RootLevelRestriction
Definition: RootLevelRestriction.php:25
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\emitRecordCreatedSignal
‪emitRecordCreatedSignal(array $data)
Definition: MetaDataRepository.php:263
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\getInstance
‪static MetaDataRepository getInstance()
Definition: MetaDataRepository.php:281
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\emitRecordUpdatedSignal
‪emitRecordUpdatedSignal(array $data)
Definition: MetaDataRepository.php:253
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:200
‪TYPO3\CMS\Extbase\SignalSlot\Dispatcher\dispatch
‪mixed dispatch($signalClassName, $signalName, array $signalArguments=[])
Definition: Dispatcher.php:115
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\$tableName
‪string $tableName
Definition: MetaDataRepository.php:40
‪TYPO3\CMS\Core\Resource\Index
Definition: ExtractorInterface.php:2
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\$tableFields
‪array $tableFields
Definition: MetaDataRepository.php:46
‪TYPO3\CMS\Extbase\Object\ObjectManager\get
‪object &T get($objectName,... $constructorArguments)
Definition: ObjectManager.php:94
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\update
‪update($fileUid, array $data)
Definition: MetaDataRepository.php:161
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\emitRecordDeletedSignal
‪emitRecordDeletedSignal($fileUid)
Definition: MetaDataRepository.php:273
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\getObjectManager
‪ObjectManager getObjectManager()
Definition: MetaDataRepository.php:231
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:25
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\createMetaDataRecord
‪array createMetaDataRecord($fileUid, array $additionalFields=[])
Definition: MetaDataRepository.php:126
‪TYPO3\CMS\Core\Database\Connection\PARAM_LOB
‪const PARAM_LOB
Definition: Connection.php:52
‪TYPO3\CMS\Extbase\SignalSlot\Dispatcher
Definition: Dispatcher.php:28
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:361