‪TYPO3CMS  9.5
CleanerTask.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  */
20 
27 {
31  protected ‪$period = 0;
32 
36  protected ‪$tcaTables = [];
37 
44  public function ‪execute()
45  {
46  $success = true;
47  $tables = $this->‪getTcaTables();
48  foreach ($tables as $table) {
49  if (!$this->‪cleanTable($table)) {
50  $success = false;
51  }
52  }
53 
54  return $success;
55  }
56 
63  protected function ‪cleanTable($tableName)
64  {
65  if (isset(‪$GLOBALS['TCA'][$tableName]['ctrl']['delete'])) {
66  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
67  $queryBuilder->getRestrictions()->removeAll();
68 
69  $constraints = [
70  $queryBuilder->expr()->eq(
71  ‪$GLOBALS['TCA'][$tableName]['ctrl']['delete'],
72  $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT)
73  )
74  ,
75  ];
76 
77  if (‪$GLOBALS['TCA'][$tableName]['ctrl']['tstamp']) {
78  $dateBefore = $this->‪getPeriodAsTimestamp();
79  $constraints[] = $queryBuilder->expr()->lt(
80  ‪$GLOBALS['TCA'][$tableName]['ctrl']['tstamp'],
81  $queryBuilder->createNamedParameter($dateBefore, \PDO::PARAM_INT)
82  );
83  }
85  try {
86  $queryBuilder->delete($tableName)
87  ->where(...$constraints)
88  ->execute();
89  } catch (\Doctrine\DBAL\DBALException $e) {
90  return false;
91  }
92  }
93  return true;
94  }
95 
101  public function ‪getAdditionalInformation()
102  {
103  $message = '';
104 
105  $message .= sprintf(
106  $this->‪getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskDescriptionTables'),
107  implode(', ', $this->‪getTcaTables())
108  );
109 
110  $message .= '; ';
111 
112  $message .= sprintf(
113  $this->‪getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskDescriptionDays'),
114  $this->‪getPeriod()
115  );
116 
117  return $message;
118  }
119 
125  public function ‪setPeriod(‪$period)
126  {
127  $this->period = (int)‪$period;
128  }
129 
135  public function ‪getPeriod()
136  {
137  return ‪$this->period;
138  }
139 
143  public function ‪getPeriodAsTimestamp()
144  {
145  return strtotime('-' . $this->‪getPeriod() . ' days');
146  }
147 
153  public function ‪setTcaTables(‪$tcaTables = [])
154  {
155  $this->tcaTables = ‪$tcaTables;
156  }
157 
163  public function ‪getTcaTables()
164  {
165  return ‪$this->tcaTables;
166  }
167 
174  protected function ‪checkFileResourceFieldsBeforeDeletion($table)
175  {
176  $fieldList = $this->‪getFileResourceFields($table);
177  if (!empty($fieldList)) {
178  $this->‪deleteFilesForTable($table, $fieldList);
179  }
180  }
181 
188  protected function ‪deleteFilesForTable($table, array $fieldList)
189  {
190  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
191  $queryBuilder->getRestrictions()->removeAll();
192 
193  $constraints = [
194  $queryBuilder->expr()->eq(
195  ‪$GLOBALS['TCA'][$table]['ctrl']['delete'],
196  $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT)
197  )
198  ];
199 
200  if (‪$GLOBALS['TCA'][$table]['ctrl']['tstamp']) {
201  $dateBefore = $this->‪getPeriodAsTimestamp();
202  $constraints[] = $queryBuilder->expr()->lt(
203  ‪$GLOBALS['TCA'][$table]['ctrl']['tstamp'],
204  $queryBuilder->createNamedParameter($dateBefore, \PDO::PARAM_INT)
205  );
206  }
207 
208  $result = $queryBuilder
209  ->select(...$fieldList)
210  ->from($table)
211  ->where(...$constraints)
212  ->execute();
213 
214  while ($row = $result->fetch()) {
215  foreach ($fieldList as $fieldName) {
216  $uploadDir = ‪Environment::getPublicPath() . '/' . ‪$GLOBALS['TCA'][$table]['columns'][$fieldName]['config']['uploadfolder'] . '/';
217  $fileList = GeneralUtility::trimExplode(',', $row[$fieldName]);
218  foreach ($fileList as $fileName) {
219  @unlink($uploadDir . $fileName);
220  }
221  }
222  }
223  }
224 
232  protected function ‪getFileResourceFields($table)
233  {
234  $result = [];
235  if (isset(‪$GLOBALS['TCA'][$table]['columns'])) {
236  foreach (‪$GLOBALS['TCA'][$table]['columns'] as $fieldName => $fieldConfiguration) {
237  if ($fieldConfiguration['config']['type'] === 'group'
238  && $fieldConfiguration['config']['internal_type'] === 'file'
239  ) {
240  $result[] = $fieldName;
241  }
242  }
243  }
244  return $result;
245  }
246 
250  protected function ‪getLanguageService()
251  {
252  return ‪$GLOBALS['LANG'];
253  }
254 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3\CMS\Recycler\Task\CleanerTask\getPeriodAsTimestamp
‪int getPeriodAsTimestamp()
Definition: CleanerTask.php:141
‪TYPO3\CMS\Recycler\Task\CleanerTask\setTcaTables
‪setTcaTables($tcaTables=[])
Definition: CleanerTask.php:151
‪TYPO3\CMS\Recycler\Task\CleanerTask\getTcaTables
‪array getTcaTables()
Definition: CleanerTask.php:161
‪TYPO3\CMS\Recycler\Task\CleanerTask\setPeriod
‪setPeriod($period)
Definition: CleanerTask.php:123
‪TYPO3\CMS\Recycler\Task\CleanerTask\execute
‪bool execute()
Definition: CleanerTask.php:42
‪TYPO3\CMS\Recycler\Task\CleanerTask\getAdditionalInformation
‪string getAdditionalInformation()
Definition: CleanerTask.php:99
‪TYPO3\CMS\Recycler\Task\CleanerTask\deleteFilesForTable
‪deleteFilesForTable($table, array $fieldList)
Definition: CleanerTask.php:186
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:32
‪TYPO3\CMS\Recycler\Task\CleanerTask
Definition: CleanerTask.php:27
‪TYPO3\CMS\Recycler\Task\CleanerTask\$tcaTables
‪array $tcaTables
Definition: CleanerTask.php:34
‪TYPO3\CMS\Recycler\Task
Definition: CleanerFieldProvider.php:2
‪TYPO3\CMS\Recycler\Task\CleanerTask\cleanTable
‪bool cleanTable($tableName)
Definition: CleanerTask.php:61
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Recycler\Task\CleanerTask\$period
‪int $period
Definition: CleanerTask.php:30
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Recycler\Task\CleanerTask\checkFileResourceFieldsBeforeDeletion
‪checkFileResourceFieldsBeforeDeletion($table)
Definition: CleanerTask.php:172
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Recycler\Task\CleanerTask\getFileResourceFields
‪array getFileResourceFields($table)
Definition: CleanerTask.php:230
‪TYPO3\CMS\Recycler\Task\CleanerTask\getPeriod
‪int getPeriod()
Definition: CleanerTask.php:133
‪TYPO3\CMS\Recycler\Task\CleanerTask\getLanguageService
‪TYPO3 CMS Core Localization LanguageService getLanguageService()
Definition: CleanerTask.php:248