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