11cad4832698b4192ff503d5414319a5f2fbca68
[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 * @var \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility
58 * @inject
59 */
60 protected $configurationUtility;
61
62 /**
63 * Check extension dependencies
64 *
65 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
66 * @throws \Exception
67 */
68 public function checkDependenciesAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
69 $message = '';
70 $title = '';
71 $hasDependencies = FALSE;
72 $hasErrors = FALSE;
73 try {
74 $dependencyTypes = $this->managementService->getAndResolveDependencies($extension);
75 if (count($dependencyTypes) > 0) {
76 $hasDependencies = TRUE;
77 $message = '<p>' . $this->translate('downloadExtension.dependencies.headline') . '</p>';
78 foreach ($dependencyTypes as $dependencyType => $dependencies) {
79 $extensions = '';
80 foreach ($dependencies as $extensionKey => $dependency) {
81 $extensions .= htmlspecialchars($extensionKey) . '<br />';
82 }
83 $message .= $this->translate('downloadExtension.dependencies.typeHeadline',
84 array(
85 $this->translate('downloadExtension.dependencyType.' . $dependencyType),
86 $extensions
87 )
88 );
89 }
90 $title = $this->translate('downloadExtension.dependencies.resolveAutomatically');
91 }
92 $this->view->assign('dependencies', $dependencyTypes);
93 } catch (\Exception $e) {
94 $hasErrors = TRUE;
95 $title = $this->translate('downloadExtension.dependencies.errorTitle');
96 $message = $e->getMessage();
97 }
98 $this->view->assign('extension', $extension)
99 ->assign('hasDependencies', $hasDependencies)
100 ->assign('hasErrors', $hasErrors)
101 ->assign('message', $message)
102 ->assign('title', $title);
103 }
104
105 /**
106 * Install an extension from TER action
107 *
108 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
109 * @param string $downloadPath
110 */
111 public function installFromTerAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension, $downloadPath) {
112 list($result, $errorMessages) = $this->installFromTer($extension, $downloadPath);
113 $emConfiguration = $this->configurationUtility->getCurrentConfiguration('extensionmanager');
114 $this->view
115 ->assign('result', $result)
116 ->assign('extension', $extension)
117 ->assign('installationTypeLanguageKey', (bool)$emConfiguration['automaticInstallation']['value'] ? '' : '.downloadOnly')
118 ->assign('unresolvedDependencies', $errorMessages);
119 }
120
121 /**
122 * Check extension dependencies with special dependencies
123 *
124 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
125 * @throws \Exception
126 */
127 public function installExtensionWithoutSystemDependencyCheckAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
128 $this->managementService->setSkipSystemDependencyCheck(TRUE);
129 $this->forward('installFromTer', NULL, NULL, array('extension' => $extension, 'downloadPath' => 'Local'));
130 }
131
132 /**
133 * Action for installing a distribution -
134 * redirects directly to configuration after installing
135 *
136 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
137 * @return void
138 */
139 public function installDistributionAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
140 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('impexp')) {
141 $this->forward('distributions', 'List');
142 }
143 list($result, $errorMessages) = $this->installFromTer($extension);
144 if ($errorMessages) {
145 foreach ($errorMessages as $extensionKey => $messages) {
146 foreach ($messages as $message) {
147 $this->addFlashMessage(
148 $message['message'],
149 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
150 'distribution.error.headline',
151 'extensionmanager',
152 array($extensionKey)
153 ),
154 \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR
155 );
156 }
157 }
158
159 // Redirect back to distributions list action
160 $this->redirect(
161 'distributions',
162 'List'
163 );
164 } else {
165 // FlashMessage that extension is installed
166 $this->addFlashMessage(
167 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('distribution.welcome.message', 'extensionmanager')
168 . ' <strong>' . $extension->getExtensionKey() . '</strong>',
169 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('distribution.welcome.headline', 'extensionmanager')
170 );
171
172 // Redirect to show action
173 $this->redirect(
174 'show',
175 'Distribution',
176 NULL,
177 array('extension' => $extension)
178 );
179 }
180 }
181
182 /**
183 * Update an extension. Makes no sanity check but directly searches highest
184 * available version from TER and updates. Update check is done by the list
185 * already. This method should only be called if we are sure that there is
186 * an update.
187 *
188 * @return string
189 */
190 protected function updateExtensionAction() {
191 $extensionKey = $this->request->getArgument('extension');
192 $version = $this->request->getArgument('version');
193 $extension = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionKey, $version);
194 if (!$extension instanceof Extension) {
195 $extension = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
196 }
197 try {
198 $this->managementService->downloadMainExtension($extension);
199 $this->addFlashMessage(
200 htmlspecialchars($this->translate('extensionList.updateFlashMessage.body', array($extensionKey))),
201 $this->translate('extensionList.updateFlashMessage.title')
202 );
203 } catch (\Exception $e) {
204 $this->addFlashMessage(htmlspecialchars($e->getMessage()), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
205 }
206
207 return '';
208 }
209
210 /**
211 * Show update comments for extensions that can be updated.
212 * Fetches update comments for all versions between the current
213 * installed and the highest version.
214 *
215 * @return void
216 */
217 protected function updateCommentForUpdatableVersionsAction() {
218 $extensionKey = $this->request->getArgument('extension');
219 $version = $this->request->getArgument('integerVersion');
220 $updateComments = array();
221 /** @var Extension[] $updatableVersions */
222 $updatableVersions = $this->extensionRepository->findByVersionRangeAndExtensionKeyOrderedByVersion($extensionKey, $version, 0, FALSE);
223 foreach ($updatableVersions as $updatableVersion) {
224 $updateComments[$updatableVersion->getVersion()] = $updatableVersion->getUpdateComment();
225 }
226 $this->view->assign('updateComments', $updateComments)->assign('extensionKey', $extensionKey);
227 }
228
229 /**
230 * Install an action from TER
231 * Downloads the extension, resolves dependencies and installs it
232 *
233 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
234 * @param string $downloadPath
235 * @return array
236 */
237 protected function installFromTer(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension, $downloadPath = 'Local') {
238 $result = FALSE;
239 $errorMessages = array();
240 try {
241 $this->downloadUtility->setDownloadPath($downloadPath);
242 $this->managementService->setAutomaticInstallationEnabled($this->configurationUtility->getCurrentConfiguration('extensionmanager')['automaticInstallation']['value']);
243 if (($result = $this->managementService->installExtension($extension)) === FALSE) {
244 $errorMessages = $this->managementService->getDependencyErrors();
245 }
246 } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
247 $errorMessages = array(
248 $extension->getExtensionKey() => array(
249 array(
250 'code' => $e->getCode(),
251 'message' => $e->getMessage(),
252 )
253 ),
254 );
255 }
256
257 return array($result, $errorMessages);
258 }
259
260 }