‪TYPO3CMS  11.5
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 
28 
34 {
38  public const ‪TABLE_NAME = 'tx_extensionmanager_domain_model_extension';
39 
41 
46  {
47  $this->querySettings = ‪$querySettings;
48  }
49 
53  public function ‪initializeObject()
54  {
55  $this->‪setDefaultQuerySettings($this->querySettings->setRespectStoragePage(false));
56  }
57 
63  public function ‪countAll()
64  {
65  $query = $this->‪createQuery();
66  $query = $this->‪addDefaultConstraints($query);
67  return $query->execute()->count();
68  }
69 
75  public function ‪findAll()
76  {
77  $query = $this->‪createQuery();
78  $query = $this->‪addDefaultConstraints($query);
79  $query->setOrderings(
80  [
81  'lastUpdated' => ‪QueryInterface::ORDER_DESCENDING,
82  ]
83  );
84  return $query->execute();
85  }
86 
93  public function ‪findByExtensionKeyOrderedByVersion($extensionKey)
94  {
95  $query = $this->‪createQuery();
96  $query->matching(
97  $query->logicalAnd(
98  $query->equals('extensionKey', $extensionKey),
99  $query->greaterThanOrEqual('reviewState', 0)
100  )
101  );
102  $query->setOrderings(['integerVersion' => ‪QueryInterface::ORDER_DESCENDING]);
103  return $query->execute();
104  }
105 
112  public function ‪findOneByCurrentVersionByExtensionKey($extensionKey)
113  {
114  $query = $this->‪createQuery();
115  $query->matching(
116  $query->logicalAnd(
117  $query->equals('extensionKey', $extensionKey),
118  $query->greaterThanOrEqual('reviewState', 0),
119  $query->equals('currentVersion', 1)
120  )
121  );
122  $query->setLimit(1);
123  return $query->execute()->getFirst();
124  }
125 
133  public function ‪findOneByExtensionKeyAndVersion($extensionKey, $version)
134  {
135  $query = $this->‪createQuery();
136  // Hint: This method must not filter out insecure extensions, if needed,
137  // it should be done on a different level, or with a helper method.
138  $query->matching($query->logicalAnd(
139  $query->equals('extensionKey', $extensionKey),
140  $query->equals('version', $version)
141  ));
142  return $query->setLimit(1)->execute()->getFirst();
143  }
144 
154  public function ‪findByTitleOrAuthorNameOrExtensionKey($searchString)
155  {
156  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
157  ->getQueryBuilderForTable(self::TABLE_NAME);
158 
159  $searchPlaceholderForLike = '%' . $queryBuilder->escapeLikeWildcards($searchString) . '%';
160 
161  $searchConstraints = [
162  'extension_key' => $queryBuilder->expr()->eq(
163  'extension_key',
164  $queryBuilder->createNamedParameter($searchString, ‪Connection::PARAM_STR)
165  ),
166  'extension_key_like' => $queryBuilder->expr()->like(
167  'extension_key',
168  $queryBuilder->createNamedParameter($searchPlaceholderForLike, ‪Connection::PARAM_STR)
169  ),
170  'title' => $queryBuilder->expr()->like(
171  'title',
172  $queryBuilder->createNamedParameter($searchPlaceholderForLike, ‪Connection::PARAM_STR)
173  ),
174  'description' => $queryBuilder->expr()->like(
175  'description',
176  $queryBuilder->createNamedParameter($searchPlaceholderForLike, ‪Connection::PARAM_STR)
177  ),
178  'author_name' => $queryBuilder->expr()->like(
179  'author_name',
180  $queryBuilder->createNamedParameter($searchPlaceholderForLike, ‪Connection::PARAM_STR)
181  ),
182  ];
183 
184  $caseStatement = 'CASE ' .
185  'WHEN ' . $searchConstraints['extension_key'] . ' THEN 16 ' .
186  'WHEN ' . $searchConstraints['extension_key_like'] . ' THEN 8 ' .
187  'WHEN ' . $searchConstraints['title'] . ' THEN 4 ' .
188  'WHEN ' . $searchConstraints['description'] . ' THEN 2 ' .
189  'WHEN ' . $searchConstraints['author_name'] . ' THEN 1 ' .
190  'END AS ' . $queryBuilder->quoteIdentifier('position');
191 
192  $result = $queryBuilder
193  ->select('*')
194  ->addSelectLiteral($caseStatement)
195  ->from(self::TABLE_NAME)
196  ->where(
197  $queryBuilder->expr()->orX(...array_values($searchConstraints)),
198  $queryBuilder->expr()->eq('current_version', $queryBuilder->createNamedParameter(1, ‪Connection::PARAM_INT)),
199  $queryBuilder->expr()->in('review_state', $queryBuilder->createNamedParameter([0, -2], Connection::PARAM_INT_ARRAY))
200  )
201  ->orderBy('position', 'DESC')
202  ->executeQuery()
203  ->fetchAllAssociative();
204 
205  $dataMapper = GeneralUtility::makeInstance(DataMapper::class);
206  return $dataMapper->map(Extension::class, $result);
207  }
208 
218  public function ‪findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion = 0, $highestVersion = 0, $includeCurrentVersion = true)
219  {
220  $query = $this->‪createQuery();
221  $constraint = null;
222  if ($lowestVersion !== 0 && $highestVersion !== 0) {
223  if ($includeCurrentVersion) {
224  $constraint = $query->logicalAnd($query->lessThanOrEqual('integerVersion', $highestVersion), $query->greaterThanOrEqual('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
225  } else {
226  $constraint = $query->logicalAnd($query->lessThanOrEqual('integerVersion', $highestVersion), $query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
227  }
228  } elseif ($lowestVersion === 0 && $highestVersion !== 0) {
229  if ($includeCurrentVersion) {
230  $constraint = $query->logicalAnd($query->lessThanOrEqual('integerVersion', $highestVersion), $query->equals('extensionKey', $extensionKey));
231  } else {
232  $constraint = $query->logicalAnd($query->lessThan('integerVersion', $highestVersion), $query->equals('extensionKey', $extensionKey));
233  }
234  } elseif ($lowestVersion !== 0 && $highestVersion === 0) {
235  if ($includeCurrentVersion) {
236  $constraint = $query->logicalAnd($query->greaterThanOrEqual('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
237  } else {
238  $constraint = $query->logicalAnd($query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
239  }
240  } elseif ($lowestVersion === 0 && $highestVersion === 0) {
241  $constraint = $query->equals('extensionKey', $extensionKey);
242  }
243  if ($constraint) {
244  $query->matching($query->logicalAnd($constraint, $query->greaterThanOrEqual('reviewState', 0)));
245  }
246  $query->setOrderings([
247  'integerVersion' => ‪QueryInterface::ORDER_DESCENDING,
248  ]);
249  return $query->execute();
250  }
251 
258  public function ‪findAllCommunityDistributions(bool $showUnsuitableDistributions = false): array
259  {
260  $query = $this->‪createQuery();
261  $query->matching(
262  $query->logicalAnd(
263  $query->equals('category', ‪Extension::DISTRIBUTION_CATEGORY),
264  $query->logicalNot($query->equals('ownerusername', 'typo3v4'))
265  )
266  );
267 
268  $query->setOrderings([
269  'alldownloadcounter' => ‪QueryInterface::ORDER_DESCENDING,
270  ]);
271 
272  return $this->‪filterYoungestVersionOfExtensionList($query->execute()->toArray(), $showUnsuitableDistributions);
273  }
274 
281  public function ‪findAllOfficialDistributions(bool $showUnsuitableDistributions = false): array
282  {
283  $query = $this->‪createQuery();
284  $query->matching(
285  $query->logicalAnd(
286  $query->equals('category', ‪Extension::DISTRIBUTION_CATEGORY),
287  $query->equals('ownerusername', 'typo3v4')
288  )
289  );
290 
291  $query->setOrderings([
292  'alldownloadcounter' => ‪QueryInterface::ORDER_DESCENDING,
293  ]);
294 
295  return $this->‪filterYoungestVersionOfExtensionList($query->execute()->toArray(), $showUnsuitableDistributions);
296  }
297 
306  public function ‪countByVersionRangeAndExtensionKey($extensionKey, $lowestVersion = 0, $highestVersion = 0)
307  {
308  return $this->‪findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion, $highestVersion)->count();
309  }
310 
317  public function ‪findHighestAvailableVersion($extensionKey)
318  {
319  $query = $this->‪createQuery();
320  $query->matching($query->logicalAnd($query->equals('extensionKey', $extensionKey), $query->greaterThanOrEqual('reviewState', 0)));
321  $query->setOrderings([
322  'integerVersion' => ‪QueryInterface::ORDER_DESCENDING,
323  ]);
324  return $query->setLimit(1)->execute()->getFirst();
325  }
326 
335  {
336  if ($query->‪getConstraint()) {
337  $query->‪matching($query->‪logicalAnd(
338  $query->‪getConstraint(),
339  $query->‪equals('current_version', true),
340  $query->‪greaterThanOrEqual('reviewState', 0)
341  ));
342  } else {
343  $query->‪matching($query->‪logicalAnd(
344  $query->‪equals('current_version', true),
345  $query->‪greaterThanOrEqual('reviewState', 0)
346  ));
347  }
348  return $query;
349  }
350 
357  protected function ‪getExtensionsSuitableForTypo3Version(array $extensions): array
358  {
359  $suitableExtensions = [];
360  foreach ($extensions as $extension) {
361  $dependency = $extension->getTypo3Dependency();
362  if ($dependency !== null && $dependency->isVersionCompatible(‪VersionNumberUtility::getNumericTypo3Version())) {
363  $suitableExtensions[] = $extension;
364  }
365  }
366  return $suitableExtensions;
367  }
368 
378  protected function ‪filterYoungestVersionOfExtensionList(array $extensions, bool $showUnsuitable): array
379  {
380  if (!$showUnsuitable) {
381  $extensions = $this->‪getExtensionsSuitableForTypo3Version($extensions);
382  }
383  $filteredExtensions = [];
384  foreach ($extensions as $extension) {
385  $extensionKey = $extension->getExtensionKey();
386  if (!array_key_exists($extensionKey, $filteredExtensions)) {
387  $filteredExtensions[$extensionKey] = $extension;
388  continue;
389  }
390  $currentVersion = $filteredExtensions[$extensionKey]->getVersion();
391  $newVersion = $extension->getVersion();
392  if (version_compare($newVersion, $currentVersion, '>')) {
393  $filteredExtensions[$extensionKey] = $extension;
394  }
395  }
396  return $filteredExtensions;
397  }
398 }
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\getConstraint
‪ConstraintInterface null getConstraint()
‪TYPO3\CMS\Core\Utility\VersionNumberUtility
Definition: VersionNumberUtility.php:24
‪TYPO3\CMS\Core\Utility\VersionNumberUtility\getNumericTypo3Version
‪static string getNumericTypo3Version()
Definition: VersionNumberUtility.php:51
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\injectQuerySettings
‪injectQuerySettings(QuerySettingsInterface $querySettings)
Definition: ExtensionRepository.php:45
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findOneByCurrentVersionByExtensionKey
‪object null findOneByCurrentVersionByExtensionKey($extensionKey)
Definition: ExtensionRepository.php:112
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:29
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension
Definition: Extension.php:28
‪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:32
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\$querySettings
‪QuerySettingsInterface $querySettings
Definition: ExtensionRepository.php:40
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findOneByExtensionKeyAndVersion
‪object null findOneByExtensionKeyAndVersion($extensionKey, $version)
Definition: ExtensionRepository.php:133
‪TYPO3\CMS\Extensionmanager\Domain\Repository
Definition: BulkExtensionRepositoryWriter.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:154
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findHighestAvailableVersion
‪object null findHighestAvailableVersion($extensionKey)
Definition: ExtensionRepository.php:317
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findByVersionRangeAndExtensionKeyOrderedByVersion
‪QueryResultInterface findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion=0, $highestVersion=0, $includeCurrentVersion=true)
Definition: ExtensionRepository.php:218
‪TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
Definition: DataMapper.php:51
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findByExtensionKeyOrderedByVersion
‪QueryResultInterface findByExtensionKeyOrderedByVersion($extensionKey)
Definition: ExtensionRepository.php:93
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\filterYoungestVersionOfExtensionList
‪Extension[] filterYoungestVersionOfExtensionList(array $extensions, bool $showUnsuitable)
Definition: ExtensionRepository.php:378
‪TYPO3\CMS\Core\Database\Connection\PARAM_STR
‪const PARAM_STR
Definition: Connection.php:54
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\addDefaultConstraints
‪QueryInterface addDefaultConstraints(QueryInterface $query)
Definition: ExtensionRepository.php:334
‪TYPO3\CMS\Extbase\Persistence\Repository\createQuery
‪TYPO3 CMS Extbase Persistence QueryInterface createQuery()
Definition: Repository.php:206
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
Definition: ExtensionRepository.php:34
‪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
Definition: Repository.php:29
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\countAll
‪int countAll()
Definition: ExtensionRepository.php:63
‪TYPO3\CMS\Extbase\Persistence\QueryResultInterface
Definition: QueryResultInterface.php:22
‪TYPO3\CMS\Extbase\Persistence\Repository\setDefaultQuerySettings
‪setDefaultQuerySettings(QuerySettingsInterface $defaultQuerySettings)
Definition: Repository.php:196
‪TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
Definition: QuerySettingsInterface.php:22
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findAllOfficialDistributions
‪Extension[] findAllOfficialDistributions(bool $showUnsuitableDistributions=false)
Definition: ExtensionRepository.php:281
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\countByVersionRangeAndExtensionKey
‪int countByVersionRangeAndExtensionKey($extensionKey, $lowestVersion=0, $highestVersion=0)
Definition: ExtensionRepository.php:306
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\getExtensionsSuitableForTypo3Version
‪Extension[] getExtensionsSuitableForTypo3Version(array $extensions)
Definition: ExtensionRepository.php:357
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\greaterThanOrEqual
‪ComparisonInterface greaterThanOrEqual($propertyName, $operand)
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\TABLE_NAME
‪const TABLE_NAME
Definition: ExtensionRepository.php:38
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findAllCommunityDistributions
‪Extension[] findAllCommunityDistributions(bool $showUnsuitableDistributions=false)
Definition: ExtensionRepository.php:258
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\initializeObject
‪initializeObject()
Definition: ExtensionRepository.php:53
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository\findAll
‪array QueryResultInterface findAll()
Definition: ExtensionRepository.php:75