TYPO3 CMS  TYPO3_8-7
ProcessedFileRepository.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
26 {
34  protected $objectType = ProcessedFile::class;
35 
42  protected $table = 'sys_file_processedfile';
43 
50  protected $tableColumns = [];
51 
55  public function __construct()
56  {
57  parent::__construct();
58  }
59 
68  public function createNewProcessedFileObject(FileInterface $originalFile, $taskType, array $configuration)
69  {
71  $this->objectType,
72  $originalFile,
73  $taskType,
74  $configuration
75  );
76  }
77 
82  protected function createDomainObject(array $databaseRow)
83  {
84  $originalFile = $this->factory->getFileObject((int)$databaseRow['original']);
85  $originalFile->setStorage($this->factory->getStorageObject($originalFile->getProperty('storage')));
86  $taskType = $databaseRow['task_type'];
87  $configuration = unserialize($databaseRow['configuration']);
88 
90  $this->objectType,
91  $originalFile,
92  $taskType,
93  $configuration,
94  $databaseRow
95  );
96  }
97 
104  public function findByStorageAndIdentifier(ResourceStorage $storage, $identifier)
105  {
106  $processedFileObject = null;
107  if ($storage->hasFile($identifier)) {
108  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
109  $databaseRow = $queryBuilder
110  ->select('*')
111  ->from($this->table)
112  ->where(
113  $queryBuilder->expr()->eq(
114  'storage',
115  $queryBuilder->createNamedParameter($storage->getUid(), \PDO::PARAM_INT)
116  ),
117  $queryBuilder->expr()->eq(
118  'identifier',
119  $queryBuilder->createNamedParameter($identifier, \PDO::PARAM_STR)
120  )
121  )
122  ->execute()
123  ->fetch();
124 
125  if ($databaseRow) {
126  $processedFileObject = $this->createDomainObject($databaseRow);
127  }
128  }
129  return $processedFileObject;
130  }
136  public function add($processedFile)
137  {
138  if ($processedFile->isPersisted()) {
139  $this->update($processedFile);
140  } else {
141  $insertFields = $processedFile->toArray();
142  $insertFields['crdate'] = $insertFields['tstamp'] = time();
143  $insertFields = $this->cleanUnavailableColumns($insertFields);
144 
145  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->table);
146 
147  $connection->insert(
148  $this->table,
149  $insertFields
150  );
151 
152  $uid = $connection->lastInsertId($this->table);
153  $processedFile->updateProperties(['uid' => $uid]);
154  }
155  }
156 
162  public function update($processedFile)
163  {
164  if ($processedFile->isPersisted()) {
165  $uid = (int)$processedFile->getUid();
166  $updateFields = $this->cleanUnavailableColumns($processedFile->toArray());
167  $updateFields['tstamp'] = time();
168 
169  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->table);
170  $connection->update(
171  $this->table,
172  $updateFields,
173  [
174  'uid' => (int)$uid
175  ]
176  );
177  }
178  }
179 
187  public function findOneByOriginalFileAndTaskTypeAndConfiguration(FileInterface $file, $taskType, array $configuration)
188  {
189  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
190 
191  $databaseRow = $queryBuilder
192  ->select('*')
193  ->from($this->table)
194  ->where(
195  $queryBuilder->expr()->eq(
196  'original',
197  $queryBuilder->createNamedParameter($file->getUid(), \PDO::PARAM_INT)
198  ),
199  $queryBuilder->expr()->eq('task_type', $queryBuilder->createNamedParameter($taskType, \PDO::PARAM_STR)),
200  $queryBuilder->expr()->eq(
201  'configurationsha1',
202  $queryBuilder->createNamedParameter(sha1(serialize($configuration)), \PDO::PARAM_STR)
203  )
204  )
205  ->execute()
206  ->fetch();
207 
208  if (is_array($databaseRow)) {
209  $processedFile = $this->createDomainObject($databaseRow);
210  } else {
211  $processedFile = $this->createNewProcessedFileObject($file, $taskType, $configuration);
212  }
213  return $processedFile;
214  }
215 
221  public function findAllByOriginalFile(FileInterface $file)
222  {
223  if (!$file instanceof File) {
224  throw new \InvalidArgumentException('Parameter is no File object but got type "'
225  . (is_object($file) ? get_class($file) : gettype($file)) . '"', 1382006142);
226  }
227 
228  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
229  $result = $queryBuilder
230  ->select('*')
231  ->from($this->table)
232  ->where(
233  $queryBuilder->expr()->eq(
234  'original',
235  $queryBuilder->createNamedParameter($file->getUid(), \PDO::PARAM_INT)
236  )
237  )
238  ->execute();
239 
240  $itemList = [];
241  while ($row = $result->fetch()) {
242  $itemList[] = $this->createDomainObject($row);
243  }
244  return $itemList;
245  }
246 
253  public function removeAll($storageUid = null)
254  {
255  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
256  $result = $queryBuilder
257  ->select('*')
258  ->from($this->table)
259  ->where(
260  $queryBuilder->expr()->neq('identifier', $queryBuilder->createNamedParameter('', \PDO::PARAM_STR))
261  )
262  ->execute();
263 
264  $logger = $this->getLogger();
265  $errorCount = 0;
266 
267  while ($row = $result->fetch()) {
268  if ($storageUid && (int)$storageUid !== (int)$row['storage']) {
269  continue;
270  }
271  try {
272  $file = $this->createDomainObject($row);
273  $file->getStorage()->setEvaluatePermissions(false);
274  $file->delete(true);
275  } catch (\Exception $e) {
276  $logger->error(
277  'Failed to delete file "' . $row['identifier'] . '" in storage uid ' . $row['storage'] . '.',
278  [
279  'exception' => $e
280  ]
281  );
282  ++$errorCount;
283  }
284  }
285 
286  GeneralUtility::makeInstance(ConnectionPool::class)
287  ->getConnectionForTable($this->table)
288  ->truncate($this->table);
289 
290  return $errorCount;
291  }
292 
300  protected function cleanUnavailableColumns(array $data)
301  {
302  // As determining the table columns is a costly operation this is done only once during runtime and cached then
303  if (empty($this->tableColumns[$this->table])) {
304  $this->tableColumns[$this->table] = GeneralUtility::makeInstance(ConnectionPool::class)
305  ->getConnectionForTable($this->table)
306  ->getSchemaManager()
307  ->listTableColumns($this->table);
308  }
309 
310  return array_intersect_key($data, $this->tableColumns[$this->table]);
311  }
312 
316  protected function getLogger()
317  {
318  return GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
319  }
320 }
createNewProcessedFileObject(FileInterface $originalFile, $taskType, array $configuration)
static makeInstance($className,... $constructorArguments)
findOneByOriginalFileAndTaskTypeAndConfiguration(FileInterface $file, $taskType, array $configuration)
findByStorageAndIdentifier(ResourceStorage $storage, $identifier)