‪TYPO3CMS  11.5
DatabaseService.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 
24 
33 {
43  public function ‪getReferencesByPersistenceIdentifier(string $persistenceIdentifier): array
44  {
45  if (empty($persistenceIdentifier)) {
46  throw new \InvalidArgumentException('$persistenceIdentifier must not be empty.', 1472238493);
47  }
48 
49  $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
50  $file = $resourceFactory->retrieveFileOrFolderObject($persistenceIdentifier);
51 
52  if ($file === null) {
53  return [];
54  }
55 
56  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
57 
58  return $queryBuilder
59  ->select('*')
60  ->from('sys_refindex')
61  ->where(
62  $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('formPersistenceIdentifier', ‪Connection::PARAM_STR)),
63  $queryBuilder->expr()->orX(
64  $queryBuilder->expr()->eq('ref_string', $queryBuilder->createNamedParameter($persistenceIdentifier, ‪Connection::PARAM_STR)),
65  $queryBuilder->expr()->eq('ref_uid', $queryBuilder->createNamedParameter($file->getUid(), ‪Connection::PARAM_INT))
66  )
67  )
68  ->executeQuery()
69  ->fetchAllAssociative();
70  }
71 
80  {
81  $items = [];
82  foreach ($this->‪getAllReferences('ref_string') as $item) {
83  $items[$item['identifier']] = $item['items'];
84  }
85  return $items;
86  }
87 
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 
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 = [
118  $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('formPersistenceIdentifier', ‪Connection::PARAM_STR)),
119  ];
120 
121  if ($column === 'ref_string') {
122  $constraints[] = $queryBuilder->expr()->neq('ref_string', $queryBuilder->createNamedParameter('', ‪Connection::PARAM_STR));
123  } else {
124  $constraints[] = $queryBuilder->expr()->gt('ref_uid', $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT));
125  }
126 
127  return $queryBuilder
128  ->select($column . ' AS identifier')
129  ->addSelectLiteral('COUNT(' . $queryBuilder->quoteIdentifier($column) . ') AS ' . $queryBuilder->quoteIdentifier('items'))
130  ->from('sys_refindex')
131  ->where(...$constraints)
132  ->groupBy($column)
133  ->executeQuery()
134  ->fetchAllAssociative();
135  }
136 }
‪TYPO3\CMS\Form\Service\DatabaseService
Definition: DatabaseService.php:33
‪TYPO3\CMS\Form\Service\DatabaseService\getAllReferencesForPersistenceIdentifier
‪array getAllReferencesForPersistenceIdentifier()
Definition: DatabaseService.php:79
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Form\Service\DatabaseService\getAllReferences
‪array getAllReferences(string $column)
Definition: DatabaseService.php:109
‪TYPO3\CMS\Form\Service\DatabaseService\getReferencesByPersistenceIdentifier
‪array getReferencesByPersistenceIdentifier(string $persistenceIdentifier)
Definition: DatabaseService.php:43
‪TYPO3\CMS\Core\Database\Connection\PARAM_STR
‪const PARAM_STR
Definition: Connection.php:54
‪TYPO3\CMS\Form\Service
Definition: DatabaseService.php:18
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Form\Service\DatabaseService\getAllReferencesForFileUid
‪array getAllReferencesForFileUid()
Definition: DatabaseService.php:95