‪TYPO3CMS  ‪main
ClearTableService.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 
29 final readonly class ‪ClearTableService
30 {
31  public function ‪__construct(private ‪FailsafePackageManager $packageManager) {}
32 
36  public function ‪getTableStatistics(): array
37  {
38  $tableStatistics = [];
39  foreach ($this->‪getTableList() as $table) {
40  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table['name']);
41  if ($connection->createSchemaManager()->tablesExist([$table['name']])) {
42  $table['rowCount'] = $connection->count(
43  '*',
44  $table['name'],
45  []
46  );
47  $tableStatistics[] = $table;
48  }
49  }
50  return $tableStatistics;
51  }
52 
56  public function ‪clearSelectedTable(string $tableName): void
57  {
58  $tableFound = false;
59  foreach ($this->‪getTableList() as $table) {
60  if ($table['name'] === $tableName) {
61  $tableFound = true;
62  break;
63  }
64  }
65  if (!$tableFound) {
66  throw new \RuntimeException(
67  'Selected table ' . $tableName . ' can not be cleared',
68  1501942151
69  );
70  }
71  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName)->truncate($tableName);
72  }
73 
77  private function ‪getTableList(): array
78  {
79  $tableList = [
80  [
81  'name' => 'be_sessions',
82  'description' => 'Backend user sessions',
83  ],
84  [
85  'name' => 'fe_sessions',
86  'description' => 'Frontend user sessions',
87  ],
88  [
89  'name' => 'sys_history',
90  'description' => 'Tracking of database record changes through TYPO3 backend forms',
91  ],
92  [
93  'name' => 'sys_lockedrecords',
94  'description' => 'Record locking of backend user editing',
95  ],
96  [
97  'name' => 'sys_log',
98  'description' => 'General log table',
99  ],
100  [
101  'name' => 'sys_preview',
102  'description' => 'Workspace preview links',
103  ],
104  ];
105  if ($this->packageManager->isPackageActive('extensionmanager')) {
106  $tableList[] = [
107  'name' => 'tx_extensionmanager_domain_model_extension',
108  'description' => 'List of TER extensions',
109  ];
110  }
111  return $tableList;
112  }
113 }
‪TYPO3\CMS\Install\Service\ClearTableService\getTableStatistics
‪getTableStatistics()
Definition: ClearTableService.php:36
‪TYPO3\CMS\Core\Package\FailsafePackageManager
Definition: FailsafePackageManager.php:27
‪TYPO3\CMS\Install\Service\ClearTableService
Definition: ClearTableService.php:30
‪TYPO3\CMS\Install\Service\ClearTableService\getTableList
‪getTableList()
Definition: ClearTableService.php:77
‪TYPO3\CMS\Install\Service\ClearTableService\__construct
‪__construct(private FailsafePackageManager $packageManager)
Definition: ClearTableService.php:31
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Install\Service\ClearTableService\clearSelectedTable
‪clearSelectedTable(string $tableName)
Definition: ClearTableService.php:56
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:16