2 namespace TYPO3\CMS\Extensionmanager\Domain\Repository
;
4 /***************************************************************
7 * (c) 2012-2013 Susanne Moog, <typo3@susannemoog.de>
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
27 * A repository for extensions
29 * @author Susanne Moog <typo3@susannemoog.de>
31 class ExtensionRepository
extends \TYPO3\CMS\Extbase\Persistence\Repository
{
34 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
36 protected $databaseConnection;
39 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
42 protected $dataMapper;
45 * Do not include pid in queries
49 public function initializeObject() {
50 /** @var $defaultQuerySettings \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface */
51 $defaultQuerySettings = $this->objectManager
->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface');
52 $defaultQuerySettings->setRespectStoragePage(FALSE);
53 $this->setDefaultQuerySettings($defaultQuerySettings);
54 $this->databaseConnection
= $GLOBALS['TYPO3_DB'];
58 * Count all extensions
62 public function countAll() {
63 $query = $this->createQuery();
64 $query = $this->addDefaultConstraints($query);
65 return $query->execute()->count();
69 * Finds all extensions
71 * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
73 public function findAll() {
74 $query = $this->createQuery();
75 $query = $this->addDefaultConstraints($query);
76 return $query->execute();
80 * Find an extension by extension key ordered by version
82 * @param string $extensionKey
83 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
85 public function findByExtensionKeyOrderedByVersion($extensionKey) {
86 $query = $this->createQuery();
87 $query->matching($query->logicalAnd($query->equals('extensionKey', $extensionKey), $query->greaterThanOrEqual('reviewState', 0)));
88 $query->setOrderings(array('version' => \TYPO3\CMS\Extbase\Persistence\QueryInterface
::ORDER_DESCENDING
));
89 return $query->execute();
93 * Find the current version by extension key
95 * @param string $extensionKey
96 * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
98 public function findOneByCurrentVersionByExtensionKey($extensionKey) {
99 $query = $this->createQuery();
102 $query->equals('extensionKey', $extensionKey),
103 $query->greaterThanOrEqual('reviewState', 0),
104 $query->equals('currentVersion', 1)
108 return $query->execute()->getFirst();
112 * Find one extension by extension key and version
114 * @param string $extensionKey
115 * @param string $version (example: 4.3.10)
116 * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
118 public function findOneByExtensionKeyAndVersion($extensionKey, $version) {
119 $query = $this->createQuery();
120 // Hint: This method must not filter out insecure extensions, if needed,
121 // it should be done on a different level, or with a helper method.
122 $query->matching($query->logicalAnd(
123 $query->equals('extensionKey', $extensionKey),
124 $query->equals('version', $version)
126 return $query->setLimit(1)->execute()->getFirst();
130 * Find an extension by title, author name or extension key
131 * This is the function used by the TER search. It is using a
132 * scoring for the matches to sort the extension with an
133 * exact key match on top
135 * @param string $searchString The string to search for extensions
138 public function findByTitleOrAuthorNameOrExtensionKey($searchString) {
139 $quotedSearchString = $this->databaseConnection
->escapeStrForLike($this->databaseConnection
->quoteStr($searchString, 'tx_extensionmanager_domain_model_extension'), 'tx_extensionmanager_domain_model_extension');
140 $quotedSearchStringForLike = '\'%' . $quotedSearchString . '%\'';
141 $quotedSearchString = '\'' . $quotedSearchString . '\'';
142 $select = 'tx_extensionmanager_domain_model_extension.*,
144 (extension_key like ' . $quotedSearchString . ') * 8 +
145 (extension_key like ' . $quotedSearchStringForLike . ') * 4 +
146 (title like ' . $quotedSearchStringForLike . ') * 2 +
147 (author_name like ' . $quotedSearchStringForLike . ')
149 $from = 'tx_extensionmanager_domain_model_extension';
151 extension_key = ' . $quotedSearchString . '
153 extension_key LIKE ' . $quotedSearchStringForLike . '
155 title LIKE ' . $quotedSearchStringForLike . '
157 description LIKE ' . $quotedSearchStringForLike . '
159 author_name LIKE ' . $quotedSearchStringForLike . '
161 AND current_version=1 AND review_state >= 0
162 HAVING position > 0';
163 $order = 'position desc';
164 $result = $this->databaseConnection
->exec_SELECTgetRows($select, $from, $where, '', $order);
165 return $this->dataMapper
->map('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\Extension', $result);
169 * Find an extension between a certain version range ordered by version number
171 * @param string $extensionKey
172 * @param integer $lowestVersion
173 * @param integer $highestVersion
174 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
176 public function findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion = 0, $highestVersion = 0) {
177 $query = $this->createQuery();
179 if ($lowestVersion !== 0 && $highestVersion !== 0) {
180 $constraint = $query->logicalAnd($query->lessThan('integerVersion', $highestVersion), $query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
181 } elseif ($lowestVersion === 0 && $highestVersion !== 0) {
182 $constraint = $query->logicalAnd($query->lessThan('integerVersion', $highestVersion), $query->equals('extensionKey', $extensionKey));
183 } elseif ($lowestVersion !== 0 && $highestVersion === 0) {
184 $constraint = $query->logicalAnd($query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
185 } elseif ($lowestVersion === 0 && $highestVersion === 0) {
186 $constraint = $query->equals('extensionKey', $extensionKey);
189 $query->matching($query->logicalAnd($constraint, $query->greaterThanOrEqual('reviewState', 0)));
191 $query->setOrderings(array(
192 'integerVersion' => \TYPO3\CMS\Extbase\Persistence\QueryInterface
::ORDER_DESCENDING
194 return $query->execute();
198 * Finds all extensions with category "distribution"
200 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
202 public function findAllDistributions() {
203 $query = $this->createQuery();
206 $query->equals('category', \TYPO3\CMS\Extensionmanager\Domain\Model\Extension
::DISTRIBUTION_CATEGORY
),
207 $query->equals('currentVersion', 1)
210 return $query->execute();
214 * Count extensions with a certain key between a given version range
216 * @param string $extensionKey
217 * @param integer $lowestVersion
218 * @param integer $highestVersion
221 public function countByVersionRangeAndExtensionKey($extensionKey, $lowestVersion = 0, $highestVersion = 0) {
222 return $this->findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion, $highestVersion)->count();
226 * Find highest version available of an extension
228 * @param string $extensionKey
231 public function findHighestAvailableVersion($extensionKey) {
232 $query = $this->createQuery();
233 $query->matching($query->logicalAnd($query->equals('extensionKey', $extensionKey), $query->greaterThanOrEqual('reviewState', 0)));
234 $query->setOrderings(array(
235 'integerVersion' => \TYPO3\CMS\Extbase\Persistence\QueryInterface
::ORDER_DESCENDING
237 return $query->setLimit(1)->execute()->getFirst();
241 * Update the current_version field after update
242 * For performance reason "native" TYPO3_DB is
243 * used here directly.
245 * @param integer $repositoryUid
248 public function insertLastVersion($repositoryUid = 1) {
249 $groupedRows = $this->databaseConnection
->exec_SELECTgetRows(
250 'extension_key, max(integer_version) as maxintversion',
251 'tx_extensionmanager_domain_model_extension',
252 'repository=' . intval($repositoryUid),
255 $extensions = count($groupedRows);
257 if ($extensions > 0) {
259 $this->databaseConnection
->exec_UPDATEquery(
260 'tx_extensionmanager_domain_model_extension',
261 'current_version=1 AND repository=' . intval($repositoryUid),
262 array('current_version' => 0)
264 // Find latest version of extensions and set current_version to 1 for these
265 foreach ($groupedRows as $row) {
266 $this->databaseConnection
->exec_UPDATEquery(
267 'tx_extensionmanager_domain_model_extension',
269 $this->databaseConnection
->fullQuoteStr($row['extension_key'], 'tx_extensionmanager_domain_model_extension') .
270 ' AND integer_version=' . intval($row['maxintversion']) .
271 ' AND repository=' . intval($repositoryUid),
272 array('current_version' => 1)
280 * Adds default constraints to the query - in this case it
281 * enables us to always just search for the latest version of an extension
283 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Query $query the query to adjust
284 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Query
286 protected function addDefaultConstraints(\TYPO3\CMS\Extbase\Persistence\Generic\Query
$query) {
287 if ($query->getConstraint()) {
288 $query->matching($query->logicalAnd(
289 $query->getConstraint(),
290 $query->equals('current_version', TRUE),
291 $query->greaterThanOrEqual('reviewState', 0)
294 $query->matching($query->logicalAnd(
295 $query->equals('current_version', TRUE),
296 $query->greaterThanOrEqual('reviewState', 0)