TYPO3 CMS  TYPO3_8-7
DatabaseService.php
Go to the documentation of this file.
1 <?php
2 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 
21 
30 {
31 
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 
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 = [$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 }
static makeInstance($className,... $constructorArguments)
getReferencesByPersistenceIdentifier(string $persistenceIdentifier)