‪TYPO3CMS  ‪main
RecordFinder.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 
22 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
30 
37 final class ‪RecordFinder
38 {
44  public function ‪findUidsOfStyleguideEntryPages(): array
45  {
46  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
47  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
48  $result = $queryBuilder->select('uid')
49  ->from('pages')
50  ->where(
51  $queryBuilder->expr()->eq(
52  'pid',
53  $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)
54  ),
55  $queryBuilder->expr()->eq(
56  'tx_styleguide_containsdemo',
57  $queryBuilder->createNamedParameter('tx_styleguide')
58  )
59  )
60  ->executeQuery();
61  $uidList = [];
62  while ($row = $result->fetchAssociative()) {
63  $uidList[] = (int)$row['uid'];
64  }
65  return $uidList;
66  }
67 
75  public function ‪findPidOfMainTableRecord(string $tableName): int
76  {
77  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
78  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
79  $row = $queryBuilder->select('uid')
80  ->from('pages')
81  ->where(
82  $queryBuilder->expr()->eq(
83  'tx_styleguide_containsdemo',
84  $queryBuilder->createNamedParameter($tableName)
85  ),
86  // only default language pages needed
87  $queryBuilder->expr()->eq(
88  'sys_language_uid',
89  $queryBuilder->createNamedParameter(0, ‪Connection::PARAM_INT)
90  )
91  )
92  ->orderBy('pid', 'DESC')
93  // add uid as deterministic last sorting, as not all dbms in all versions do that
94  ->addOrderBy('uid', 'ASC')
95  ->executeQuery()
96  ->fetchAssociative();
97  if (count($row) !== 1) {
98  throw new ‪Exception(
99  'Found no page for main table ' . $tableName,
100  1457690656
101  );
102  }
103  return (int)$row['uid'];
104  }
105 
111  public function ‪findIdsOfDemoLanguages(): array
112  {
113  try {
114  $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByRootPageId($this->‪findUidsOfStyleguideEntryPages()[0]);
115  } catch (‪SiteNotFoundException $e) {
116  return [];
117  }
118 
119  $result = [];
120  foreach ($site->getAllLanguages() as $language) {
121  if ($language->getLanguageId() === 0) {
122  continue;
123  }
124  $result[] = $language->getLanguageId();
125  }
126  return $result;
127  }
128 
132  public function ‪findHighestLanguageId(): int
133  {
134  $lastLanguageId = 0;
135  foreach (GeneralUtility::makeInstance(SiteFinder::class)->getAllSites() as $site) {
136  foreach ($site->getAllLanguages() as $language) {
137  if ($language->getLanguageId() > $lastLanguageId) {
138  $lastLanguageId = $language->getLanguageId();
139  }
140  }
141  }
142  return $lastLanguageId;
143  }
144 
150  public function ‪findUidsOfDemoBeGroups(): array
151  {
152  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_groups');
153  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
154  $rows = $queryBuilder->select('uid')
155  ->from('be_groups')
156  ->where(
157  $queryBuilder->expr()->eq(
158  'tx_styleguide_isdemorecord',
159  $queryBuilder->createNamedParameter(1, ‪Connection::PARAM_INT)
160  )
161  )
162  ->executeQuery()
163  ->fetchAllAssociative();
164  $result = [];
165  if (is_array($rows)) {
166  foreach ($rows as $row) {
167  $result[] = $row['uid'];
168  }
169  }
170  return $result;
171  }
172 
178  public function ‪findUidsOfDemoBeUsers(): array
179  {
180  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
181  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
182  $rows = $queryBuilder->select('uid')
183  ->from('be_users')
184  ->where(
185  $queryBuilder->expr()->eq(
186  'tx_styleguide_isdemorecord',
187  $queryBuilder->createNamedParameter(1, ‪Connection::PARAM_INT)
188  )
189  )
190  ->executeQuery()
191  ->fetchAllAssociative();
192  $result = [];
193  if (is_array($rows)) {
194  foreach ($rows as $row) {
195  $result[] = $row['uid'];
196  }
197  }
198  return $result;
199  }
200 
206  public function ‪findUidsOfStaticdata(): array
207  {
208  $pageUid = $this->‪findPidOfMainTableRecord('tx_styleguide_staticdata');
209  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_styleguide_staticdata');
210  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
211  $rows = $queryBuilder->select('uid')
212  ->from('tx_styleguide_staticdata')
213  ->where(
214  $queryBuilder->expr()->eq(
215  'pid',
216  $queryBuilder->createNamedParameter($pageUid, ‪Connection::PARAM_INT)
217  )
218  )
219  ->executeQuery()
220  ->fetchAllAssociative();
221  $result = [];
222  if (is_array($rows)) {
223  foreach ($rows as $row) {
224  $result[] = $row['uid'];
225  }
226  }
227  return $result;
228  }
229 
235  public function ‪findDemoFileObjects(string $path = 'styleguide'): array
236  {
237  $storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
238  $storage = $storageRepository->findByUid(1);
239  $folder = $storage->getRootLevelFolder();
240  $folder = $folder->getSubfolder($path);
241  return $folder->getFiles();
242  }
243 
248  {
249  $storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
250  $storage = $storageRepository->findByUid(1);
251  $folder = $storage->getRootLevelFolder();
252  return $folder->getSubfolder('styleguide');
253  }
254 
258  public function ‪findUidsOfFrontendPages(array $types = ['tx_styleguide_frontend_root', 'tx_styleguide_frontend'], array $doktype = []): array
259  {
260  $allowedTypes = ['tx_styleguide_frontend_root', 'tx_styleguide_frontend'];
261  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
262  $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
263 
265  $queryBuilder->select('uid')
266  ->from('pages');
267 
268  foreach ($types as $type) {
269  if (!in_array($type, $allowedTypes)) {
270  continue;
271  }
272 
273  if (!empty($doktype)) {
274  $queryBuilder->orWhere(
275  $queryBuilder->expr()->and(
276  $queryBuilder->expr()->eq(
277  'tx_styleguide_containsdemo',
278  $queryBuilder->createNamedParameter((string)$type),
279  ),
280  $queryBuilder->expr()->eq(
281  'doktype',
282  $queryBuilder->createNamedParameter('254')
283  )
284  )
285  );
286  } else {
287  $queryBuilder->orWhere(
288  $queryBuilder->expr()->eq(
289  'tx_styleguide_containsdemo',
290  $queryBuilder->createNamedParameter((string)$type)
291  )
292  );
293  }
294  }
295 
296  $rows = $queryBuilder->orderBy('pid', 'DESC')->executeQuery()->fetchAllAssociative();
297  $result = [];
298  if (is_array($rows)) {
299  $result = array_column($rows, 'uid');
300  sort($result);
301  }
302 
303  return $result;
304  }
305 
309  public function ‪findTtContent(array $types = ['textmedia', 'textpic', 'image', 'uploads'], string ‪$identifier = 'tx_styleguide_frontend'): array
310  {
311  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
312 
314  $queryBuilder->select('uid', 'pid', 'CType')
315  ->from('tt_content')->where(
316  $queryBuilder->expr()->eq(
317  'tx_styleguide_containsdemo',
318  $queryBuilder->createNamedParameter(‪$identifier)
319  )
320  );
321 
322  if (!empty($types)) {
323  $orExpression = $queryBuilder->expr()->or();
324  foreach ($types as $type) {
325  $orExpression = $orExpression->with($queryBuilder->expr()->eq('CType', $queryBuilder->createNamedParameter($type)));
326  }
327  $queryBuilder->andWhere((string)$orExpression);
328  }
329 
330  return $queryBuilder->orderBy('uid', 'DESC')->executeQuery()->fetchAllAssociative();
331  }
332 
333  public function ‪findFeUserGroups(): array
334  {
335  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('fe_groups');
336 
338  $queryBuilder->select('uid', 'pid', 'title')
339  ->from('fe_groups')->where(
340  $queryBuilder->expr()->eq(
341  'tx_styleguide_containsdemo',
342  $queryBuilder->createNamedParameter('tx_styleguide_frontend')
343  )
344  );
345 
346  return $queryBuilder->orderBy('uid', 'DESC')->executeQuery()->fetchAllAssociative();
347  }
348 
349  public function ‪findFeUsers(): array
350  {
351  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('fe_users');
352 
354  $queryBuilder->select('uid', 'pid', 'username')
355  ->from('fe_users')->where(
356  $queryBuilder->expr()->eq(
357  'tx_styleguide_containsdemo',
358  $queryBuilder->createNamedParameter('tx_styleguide_frontend')
359  )
360  );
361 
362  return $queryBuilder->orderBy('uid', 'DESC')->executeQuery()->fetchAllAssociative();
363  }
364 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findUidsOfStyleguideEntryPages
‪findUidsOfStyleguideEntryPages()
Definition: RecordFinder.php:44
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findTtContent
‪findTtContent(array $types=['textmedia', 'textpic', 'image', 'uploads'], string $identifier='tx_styleguide_frontend')
Definition: RecordFinder.php:309
‪TYPO3\CMS\Core\Exception\SiteNotFoundException
Definition: SiteNotFoundException.php:25
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findPidOfMainTableRecord
‪findPidOfMainTableRecord(string $tableName)
Definition: RecordFinder.php:75
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findDemoFolderObject
‪findDemoFolderObject()
Definition: RecordFinder.php:247
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findFeUsers
‪findFeUsers()
Definition: RecordFinder.php:349
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findUidsOfStaticdata
‪array findUidsOfStaticdata()
Definition: RecordFinder.php:206
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findDemoFileObjects
‪File[] findDemoFileObjects(string $path='styleguide')
Definition: RecordFinder.php:235
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findUidsOfFrontendPages
‪findUidsOfFrontendPages(array $types=['tx_styleguide_frontend_root', 'tx_styleguide_frontend'], array $doktype=[])
Definition: RecordFinder.php:258
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:38
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findUidsOfDemoBeUsers
‪array findUidsOfDemoBeUsers()
Definition: RecordFinder.php:178
‪TYPO3\CMS\Styleguide\TcaDataGenerator
Definition: AbstractGenerator.php:18
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findIdsOfDemoLanguages
‪array findIdsOfDemoLanguages()
Definition: RecordFinder.php:111
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findUidsOfDemoBeGroups
‪array findUidsOfDemoBeGroups()
Definition: RecordFinder.php:150
‪TYPO3\CMS\Styleguide\TcaDataGenerator\Exception
Definition: Exception.php:25
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findHighestLanguageId
‪findHighestLanguageId()
Definition: RecordFinder.php:132
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder\findFeUserGroups
‪findFeUserGroups()
Definition: RecordFinder.php:333
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder
Definition: RecordFinder.php:38
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37