‪TYPO3CMS  9.5
ExtensionRepository.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
27 {
31  const ‪TABLE_NAME = 'tx_extensionmanager_domain_model_extension';
32 
36  public function ‪initializeObject()
37  {
39  ‪$defaultQuerySettings = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\QuerySettingsInterface::class);
40  ‪$defaultQuerySettings->setRespectStoragePage(false);
42  }
43 
49  public function ‪countAll()
50  {
51  $query = $this->‪createQuery();
52  $query = $this->‪addDefaultConstraints($query);
53  return $query->execute()->count();
54  }
55 
61  public function ‪findAll()
62  {
63  $query = $this->‪createQuery();
64  $query = $this->‪addDefaultConstraints($query);
65  $query->setOrderings(
66  [
67  'lastUpdated' => \‪TYPO3\CMS\‪Extbase\Persistence\QueryInterface::ORDER_DESCENDING
68  ]
69  );
70  return $query->execute();
71  }
72 
79  public function ‪findByExtensionKeyOrderedByVersion($extensionKey)
80  {
81  $query = $this->‪createQuery();
82  $query->matching($query->logicalAnd($query->equals('extensionKey', $extensionKey), $query->greaterThanOrEqual('reviewState', 0)));
83  $query->setOrderings(['integerVersion' => \‪TYPO3\CMS\‪Extbase\Persistence\QueryInterface::ORDER_DESCENDING]);
84  return $query->execute();
85  }
86 
93  public function ‪findOneByCurrentVersionByExtensionKey($extensionKey)
94  {
95  $query = $this->‪createQuery();
96  $query->matching(
97  $query->logicalAnd(
98  $query->equals('extensionKey', $extensionKey),
99  $query->greaterThanOrEqual('reviewState', 0),
100  $query->equals('currentVersion', 1)
101  )
102  );
103  $query->setLimit(1);
104  return $query->execute()->getFirst();
105  }
106 
114  public function ‪findOneByExtensionKeyAndVersion($extensionKey, $version)
115  {
116  $query = $this->‪createQuery();
117  // Hint: This method must not filter out insecure extensions, if needed,
118  // it should be done on a different level, or with a helper method.
119  $query->matching($query->logicalAnd(
120  $query->equals('extensionKey', $extensionKey),
121  $query->equals('version', $version)
122  ));
123  return $query->setLimit(1)->execute()->getFirst();
124  }
125 
135  public function ‪findByTitleOrAuthorNameOrExtensionKey($searchString)
136  {
137  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
138  ->getQueryBuilderForTable(self::TABLE_NAME);
139 
140  $searchPlaceholderForLike = '%' . $queryBuilder->escapeLikeWildcards($searchString) . '%';
141 
142  $searchConstraints = [
143  'extension_key' => $queryBuilder->expr()->eq(
144  'extension_key',
145  $queryBuilder->createNamedParameter($searchString, \PDO::PARAM_STR)
146  ),
147  'extension_key_like' => $queryBuilder->expr()->like(
148  'extension_key',
149  $queryBuilder->createNamedParameter($searchPlaceholderForLike, \PDO::PARAM_STR)
150  ),
151  'title' => $queryBuilder->expr()->like(
152  'title',
153  $queryBuilder->createNamedParameter($searchPlaceholderForLike, \PDO::PARAM_STR)
154  ),
155  'description' => $queryBuilder->expr()->like(
156  'description',
157  $queryBuilder->createNamedParameter($searchPlaceholderForLike, \PDO::PARAM_STR)
158  ),
159  'author_name' => $queryBuilder->expr()->like(
160  'author_name',
161  $queryBuilder->createNamedParameter($searchPlaceholderForLike, \PDO::PARAM_STR)
162  ),
163  ];
164 
165  $caseStatement = 'CASE ' .
166  'WHEN ' . $searchConstraints['extension_key'] . ' THEN 16 ' .
167  'WHEN ' . $searchConstraints['extension_key_like'] . ' THEN 8 ' .
168  'WHEN ' . $searchConstraints['title'] . ' THEN 4 ' .
169  'WHEN ' . $searchConstraints['description'] . ' THEN 2 ' .
170  'WHEN ' . $searchConstraints['author_name'] . ' THEN 1 ' .
171  'END AS ' . $queryBuilder->quoteIdentifier('position');
172 
173  $result = $queryBuilder
174  ->select('*')
175  ->addSelectLiteral($caseStatement)
176  ->from(self::TABLE_NAME)
177  ->where(
178  $queryBuilder->expr()->orX(...array_values($searchConstraints)),
179  $queryBuilder->expr()->eq('current_version', $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT)),
180  $queryBuilder->expr()->gte('review_state', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
181  )
182  ->orderBy('position', 'DESC')
183  ->execute()
184  ->fetchAll();
185 
186  $dataMapper = $this->objectManager->get(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\Mapper\DataMapper::class);
187  return $dataMapper->map(\‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension::class, $result);
188  }
189 
199  public function ‪findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion = 0, $highestVersion = 0, $includeCurrentVersion = true)
200  {
201  $query = $this->‪createQuery();
202  $constraint = null;
203  if ($lowestVersion !== 0 && $highestVersion !== 0) {
204  if ($includeCurrentVersion) {
205  $constraint = $query->logicalAnd($query->lessThanOrEqual('integerVersion', $highestVersion), $query->greaterThanOrEqual('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
206  } else {
207  $constraint = $query->logicalAnd($query->lessThanOrEqual('integerVersion', $highestVersion), $query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
208  }
209  } elseif ($lowestVersion === 0 && $highestVersion !== 0) {
210  if ($includeCurrentVersion) {
211  $constraint = $query->logicalAnd($query->lessThanOrEqual('integerVersion', $highestVersion), $query->equals('extensionKey', $extensionKey));
212  } else {
213  $constraint = $query->logicalAnd($query->lessThan('integerVersion', $highestVersion), $query->equals('extensionKey', $extensionKey));
214  }
215  } elseif ($lowestVersion !== 0 && $highestVersion === 0) {
216  if ($includeCurrentVersion) {
217  $constraint = $query->logicalAnd($query->greaterThanOrEqual('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
218  } else {
219  $constraint = $query->logicalAnd($query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
220  }
221  } elseif ($lowestVersion === 0 && $highestVersion === 0) {
222  $constraint = $query->equals('extensionKey', $extensionKey);
223  }
224  if ($constraint) {
225  $query->matching($query->logicalAnd($constraint, $query->greaterThanOrEqual('reviewState', 0)));
226  }
227  $query->setOrderings([
228  'integerVersion' => \‪TYPO3\CMS\‪Extbase\Persistence\QueryInterface::ORDER_DESCENDING
229  ]);
230  return $query->execute();
231  }
232 
239  {
240  $query = $this->‪createQuery();
241  $query->matching(
242  $query->logicalAnd(
243  $query->equals('category', \‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension::DISTRIBUTION_CATEGORY),
244  $query->logicalNot($query->equals('ownerusername', 'typo3v4'))
245  )
246  );
247 
248  $query->setOrderings([
249  'alldownloadcounter' => \‪TYPO3\CMS\‪Extbase\Persistence\QueryInterface::ORDER_DESCENDING
250  ]);
251 
252  return $query->execute();
253  }
254 
261  {
262  $query = $this->‪createQuery();
263  $query->matching(
264  $query->logicalAnd(
265  $query->equals('category', \‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension::DISTRIBUTION_CATEGORY),
266  $query->equals('ownerusername', 'typo3v4')
267  )
268  );
269 
270  $query->setOrderings([
271  'alldownloadcounter' => \‪TYPO3\CMS\‪Extbase\Persistence\QueryInterface::ORDER_DESCENDING
272  ]);
273 
274  return $query->execute();
275  }
276 
285  public function ‪countByVersionRangeAndExtensionKey($extensionKey, $lowestVersion = 0, $highestVersion = 0)
286  {
287  return $this->‪findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion, $highestVersion)->count();
288  }
289 
296  public function ‪findHighestAvailableVersion($extensionKey)
297  {
298  $query = $this->‪createQuery();
299  $query->matching($query->logicalAnd($query->equals('extensionKey', $extensionKey), $query->greaterThanOrEqual('reviewState', 0)));
300  $query->setOrderings([
301  'integerVersion' => \‪TYPO3\CMS\‪Extbase\Persistence\QueryInterface::ORDER_DESCENDING
302  ]);
303  return $query->setLimit(1)->execute()->getFirst();
304  }
305 
312  public function ‪insertLastVersion($repositoryUid = 1)
313  {
314  $this->‪markExtensionWithMaximumVersionAsCurrent($repositoryUid);
315 
316  return $this->‪getNumberOfCurrentExtensions();
317  }
318 
326  protected function ‪markExtensionWithMaximumVersionAsCurrent($repositoryUid)
327  {
328  $uidsOfCurrentVersion = $this->‪fetchMaximalVersionsForAllExtensions($repositoryUid);
329  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
330  ->getQueryBuilderForTable(self::TABLE_NAME);
331  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
332  ->getConnectionForTable(self::TABLE_NAME);
334  $connection->getDatabasePlatform()
335  );
336 
337  foreach (array_chunk($uidsOfCurrentVersion, $maxBindParameters - 10) as $chunk) {
338  $queryBuilder
339  ->update(self::TABLE_NAME)
340  ->where(
341  $queryBuilder->expr()->in(
342  'uid',
343  $queryBuilder->createNamedParameter($chunk, Connection::PARAM_INT_ARRAY)
344  )
345  )
346  ->set('current_version', 1)
347  ->execute();
348  }
349  }
350 
359  protected function ‪fetchMaximalVersionsForAllExtensions($repositoryUid)
360  {
361  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
362  ->getQueryBuilderForTable(self::TABLE_NAME);
363 
364  $queryResult = $queryBuilder
365  ->select('a.uid AS uid')
366  ->from(self::TABLE_NAME, 'a')
367  ->leftJoin(
368  'a',
369  self::TABLE_NAME,
370  'b',
371  $queryBuilder->expr()->andX(
372  $queryBuilder->expr()->eq('a.repository', $queryBuilder->quoteIdentifier('b.repository')),
373  $queryBuilder->expr()->eq('a.extension_key', $queryBuilder->quoteIdentifier('b.extension_key')),
374  $queryBuilder->expr()->lt('a.integer_version', $queryBuilder->quoteIdentifier('b.integer_version'))
375  )
376  )
377  ->where(
378  $queryBuilder->expr()->eq(
379  'a.repository',
380  $queryBuilder->createNamedParameter($repositoryUid, \PDO::PARAM_INT)
381  ),
382  $queryBuilder->expr()->isNull('b.extension_key')
383  )
384  ->orderBy('a.uid')
385  ->execute();
386 
387  $extensionUids = [];
388  while ($row = $queryResult->fetch()) {
389  $extensionUids[] = $row['uid'];
390  }
391 
392  return $extensionUids;
393  }
394 
400  protected function ‪getNumberOfCurrentExtensions()
401  {
402  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
403  ->getQueryBuilderForTable(self::TABLE_NAME);
404 
405  return (int)$queryBuilder
406  ->count('*')
407  ->from(self::TABLE_NAME)
408  ->where($queryBuilder->expr()->eq(
409  'current_version',
410  $queryBuilder->createNamedParameter(1, \PDO::PARAM_INT)
411  ))
412  ->execute()
413  ->fetchColumn(0);
414  }
415 
423  protected function ‪addDefaultConstraints(\‪TYPO3\CMS\‪Extbase\Persistence\Generic\Query $query)
424  {
425  if ($query->getConstraint()) {
426  $query->matching($query->logicalAnd(
427  $query->getConstraint(),
428  $query->equals('current_version', true),
429  $query->greaterThanOrEqual('reviewState', 0)
430  ));
431  } else {
432  $query->matching($query->logicalAnd(
433  $query->equals('current_version', true),
434  $query->greaterThanOrEqual('reviewState', 0)
435  ));
436  }
437  return $query;
438  }
439 }
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\markExtensionWithMaximumVersionAsCurrent
‪markExtensionWithMaximumVersionAsCurrent($repositoryUid)
Definition: ExtensionRepository.php:326
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\addDefaultConstraints
‪TYPO3 CMS Extbase Persistence Generic Query addDefaultConstraints(\TYPO3\CMS\Extbase\Persistence\Generic\Query $query)
Definition: ExtensionRepository.php:423
‪TYPO3\CMS\Extensionmanager\Domain\Repository
Definition: ExtensionRepository.php:2
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findByTitleOrAuthorNameOrExtensionKey
‪mixed findByTitleOrAuthorNameOrExtensionKey($searchString)
Definition: ExtensionRepository.php:135
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\fetchMaximalVersionsForAllExtensions
‪array fetchMaximalVersionsForAllExtensions($repositoryUid)
Definition: ExtensionRepository.php:359
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findOneByExtensionKeyAndVersion
‪array TYPO3 CMS Extbase Persistence QueryResultInterface findOneByExtensionKeyAndVersion($extensionKey, $version)
Definition: ExtensionRepository.php:114
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation\getMaxBindParameters
‪static int getMaxBindParameters(AbstractPlatform $platform)
Definition: PlatformInformation.php:71
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\getNumberOfCurrentExtensions
‪int getNumberOfCurrentExtensions()
Definition: ExtensionRepository.php:400
‪TYPO3\CMS\Extbase\Persistence\Repository\createQuery
‪TYPO3 CMS Extbase Persistence QueryInterface createQuery()
Definition: Repository.php:189
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
Definition: ExtensionRepository.php:27
‪TYPO3\CMS\Extbase\Persistence\Repository\setDefaultQuerySettings
‪setDefaultQuerySettings(\TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface $defaultQuerySettings)
Definition: Repository.php:179
‪TYPO3\CMS\Extbase\Persistence\Repository\$defaultQuerySettings
‪TYPO3 CMS Extbase Persistence Generic QuerySettingsInterface $defaultQuerySettings
Definition: Repository.php:42
‪TYPO3\CMS\Extbase\Persistence\Repository
Definition: Repository.php:23
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\countAll
‪int countAll()
Definition: ExtensionRepository.php:49
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:21
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findByVersionRangeAndExtensionKeyOrderedByVersion
‪TYPO3 CMS Extbase Persistence QueryResultInterface findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion=0, $highestVersion=0, $includeCurrentVersion=true)
Definition: ExtensionRepository.php:199
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\countByVersionRangeAndExtensionKey
‪int countByVersionRangeAndExtensionKey($extensionKey, $lowestVersion=0, $highestVersion=0)
Definition: ExtensionRepository.php:285
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findAllOfficialDistributions
‪TYPO3 CMS Extbase Persistence QueryResultInterface findAllOfficialDistributions()
Definition: ExtensionRepository.php:260
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findAllCommunityDistributions
‪TYPO3 CMS Extbase Persistence QueryResultInterface findAllCommunityDistributions()
Definition: ExtensionRepository.php:238
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findHighestAvailableVersion
‪TYPO3 CMS Extensionmanager Domain Model Extension findHighestAvailableVersion($extensionKey)
Definition: ExtensionRepository.php:296
‪TYPO3\CMS\Core\Database\Platform\PlatformInformation
Definition: PlatformInformation.php:31
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findByExtensionKeyOrderedByVersion
‪TYPO3 CMS Extbase Persistence QueryResultInterface findByExtensionKeyOrderedByVersion($extensionKey)
Definition: ExtensionRepository.php:79
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\TABLE_NAME
‪const TABLE_NAME
Definition: ExtensionRepository.php:31
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\initializeObject
‪initializeObject()
Definition: ExtensionRepository.php:36
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\insertLastVersion
‪int insertLastVersion($repositoryUid=1)
Definition: ExtensionRepository.php:312
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findAll
‪array TYPO3 CMS Extbase Persistence QueryResultInterface findAll()
Definition: ExtensionRepository.php:61
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findOneByCurrentVersionByExtensionKey
‪array TYPO3 CMS Extbase Persistence QueryResultInterface findOneByCurrentVersionByExtensionKey($extensionKey)
Definition: ExtensionRepository.php:93