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