[FEATURE] Add "processed files" cleanup tool to Install Tool
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / ProcessedFileRepository.php
index 8123113..6a77390 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Core\Resource;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Log\LogManager;
 use TYPO3\CMS\Core\Utility;
 
 /**
@@ -32,7 +33,7 @@ class ProcessedFileRepository extends AbstractRepository {
         *
         * @var string
         */
-       protected $objectType = \TYPO3\CMS\Core\Resource\ProcessedFile::class;
+       protected $objectType = ProcessedFile::class;
 
        /**
         * Main File object storage table. Note that this repository also works on
@@ -56,7 +57,7 @@ class ProcessedFileRepository extends AbstractRepository {
         * Creates this object.
         */
        public function __construct() {
-               $this->resourceFactory = Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
+               $this->resourceFactory = Utility\GeneralUtility::makeInstance(ResourceFactory::class);
                $this->databaseConnection = $GLOBALS['TYPO3_DB'];
        }
 
@@ -197,6 +198,40 @@ class ProcessedFileRepository extends AbstractRepository {
                return $itemList;
        }
 
+       /**
+        * Removes all processed files and also deletes the associated physical files
+        *
+        * @param int|NULL $storageUid If not NULL, only the processed files of the given storage are removed
+        * @return int Number of failed deletions
+        */
+       public function removeAll($storageUid = NULL) {
+               $res = $this->databaseConnection->exec_SELECTquery('*', $this->table, 'identifier <> \'\'');
+               $logger = $this->getLogger();
+               $errorCount = 0;
+               while ($row = $this->databaseConnection->sql_fetch_assoc($res)) {
+                       if ($storageUid && (int)$storageUid !== (int)$row['storage']) {
+                               continue;
+                       }
+                       try {
+                               $file = $this->createDomainObject($row);
+                               $file->getStorage()->setEvaluatePermissions(FALSE);
+                               $file->delete(TRUE);
+                       } catch (\Exception $e) {
+                               $logger->error(
+                                       'Failed to delete file "' . $row['identifier'] . '" in storage uid ' . $row['storage'] . '.',
+                                       array(
+                                               'exception' => $e
+                                       )
+                               );
+                               ++$errorCount;
+                       }
+               }
+
+               $this->databaseConnection->exec_TRUNCATEquery($this->table);
+
+               return $errorCount;
+       }
+
 
        /**
         * Removes all array keys which cannot be persisted
@@ -209,4 +244,11 @@ class ProcessedFileRepository extends AbstractRepository {
                return array_intersect_key($data, $this->databaseConnection->admin_get_fields($this->table));
        }
 
-}
\ No newline at end of file
+       /**
+        * @return \TYPO3\CMS\Core\Log\Logger
+        */
+       protected function getLogger() {
+               return Utility\GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
+       }
+
+}