[TASK] Re-implement extension status for reports module
[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 // Hint: This method must not filter out insecure extensions, if needed,
113 // it should be done on a different level, or with a helper method.
114 $query->matching($query->logicalAnd(
115 $query->equals('extensionKey', $extensionKey),
116 $query->equals('version', $version)
117 ));
118 return $query->setLimit(1)->execute()->getFirst();
119 }
120
121 /**
122 * Find an extension by title, author name or extension key
123 * This is the function used by the TER search. It is using a
124 * scoring for the matches to sort the extension with an
125 * exact key match on top
126 *
127 * @param string $searchString The string to search for extensions
128 * @return mixed
129 */
130 public function findByTitleOrAuthorNameOrExtensionKey($searchString) {
131 $quotedSearchString = $this->databaseConnection->escapeStrForLike($this->databaseConnection->quoteStr($searchString, 'tx_extensionmanager_domain_model_extension'), 'tx_extensionmanager_domain_model_extension');
132 $quotedSearchStringForLike = '\'%' . $quotedSearchString . '%\'';
133 $quotedSearchString = '\'' . $quotedSearchString . '\'';
134 $select = 'tx_extensionmanager_domain_model_extension.*,
135 (
136 (extension_key like ' . $quotedSearchString . ') * 8 +
137 (extension_key like ' . $quotedSearchStringForLike . ') * 4 +
138 (title like ' . $quotedSearchStringForLike . ') * 2 +
139 (author_name like ' . $quotedSearchStringForLike . ')
140 ) as position';
141 $from = 'tx_extensionmanager_domain_model_extension';
142 $where = '(
143 extension_key = ' . $quotedSearchString . '
144 OR
145 extension_key LIKE ' . $quotedSearchStringForLike . '
146 OR
147 title LIKE ' . $quotedSearchStringForLike . '
148 OR
149 description LIKE ' . $quotedSearchStringForLike . '
150 OR
151 author_name LIKE ' . $quotedSearchStringForLike . '
152 )
153 AND current_version=1 AND review_state >= 0
154 HAVING position > 0';
155 $order = 'position desc';
156 $result = $this->databaseConnection->exec_SELECTgetRows($select, $from, $where, '', $order);
157 return $this->dataMapper->map('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\Extension', $result);
158 }
159
160 /**
161 * Find an extension between a certain version range ordered by version number
162 *
163 * @param string $extensionKey
164 * @param integer $lowestVersion
165 * @param integer $highestVersion
166 * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface
167 */
168 public function findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion = 0, $highestVersion = 0) {
169 $query = $this->createQuery();
170 $constraint = NULL;
171 if ($lowestVersion !== 0 && $highestVersion !== 0) {
172 $constraint = $query->logicalAnd($query->lessThan('integerVersion', $highestVersion), $query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
173 } elseif ($lowestVersion === 0 && $highestVersion !== 0) {
174 $constraint = $query->logicalAnd($query->lessThan('integerVersion', $highestVersion), $query->equals('extensionKey', $extensionKey));
175 } elseif ($lowestVersion !== 0 && $highestVersion === 0) {
176 $constraint = $query->logicalAnd($query->greaterThan('integerVersion', $lowestVersion), $query->equals('extensionKey', $extensionKey));
177 } elseif ($lowestVersion === 0 && $highestVersion === 0) {
178 $constraint = $query->equals('extensionKey', $extensionKey);
179 }
180 if ($constraint) {
181 $query->matching($query->logicalAnd($constraint, $query->greaterThanOrEqual('reviewState', 0)));
182 }
183 $query->setOrderings(array(
184 'integerVersion' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
185 ));
186 return $query->execute();
187 }
188
189 /**
190 * Count extensions with a certain key between a given version range
191 *
192 * @param string $extensionKey
193 * @param integer $lowestVersion
194 * @param integer $highestVersion
195 * @return integer
196 */
197 public function countByVersionRangeAndExtensionKey($extensionKey, $lowestVersion = 0, $highestVersion = 0) {
198 return $this->findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $lowestVersion, $highestVersion)->count();
199 }
200
201 /**
202 * Find highest version available of an extension
203 *
204 * @param string $extensionKey
205 * @return object
206 */
207 public function findHighestAvailableVersion($extensionKey) {
208 $query = $this->createQuery();
209 $query->matching($query->logicalAnd($query->equals('extensionKey', $extensionKey), $query->greaterThanOrEqual('reviewState', 0)));
210 $query->setOrderings(array(
211 'integerVersion' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
212 ));
213 return $query->setLimit(1)->execute()->getFirst();
214 }
215
216 /**
217 * Update the current_version field after update
218 * For performance reason "native" TYPO3_DB is
219 * used here directly.
220 *
221 * @param integer $repositoryUid
222 * @return integer
223 */
224 public function insertLastVersion($repositoryUid = 1) {
225 $groupedRows = $this->databaseConnection->exec_SELECTgetRows(
226 'extension_key, max(integer_version) as maxintversion',
227 'tx_extensionmanager_domain_model_extension',
228 'repository=' . intval($repositoryUid),
229 'extension_key'
230 );
231 $extensions = count($groupedRows);
232
233 if ($extensions > 0) {
234 // set all to 0
235 $this->databaseConnection->exec_UPDATEquery(
236 'tx_extensionmanager_domain_model_extension',
237 'current_version=1 AND repository=' . intval($repositoryUid),
238 array('current_version' => 0)
239 );
240 // Find latest version of extensions and set current_version to 1 for these
241 foreach ($groupedRows as $row) {
242 $this->databaseConnection->exec_UPDATEquery(
243 'tx_extensionmanager_domain_model_extension',
244 'extension_key=' .
245 $this->databaseConnection->fullQuoteStr($row['extension_key'], 'tx_extensionmanager_domain_model_extension') .
246 ' AND integer_version=' . intval($row['maxintversion']) .
247 ' AND repository=' . intval($repositoryUid),
248 array('current_version' => 1)
249 );
250 }
251 }
252 return $extensions;
253 }
254
255 /**
256 * Adds default constraints to the query - in this case it
257 * enables us to always just search for the latest version of an extension
258 *
259 * @param \TYPO3\CMS\Extbase\Persistence\Generic\Query $query the query to adjust
260 * @return \TYPO3\CMS\Extbase\Persistence\Generic\Query
261 */
262 protected function addDefaultConstraints(\TYPO3\CMS\Extbase\Persistence\Generic\Query $query) {
263 if ($query->getConstraint()) {
264 $query->matching($query->logicalAnd(
265 $query->getConstraint(),
266 $query->equals('current_version', TRUE),
267 $query->greaterThanOrEqual('reviewState', 0)
268 ));
269 } else {
270 $query->matching($query->logicalAnd(
271 $query->equals('current_version', TRUE),
272 $query->greaterThanOrEqual('reviewState', 0)
273 ));
274 }
275 return $query;
276 }
277
278 }
279
280
281 ?>