‪TYPO3CMS  10.4
MetaDataRepository.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Doctrine\DBAL\Platforms\SQLServerPlatform;
19 use Psr\EventDispatcher\EventDispatcherInterface;
32 
40 {
44  protected ‪$tableName = 'sys_file_metadata';
45 
51  protected ‪$tableFields = [];
52 
56  protected ‪$eventDispatcher;
57 
58  public function ‪__construct(EventDispatcherInterface ‪$eventDispatcher)
59  {
60  $this->eventDispatcher = ‪$eventDispatcher;
61  }
62 
69  public function ‪findByFile(‪File $file)
70  {
71  $record = $this->‪findByFileUid($file->‪getUid());
72 
73  // It could be possible that the meta information is freshly
74  // created and inserted into the database. If this is the case
75  // we have to take care about correct meta information for width and
76  // height in case of an image.
77  // This logic can be transferred into a custom PSR-14 event listener in the future by just using
78  // the AfterMetaDataCreated event.
79  if (!empty($record['crdate']) && (int)$record['crdate'] === ‪$GLOBALS['EXEC_TIME']) {
80  if ($file->‪getType() === ‪File::FILETYPE_IMAGE && $file->‪getStorage()->‪getDriverType() === 'Local') {
81  $fileNameAndPath = $file->‪getForLocalProcessing(false);
82 
83  $imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $fileNameAndPath);
84 
85  $additionalMetaInformation = [
86  'width' => $imageInfo->getWidth(),
87  'height' => $imageInfo->getHeight(),
88  ];
89 
90  $this->‪update($file->‪getUid(), $additionalMetaInformation);
91  }
92  $record = $this->‪findByFileUid($file->‪getUid());
93  }
94 
95  return $record;
96  }
97 
105  public function ‪findByFileUid($uid)
106  {
107  $uid = (int)$uid;
108  if ($uid <= 0) {
109  throw new ‪InvalidUidException('Metadata can only be retrieved for indexed files. UID: "' . $uid . '"', 1381590731);
110  }
111 
112  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
113 
114  $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(RootLevelRestriction::class));
115 
116  $record = $queryBuilder
117  ->select('*')
118  ->from($this->tableName)
119  ->where(
120  $queryBuilder->expr()->eq('file', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)),
121  $queryBuilder->expr()->in('sys_language_uid', $queryBuilder->createNamedParameter([0, -1], Connection::PARAM_INT_ARRAY))
122  )
123  ->execute()
124  ->fetch();
125 
126  if (empty($record)) {
127  return [];
128  }
129 
130  return $this->eventDispatcher->dispatch(new ‪EnrichFileMetaDataEvent($uid, (int)$record['uid'], $record))->getRecord();
131  }
132 
140  public function ‪createMetaDataRecord($fileUid, array $additionalFields = [])
141  {
142  $emptyRecord = [
143  'file' => (int)$fileUid,
144  'pid' => 0,
145  'crdate' => ‪$GLOBALS['EXEC_TIME'],
146  'tstamp' => ‪$GLOBALS['EXEC_TIME'],
147  'cruser_id' => isset(‪$GLOBALS['BE_USER']->user['uid']) ? (int)‪$GLOBALS['BE_USER']->user['uid'] : 0,
148  'l10n_diffsource' => ''
149  ];
150  $additionalFields = array_intersect_key($additionalFields, $this->‪getTableFields());
151  $emptyRecord = array_merge($emptyRecord, $additionalFields);
152 
153  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->tableName);
154  $connection->insert(
155  $this->tableName,
156  $emptyRecord,
157  ['l10n_diffsource' => ‪Connection::PARAM_LOB]
158  );
159 
160  $record = $emptyRecord;
161  $record['uid'] = $connection->lastInsertId($this->tableName);
162 
163  return $this->eventDispatcher->dispatch(new ‪AfterFileMetaDataCreatedEvent($fileUid, (int)$record['uid'], $record))->getRecord();
164  }
165 
173  public function ‪update($fileUid, array $data)
174  {
175  $updateRow = array_intersect_key($data, $this->‪getTableFields());
176  if (array_key_exists('uid', $updateRow)) {
177  unset($updateRow['uid']);
178  }
179  $row = $this->‪findByFileUid($fileUid);
180  if (!empty($updateRow)) {
181  $updateRow['tstamp'] = time();
182  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->tableName);
183  $types = [];
184  if ($connection->getDatabasePlatform() instanceof SQLServerPlatform) {
185  // mssql needs to set proper PARAM_LOB and others to update fields
186  $tableDetails = $connection->getSchemaManager()->listTableDetails($this->tableName);
187  foreach ($updateRow as $columnName => $columnValue) {
188  $types[$columnName] = $tableDetails->getColumn($columnName)->getType()->getBindingType();
189  }
190  }
191  $connection->update(
192  $this->tableName,
193  $updateRow,
194  [
195  'uid' => (int)$row['uid']
196  ],
197  $types
198  );
199 
200  $this->eventDispatcher->dispatch(new ‪AfterFileMetaDataUpdatedEvent($fileUid, (int)$row['uid'], array_merge($row, $updateRow)));
201  }
202  }
203 
209  public function ‪removeByFileUid($fileUid)
210  {
211  GeneralUtility::makeInstance(ConnectionPool::class)
212  ->getConnectionForTable($this->tableName)
213  ->delete(
214  $this->tableName,
215  [
216  'file' => (int)$fileUid
217  ]
218  );
219 
220  $this->eventDispatcher->dispatch(new ‪AfterFileMetaDataDeletedEvent((int)$fileUid));
221  }
222 
228  protected function ‪getTableFields(): array
229  {
230  if (empty($this->tableFields)) {
231  $this->tableFields = GeneralUtility::makeInstance(ConnectionPool::class)
232  ->getConnectionForTable($this->tableName)
233  ->getSchemaManager()
234  ->listTableColumns($this->tableName);
235  }
236 
237  return ‪$this->tableFields;
238  }
239 
243  public static function ‪getInstance()
244  {
245  return GeneralUtility::makeInstance(self::class);
246  }
247 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:40
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:286
‪TYPO3\CMS\Core\Resource\AbstractFile\getForLocalProcessing
‪string getForLocalProcessing($writable=true)
Definition: AbstractFile.php:577
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\findByFileUid
‪array findByFileUid($uid)
Definition: MetaDataRepository.php:102
‪TYPO3\CMS\Core\Resource\ResourceStorage\getDriverType
‪string getDriverType()
Definition: ResourceStorage.php:2865
‪TYPO3\CMS\Core\Resource\Event\AfterFileMetaDataDeletedEvent
Definition: AfterFileMetaDataDeletedEvent.php:25
‪TYPO3\CMS\Core\Resource\Event\AfterFileMetaDataCreatedEvent
Definition: AfterFileMetaDataCreatedEvent.php:25
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\getTableFields
‪array getTableFields()
Definition: MetaDataRepository.php:225
‪TYPO3\CMS\Core\Resource\Exception\InvalidUidException
Definition: InvalidUidException.php:24
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\findByFile
‪array findByFile(File $file)
Definition: MetaDataRepository.php:66
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\removeByFileUid
‪removeByFileUid($fileUid)
Definition: MetaDataRepository.php:206
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_IMAGE
‪const FILETYPE_IMAGE
Definition: AbstractFile.php:78
‪TYPO3\CMS\Core\Database\Query\Restriction\RootLevelRestriction
Definition: RootLevelRestriction.php:27
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\getInstance
‪static MetaDataRepository getInstance()
Definition: MetaDataRepository.php:240
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\__construct
‪__construct(EventDispatcherInterface $eventDispatcher)
Definition: MetaDataRepository.php:55
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: MetaDataRepository.php:53
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:202
‪TYPO3\CMS\Core\Resource\Event\EnrichFileMetaDataEvent
Definition: EnrichFileMetaDataEvent.php:26
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\$tableName
‪string $tableName
Definition: MetaDataRepository.php:43
‪TYPO3\CMS\Core\Resource\Index
Definition: ExtractorInterface.php:16
‪TYPO3\CMS\Core\Type\File\ImageInfo
Definition: ImageInfo.php:27
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$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:49
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\update
‪update($fileUid, array $data)
Definition: MetaDataRepository.php:170
‪TYPO3\CMS\Core\Resource\Event\AfterFileMetaDataUpdatedEvent
Definition: AfterFileMetaDataUpdatedEvent.php:24
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository\createMetaDataRecord
‪array createMetaDataRecord($fileUid, array $additionalFields=[])
Definition: MetaDataRepository.php:137
‪TYPO3\CMS\Core\Database\Connection\PARAM_LOB
‪const PARAM_LOB
Definition: Connection.php:57
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:390