TYPO3 CMS  TYPO3_8-7
RecyclerGarbageCollectionTask.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 
32 {
39  public $numberOfDays = 0;
40 
47  public function execute()
48  {
49  $recyclerFolders = [];
50  $storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
51  // takes only _recycler_ folder on the first level into account
52  foreach ($storageRepository->findAll() as $storage) {
53  $rootLevelFolder = $storage->getRootLevelFolder(false);
54  foreach ($rootLevelFolder->getSubfolders() as $subFolder) {
55  if ($subFolder->getRole() === $subFolder::ROLE_RECYCLER) {
56  $recyclerFolders[] = $subFolder;
57  break;
58  }
59  }
60  }
61 
62  // Execute cleanup
63  $seconds = 60 * 60 * 24 * (int)$this->numberOfDays;
64  $timestamp = $GLOBALS['EXEC_TIME'] - $seconds;
65  foreach ($recyclerFolders as $recyclerFolder) {
66  $this->cleanupRecycledFiles($recyclerFolder, $timestamp);
67  }
68  return true;
69  }
70 
78  protected function cleanupRecycledFiles(Folder $folder, $timestamp)
79  {
80  foreach ($folder->getFiles() as $file) {
81  if ($timestamp > $file->getModificationTime()) {
82  $file->delete();
83  }
84  }
85  foreach ($folder->getSubfolders() as $subFolder) {
86  $this->cleanupRecycledFiles($subFolder, $timestamp);
87  // if no more files and subdirectories are in the folder, remove the folder as well
88  if ($subFolder->getFileCount() === 0 && count($subFolder->getSubfolders()) === 0) {
89  $subFolder->delete(true);
90  }
91  }
92  }
93 }
static makeInstance($className,... $constructorArguments)
getFiles($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false, $sort='', $sortRev=false)
Definition: Folder.php:217
getSubfolders($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false)
Definition: Folder.php:272
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']