[FEATURE] Description-field for scheduler-jobs
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / RecyclerGarbageCollectionTask.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Task;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Kai Vogel <kai.vogel@speedprogs.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Recycler folder garbage collection task
28 *
29 * This task finds all "_recycler_" folders below fileadmin and
30 * deletes all file in them that where not change for more than
31 * given number of days.
32 *
33 * @author Kai Vogel <kai.vogel@speedprogs.de>
34 */
35 class RecyclerGarbageCollectionTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask {
36
37 /**
38 * Elapsed period since last modification before a file will
39 * be deleted in a recycler directory.
40 *
41 * @var int Number of days before cleaning up files
42 */
43 public $numberOfDays = 0;
44
45 /**
46 * Name of the recycler directories below the fileadmin dir.
47 *
48 * @var string Recycler directory name
49 */
50 protected $recyclerDirectory = '_recycler_';
51
52 /**
53 * Cleanup recycled files, called by scheduler.
54 *
55 * @return boolean TRUE if task run was successful
56 */
57 public function execute() {
58 // There is no file ctime on windows, so this task disables itself if OS = win
59 if (TYPO3_OS == 'WIN') {
60 throw new \BadMethodCallException('This task is not reliable for Windows OS', 1308270454);
61 }
62 $seconds = 60 * 60 * 24 * (int) $this->numberOfDays;
63 $timestamp = $GLOBALS['EXEC_TIME'] - $seconds;
64 // Get fileadmin directory
65 $directory = PATH_site . 'fileadmin/';
66 if (!empty($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'])) {
67 $directory = PATH_site . trim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir']);
68 }
69 // Execute cleanup
70 return $this->cleanupRecycledFiles($directory, $timestamp);
71 }
72
73 /**
74 * Gets a list of all files in a directory recursively and removes
75 * old ones.
76 *
77 * @throws \RuntimeException If folders are not found or files can not be deleted
78 * @param string $directory Path to the directory
79 * @param integer $timestamp Timestamp of the last file modification
80 * @return boolean TRUE if success
81 */
82 protected function cleanupRecycledFiles($directory, $timestamp) {
83 $directory = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($directory);
84 $timestamp = (int) $timestamp;
85 // Check if given directory exists
86 if (!@is_dir($directory)) {
87 throw new \RuntimeException('Given directory "' . $directory . '" does not exist', 1301614535);
88 }
89 // Find all _recycler_ directories
90 $directoryContent = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory));
91 foreach ($directoryContent as $fileName => $file) {
92 // Skip directories and files without recycler directory in absolute path
93 $filePath = $file->getPath();
94 if (substr($filePath, strrpos($filePath, '/') + 1) !== $this->recyclerDirectory) {
95 continue;
96 }
97 // Remove files from _recycler_ that where moved to this folder for more than 'number of days'
98 if ($file->isFile() && $timestamp > $file->getCTime()) {
99 if (!@unlink($fileName)) {
100 throw new \RuntimeException('Could not remove file "' . $fileName . '"', 1301614537);
101 }
102 }
103 }
104 return TRUE;
105 }
106
107 }