[BUGFIX] Allow overwrite when uploading an existing extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Controller / DownloadController.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Controller;
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 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Controller for actions related to the TER download of an extension
31 *
32 * @author Susanne Moog, <typo3@susannemoog.de>
33 * @package Extension Manager
34 * @subpackage Controller
35 */
36 class DownloadController extends \TYPO3\CMS\Extensionmanager\Controller\AbstractController {
37
38 /**
39 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
40 */
41 protected $extensionRepository;
42
43 /**
44 * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
45 */
46 protected $fileHandlingUtility;
47
48 /**
49 * @var \TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService
50 */
51 protected $managementService;
52
53 /**
54 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
55 */
56 protected $installUtility;
57
58 /**
59 * @var \TYPO3\CMS\Extensionmanager\Utility\DownloadUtility
60 */
61 protected $downloadUtility;
62
63 /**
64 * @param \TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility
65 * @return void
66 */
67 public function injectInstallUtility(\TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility) {
68 $this->installUtility = $installUtility;
69 }
70
71 /**
72 * Dependency injection of the Extension Repository
73 *
74 * @param \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository
75 * @return void
76 */
77 public function injectExtensionRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository) {
78 $this->extensionRepository = $extensionRepository;
79 }
80
81 /**
82 * @param \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility
83 * @return void
84 */
85 public function injectFileHandlingUtility(\TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility) {
86 $this->fileHandlingUtility = $fileHandlingUtility;
87 }
88
89 /**
90 * @param \TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService $managementService
91 * @return void
92 */
93 public function injectManagementService(\TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService $managementService) {
94 $this->managementService = $managementService;
95 }
96
97 /**
98 * @param \TYPO3\CMS\Extensionmanager\Utility\DownloadUtility $downloadUtility
99 * @return void
100 */
101 public function injectDownloadUtility(\TYPO3\CMS\Extensionmanager\Utility\DownloadUtility $downloadUtility) {
102 $this->downloadUtility = $downloadUtility;
103 }
104
105 /**
106 * Check extension dependencies
107 *
108 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
109 * @throws \Exception
110 */
111 public function checkDependenciesAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
112 $message = '';
113 $title = '';
114 $hasDependiencies = FALSE;
115 $hasErrors = FALSE;
116 try {
117 $dependencyTypes = $this->managementService->getAndResolveDependencies($extension);
118 if (count($dependencyTypes) > 0) {
119 $hasDependiencies = TRUE;
120 $message = $this->translate('downloadExtension.dependencies.headline');
121 foreach ($dependencyTypes as $dependencyType => $dependencies) {
122 $extensions = '';
123 foreach ($dependencies as $extensionKey => $dependency) {
124 $extensions .= htmlspecialchars($extensionKey) . '<br />';
125 }
126 $message .= $this->translate('downloadExtension.dependencies.typeHeadline',
127 array(
128 $this->translate('downloadExtension.dependencyType.' . $dependencyType),
129 $extensions
130 )
131 );
132 }
133 $title = $this->translate('downloadExtension.dependencies.reloveAutomatically');
134 }
135 $this->view->assign('dependencies', $dependencyTypes);
136 } catch (\Exception $e) {
137 $hasErrors = TRUE;
138 $title = $this->translate('downloadExtension.dependencies.errorTitle');
139 $message = $e->getMessage();
140 }
141 $this->view->assign('extension', $extension)
142 ->assign('hasDependencies', $hasDependiencies)
143 ->assign('hasErrors', $hasErrors)
144 ->assign('message', $message)
145 ->assign('title', $title);
146 }
147
148 /**
149 * Install an extension from TER
150 *
151 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
152 * @param string $downloadPath
153 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
154 */
155 public function installFromTerAction(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension, $downloadPath) {
156 $result = FALSE;
157 $errorMessage = '';
158 try {
159 $this->downloadUtility->setDownloadPath($downloadPath);
160 $this->prepareExtensionForImport($extension);
161 $result = $this->managementService->resolveDependenciesAndInstall($extension);
162 } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
163 $errorMessage = $e->getMessage();
164 }
165 $this->view->assign('result', $result)->assign('extension', $extension)->assign('errorMessage', $errorMessage);
166 }
167
168 /**
169 * Prepares an extension for import from TER
170 * Uninstalls the extension if it is already loaded (case: update)
171 * and reloads the caches.
172 *
173 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
174 * @return void
175 */
176 protected function prepareExtensionForImport(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension) {
177 if (\TYPO3\CMS\Core\Extension\ExtensionManager::isLoaded($extension->getExtensionKey())) {
178 \TYPO3\CMS\Core\Extension\ExtensionManager::unloadExtension($extension->getExtensionKey());
179 $this->installUtility->reloadCaches();
180 }
181 }
182
183 /**
184 * Update an extension. Makes no sanity check but directly searches highest
185 * available version from TER and updates. Update check is done by the list
186 * already. This method should only be called if we are sure that there is
187 * an update.
188 *
189 * @return void
190 */
191 protected function updateExtensionAction() {
192 $extensionKey = $this->request->getArgument('extension');
193 /** @var $highestTerVersionExtension \TYPO3\CMS\Extensionmanager\Domain\Model\Extension */
194 $highestTerVersionExtension = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
195 $this->prepareExtensionForImport($highestTerVersionExtension);
196 $result = $this->managementService->resolveDependenciesAndInstall($highestTerVersionExtension);
197 $this->view->assign('result', $result)->assign('extension', $highestTerVersionExtension);
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 $updatableVersion \TYPO3\CMS\Extensionmanager\Domain\Model\Extension */
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
221 ?>