TYPO3  7.6
ProcessedFileRepository.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
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 
19 
25 {
33  protected $objectType = ProcessedFile::class;
34 
41  protected $table = 'sys_file_processedfile';
42 
46  protected $resourceFactory;
47 
52 
56  public function __construct()
57  {
58  $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
59  $this->databaseConnection = $GLOBALS['TYPO3_DB'];
60  }
61 
70  public function createNewProcessedFileObject(FileInterface $originalFile, $taskType, array $configuration)
71  {
73  $this->objectType,
74  $originalFile,
75  $taskType,
76  $configuration
77  );
78  }
79 
84  protected function createDomainObject(array $databaseRow)
85  {
86  $originalFile = $this->resourceFactory->getFileObject((int)$databaseRow['original']);
87  $originalFile->setStorage($this->resourceFactory->getStorageObject($originalFile->getProperty('storage')));
88  $taskType = $databaseRow['task_type'];
89  $configuration = unserialize($databaseRow['configuration']);
90 
92  $this->objectType,
93  $originalFile,
94  $taskType,
95  $configuration,
96  $databaseRow
97  );
98  }
99 
106  public function findByStorageAndIdentifier(ResourceStorage $storage, $identifier)
107  {
108  $processedFileObject = null;
109  if ($storage->hasFile($identifier)) {
110  $databaseRow = $this->databaseConnection->exec_SELECTgetSingleRow(
111  '*',
112  $this->table,
113  'storage = ' . (int)$storage->getUid() .
114  ' AND identifier = ' . $this->databaseConnection->fullQuoteStr($identifier, $this->table)
115  );
116  if ($databaseRow) {
117  $processedFileObject = $this->createDomainObject($databaseRow);
118  }
119  }
120  return $processedFileObject;
121  }
128  public function add($processedFile)
129  {
130  if ($processedFile->isPersisted()) {
131  $this->update($processedFile);
132  } else {
133  $insertFields = $processedFile->toArray();
134  $insertFields['crdate'] = $insertFields['tstamp'] = time();
135  $insertFields = $this->cleanUnavailableColumns($insertFields);
136  $this->databaseConnection->exec_INSERTquery($this->table, $insertFields);
137  $uid = $this->databaseConnection->sql_insert_id();
138  $processedFile->updateProperties(array('uid' => $uid));
139  }
140  }
141 
148  public function update($processedFile)
149  {
150  if ($processedFile->isPersisted()) {
151  $uid = (int)$processedFile->getUid();
152  $updateFields = $this->cleanUnavailableColumns($processedFile->toArray());
153  $updateFields['tstamp'] = time();
154  $this->databaseConnection->exec_UPDATEquery($this->table, 'uid=' . (int)$uid, $updateFields);
155  }
156  }
157 
165  public function findOneByOriginalFileAndTaskTypeAndConfiguration(FileInterface $file, $taskType, array $configuration)
166  {
167  $databaseRow = $this->databaseConnection->exec_SELECTgetSingleRow(
168  '*',
169  $this->table,
170  'original=' . (int)$file->getUid() .
171  ' AND task_type=' . $this->databaseConnection->fullQuoteStr($taskType, $this->table) .
172  ' AND configurationsha1=' . $this->databaseConnection->fullQuoteStr(sha1(serialize($configuration)), $this->table)
173  );
174 
175  if (is_array($databaseRow)) {
176  $processedFile = $this->createDomainObject($databaseRow);
177  } else {
178  $processedFile = $this->createNewProcessedFileObject($file, $taskType, $configuration);
179  }
180  return $processedFile;
181  }
182 
188  public function findAllByOriginalFile(FileInterface $file)
189  {
190  if (!$file instanceof File) {
191  throw new \InvalidArgumentException('Parameter is no File object but got type "'
192  . (is_object($file) ? get_class($file) : gettype($file)) . '"', 1382006142);
193  }
194  $whereClause = 'original=' . (int)$file->getUid();
195  $rows = $this->databaseConnection->exec_SELECTgetRows('*', $this->table, $whereClause);
196 
197  $itemList = array();
198  if ($rows !== null) {
199  foreach ($rows as $row) {
200  $itemList[] = $this->createDomainObject($row);
201  }
202  }
203  return $itemList;
204  }
205 
212  public function removeAll($storageUid = null)
213  {
214  $res = $this->databaseConnection->exec_SELECTquery('*', $this->table, 'identifier <> \'\'');
215  $logger = $this->getLogger();
216  $errorCount = 0;
217  while ($row = $this->databaseConnection->sql_fetch_assoc($res)) {
218  if ($storageUid && (int)$storageUid !== (int)$row['storage']) {
219  continue;
220  }
221  try {
222  $file = $this->createDomainObject($row);
223  $file->getStorage()->setEvaluatePermissions(false);
224  $file->delete(true);
225  } catch (\Exception $e) {
226  $logger->error(
227  'Failed to delete file "' . $row['identifier'] . '" in storage uid ' . $row['storage'] . '.',
228  array(
229  'exception' => $e
230  )
231  );
232  ++$errorCount;
233  }
234  }
235 
236  $this->databaseConnection->exec_TRUNCATEquery($this->table);
237 
238  return $errorCount;
239  }
240 
241 
249  protected function cleanUnavailableColumns(array $data)
250  {
251  return array_intersect_key($data, $this->databaseConnection->admin_get_fields($this->table));
252  }
253 
257  protected function getLogger()
258  {
259  return GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
260  }
261 }