‪TYPO3CMS  10.4
ExtensionRepository.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
29 
35 {
39  const ‪TABLE_NAME = 'tx_extensionmanager_domain_model_extension';
40 
44  public function ‪initializeObject()
45  {
46  ‪$defaultQuerySettings = $this->objectManager->get(QuerySettingsInterface::class);
49  }
50 
56  public function ‪countAll()
57  {
58  $query = $this->‪createQuery();
59  $query = $this->‪addDefaultConstraints($query);
60  return $query->execute()->count();
61  }
62 
68  public function ‪findAll()
69  {
70  $query = $this->‪createQuery();
71  $query = $this->‪addDefaultConstraints($query);
72  $query->setOrderings(
73  [
75  ]
76  );
77  return $query->execute();
78  }
79 
86  public function ‪findByExtensionKeyOrderedByVersion($extensionKey)
87  {
88  $query = $this->‪createQuery();
89  $query->matching(
90  $query->logicalAnd(
91  $query->equals('extensionKey', $extensionKey),
92  $query->greaterThanOrEqual('reviewState', 0)
93  )
94  );
95  $query->setOrderings(['integerVersion' => ‪QueryInterface::ORDER_DESCENDING]);
96  return $query->execute();
97  }
98 
105  public function ‪findOneByCurrentVersionByExtensionKey($extensionKey)
106  {
107  $query = $this->‪createQuery();
108  $query->matching(
109  $query->logicalAnd(
110  $query->equals('extensionKey', $extensionKey),
111  $query->greaterThanOrEqual('reviewState', 0),
112  $query->equals('currentVersion', 1)
113  )
114  );
115  $query->setLimit(1);
116  return $query->execute()->getFirst();
117  }
118 
126  public function ‪findOneByExtensionKeyAndVersion($extensionKey, $version)
127  {
128  $query = $this->‪createQuery();
129  // Hint: This method must not filter out insecure extensions, if needed,
130  // it should be done on a different level, or with a helper method.
131  $query->matching($query->logicalAnd(
132  $query->equals('extensionKey', $extensionKey),
133  $query->equals('version', $version)
134  ));
135  return $query->setLimit(1)->execute()->getFirst();
136  }
137 
147  public function ‪findByTitleOrAuthorNameOrExtensionKey($searchString)
148  {
149  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
150  ->getQueryBuilderForTable(self::TABLE_NAME);
151 
152  $searchPlaceholderForLike = '%' . $queryBuilder->escapeLikeWildcards($searchString) . '%';
153 
154  $searchConstraints = [
155  'extension_key' => $queryBuilder->expr()->eq(
156  'extension_key',
157  $queryBuilder->createNamedParameter($searchString, \PDO::PARAM_STR)
158  ),
159  'extension_key_like' => $queryBuilder->expr()->like(
160  'extension_key',
161  $queryBuilder->createNamedParameter($searchPlaceholderForLike, \PDO::PARAM_STR)
162  ),
163  'title' => $queryBuilder->expr()->like(
164  'title',
165  $queryBuilder->createNamedParameter($searchPlaceholderForLike, \PDO::PARAM_STR)
166  ),
167  'description' => $queryBuilder->expr()->like(
168  'description',
169  $queryBuilder->createNamedParameter($searchPlaceholderForLike, \PDO::PARAM_STR)
170  ),
171  'author_name' => $queryBuilder->expr()->like(
172  'author_name',
173  $queryBuilder->createNamedParameter($searchPlaceholderForLike, \PDO::PARAM_STR)
174  ),
175  ];
176 
177  $caseStatement = 'CASE ' .
178  'WHEN ' . $searchConstraints['extension_key'] . ' THEN 16 ' .
179  'WHEN ' . $searchConstraints['extension_key_like'] . ' THEN 8 ' .
180  'WHEN ' . $searchConstraints['title'] . ' THEN 4 ' .
181  'WHEN ' . $searchConstraints['description'] . ' THEN 2 ' .
182  'WHEN ' . $searchConstraints['author_name'] . ' THEN 1 ' .
183  'END AS ' . $queryBuilder->quoteIdentifier('position');
184 
185  $result = $queryBuilder
186  ->select('*')
187  ->addSelectLiteral($caseStatement)
188  ->from(self::TABLE_NAME)
189  ->where(
190  $queryBuilder->expr()->orX(...array_values($searchConstraints)),
191  $queryBuilder->expr()->eq('current_version', $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT)),
192  $queryBuilder->expr()->gte('review_state', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
193  )
194  ->orderBy('position', 'DESC')
195  ->execute()
196  ->fetchAll();
197 
198  $dataMapper = $this->objectManager->get(DataMapper::class);
199  return $dataMapper->map(Extension::class, $result);
200  }
201 
211  public function ‪findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion = 0, $highestVersion = 0, $includeCurrentVersion = true)
212  {
213  $query = $this->‪createQuery();
214  $constraint = null;
215  if ($lowestVersion !== 0 && $highestVersion !== 0) {
216  if ($includeCurrentVersion) {
217  $constraint = $query->logicalAnd($query->lessThanOrEqual('integerVersion', $highestVersion), $query->greaterThanOrEqual('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
218  } else {
219  $constraint = $query->logicalAnd($query->lessThanOrEqual('integerVersion', $highestVersion), $query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
220  }
221  } elseif ($lowestVersion === 0 && $highestVersion !== 0) {
222  if ($includeCurrentVersion) {
223  $constraint = $query->logicalAnd($query->lessThanOrEqual('integerVersion', $highestVersion), $query->equals('extensionKey', $extensionKey));
224  } else {
225  $constraint = $query->logicalAnd($query->lessThan('integerVersion', $highestVersion), $query->equals('extensionKey', $extensionKey));
226  }
227  } elseif ($lowestVersion !== 0 && $highestVersion === 0) {
228  if ($includeCurrentVersion) {
229  $constraint = $query->logicalAnd($query->greaterThanOrEqual('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
230  } else {
231  $constraint = $query->logicalAnd($query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
232  }
233  } elseif ($lowestVersion === 0 && $highestVersion === 0) {
234  $constraint = $query->equals('extensionKey', $extensionKey);
235  }
236  if ($constraint) {
237  $query->matching($query->logicalAnd($constraint, $query->greaterThanOrEqual('reviewState', 0)));
238  }
239  $query->setOrderings([
240  'integerVersion' => ‪QueryInterface::ORDER_DESCENDING
241  ]);
242  return $query->execute();
243  }
244 
251  {
252  $query = $this->‪createQuery();
253  $query->matching(
254  $query->logicalAnd(
255  $query->equals('category', ‪Extension::DISTRIBUTION_CATEGORY),
256  $query->logicalNot($query->equals('ownerusername', 'typo3v4'))
257  )
258  );
259 
260  $query->setOrderings([
261  'alldownloadcounter' => ‪QueryInterface::ORDER_DESCENDING
262  ]);
263 
264  return $query->execute();
265  }
266 
273  {
274  $query = $this->‪createQuery();
275  $query->matching(
276  $query->logicalAnd(
277  $query->equals('category', ‪Extension::DISTRIBUTION_CATEGORY),
278  $query->equals('ownerusername', 'typo3v4')
279  )
280  );
281 
282  $query->setOrderings([
283  'alldownloadcounter' => ‪QueryInterface::ORDER_DESCENDING
284  ]);
285 
286  return $query->execute();
287  }
288 
297  public function ‪countByVersionRangeAndExtensionKey($extensionKey, $lowestVersion = 0, $highestVersion = 0)
298  {
299  return $this->‪findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion, $highestVersion)->count();
300  }
301 
308  public function ‪findHighestAvailableVersion($extensionKey)
309  {
310  $query = $this->‪createQuery();
311  $query->matching($query->logicalAnd($query->equals('extensionKey', $extensionKey), $query->greaterThanOrEqual('reviewState', 0)));
312  $query->setOrderings([
313  'integerVersion' => ‪QueryInterface::ORDER_DESCENDING
314  ]);
315  return $query->setLimit(1)->execute()->getFirst();
316  }
317 
324  public function ‪insertLastVersion($repositoryUid = 1)
325  {
326  $this->‪markExtensionWithMaximumVersionAsCurrent($repositoryUid);
327 
328  return $this->‪getNumberOfCurrentExtensions();
329  }
330 
338  protected function ‪markExtensionWithMaximumVersionAsCurrent($repositoryUid)
339  {
340  $uidsOfCurrentVersion = $this->‪fetchMaximalVersionsForAllExtensions($repositoryUid);
341  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
342  ->getQueryBuilderForTable(self::TABLE_NAME);
343  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
344  ->getConnectionForTable(self::TABLE_NAME);
346  $connection->getDatabasePlatform()
347  );
348 
349  foreach (array_chunk($uidsOfCurrentVersion, $maxBindParameters - 10) as $chunk) {
350  $queryBuilder
351  ->update(self::TABLE_NAME)
352  ->where(
353  $queryBuilder->expr()->in(
354  'uid',
355  $queryBuilder->createNamedParameter($chunk, Connection::PARAM_INT_ARRAY)
356  )
357  )
358  ->set('current_version', 1)
359  ->execute();
360  }
361  }
362 
371  protected function ‪fetchMaximalVersionsForAllExtensions($repositoryUid)
372  {
373  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
374  ->getQueryBuilderForTable(self::TABLE_NAME);
375 
376  $queryResult = $queryBuilder
377  ->select('a.uid AS uid')
378  ->from(self::TABLE_NAME, 'a')
379  ->leftJoin(
380  'a',
381  self::TABLE_NAME,
382  'b',
383  $queryBuilder->expr()->andX(
384  $queryBuilder->expr()->eq('a.repository', $queryBuilder->quoteIdentifier('b.repository')),
385  $queryBuilder->expr()->eq('a.extension_key', $queryBuilder->quoteIdentifier('b.extension_key')),
386  $queryBuilder->expr()->lt('a.integer_version', $queryBuilder->quoteIdentifier('b.integer_version'))
387  )
388  )
389  ->where(
390  $queryBuilder->expr()->eq(
391  'a.repository',
392  $queryBuilder->createNamedParameter($repositoryUid, \PDO::PARAM_INT)
393  ),
394  $queryBuilder->expr()->isNull('b.extension_key')
395  )
396  ->orderBy('a.uid')
397  ->execute();
398 
399  $extensionUids = [];
400  while ($row = $queryResult->fetch()) {
401  $extensionUids[] = $row['uid'];
402  }
403 
404  return $extensionUids;
405  }
406 
412  protected function ‪getNumberOfCurrentExtensions()
413  {
414  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
415  ->getQueryBuilderForTable(self::TABLE_NAME);
416 
417  return (int)$queryBuilder
418  ->count('*')
419  ->from(self::TABLE_NAME)
420  ->where($queryBuilder->expr()->eq(
421  'current_version',
422  $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT)
423  ))
424  ->execute()
425  ->fetchColumn(0);
426  }
427 
436  {
437  if ($query->‪getConstraint()) {
438  $query->‪matching($query->‪logicalAnd(
439  $query->‪getConstraint(),
440  $query->‪equals('current_version', true),
441  $query->‪greaterThanOrEqual('reviewState', 0)
442  ));
443  } else {
444  $query->‪matching($query->‪logicalAnd(
445  $query->‪equals('current_version', true),
446  $query->‪greaterThanOrEqual('reviewState', 0)
447  ));
448  }
449  return $query;
450  }
451 }
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\getConstraint
‪ConstraintInterface null getConstraint()
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\markExtensionWithMaximumVersionAsCurrent
‪markExtensionWithMaximumVersionAsCurrent($repositoryUid)
Definition: ExtensionRepository.php:338
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:29
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension
Definition: Extension.php:29
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\equals
‪TYPO3 CMS Extbase Persistence Generic Qom ComparisonInterface equals($propertyName, $operand, $caseSensitive=true)
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension\DISTRIBUTION_CATEGORY
‪const DISTRIBUTION_CATEGORY
Definition: Extension.php:33
‪TYPO3\CMS\Extensionmanager\Domain\Repository
Definition: ExtensionRepository.php:16
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\ORDER_DESCENDING
‪const ORDER_DESCENDING
Definition: QueryInterface.php:99
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findByTitleOrAuthorNameOrExtensionKey
‪mixed findByTitleOrAuthorNameOrExtensionKey($searchString)
Definition: ExtensionRepository.php:147
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findOneByCurrentVersionByExtensionKey
‪array QueryResultInterface findOneByCurrentVersionByExtensionKey($extensionKey)
Definition: ExtensionRepository.php:105
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface\setRespectStoragePage
‪TYPO3 CMS Extbase Persistence Generic QuerySettingsInterface setRespectStoragePage($respectStoragePage)
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findByVersionRangeAndExtensionKeyOrderedByVersion
‪QueryResultInterface findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion=0, $highestVersion=0, $includeCurrentVersion=true)
Definition: ExtensionRepository.php:211
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:52
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findByExtensionKeyOrderedByVersion
‪QueryResultInterface findByExtensionKeyOrderedByVersion($extensionKey)
Definition: ExtensionRepository.php:86
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\fetchMaximalVersionsForAllExtensions
‪array fetchMaximalVersionsForAllExtensions($repositoryUid)
Definition: ExtensionRepository.php:371
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findAllCommunityDistributions
‪QueryResultInterface findAllCommunityDistributions()
Definition: ExtensionRepository.php:250
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\addDefaultConstraints
‪QueryInterface addDefaultConstraints(QueryInterface $query)
Definition: ExtensionRepository.php:435
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation\getMaxBindParameters
‪static int getMaxBindParameters(AbstractPlatform $platform)
Definition: PlatformInformation.php:125
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\getNumberOfCurrentExtensions
‪int getNumberOfCurrentExtensions()
Definition: ExtensionRepository.php:412
‪TYPO3\CMS\Extbase\Persistence\Repository\createQuery
‪TYPO3 CMS Extbase Persistence QueryInterface createQuery()
Definition: Repository.php:204
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
Definition: ExtensionRepository.php:35
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\matching
‪TYPO3 CMS Extbase Persistence QueryInterface matching($constraint)
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\logicalAnd
‪AndInterface logicalAnd($constraint1)
‪TYPO3\CMS\Extbase\Persistence\Repository\$defaultQuerySettings
‪TYPO3 CMS Extbase Persistence Generic QuerySettingsInterface $defaultQuerySettings
Definition: Repository.php:51
‪TYPO3\CMS\Extbase\Persistence\Repository
Definition: Repository.php:29
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\countAll
‪int countAll()
Definition: ExtensionRepository.php:56
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Repository\setDefaultQuerySettings
‪setDefaultQuerySettings(QuerySettingsInterface $defaultQuerySettings)
Definition: Repository.php:194
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
Definition: QuerySettingsInterface.php:22
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\countByVersionRangeAndExtensionKey
‪int countByVersionRangeAndExtensionKey($extensionKey, $lowestVersion=0, $highestVersion=0)
Definition: ExtensionRepository.php:297
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findHighestAvailableVersion
‪Extension findHighestAvailableVersion($extensionKey)
Definition: ExtensionRepository.php:308
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation
Definition: PlatformInformation.php:33
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\greaterThanOrEqual
‪ComparisonInterface greaterThanOrEqual($propertyName, $operand)
‪TYPO3\CMS\Extbase\Persistence\Generic\Query
Definition: Query.php:38
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\TABLE_NAME
‪const TABLE_NAME
Definition: ExtensionRepository.php:39
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findOneByExtensionKeyAndVersion
‪array QueryResultInterface findOneByExtensionKeyAndVersion($extensionKey, $version)
Definition: ExtensionRepository.php:126
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\initializeObject
‪initializeObject()
Definition: ExtensionRepository.php:44
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findAllOfficialDistributions
‪QueryResultInterface findAllOfficialDistributions()
Definition: ExtensionRepository.php:272
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\insertLastVersion
‪int insertLastVersion($repositoryUid=1)
Definition: ExtensionRepository.php:324
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findAll
‪array QueryResultInterface findAll()
Definition: ExtensionRepository.php:68