TYPO3  7.6
CleanerTask.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Recycler\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  */
17 
23 {
27  protected $period = 0;
28 
32  protected $tcaTables = array();
33 
37  protected $databaseConnection = null;
38 
45  public function execute()
46  {
47  $success = true;
48  $tables = $this->getTcaTables();
49  foreach ($tables as $table) {
50  if (!$this->cleanTable($table)) {
51  $success = false;
52  }
53  }
54 
55  return $success;
56  }
57 
64  protected function cleanTable($tableName)
65  {
66  $queryParts = array();
67  if (isset($GLOBALS['TCA'][$tableName]['ctrl']['delete'])) {
68  $queryParts[] = $GLOBALS['TCA'][$tableName]['ctrl']['delete'] . ' = 1';
69  if ($GLOBALS['TCA'][$tableName]['ctrl']['tstamp']) {
70  $dateBefore = $this->getPeriodAsTimestamp();
71  $queryParts[] = $GLOBALS['TCA'][$tableName]['ctrl']['tstamp'] . ' < ' . $dateBefore;
72  }
73  $where = implode(' AND ', $queryParts);
74 
75  $this->checkFileResourceFieldsBeforeDeletion($tableName, $where);
76 
77  $this->getDatabaseConnection()->exec_DELETEquery($tableName, $where);
78  }
79 
80  return $this->getDatabaseConnection()->sql_error() === '';
81  }
82 
88  public function getAdditionalInformation()
89  {
90  $message = '';
91 
92  $message .= sprintf(
93  $this->getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskDescriptionTables'),
94  implode(', ', $this->getTcaTables())
95  );
96 
97  $message .= '; ';
98 
99  $message .= sprintf(
100  $this->getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskDescriptionDays'),
101  $this->getPeriod()
102  );
103 
104  return $message;
105  }
106 
112  public function setPeriod($period)
113  {
114  $this->period = (int)$period;
115  }
116 
122  public function getPeriod()
123  {
124  return $this->period;
125  }
126 
130  public function getPeriodAsTimestamp()
131  {
132  return strtotime('-' . $this->getPeriod() . ' days');
133  }
134 
140  public function setTcaTables($tcaTables = array())
141  {
142  $this->tcaTables = $tcaTables;
143  }
144 
150  public function getTcaTables()
151  {
152  return $this->tcaTables;
153  }
154 
159  {
160  $this->databaseConnection = $databaseConnection;
161  }
162 
170  protected function checkFileResourceFieldsBeforeDeletion($table, $where)
171  {
172  $fieldList = $this->getFileResourceFields($table);
173  if (!empty($fieldList)) {
174  $this->deleteFilesForTable($table, $where, $fieldList);
175  }
176  }
177 
186  protected function deleteFilesForTable($table, $where, array $fieldList)
187  {
188  $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
189  implode(',', $fieldList),
190  $table,
191  $where
192  );
193  foreach ($rows as $row) {
194  foreach ($fieldList as $fieldName) {
195  $uploadDir = PATH_site . $GLOBALS['TCA'][$table]['columns'][$fieldName]['config']['uploadfolder'] . '/';
196  $fileList = GeneralUtility::trimExplode(',', $row[$fieldName]);
197  foreach ($fileList as $fileName) {
198  @unlink($uploadDir . $fileName);
199  }
200  }
201  }
202  }
203 
210  protected function getFileResourceFields($table)
211  {
212  $result = array();
213  if (isset($GLOBALS['TCA'][$table]['columns'])) {
214  foreach ($GLOBALS['TCA'][$table]['columns'] as $fieldName => $fieldConfiguration) {
215  if ($fieldConfiguration['config']['type'] === 'group'
216  && $fieldConfiguration['config']['internal_type'] === 'file'
217  ) {
218  $result[] = $fieldName;
219  break;
220  }
221  }
222  }
223  return $result;
224  }
225 
229  protected function getDatabaseConnection()
230  {
231  if ($this->databaseConnection === null) {
232  $this->databaseConnection = $GLOBALS['TYPO3_DB'];
233  }
235  }
236 
240  protected function getLanguageService()
241  {
242  return $GLOBALS['LANG'];
243  }
244 }