Revert "[TASK] Avoid slow array functions in loops"
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Service / RecordService.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\Service;
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 */
16 use TYPO3\CMS\Core\Database\Connection;
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\SingletonInterface;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Workspaces\Domain\Model\DatabaseRecord;
21
22 /**
23 * Service for records
24 */
25 class RecordService implements SingletonInterface
26 {
27 /**
28 * @var DatabaseRecord[]
29 */
30 protected $records = [];
31
32 /**
33 * @param string $tableName
34 * @param int $id
35 */
36 public function add($tableName, $id)
37 {
38 $databaseRecord = DatabaseRecord::create($tableName, $id);
39 if (!isset($this->records[$databaseRecord->getIdentifier()])) {
40 $this->records[$databaseRecord->getIdentifier()] = $databaseRecord;
41 }
42 }
43
44 /**
45 * @return array
46 */
47 public function getIdsPerTable()
48 {
49 $idsPerTable = [];
50 foreach ($this->records as $databaseRecord) {
51 if (!isset($idsPerTable[$databaseRecord->getTable()])) {
52 $idsPerTable[$databaseRecord->getTable()] = [];
53 }
54 $idsPerTable[$databaseRecord->getTable()][] = $databaseRecord->getUid();
55 }
56 return $idsPerTable;
57 }
58
59 /**
60 * @return array
61 */
62 public function getCreateUserIds()
63 {
64 $createUserIds = [];
65 foreach ($this->getIdsPerTable() as $tableName => $ids) {
66 if (empty($GLOBALS['TCA'][$tableName]['ctrl']['cruser_id'])) {
67 continue;
68 }
69 $createUserIdFieldName = $GLOBALS['TCA'][$tableName]['ctrl']['cruser_id'];
70
71 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
72 $queryBuilder->getRestrictions()->removeAll();
73
74 $records = $queryBuilder
75 ->select($createUserIdFieldName)
76 ->from($tableName)
77 ->where(
78 $queryBuilder->expr()->in(
79 'uid',
80 $queryBuilder->createNamedParameter($ids, Connection::PARAM_INT_ARRAY)
81 )
82 )
83 ->groupBy($createUserIdFieldName)
84 ->execute()
85 ->fetchAll();
86
87 $records = array_column($records, $createUserIdFieldName);
88
89 if (!empty($records)) {
90 $createUserIds = array_merge($createUserIds, $records);
91 }
92 }
93 return array_unique($createUserIds);
94 }
95 }