‪TYPO3CMS  10.4
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 
23 
32 {
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('deleted', 0),
63  $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('formPersistenceIdentifier', \PDO::PARAM_STR)),
64  $queryBuilder->expr()->orX(
65  $queryBuilder->expr()->eq('ref_string', $queryBuilder->createNamedParameter($persistenceIdentifier, \PDO::PARAM_STR)),
66  $queryBuilder->expr()->eq('ref_uid', $queryBuilder->createNamedParameter($file->getUid(), \PDO::PARAM_INT))
67  )
68  )
69  ->execute()
70  ->fetchAll();
71  }
72 
81  {
82  $items = [];
83  foreach ($this->‪getAllReferences('ref_string') as $item) {
84  $items[$item['identifier']] = $item['items'];
85  }
86  return $items;
87  }
88 
96  public function ‪getAllReferencesForFileUid(): array
97  {
98  $items = [];
99  foreach ($this->‪getAllReferences('ref_uid') as $item) {
100  $items[$item['identifier']] = $item['items'];
101  }
102  return $items;
103  }
104 
110  protected function ‪getAllReferences(string $column): array
111  {
112  if ($column !== 'ref_string' && $column !== 'ref_uid') {
113  throw new \InvalidArgumentException('$column must not be "ref_string" or "ref_uid".', 1535406600);
114  }
115 
116  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
117 
118  $constraints = [
119  $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('formPersistenceIdentifier', \PDO::PARAM_STR)),
120  $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
121  ];
122 
123  if ($column === 'ref_string') {
124  $constraints[] = $queryBuilder->expr()->neq('ref_string', $queryBuilder->createNamedParameter('', \PDO::PARAM_STR));
125  } else {
126  $constraints[] = $queryBuilder->expr()->gt('ref_uid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT));
127  }
128 
129  return $queryBuilder
130  ->select($column . ' AS identifier')
131  ->addSelectLiteral('COUNT(' . $queryBuilder->quoteIdentifier($column) . ') AS ' . $queryBuilder->quoteIdentifier('items'))
132  ->from('sys_refindex')
133  ->where(...$constraints)
134  ->groupBy($column)
135  ->execute()
136  ->fetchAll();
137  }
138 }
‪TYPO3\CMS\Form\Service\DatabaseService
Definition: DatabaseService.php:32
‪TYPO3\CMS\Form\Service\DatabaseService\getAllReferencesForPersistenceIdentifier
‪array getAllReferencesForPersistenceIdentifier()
Definition: DatabaseService.php:80
‪TYPO3\CMS\Form\Service\DatabaseService\getAllReferences
‪array getAllReferences(string $column)
Definition: DatabaseService.php:110
‪TYPO3\CMS\Form\Service\DatabaseService\getReferencesByPersistenceIdentifier
‪array getReferencesByPersistenceIdentifier(string $persistenceIdentifier)
Definition: DatabaseService.php:43
‪TYPO3\CMS\Form\Service
Definition: DatabaseService.php:18
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Form\Service\DatabaseService\getAllReferencesForFileUid
‪array getAllReferencesForFileUid()
Definition: DatabaseService.php:96