[BUGFIX] Fix dependency handling of EM
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Domain / Repository / ExtensionRepository.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Domain\Repository;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Susanne Moog, <typo3@susannemoog.de>
8 * All rights reserved
9 *
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.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
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.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * A repository for extensions
28 *
29 * @author Susanne Moog <typo3@susannemoog.de>
30 * @package Extension Manager
31 * @subpackage Repository
32 */
33 class ExtensionRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
34
35 /**
36 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
37 */
38 protected $databaseConnection;
39
40 /**
41 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
42 */
43 protected $dataMapper;
44
45 /**
46 * Injects the DataMapper to map records to objects
47 *
48 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper
49 * @return void
50 */
51 public function injectDataMapper(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper $dataMapper) {
52 $this->dataMapper = $dataMapper;
53 }
54
55 /**
56 * Do not include pid in queries
57 *
58 * @return void
59 */
60 public function initializeObject() {
61 /** @var $defaultQuerySettings \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface */
62 $defaultQuerySettings = $this->objectManager->create('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface');
63 $defaultQuerySettings->setRespectStoragePage(FALSE);
64 $this->setDefaultQuerySettings($defaultQuerySettings);
65 $this->databaseConnection = $GLOBALS['TYPO3_DB'];
66 }
67
68 /**
69 * Count all extensions
70 *
71 * @return integer
72 */
73 public function countAll() {
74 $query = $this->createQuery();
75 $query = $this->addDefaultConstraints($query);
76 return $query->execute()->count();
77 }
78
79 /**
80 * Finds all extensions
81 *
82 * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
83 */
84 public function findAll() {
85 $query = $this->createQuery();
86 $query = $this->addDefaultConstraints($query);
87 return $query->execute();
88 }
89
90 /**
91 * Find an extension by extension key ordered by version
92 *
93 * @param string $extensionKey
94 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
95 */
96 public function findByExtensionKeyOrderedByVersion($extensionKey) {
97 $query = $this->createQuery();
98 $query->matching($query->logicalAnd($query->equals('extensionKey', $extensionKey), $query->greaterThanOrEqual('reviewState', 0)));
99 $query->setOrderings(array('version' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING));
100 return $query->execute();
101 }
102
103 /**
104 * Find one extension by extension key and version
105 *
106 * @param string $extensionKey
107 * @param string $version (example: 4.3.10)
108 * @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
109 */
110 public function findOneByExtensionKeyAndVersion($extensionKey, $version) {
111 $query = $this->createQuery();
112 $query->matching($query->logicalAnd(
113 $query->equals('extensionKey', $extensionKey),
114 $query->equals('version', $version),
115 $query->greaterThanOrEqual('reviewState', 0)
116 ));
117 return $query->setLimit(1)->execute()->getFirst();
118 }
119
120 /**
121 * Find an extension by title, author name or extension key
122 * This is the function used by the TER search. It is using a
123 * scoring for the matches to sort the extension with an
124 * exact key match on top
125 *
126 * @param string $searchString The string to search for extensions
127 * @return mixed
128 */
129 public function findByTitleOrAuthorNameOrExtensionKey($searchString) {
130 $quotedSearchString = $this->databaseConnection->escapeStrForLike($this->databaseConnection->quoteStr($searchString, 'tx_extensionmanager_domain_model_extension'), 'tx_extensionmanager_domain_model_extension');
131 $quotedSearchStringForLike = '\'%' . $quotedSearchString . '%\'';
132 $quotedSearchString = '\'' . $quotedSearchString . '\'';
133 $select = 'tx_extensionmanager_domain_model_extension.*,
134 (
135 (extension_key like ' . $quotedSearchString . ') * 8 +
136 (extension_key like ' . $quotedSearchStringForLike . ') * 4 +
137 (title like ' . $quotedSearchStringForLike . ') * 2 +
138 (author_name like ' . $quotedSearchStringForLike . ')
139 ) as position';
140 $from = 'tx_extensionmanager_domain_model_extension';
141 $where = '(
142 extension_key = ' . $quotedSearchString . '
143 OR
144 extension_key LIKE ' . $quotedSearchStringForLike . '
145 OR
146 title LIKE ' . $quotedSearchStringForLike . '
147 OR
148 description LIKE ' . $quotedSearchStringForLike . '
149 OR
150 author_name LIKE ' . $quotedSearchStringForLike . '
151 )
152 AND current_version=1 AND review_state >= 0
153 HAVING position > 0';
154 $order = 'position desc';
155 $result = $this->databaseConnection->exec_SELECTgetRows($select, $from, $where, '', $order);
156 return $this->dataMapper->map('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\Extension', $result);
157 }
158
159 /**
160 * Find an extension between a certain version range ordered by version number
161 *
162 * @param string $extensionKey
163 * @param integer $lowestVersion
164 * @param integer $highestVersion
165 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
166 */
167 public function findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion = 0, $highestVersion = 0) {
168 $query = $this->createQuery();
169 $constraint = NULL;
170 if ($lowestVersion !== 0 && $highestVersion !== 0) {
171 $constraint = $query->logicalAnd($query->lessThan('integerVersion', $highestVersion), $query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
172 } elseif ($lowestVersion === 0 && $highestVersion !== 0) {
173 $constraint = $query->logicalAnd($query->lessThan('integerVersion', $highestVersion), $query->equals('extensionKey', $extensionKey));
174 } elseif ($lowestVersion !== 0 && $highestVersion === 0) {
175 $constraint = $query->logicalAnd($query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
176 } elseif ($lowestVersion === 0 && $highestVersion === 0) {
177 $constraint = $query->equals('extensionKey', $extensionKey);
178 }
179 if ($constraint) {
180 $query->matching($query->logicalAnd($constraint, $query->greaterThanOrEqual('reviewState', 0)));
181 }
182 $query->setOrderings(array(
183 'integerVersion' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
184 ));
185 return $query->execute();
186 }
187
188 /**
189 * Count extensions with a certain key between a given version range
190 *
191 * @param string $extensionKey
192 * @param integer $lowestVersion
193 * @param integer $highestVersion
194 * @return integer
195 */
196 public function countByVersionRangeAndExtensionKey($extensionKey, $lowestVersion = 0, $highestVersion = 0) {
197 return $this->findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion, $highestVersion)->count();
198 }
199
200 /**
201 * Find highest version available of an extension
202 *
203 * @param string $extensionKey
204 * @return object
205 */
206 public function findHighestAvailableVersion($extensionKey) {
207 $query = $this->createQuery();
208 $query->matching($query->logicalAnd($query->equals('extensionKey', $extensionKey), $query->greaterThanOrEqual('reviewState', 0)));
209 $query->setOrderings(array(
210 'integerVersion' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
211 ));
212 return $query->setLimit(1)->execute()->getFirst();
213 }
214
215 /**
216 * Update the current_version field after update
217 * For performance reason "native" TYPO3_DB is
218 * used here directly.
219 *
220 * @param integer $repositoryUid
221 * @return integer
222 */
223 public function insertLastVersion($repositoryUid = 1) {
224 $groupedRows = $this->databaseConnection->exec_SELECTgetRows(
225 'extension_key, max(integer_version) as maxintversion',
226 'tx_extensionmanager_domain_model_extension',
227 'repository=' . intval($repositoryUid),
228 'extension_key'
229 );
230 $extensions = count($groupedRows);
231
232 if ($extensions > 0) {
233 // set all to 0
234 $this->databaseConnection->exec_UPDATEquery(
235 'tx_extensionmanager_domain_model_extension',
236 'current_version=1 AND repository=' . intval($repositoryUid),
237 array('current_version' => 0)
238 );
239 // Find latest version of extensions and set current_version to 1 for these
240 foreach ($groupedRows as $row) {
241 $this->databaseConnection->exec_UPDATEquery(
242 'tx_extensionmanager_domain_model_extension',
243 'extension_key=' .
244 $this->databaseConnection->fullQuoteStr($row['extension_key'], 'tx_extensionmanager_domain_model_extension') .
245 ' AND integer_version=' . intval($row['maxintversion']) .
246 ' AND repository=' . intval($repositoryUid),
247 array('current_version' => 1)
248 );
249 }
250 }
251 return $extensions;
252 }
253
254 /**
255 * Adds default constraints to the query - in this case it
256 * enables us to always just search for the latest version of an extension
257 *
258 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Query $query the query to adjust
259 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Query
260 */
261 protected function addDefaultConstraints(\TYPO3\CMS\Extbase\Persistence\Generic\Query $query) {
262 if ($query->getConstraint()) {
263 $query->matching($query->logicalAnd(
264 $query->getConstraint(),
265 $query->equals('current_version', TRUE),
266 $query->greaterThanOrEqual('reviewState', 0)
267 ));
268 } else {
269 $query->matching($query->logicalAnd(
270 $query->equals('current_version', TRUE),
271 $query->greaterThanOrEqual('reviewState', 0)
272 ));
273 }
274 return $query;
275 }
276
277 }
278
279
280 ?>