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