[BUGFIX] Prevent current extension version in update dialog
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Controller / DownloadController.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
18
19 /**
20 * Controller for actions related to the TER download of an extension
21 *
22 * @author Susanne Moog, <typo3@susannemoog.de>
23 */
24 class DownloadController extends AbstractController {
25
26 /**
27 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
28 * @inject
29 */
30 protected $extensionRepository;
31
32 /**
33 * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
34 * @inject
35 */
36 protected $fileHandlingUtility;
37
38 /**
39 * @var \TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService
40 * @inject
41 */
42 protected $managementService;
43
44 /**
45 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
46 * @inject
47 */
48 protected $installUtility;
49
50 /**
51 * @var \TYPO3\CMS\Extensionmanager\Utility\DownloadUtility
52 * @inject
53 */
54 protected $downloadUtility;
55
56 /**
57 * Check extension dependencies
58 *
59 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
60 * @throws \Exception
61 */
62 public function checkDependenciesAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
63 $message = '';
64 $title = '';
65 $hasDependencies = FALSE;
66 $hasErrors = FALSE;
67 try {
68 $dependencyTypes = $this->managementService->getAndResolveDependencies($extension);
69 if (count($dependencyTypes) > 0) {
70 $hasDependencies = TRUE;
71 $message = '<p>' . $this->translate('downloadExtension.dependencies.headline') . '</p>';
72 foreach ($dependencyTypes as $dependencyType => $dependencies) {
73 $extensions = '';
74 foreach ($dependencies as $extensionKey => $dependency) {
75 $extensions .= htmlspecialchars($extensionKey) . '<br />';
76 }
77 $message .= $this->translate('downloadExtension.dependencies.typeHeadline',
78 array(
79 $this->translate('downloadExtension.dependencyType.' . $dependencyType),
80 $extensions
81 )
82 );
83 }
84 $title = $this->translate('downloadExtension.dependencies.resolveAutomatically');
85 }
86 $this->view->assign('dependencies', $dependencyTypes);
87 } catch (\Exception $e) {
88 $hasErrors = TRUE;
89 $title = $this->translate('downloadExtension.dependencies.errorTitle');
90 $message = $e->getMessage();
91 }
92 $this->view->assign('extension', $extension)
93 ->assign('hasDependencies', $hasDependencies)
94 ->assign('hasErrors', $hasErrors)
95 ->assign('message', $message)
96 ->assign('title', $title);
97 }
98
99 /**
100 * Install an extension from TER action
101 *
102 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
103 * @param string $downloadPath
104 */
105 public function installFromTerAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension, $downloadPath) {
106 list($result, $errorMessages) = $this->installFromTer($extension, $downloadPath);
107 $this->view
108 ->assign('result', $result)
109 ->assign('extension', $extension)
110 ->assign('unresolvedDependencies', $errorMessages);
111 }
112
113 /**
114 * Check extension dependencies with special dependencies
115 *
116 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
117 * @throws \Exception
118 */
119 public function installExtensionWithoutSystemDependencyCheckAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
120 $this->managementService->setSkipSystemDependencyCheck(TRUE);
121 $this->forward('installFromTer', NULL, NULL, array('extension' => $extension, 'downloadPath' => 'Local'));
122 }
123
124 /**
125 * Action for installing a distribution -
126 * redirects directly to configuration after installing
127 *
128 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
129 * @return void
130 */
131 public function installDistributionAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
132 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('impexp')) {
133 $this->forward('distributions', 'List');
134 }
135 list($result, $errorMessages) = $this->installFromTer($extension);
136 if ($errorMessages) {
137 foreach ($errorMessages as $extensionKey => $messages) {
138 foreach ($messages as $message) {
139 $this->addFlashMessage(
140 $message['message'],
141 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
142 'distribution.error.headline',
143 'extensionmanager',
144 array($extensionKey)
145 ),
146 \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR
147 );
148 }
149 }
150
151 // Redirect back to distributions list action
152 $this->redirect(
153 'distributions',
154 'List'
155 );
156 } else {
157 // FlashMessage that extension is installed
158 $this->addFlashMessage(
159 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('distribution.welcome.message', 'extensionmanager')
160 . ' <strong>' . $extension->getExtensionKey() . '</strong>',
161 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('distribution.welcome.headline', 'extensionmanager')
162 );
163
164 // Redirect to show action
165 $this->redirect(
166 'show',
167 'Distribution',
168 NULL,
169 array('extension' => $extension)
170 );
171 }
172 }
173
174 /**
175 * Update an extension. Makes no sanity check but directly searches highest
176 * available version from TER and updates. Update check is done by the list
177 * already. This method should only be called if we are sure that there is
178 * an update.
179 *
180 * @return string
181 */
182 protected function updateExtensionAction() {
183 $extensionKey = $this->request->getArgument('extension');
184 $highestTerVersionExtension = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
185 try {
186 $this->managementService->downloadMainExtension($highestTerVersionExtension);
187 $this->addFlashMessage(
188 htmlspecialchars($this->translate('extensionList.updateFlashMessage.body', array($extensionKey))),
189 $this->translate('extensionList.updateFlashMessage.title')
190 );
191 } catch (\Exception $e) {
192 $this->addFlashMessage(htmlspecialchars($e->getMessage()), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
193 }
194
195 return '';
196 }
197
198 /**
199 * Show update comments for extensions that can be updated.
200 * Fetches update comments for all versions between the current
201 * installed and the highest version.
202 *
203 * @return void
204 */
205 protected function updateCommentForUpdatableVersionsAction() {
206 $extensionKey = $this->request->getArgument('extension');
207 $version = $this->request->getArgument('integerVersion');
208 $updateComments = array();
209 /** @var Extension[] $updatableVersions */
210 $updatableVersions = $this->extensionRepository->findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $version, 0, FALSE);
211 foreach ($updatableVersions as $updatableVersion) {
212 $updateComments[$updatableVersion->getVersion()] = $updatableVersion->getUpdateComment();
213 }
214 $this->view->assign('updateComments', $updateComments)->assign('extensionKey', $extensionKey);
215 }
216
217 /**
218 * Install an action from TER
219 * Downloads the extension, resolves dependencies and installs it
220 *
221 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
222 * @param string $downloadPath
223 * @return array
224 */
225 protected function installFromTer(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension, $downloadPath = 'Local') {
226 $result = FALSE;
227 $errorMessages = array();
228 try {
229 $this->downloadUtility->setDownloadPath($downloadPath);
230 if (($result = $this->managementService->installExtension($extension)) === FALSE) {
231 $errorMessages = $this->managementService->getDependencyErrors();
232 }
233 } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
234 $errorMessages = array(
235 $extension->getExtensionKey() => array(
236 array(
237 'code' => $e->getCode(),
238 'message' => $e->getMessage(),
239 )
240 ),
241 );
242 }
243
244 return array($result, $errorMessages);
245 }
246
247 }