‪TYPO3CMS  10.4
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\DBALException;
22 
29 {
33  protected ‪$period = 0;
34 
38  protected ‪$tcaTables = [];
39 
46  public function ‪execute()
47  {
48  $success = true;
49  $tables = $this->‪getTcaTables();
50  foreach ($tables as $table) {
51  if (!$this->‪cleanTable($table)) {
52  $success = false;
53  }
54  }
55 
56  return $success;
57  }
58 
65  protected function ‪cleanTable($tableName)
66  {
67  if (isset(‪$GLOBALS['TCA'][$tableName]['ctrl']['delete'])) {
68  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
69  $queryBuilder->getRestrictions()->removeAll();
70 
71  $constraints = [
72  $queryBuilder->expr()->eq(
73  ‪$GLOBALS['TCA'][$tableName]['ctrl']['delete'],
74  $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT)
75  )
76  ,
77  ];
78 
79  if (‪$GLOBALS['TCA'][$tableName]['ctrl']['tstamp']) {
80  $dateBefore = $this->‪getPeriodAsTimestamp();
81  $constraints[] = $queryBuilder->expr()->lt(
82  ‪$GLOBALS['TCA'][$tableName]['ctrl']['tstamp'],
83  $queryBuilder->createNamedParameter($dateBefore, \PDO::PARAM_INT)
84  );
85  }
86  try {
87  $queryBuilder->delete($tableName)
88  ->where(...$constraints)
89  ->execute();
90  } catch (DBALException $e) {
91  return false;
92  }
93  }
94  return true;
95  }
96 
102  public function ‪getAdditionalInformation()
103  {
104  $message = '';
105 
106  $message .= sprintf(
107  $this->‪getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskDescriptionTables'),
108  implode(', ', $this->‪getTcaTables())
109  );
110 
111  $message .= '; ';
112 
113  $message .= sprintf(
114  $this->‪getLanguageService()->sL('LLL:EXT:recycler/Resources/Private/Language/locallang_tasks.xlf:cleanerTaskDescriptionDays'),
115  $this->‪getPeriod()
116  );
117 
118  return $message;
119  }
120 
126  public function ‪setPeriod(‪$period)
127  {
128  $this->period = (int)‪$period;
129  }
130 
136  public function ‪getPeriod()
137  {
138  return ‪$this->period;
139  }
140 
144  public function ‪getPeriodAsTimestamp()
145  {
146  return strtotime('-' . $this->‪getPeriod() . ' days');
147  }
148 
154  public function ‪setTcaTables(‪$tcaTables = [])
155  {
156  $this->tcaTables = ‪$tcaTables;
157  }
158 
164  public function ‪getTcaTables()
165  {
166  return ‪$this->tcaTables;
167  }
168 }
‪TYPO3\CMS\Recycler\Task\CleanerTask\getPeriodAsTimestamp
‪int getPeriodAsTimestamp()
Definition: CleanerTask.php:142
‪TYPO3\CMS\Recycler\Task\CleanerTask\setTcaTables
‪setTcaTables($tcaTables=[])
Definition: CleanerTask.php:152
‪TYPO3\CMS\Recycler\Task\CleanerTask\getTcaTables
‪array getTcaTables()
Definition: CleanerTask.php:162
‪TYPO3\CMS\Recycler\Task\CleanerTask\setPeriod
‪setPeriod($period)
Definition: CleanerTask.php:124
‪TYPO3\CMS\Recycler\Task\CleanerTask\execute
‪bool execute()
Definition: CleanerTask.php:44
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getLanguageService
‪LanguageService null getLanguageService()
Definition: AbstractTask.php:605
‪TYPO3\CMS\Recycler\Task\CleanerTask\getAdditionalInformation
‪string getAdditionalInformation()
Definition: CleanerTask.php:100
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:35
‪TYPO3\CMS\Recycler\Task\CleanerTask
Definition: CleanerTask.php:29
‪TYPO3\CMS\Recycler\Task\CleanerTask\$tcaTables
‪array $tcaTables
Definition: CleanerTask.php:36
‪TYPO3\CMS\Recycler\Task
Definition: CleanerFieldProvider.php:16
‪TYPO3\CMS\Recycler\Task\CleanerTask\cleanTable
‪bool cleanTable($tableName)
Definition: CleanerTask.php:63
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Recycler\Task\CleanerTask\$period
‪int $period
Definition: CleanerTask.php:32
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Recycler\Task\CleanerTask\getPeriod
‪int getPeriod()
Definition: CleanerTask.php:134