[BUGFIX] Hide unsafe extensions in EM TER list
[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 description LIKE ' . $quotedSearchStringForLike . '
147 OR
148 title LIKE ' . $quotedSearchStringForLike . '
149 )
150 AND current_version=1 AND review_state >= 0
151 HAVING position > 0';
152 $order = 'position desc';
153 $result = $this->databaseConnection->exec_SELECTgetRows($select, $from, $where, '', $order);
154 return $this->dataMapper->map('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\Extension', $result);
155 }
156
157 /**
158 * Find an extension between a certain version range ordered by version number
159 *
160 * @param string $extensionKey
161 * @param integer $lowestVersion
162 * @param integer $highestVersion
163 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
164 */
165 public function findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion = 0, $highestVersion = 0) {
166 $query = $this->createQuery();
167 $constraint = NULL;
168 if ($lowestVersion !== 0 && $highestVersion !== 0) {
169 $constraint = $query->logicalAnd($query->lessThan('integerVersion', $highestVersion), $query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
170 } elseif ($lowestVersion === 0 && $highestVersion !== 0) {
171 $constraint = $query->logicalAnd($query->lessThan('integerVersion', $highestVersion), $query->equals('extensionKey', $extensionKey));
172 } elseif ($lowestVersion !== 0 && $highestVersion === 0) {
173 $constraint = $query->logicalAnd($query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
174 } elseif ($lowestVersion === 0 && $highestVersion === 0) {
175 $constraint = $query->equals('extensionKey', $extensionKey);
176 }
177 if ($constraint) {
178 $query->matching($query->logicalAnd($constraint, $query->greaterThanOrEqual('reviewState', 0)));
179 }
180 $query->setOrderings(array(
181 'integerVersion' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
182 ));
183 return $query->execute();
184 }
185
186 /**
187 * Count extensions with a certain key between a given version range
188 *
189 * @param string $extensionKey
190 * @param integer $lowestVersion
191 * @param integer $highestVersion
192 * @return integer
193 */
194 public function countByVersionRangeAndExtensionKey($extensionKey, $lowestVersion = 0, $highestVersion = 0) {
195 return $this->findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion, $highestVersion)->count();
196 }
197
198 /**
199 * Find highest version available of an extension
200 *
201 * @param string $extensionKey
202 * @return object
203 */
204 public function findHighestAvailableVersion($extensionKey) {
205 $query = $this->createQuery();
206 $query->matching($query->logicalAnd($query->equals('extensionKey', $extensionKey), $query->greaterThanOrEqual('reviewState', 0)));
207 $query->setOrderings(array(
208 'integerVersion' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
209 ));
210 return $query->setLimit(1)->execute()->getFirst();
211 }
212
213 /**
214 * Update the current_version field after update
215 * For performance reason "native" TYPO3_DB is
216 * used here directly.
217 *
218 * @param integer $repositoryUid
219 * @return integer
220 */
221 public function insertLastVersion($repositoryUid = 1) {
222 $groupedRows = $this->databaseConnection->exec_SELECTgetRows(
223 'extension_key, max(integer_version) as maxintversion',
224 'tx_extensionmanager_domain_model_extension',
225 'repository=' . intval($repositoryUid),
226 'extension_key'
227 );
228 $extensions = count($groupedRows);
229
230 if ($extensions > 0) {
231 // set all to 0
232 $this->databaseConnection->exec_UPDATEquery(
233 'tx_extensionmanager_domain_model_extension',
234 'current_version=1 AND repository=' . intval($repositoryUid),
235 array('current_version' => 0)
236 );
237 // Find latest version of extensions and set current_version to 1 for these
238 foreach ($groupedRows as $row) {
239 $this->databaseConnection->exec_UPDATEquery(
240 'tx_extensionmanager_domain_model_extension',
241 'extension_key=' .
242 $this->databaseConnection->fullQuoteStr($row['extension_key'], 'tx_extensionmanager_domain_model_extension') .
243 ' AND integer_version=' . intval($row['maxintversion']) .
244 ' AND repository=' . intval($repositoryUid),
245 array('current_version' => 1)
246 );
247 }
248 }
249 return $extensions;
250 }
251
252 /**
253 * Adds default constraints to the query - in this case it
254 * enables us to always just search for the latest version of an extension
255 *
256 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Query $query the query to adjust
257 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Query
258 */
259 protected function addDefaultConstraints(\TYPO3\CMS\Extbase\Persistence\Generic\Query $query) {
260 if ($query->getConstraint()) {
261 $query->matching($query->logicalAnd(
262 $query->getConstraint(),
263 $query->equals('current_version', TRUE),
264 $query->greaterThanOrEqual('reviewState', 0)
265 ));
266 } else {
267 $query->matching($query->logicalAnd(
268 $query->equals('current_version', TRUE),
269 $query->greaterThanOrEqual('reviewState', 0)
270 ));
271 }
272 return $query;
273 }
274
275 }
276
277
278 ?>