[TASK] Performance optimizations for the form manager module
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Service / DatabaseService.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Form\Service;
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\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Resource\ResourceFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * This class is subjected to change.
24 * **Do NOT subclass**
25 *
26 * Scope: frontend / backend
27 * @internal
28 */
29 class DatabaseService
30 {
31
32 /**
33 * Returns an array with all sys_refindex database rows which be
34 * connected to a formDefinition identified by $persistenceIdentifier
35 *
36 * @param string $persistenceIdentifier
37 * @return array
38 * @throws \InvalidArgumentException
39 * @internal
40 */
41 public function getReferencesByPersistenceIdentifier(string $persistenceIdentifier): array
42 {
43 if (empty($persistenceIdentifier)) {
44 throw new \InvalidArgumentException('$persistenceIdentifier must not be empty.', 1472238493);
45 }
46
47 $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
48 $file = $resourceFactory->retrieveFileOrFolderObject($persistenceIdentifier);
49
50 if ($file === null) {
51 return [];
52 }
53
54 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
55
56 return $queryBuilder
57 ->select('*')
58 ->from('sys_refindex')
59 ->where(
60 $queryBuilder->expr()->eq('deleted', 0),
61 $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('formPersistenceIdentifier', \PDO::PARAM_STR)),
62 $queryBuilder->expr()->orX(
63 $queryBuilder->expr()->eq('ref_string', $queryBuilder->createNamedParameter($persistenceIdentifier, \PDO::PARAM_STR)),
64 $queryBuilder->expr()->eq('ref_uid', $queryBuilder->createNamedParameter($file->getUid(), \PDO::PARAM_INT))
65 ),
66 $queryBuilder->expr()->eq('tablename', $queryBuilder->createNamedParameter('tt_content', \PDO::PARAM_STR))
67 )
68 ->execute()
69 ->fetchAll();
70 }
71
72 /**
73 * Returns an array with all form definition persistenceIdentifiers
74 * as keys and their reference counts as values.
75 *
76 * @return array
77 * @internal
78 */
79 public function getAllReferencesForPersistenceIdentifier(): array
80 {
81 $items = [];
82 foreach ($this->getAllReferences('ref_string') as $item) {
83 $items[$item['identifier']] = $item['items'];
84 }
85 return $items;
86 }
87
88 /**
89 * Returns an array with all form definition file uids as keys
90 * and their reference counts as values.
91 *
92 * @return array
93 * @internal
94 */
95 public function getAllReferencesForFileUid(): array
96 {
97 $items = [];
98 foreach ($this->getAllReferences('ref_uid') as $item) {
99 $items[$item['identifier']] = $item['items'];
100 }
101 return $items;
102 }
103
104 /**
105 * @param string $column
106 * @return array
107 * @throws \InvalidArgumentException
108 */
109 protected function getAllReferences(string $column): array
110 {
111 if ($column !== 'ref_string' && $column !== 'ref_uid') {
112 throw new \InvalidArgumentException('$column must not be "ref_string" or "ref_uid".', 1535406600);
113 }
114
115 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
116
117 $constraints = [$queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('formPersistenceIdentifier', \PDO::PARAM_STR))];
118
119 if ($column === 'ref_string') {
120 $constraints[] = $queryBuilder->expr()->neq('ref_string', $queryBuilder->createNamedParameter('', \PDO::PARAM_STR));
121 } else {
122 $constraints[] = $queryBuilder->expr()->gt('ref_uid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT));
123 }
124
125 return $queryBuilder
126 ->select($column . ' AS identifier')
127 ->addSelectLiteral('COUNT(' . $queryBuilder->quoteIdentifier($column) . ') AS ' . $queryBuilder->quoteIdentifier('items'))
128 ->from('sys_refindex')
129 ->where(...$constraints)
130 ->groupBy($column)
131 ->execute()
132 ->fetchAll();
133 }
134 }