TYPO3 CMS  TYPO3_6-2
FileIndexRepository.php
Go to the documentation of this file.
1 <?php
2 
4 
22 
32 
36  protected $table = 'sys_file';
37 
43  protected $fields = array(
44  'uid', 'pid', 'missing', 'type', 'storage', 'identifier', 'identifier_hash', 'extension',
45  'mime_type', 'name', 'sha1', 'size', 'creation_date', 'modification_date', 'folder_hash'
46  );
47 
53  protected function getDatabaseConnection() {
54  return $GLOBALS['TYPO3_DB'];
55  }
56 
62  protected function getResourceFactory() {
63  return \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
64  }
65 
66 
72  public static function getInstance() {
73  return GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Index\\FileIndexRepository');
74  }
75 
82  public function findOneByCombinedIdentifier($combinedIdentifier) {
83  list($storageUid, $identifier) = GeneralUtility::trimExplode(':', $combinedIdentifier, FALSE, 2);
84  return $this->findOneByStorageUidAndIdentifier($storageUid, $identifier);
85  }
86 
93  public function findOneByUid($fileUid) {
94  $row = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
95  implode(',', $this->fields),
96  $this->table,
97  'uid=' . (int)$fileUid
98  );
99  return is_array($row) ? $row : FALSE;
100  }
101 
111  public function findOneByStorageUidAndIdentifier($storageUid, $identifier) {
112  $identifierHash = $this->getResourceFactory()->getStorageObject($storageUid)->hashFileIdentifier($identifier);
113  return $this->findOneByStorageUidAndIdentifierHash($storageUid, $identifierHash);
114  }
115 
125  public function findOneByStorageUidAndIdentifierHash($storageUid, $identifierHash) {
126  $row = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
127  implode(',', $this->fields),
128  $this->table,
129  sprintf('storage=%u AND identifier_hash=%s', (int)$storageUid, $this->getDatabaseConnection()->fullQuoteStr($identifierHash, $this->table))
130  );
131  return is_array($row) ? $row : FALSE;
132  }
133 
142  public function findOneByFileObject(\TYPO3\CMS\Core\Resource\FileInterface $fileObject) {
143  $storageUid = $fileObject->getStorage()->getUid();
144  $identifierHash = $fileObject->getHashedIdentifier();
145  return $this->findOneByStorageUidAndIdentifierHash($storageUid, $identifierHash);
146  }
147 
155  public function findByContentHash($hash) {
156  if (!preg_match('/^[0-9a-f]{40}$/i', $hash)) {
157  return array();
158  }
159  $resultRows = $this->getDatabaseConnection()->exec_SELECTgetRows(
160  implode(',', $this->fields),
161  $this->table,
162  'sha1=' . $this->getDatabaseConnection()->fullQuoteStr($hash, $this->table)
163  );
164  return $resultRows;
165  }
166 
173  public function findByFolder(\TYPO3\CMS\Core\Resource\Folder $folder) {
174  $resultRows = $this->getDatabaseConnection()->exec_SELECTgetRows(
175  implode(',', $this->fields),
176  $this->table,
177  'folder_hash = ' . $this->getDatabaseConnection()->fullQuoteStr($folder->getHashedIdentifier(), $this->table) .
178  ' AND storage = ' . (int)$folder->getStorage()->getUid(),
179  '',
180  '',
181  '',
182  'identifier'
183  );
184  return $resultRows;
185  }
192  public function add(File $file) {
193  if ($this->hasIndexRecord($file)) {
194  $this->update($file);
195  if ($file->_getPropertyRaw('uid') === NULL) {
196  $file->updateProperties($this->findOneByFileObject($file));
197  }
198  } else {
199  $file->updateProperties(array('uid' => $this->insertRecord($file->getProperties())));
200  }
201  }
202 
209  public function addRaw(array $data) {
210  $data['uid'] = $this->insertRecord($data);
211  return $data;
212  }
213 
221  protected function insertRecord(array $data) {
222  $data = array_intersect_key($data, array_flip($this->fields));
223  $data['tstamp'] = time();
224  $this->getDatabaseConnection()->exec_INSERTquery($this->table, $data);
225  $data['uid'] = $this->getDatabaseConnection()->sql_insert_id();
226  $this->updateRefIndex($data['uid']);
227  $this->emitRecordCreatedSignal($data);
228  return $data['uid'];
229  }
236  public function hasIndexRecord(File $file) {
237  return $this->getDatabaseConnection()->exec_SELECTcountRows('uid', $this->table, $this->getWhereClauseForFile($file)) >= 1;
238  }
239 
246  public function update(File $file) {
247  $updatedProperties = array_intersect($this->fields, $file->getUpdatedProperties());
248  $updateRow = array();
249  foreach ($updatedProperties as $key) {
250  $updateRow[$key] = $file->getProperty($key);
251  }
252  if (count($updateRow) > 0) {
253  $updateRow['tstamp'] = time();
254  $this->getDatabaseConnection()->exec_UPDATEquery($this->table, $this->getWhereClauseForFile($file), $updateRow);
255  $this->updateRefIndex($file->getUid());
256  $this->emitRecordUpdatedSignal(array_intersect_key($file->getProperties(), array_flip($this->fields)));
257  }
258  }
259 
267  public function findInStorageWithIndexOutstanding(\TYPO3\CMS\Core\Resource\ResourceStorage $storage, $limit = -1) {
268  return $this->getDatabaseConnection()->exec_SELECTgetRows(
269  implode(',', $this->fields),
270  $this->table,
271  'tstamp > last_indexed AND storage = ' . (int)$storage->getUid(),
272  '',
273  'tstamp ASC',
274  (int)$limit > 0 ? (int)$limit : ''
275  );
276  }
277 
278 
286  public function findInStorageAndNotInUidList(\TYPO3\CMS\Core\Resource\ResourceStorage $storage, array $uidList) {
287  $where = 'storage = ' . (int)$storage->getUid();
288  if (!empty($uidList)) {
289  $where .= ' AND uid NOT IN (' . implode(',', $this->getDatabaseConnection()->cleanIntArray($uidList)) . ')';
290  }
291  return $this->getDatabaseConnection()->exec_SELECTgetRows(implode(',', $this->fields), $this->table, $where);
292  }
293 
300  public function updateIndexingTime($fileUid) {
301  $this->getDatabaseConnection()->exec_UPDATEquery($this->table, 'uid = ' . (int)$fileUid, array('last_indexed' => time()));
302  }
303 
310  public function markFileAsMissing($fileUid) {
311  $this->getDatabaseConnection()->exec_UPDATEquery($this->table, 'uid = ' . (int)$fileUid, array('missing' => 1));
312  }
313 
321  protected function getWhereClauseForFile(File $file) {
322  if ((int)$file->_getPropertyRaw('uid') > 0) {
323  $where = 'uid=' . (int)$file->getUid();
324  } else {
325  $where = sprintf(
326  'storage=%u AND identifier LIKE %s',
327  (int)$file->getStorage()->getUid(),
328  $this->getDatabaseConnection()->fullQuoteStr($file->_getPropertyRaw('identifier'), $this->table)
329  );
330  }
331  return $where;
332  }
333 
340  public function remove($fileUid) {
341  $this->getDatabaseConnection()->exec_DELETEquery($this->table, 'uid=' . (int)$fileUid);
342  $this->updateRefIndex($fileUid);
343  $this->emitRecordDeletedSignal($fileUid);
344  }
345 
346 
353  public function updateRefIndex($id) {
355  $refIndexObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\ReferenceIndex');
356  $refIndexObj->updateRefIndexTable($this->table, $id);
357  }
358 
359 
360  /*
361  * Get the SignalSlot dispatcher
362  *
363  * @return \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
364  */
365  protected function getSignalSlotDispatcher() {
366  return $this->getObjectManager()->get('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
367  }
368 
374  protected function getObjectManager() {
375  return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
376  }
377 
378 
379 
386  protected function emitRecordUpdatedSignal(array $data) {
387  $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\Index\\FileIndexRepository', 'recordUpdated', array($data));
388  }
389 
396  protected function emitRecordCreatedSignal(array $data) {
397  $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\Index\\FileIndexRepository', 'recordCreated', array($data));
398  }
399 
406  protected function emitRecordDeletedSignal($fileUid) {
407  $this->getSignalSlotDispatcher()->dispatch('TYPO3\\CMS\\Core\\Resource\\Index\\FileIndexRepository', 'recordDeleted', array($fileUid));
408  }
409 }
findOneByFileObject(\TYPO3\CMS\Core\Resource\FileInterface $fileObject)
findByFolder(\TYPO3\CMS\Core\Resource\Folder $folder)
updateProperties(array $properties)
Definition: File.php:210
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
findInStorageAndNotInUidList(\TYPO3\CMS\Core\Resource\ResourceStorage $storage, array $uidList)
findInStorageWithIndexOutstanding(\TYPO3\CMS\Core\Resource\ResourceStorage $storage, $limit=-1)
findOneByStorageUidAndIdentifierHash($storageUid, $identifierHash)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]