[TASK] Display info about registered Extractors in Task "Metadata Extract"
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / RecyclerGarbageCollectionTask.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Task;
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
17 /**
18 * Recycler folder garbage collection task
19 *
20 * This task finds all "_recycler_" folders below fileadmin and
21 * deletes all file in them that where not change for more than
22 * given number of days.
23 *
24 * @author Kai Vogel <kai.vogel@speedprogs.de>
25 */
26 class RecyclerGarbageCollectionTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask {
27
28 /**
29 * Elapsed period since last modification before a file will
30 * be deleted in a recycler directory.
31 *
32 * @var int Number of days before cleaning up files
33 */
34 public $numberOfDays = 0;
35
36 /**
37 * Name of the recycler directories below the fileadmin dir.
38 *
39 * @var string Recycler directory name
40 */
41 protected $recyclerDirectory = '_recycler_';
42
43 /**
44 * Cleanup recycled files, called by scheduler.
45 *
46 * @return bool TRUE if task run was successful
47 * @throws \BadMethodCallException
48 */
49 public function execute() {
50 // There is no file ctime on windows, so this task disables itself if OS = win
51 if (TYPO3_OS === 'WIN') {
52 throw new \BadMethodCallException('This task is not reliable for Windows OS', 1308270454);
53 }
54 $seconds = 60 * 60 * 24 * (int)$this->numberOfDays;
55 $timestamp = $GLOBALS['EXEC_TIME'] - $seconds;
56 // Get fileadmin directory
57 $directory = PATH_site . 'fileadmin/';
58 if (!empty($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'])) {
59 $directory = PATH_site . trim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir']);
60 }
61 // Execute cleanup
62 return $this->cleanupRecycledFiles($directory, $timestamp);
63 }
64
65 /**
66 * Gets a list of all files in a directory recursively and removes
67 * old ones.
68 *
69 * @throws \RuntimeException If folders are not found or files can not be deleted
70 * @param string $directory Path to the directory
71 * @param int $timestamp Timestamp of the last file modification
72 * @return bool TRUE if success
73 */
74 protected function cleanupRecycledFiles($directory, $timestamp) {
75 $directory = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($directory);
76 $timestamp = (int)$timestamp;
77 // Check if given directory exists
78 if (!@is_dir($directory)) {
79 throw new \RuntimeException('Given directory "' . $directory . '" does not exist', 1301614535);
80 }
81 // Find all _recycler_ directories
82 $directoryContent = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory));
83 foreach ($directoryContent as $fileName => $file) {
84 // Skip directories and files without recycler directory in absolute path
85 $filePath = $file->getPath();
86 if (substr($filePath, strrpos($filePath, '/') + 1) !== $this->recyclerDirectory) {
87 continue;
88 }
89 // Remove files from _recycler_ that where moved to this folder for more than 'number of days'
90 if ($file->isFile() && $timestamp > $file->getCTime()) {
91 if (!@unlink($fileName)) {
92 throw new \RuntimeException('Could not remove file "' . $fileName . '"', 1301614537);
93 }
94 }
95 }
96 return TRUE;
97 }
98
99 }