[TASK] Program to cache frontend interface, not implementations
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Classes / Controller / DeletedRecordsController.php
1 <?php
2
3 namespace TYPO3\CMS\Recycler\Controller;
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
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\DataHandling\DataHandler;
21 use TYPO3\CMS\Core\History\RecordHistoryStore;
22 use TYPO3\CMS\Core\Imaging\Icon;
23 use TYPO3\CMS\Core\Imaging\IconFactory;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Recycler\Utility\RecyclerUtility;
26
27 /**
28 * Deleted Records View
29 */
30 class DeletedRecordsController
31 {
32 /**
33 * @var \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
34 */
35 protected $runtimeCache = null;
36
37 /**
38 * @var DataHandler
39 */
40 protected $tce;
41
42 public function __construct()
43 {
44 $this->runtimeCache = $this->getMemoryCache();
45 $this->tce = GeneralUtility::makeInstance(DataHandler::class);
46 }
47
48 /**
49 * Transforms the rows for the deleted records
50 *
51 * @param array $deletedRowsArray Array with table as key and array with all deleted rows
52 * @param int $totalDeleted Number of deleted records in total
53 * @return string JSON array
54 */
55 public function transform($deletedRowsArray, $totalDeleted)
56 {
57 $total = 0;
58 $jsonArray = [
59 'rows' => []
60 ];
61
62 if (is_array($deletedRowsArray)) {
63 $lang = $this->getLanguageService();
64 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
65
66 foreach ($deletedRowsArray as $table => $rows) {
67 $total += count($deletedRowsArray[$table]);
68 foreach ($rows as $row) {
69 $pageTitle = $this->getPageTitle((int)$row['pid']);
70 $backendUserName = $this->getBackendUser((int)$row[$GLOBALS['TCA'][$table]['ctrl']['cruser_id']]);
71
72 $userIdWhoDeleted = $this->getUserWhoDeleted($table, (int)$row['uid']);
73
74 $jsonArray['rows'][] = [
75 'uid' => $row['uid'],
76 'pid' => $row['pid'],
77 'icon' => $iconFactory->getIconForRecord($table, $row, Icon::SIZE_SMALL)->render(),
78 'pageTitle' => $pageTitle,
79 'table' => $table,
80 'crdate' => BackendUtility::datetime($row[$GLOBALS['TCA'][$table]['ctrl']['crdate']]),
81 'tstamp' => BackendUtility::datetime($row[$GLOBALS['TCA'][$table]['ctrl']['tstamp']]),
82 'owner' => htmlspecialchars($backendUserName),
83 'owner_uid' => $row[$GLOBALS['TCA'][$table]['ctrl']['cruser_id']],
84 'tableTitle' => $lang->sL($GLOBALS['TCA'][$table]['ctrl']['title']),
85 'title' => htmlspecialchars(BackendUtility::getRecordTitle($table, $row)),
86 'path' => RecyclerUtility::getRecordPath($row['pid']),
87 'delete_user_uid' => $userIdWhoDeleted,
88 'delete_user' => $this->getBackendUser($userIdWhoDeleted),
89 'isParentDeleted' => $table === 'pages' ? RecyclerUtility::isParentPageDeleted($row['pid']) : false
90 ];
91 }
92 }
93 }
94 $jsonArray['total'] = $totalDeleted;
95 return $jsonArray;
96 }
97
98 /**
99 * Gets the page title of the given page id
100 *
101 * @param int $pageId
102 * @return string
103 */
104 protected function getPageTitle($pageId)
105 {
106 $cacheId = 'recycler-pagetitle-' . $pageId;
107 if ($this->runtimeCache->has($cacheId)) {
108 $pageTitle = $this->runtimeCache->get($cacheId);
109 } else {
110 if ($pageId === 0) {
111 $pageTitle = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
112 } else {
113 $recordInfo = $this->tce->recordInfo('pages', $pageId, 'title');
114 $pageTitle = $recordInfo['title'];
115 }
116 $this->runtimeCache->set($cacheId, $pageTitle);
117 }
118 return $pageTitle;
119 }
120
121 /**
122 * Gets the username of a given backend user
123 *
124 * @param int $userId uid of user
125 * @return string
126 */
127 protected function getBackendUser(int $userId): string
128 {
129 if ($userId === 0) {
130 return '';
131 }
132 $cacheId = 'recycler-user-' . $userId;
133 if ($this->runtimeCache->has($cacheId)) {
134 $username = $this->runtimeCache->get($cacheId);
135 } else {
136 $backendUser = BackendUtility::getRecord('be_users', $userId, 'username', '', false);
137 $username = $backendUser['username'];
138 $this->runtimeCache->set($cacheId, $username);
139 }
140 return $username;
141 }
142
143 /**
144 * Get the user uid of the user who deleted the record
145 *
146 * @param string $table table name
147 * @param int $uid uid of record
148 * @return int
149 */
150 protected function getUserWhoDeleted(string $table, int $uid): int
151 {
152 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
153 ->getQueryBuilderForTable('sys_history');
154 $queryBuilder->select('userid')
155 ->from('sys_history')
156 ->where(
157 $queryBuilder->expr()->eq(
158 'tablename',
159 $queryBuilder->createNamedParameter($table, \PDO::PARAM_STR)
160 ),
161 $queryBuilder->expr()->eq(
162 'usertype',
163 $queryBuilder->createNamedParameter('BE', \PDO::PARAM_STR)
164 ),
165 $queryBuilder->expr()->eq(
166 'recuid',
167 $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
168 ),
169 $queryBuilder->expr()->eq(
170 'actiontype',
171 $queryBuilder->createNamedParameter(RecordHistoryStore::ACTION_DELETE, \PDO::PARAM_INT)
172 )
173 )
174 ->setMaxResults(1);
175
176 return (int)$queryBuilder->execute()->fetchColumn(0);
177 }
178
179 /**
180 * Returns an instance of LanguageService
181 *
182 * @return \TYPO3\CMS\Core\Localization\LanguageService
183 */
184 protected function getLanguageService()
185 {
186 return $GLOBALS['LANG'];
187 }
188
189 /**
190 * Create and returns an instance of the CacheManager
191 *
192 * @return \TYPO3\CMS\Core\Cache\CacheManager
193 */
194 protected function getCacheManager()
195 {
196 return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
197 }
198
199 /**
200 * Gets an instance of the memory cache.
201 *
202 * @return \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
203 */
204 protected function getMemoryCache()
205 {
206 return $this->getCacheManager()->getCache('cache_runtime');
207 }
208 }