TYPO3 CMS  TYPO3_6-2
ProcessedFileRepository.php
Go to the documentation of this file.
1 <?php
3 use \TYPO3\CMS\Core\Utility;
4 
26 
34  protected $objectType = 'TYPO3\\CMS\\Core\\Resource\\ProcessedFile';
35 
42  protected $table = 'sys_file_processedfile';
43 
47  protected $resourceFactory;
48 
53 
57  public function __construct() {
58  $this->resourceFactory = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
59  $this->databaseConnection = $GLOBALS['TYPO3_DB'];
60  }
61 
70  public function createNewProcessedFileObject(FileInterface $originalFile, $taskType, array $configuration) {
72  $this->objectType,
73  $originalFile,
74  $taskType,
75  $configuration
76  );
77  }
78 
83  protected function createDomainObject(array $databaseRow) {
84  $originalFile = $this->resourceFactory->getFileObject((int)$databaseRow['original']);
85  $originalFile->setStorage($this->resourceFactory->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  $processedFileObject = NULL;
106  if ($storage->hasFile($identifier)) {
107  $databaseRow = $this->databaseConnection->exec_SELECTgetSingleRow(
108  '*',
109  $this->table,
110  'storage = ' . (int)$storage->getUid() .
111  ' AND identifier = ' . $this->databaseConnection->fullQuoteStr($identifier, $this->table)
112  );
113  if ($databaseRow) {
114  $processedFileObject = $this->createDomainObject($databaseRow);
115  }
116  }
117  return $processedFileObject;
118  }
125  public function add($processedFile) {
126  if ($processedFile->isPersisted()) {
127  $this->update($processedFile);
128  } else {
129  $insertFields = $processedFile->toArray();
130  $insertFields['crdate'] = $insertFields['tstamp'] = time();
131  $insertFields = $this->cleanUnavailableColumns($insertFields);
132  $this->databaseConnection->exec_INSERTquery($this->table, $insertFields);
133  $uid = $this->databaseConnection->sql_insert_id();
134  $processedFile->updateProperties(array('uid' => $uid));
135  }
136  }
137 
144  public function update($processedFile) {
145  if ($processedFile->isPersisted()) {
146  $uid = (int)$processedFile->getUid();
147  $updateFields = $this->cleanUnavailableColumns($processedFile->toArray());
148  $updateFields['tstamp'] = time();
149  $this->databaseConnection->exec_UPDATEquery($this->table, 'uid=' . (int)$uid, $updateFields);
150  }
151  }
152 
160  public function findOneByOriginalFileAndTaskTypeAndConfiguration(FileInterface $file, $taskType, array $configuration) {
161  $databaseRow = $this->databaseConnection->exec_SELECTgetSingleRow(
162  '*',
163  $this->table,
164  'original=' . (int)$file->getUid() .
165  ' AND task_type=' . $this->databaseConnection->fullQuoteStr($taskType, $this->table) .
166  ' AND configurationsha1=' . $this->databaseConnection->fullQuoteStr(sha1(serialize($configuration)), $this->table)
167  );
168 
169  if (is_array($databaseRow)) {
170  $processedFile = $this->createDomainObject($databaseRow);
171  } else {
172  $processedFile = $this->createNewProcessedFileObject($file, $taskType, $configuration);
173  }
174  return $processedFile;
175  }
176 
182  public function findAllByOriginalFile(FileInterface $file) {
183  if (!$file instanceof File) {
184  throw new \InvalidArgumentException('Parameter is no File object but got type "'
185  . (is_object($file) ? get_class($file) : gettype($file)) . '"', 1382006142);
186  }
187  $whereClause = 'original=' . (int)$file->getUid();
188  $rows = $this->databaseConnection->exec_SELECTgetRows('*', $this->table, $whereClause);
189 
190  $itemList = array();
191  if ($rows !== NULL) {
192  foreach ($rows as $row) {
193  $itemList[] = $this->createDomainObject($row);
194  }
195  }
196  return $itemList;
197  }
198 
205  public function removeAll($storageUid = NULL) {
206  $res = $this->databaseConnection->exec_SELECTquery('*', $this->table, 'identifier <> \'\'');
207  $logger = $this->getLogger();
208  $errorCount = 0;
209  while ($row = $this->databaseConnection->sql_fetch_assoc($res)) {
210  if ($storageUid && (int)$storageUid !== (int)$row['storage']) {
211  continue;
212  }
213  try {
214  $file = $this->createDomainObject($row);
215  $file->getStorage()->setEvaluatePermissions(FALSE);
216  $file->delete(TRUE);
217  } catch (\Exception $e) {
218  $logger->error(
219  'Failed to delete file "' . $row['identifier'] . '" in storage uid ' . $row['storage'] . '.',
220  array(
221  'exception' => $e
222  )
223  );
224  ++$errorCount;
225  }
226  }
227 
228  $this->databaseConnection->exec_TRUNCATEquery($this->table);
229 
230  return $errorCount;
231  }
232 
233 
241  protected function cleanUnavailableColumns(array $data) {
242  return array_intersect_key($data, $this->databaseConnection->admin_get_fields($this->table));
243  }
244 
248  protected function getLogger() {
249  return Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger(__CLASS__);
250  }
251 
252 }
$uid
Definition: server.php:36
createNewProcessedFileObject(FileInterface $originalFile, $taskType, array $configuration)
findOneByOriginalFileAndTaskTypeAndConfiguration(FileInterface $file, $taskType, array $configuration)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
findByStorageAndIdentifier(ResourceStorage $storage, $identifier)