‪TYPO3CMS  ‪main
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 {
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('softref_key', $queryBuilder->createNamedParameter('formPersistenceIdentifier')),
61  $queryBuilder->expr()->or(
62  $queryBuilder->expr()->eq('ref_string', $queryBuilder->createNamedParameter($persistenceIdentifier)),
63  $queryBuilder->expr()->eq('ref_uid', $queryBuilder->createNamedParameter($file->getUid(), ‪Connection::PARAM_INT))
64  )
65  )
66  ->executeQuery()
67  ->fetchAllAssociative();
68  }
69 
77  {
78  $items = [];
79  foreach ($this->‪getAllReferences('ref_string') as $item) {
80  $items[$item['identifier']] = $item['items'];
81  }
82  return $items;
83  }
84 
91  public function ‪getAllReferencesForFileUid(): array
92  {
93  $items = [];
94  foreach ($this->‪getAllReferences('ref_uid') as $item) {
95  $items[$item['identifier']] = $item['items'];
96  }
97  return $items;
98  }
99 
103  protected function ‪getAllReferences(string $column): array
104  {
105  if ($column !== 'ref_string' && $column !== 'ref_uid') {
106  throw new \InvalidArgumentException('$column must not be "ref_string" or "ref_uid".', 1535406600);
107  }
108 
109  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
110 
111  $constraints = [
112  $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('formPersistenceIdentifier')),
113  ];
114 
115  if ($column === 'ref_string') {
116  $constraints[] = $queryBuilder->expr()->neq('ref_string', $queryBuilder->createNamedParameter(''));
117  } else {
118  $constraints[] = $queryBuilder->expr()->gt('ref_uid', $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT));
119  }
120 
121  return $queryBuilder
122  ->select($column . ' AS identifier')
123  ->addSelectLiteral('COUNT(' . $queryBuilder->quoteIdentifier($column) . ') AS ' . $queryBuilder->quoteIdentifier('items'))
124  ->from('sys_refindex')
125  ->where(...$constraints)
126  ->groupBy($column)
127  ->executeQuery()
128  ->fetchAllAssociative();
129  }
130 }
‪TYPO3\CMS\Form\Service\DatabaseService
Definition: DatabaseService.php:33
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Form\Service\DatabaseService\getReferencesByPersistenceIdentifier
‪getReferencesByPersistenceIdentifier(string $persistenceIdentifier)
Definition: DatabaseService.php:41
‪TYPO3\CMS\Form\Service
Definition: DatabaseService.php:18
‪TYPO3\CMS\Form\Service\DatabaseService\getAllReferences
‪getAllReferences(string $column)
Definition: DatabaseService.php:103
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Form\Service\DatabaseService\getAllReferencesForPersistenceIdentifier
‪getAllReferencesForPersistenceIdentifier()
Definition: DatabaseService.php:76
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Form\Service\DatabaseService\getAllReferencesForFileUid
‪getAllReferencesForFileUid()
Definition: DatabaseService.php:91
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52