‪TYPO3CMS  ‪main
CleanerTask.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Doctrine\DBAL\Exception as DBALException;
23 
30 {
34  protected ‪$period = 0;
35 
39  protected ‪$tcaTables = [];
40 
47  public function ‪execute()
48  {
49  $success = true;
50  $tables = $this->‪getTcaTables();
51  foreach ($tables as $table) {
52  if (!$this->‪cleanTable($table)) {
53  $success = false;
54  }
55  }
56 
57  return $success;
58  }
59 
66  protected function ‪cleanTable($tableName)
67  {
68  if (isset(‪$GLOBALS['TCA'][$tableName]['ctrl']['delete'])) {
69  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
70  $queryBuilder->getRestrictions()->removeAll();
71 
72  $constraints = [
73  $queryBuilder->expr()->eq(
74  ‪$GLOBALS['TCA'][$tableName]['ctrl']['delete'],
75  $queryBuilder->createNamedParameter(1, ‪Connection::PARAM_INT)
76  )
77  ,
78  ];
79 
80  if (‪$GLOBALS['TCA'][$tableName]['ctrl']['tstamp'] ?? null) {
81  $dateBefore = $this->‪getPeriodAsTimestamp();
82  $constraints[] = $queryBuilder->expr()->lt(
83  ‪$GLOBALS['TCA'][$tableName]['ctrl']['tstamp'],
84  $queryBuilder->createNamedParameter($dateBefore, ‪Connection::PARAM_INT)
85  );
86  }
87  try {
88  $queryBuilder->delete($tableName)
89  ->where(...$constraints)
90  ->executeStatement();
91  } catch (DBALException $e) {
92  return false;
93  }
94  }
95  return true;
96  }
97 
103  public function ‪getAdditionalInformation()
104  {
105  $message = '';
106 
107  $message .= sprintf(
108  $this->‪getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskDescriptionTables'),
109  implode(', ', $this->‪getTcaTables())
110  );
111 
112  $message .= '; ';
113 
114  $message .= sprintf(
115  $this->‪getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskDescriptionDays'),
116  $this->‪getPeriod()
117  );
118 
119  return $message;
120  }
121 
127  public function ‪setPeriod(‪$period)
128  {
129  $this->period = (int)‪$period;
130  }
131 
137  public function ‪getPeriod()
138  {
139  return ‪$this->period;
140  }
141 
145  public function ‪getPeriodAsTimestamp()
146  {
147  $timeStamp = strtotime('-' . $this->‪getPeriod() . ' days');
148  if ($timeStamp === false) {
149  throw new \InvalidArgumentException('Period must be an integer.', 1623097600);
150  }
151  return $timeStamp;
152  }
153 
159  public function ‪setTcaTables(‪$tcaTables = [])
160  {
161  $this->tcaTables = ‪$tcaTables;
162  }
163 
169  public function ‪getTcaTables()
170  {
171  return ‪$this->tcaTables;
172  }
173 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Recycler\Task\CleanerTask\getPeriodAsTimestamp
‪int getPeriodAsTimestamp()
Definition: CleanerTask.php:143
‪TYPO3\CMS\Recycler\Task\CleanerTask\setTcaTables
‪setTcaTables($tcaTables=[])
Definition: CleanerTask.php:157
‪TYPO3\CMS\Recycler\Task\CleanerTask\getTcaTables
‪array getTcaTables()
Definition: CleanerTask.php:167
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getLanguageService
‪getLanguageService()
Definition: AbstractTask.php:431
‪TYPO3\CMS\Recycler\Task\CleanerTask\setPeriod
‪setPeriod($period)
Definition: CleanerTask.php:125
‪TYPO3\CMS\Recycler\Task\CleanerTask\execute
‪bool execute()
Definition: CleanerTask.php:45
‪TYPO3\CMS\Recycler\Task\CleanerTask\getAdditionalInformation
‪string getAdditionalInformation()
Definition: CleanerTask.php:101
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:33
‪TYPO3\CMS\Recycler\Task\CleanerTask
Definition: CleanerTask.php:30
‪TYPO3\CMS\Recycler\Task\CleanerTask\$tcaTables
‪array $tcaTables
Definition: CleanerTask.php:37
‪TYPO3\CMS\Recycler\Task
Definition: CleanerFieldProvider.php:16
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Recycler\Task\CleanerTask\cleanTable
‪bool cleanTable($tableName)
Definition: CleanerTask.php:64
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Recycler\Task\CleanerTask\$period
‪int $period
Definition: CleanerTask.php:33
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Recycler\Task\CleanerTask\getPeriod
‪int getPeriod()
Definition: CleanerTask.php:135