[BUGFIX] Create upload folder on extension install
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / InstallUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Susanne Moog <susanne.moog@typo3.org>
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 * Extension Manager Install Utility
31 *
32 * @author Susanne Moog <susanne.moog@typo3.org>
33 * @package Extension Manager
34 * @subpackage Utility
35 */
36 class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface {
37
38 /**
39 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
40 */
41 public $objectManager;
42
43 /**
44 * @var \TYPO3\CMS\Install\Sql\SchemaMigrator
45 */
46 public $installToolSqlParser;
47
48 /**
49 * @var \TYPO3\CMS\Extensionmanager\Utility\DependencyUtility
50 */
51 protected $dependencyUtility;
52
53 /**
54 * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
55 */
56 protected $fileHandlingUtility;
57
58 /**
59 * @var \TYPO3\CMS\Extensionmanager\Utility\ListUtility
60 */
61 protected $listUtility;
62
63 /**
64 * @var \TYPO3\CMS\Extensionmanager\Utility\DatabaseUtility
65 */
66 protected $databaseUtility;
67
68 /**
69 * @var \TYPO3\CMS\Core\Configuration\ConfigurationManager
70 */
71 protected $configurationManager;
72
73 /**
74 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
75 */
76 public $extensionRepository;
77
78 /**
79 * @param \TYPO3\CMS\Extensionmanager\Utility\ListUtility $listUtility
80 * @return void
81 */
82 public function injectListUtility(\TYPO3\CMS\Extensionmanager\Utility\ListUtility $listUtility) {
83 $this->listUtility = $listUtility;
84 }
85
86 /**
87 * @param \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $filehandlingUtility
88 * @return void
89 */
90 public function injectFileHandlingUtility(\TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility) {
91 $this->fileHandlingUtility = $fileHandlingUtility;
92 }
93
94 /**
95 * @param \TYPO3\CMS\Extensionmanager\Utility\DependencyUtility $dependencyUtility
96 * @return void
97 */
98 public function injectDependencyUtility(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility $dependencyUtility) {
99 $this->dependencyUtility = $dependencyUtility;
100 }
101
102 /**
103 * @param \TYPO3\CMS\Extensionmanager\Utility\DatabaseUtility $databaseUtility
104 * @return void
105 */
106 public function injectDatabaseUtility(\TYPO3\CMS\Extensionmanager\Utility\DatabaseUtility $databaseUtility) {
107 $this->databaseUtility = $databaseUtility;
108 }
109
110 /**
111 * Inject configuration manager
112 *
113 * @param \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager
114 * @return void
115 */
116 public function injectConfigurationManager(\TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager) {
117 $this->configurationManager = $configurationManager;
118 }
119
120 /**
121 * Inject emConfUtility
122 *
123 * @param \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository
124 * @return void
125 */
126 public function injectExtensionRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository) {
127 $this->extensionRepository = $extensionRepository;
128 }
129
130 /**
131 * __construct
132 */
133 public function __construct() {
134 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
135 /** @var $installToolSqlParser \TYPO3\CMS\Install\Sql\SchemaMigrator */
136 $this->installToolSqlParser = $this->objectManager->get('TYPO3\\CMS\\Install\\Sql\\SchemaMigrator');
137 $this->dependencyUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
138 }
139
140 /**
141 * Helper function to uninstall an extension
142 *
143 * @param string $extensionKey
144 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
145 * @return void
146 */
147 public function uninstall($extensionKey) {
148 $dependentExtensions = $this->dependencyUtility->findInstalledExtensionsThatDependOnMe($extensionKey);
149 if (is_array($dependentExtensions) && count($dependentExtensions) > 0) {
150 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Cannot deactivate extension ' . $extensionKey . ' - The extension(s) ' . implode(',', $dependentExtensions) . ' depend on it', 1342554622);
151 } else {
152 $this->unloadExtension($extensionKey);
153 }
154 }
155
156 /**
157 * Wrapper function for unloading extensions
158 *
159 * @param string $extensionKey
160 */
161 protected function unloadExtension($extensionKey) {
162 \TYPO3\CMS\Core\Extension\ExtensionManager::unloadExtension($extensionKey);
163 }
164
165 /**
166 * Helper function to install an extension
167 * also processes db updates and clears the cache if the extension asks for it
168 *
169 * @param string $extensionKey
170 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
171 * @return void
172 */
173 public function install($extensionKey) {
174 $extension = $this->enrichExtensionWithDetails($extensionKey);
175 $this->processDatabaseUpdates($extension);
176 $this->ensureConfiguredDirectoriesExist($extension);
177 if ($extension['clearcacheonload']) {
178 $GLOBALS['typo3CacheManager']->flushCaches();
179 }
180 $this->loadExtension($extensionKey);
181 $this->reloadCaches();
182 $this->saveDefaultConfiguration($extension['key']);
183 }
184
185 /**
186 * Wrapper function for loading extensions
187 *
188 * @param string $extensionKey
189 * @return void
190 */
191 protected function loadExtension($extensionKey) {
192 \TYPO3\CMS\Core\Extension\ExtensionManager::loadExtension($extensionKey);
193 }
194
195 /**
196 * Fetch additional information for an extension key
197 *
198 * @param string $extensionKey
199 * @access private
200 * @return array
201 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
202 */
203 public function enrichExtensionWithDetails($extensionKey) {
204 $availableExtensions = $this->listUtility->getAvailableExtensions();
205 if (isset($availableExtensions[$extensionKey])) {
206 $extension = $availableExtensions[$extensionKey];
207 } else {
208 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Extension ' . $extensionKey . ' is not available', 1342864081);
209 }
210 $availableAndInstalledExtensions = $this->listUtility->enrichExtensionsWithEmConfAndTerInformation(array($extensionKey => $extension));
211 return $availableAndInstalledExtensions[$extensionKey];
212 }
213
214 /**
215 * Creates directories as requested in ext_emconf.php
216 *
217 * @param array $extension
218 */
219 protected function ensureConfiguredDirectoriesExist(array $extension) {
220 $this->fileHandlingUtility->ensureConfiguredDirectoriesExist($extension);
221 }
222
223 /**
224 * Gets the content of the ext_tables.sql and ext_tables_static+adt.sql files
225 * Additionally adds the table definitions for the cache tables
226 *
227 * @param array $extension
228 */
229 public function processDatabaseUpdates(array $extension) {
230 $extTablesSqlFile = PATH_site . $extension['siteRelPath'] . '/ext_tables.sql';
231 if (file_exists($extTablesSqlFile)) {
232 $extTablesSqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extTablesSqlFile);
233 // @TODO: This should probably moved to TYPO3\CMS\Core\Cache\Cache->getDatabaseTableDefinitions ?!
234 $GLOBALS['typo3CacheManager']->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
235 $extTablesSqlContent .= \TYPO3\CMS\Core\Cache\Cache::getDatabaseTableDefinitions();
236 $this->updateDbWithExtTablesSql($extTablesSqlContent);
237 }
238 $extTablesStaticSqlFile = PATH_site . $extension['siteRelPath'] . '/ext_tables_static+adt.sql';
239 if (file_exists($extTablesStaticSqlFile)) {
240 $extTablesStaticSqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extTablesStaticSqlFile);
241 $this->importStaticSql($extTablesStaticSqlContent);
242 }
243 }
244
245 /**
246 * Reload Cache files and Typo3LoadedExtensions
247 *
248 * @return void
249 */
250 public function reloadCaches() {
251 \TYPO3\CMS\Core\Extension\ExtensionManager::removeCacheFiles();
252 // Set new extlist / extlistArray for extension load changes at runtime
253 $localConfiguration = $this->configurationManager->getLocalConfiguration();
254 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'] = $localConfiguration['EXT']['extList'];
255 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extListArray'] = $localConfiguration['EXT']['extListArray'];
256 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->populateTypo3LoadedExtGlobal(FALSE)->loadAdditionalConfigurationFromExtensions(FALSE);
257 }
258
259 /**
260 * Save default configuration of an extension
261 *
262 * @param string $extensionKey
263 * @return void
264 */
265 protected function saveDefaultConfiguration($extensionKey) {
266 /** @var $configUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
267 $configUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
268 $configUtility->saveDefaultConfiguration($extensionKey);
269 }
270
271 /**
272 * Update database / process db updates from ext_tables
273 *
274 * @param string $rawDefinitions The raw SQL statements from ext_tables.sql
275 * @return void
276 */
277 public function updateDbWithExtTablesSql($rawDefinitions) {
278 $fieldDefinitionsFromFile = $this->installToolSqlParser->getFieldDefinitions_fileContent($rawDefinitions);
279 if (count($fieldDefinitionsFromFile)) {
280 $fieldDefinitionsFromCurrentDatabase = $this->installToolSqlParser->getFieldDefinitions_database();
281 $diff = $this->installToolSqlParser->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase);
282 $updateStatements = $this->installToolSqlParser->getUpdateSuggestions($diff);
283 foreach ((array) $updateStatements['add'] as $string) {
284 $GLOBALS['TYPO3_DB']->admin_query($string);
285 }
286 foreach ((array) $updateStatements['change'] as $string) {
287 $GLOBALS['TYPO3_DB']->admin_query($string);
288 }
289 foreach ((array) $updateStatements['create_table'] as $string) {
290 $GLOBALS['TYPO3_DB']->admin_query($string);
291 }
292 }
293 }
294
295 /**
296 * Import static SQL data (normally used for ext_tables_static+adt.sql)
297 *
298 * @param string $rawDefinitions
299 * @return void
300 */
301 public function importStaticSql($rawDefinitions) {
302 $statements = $this->installToolSqlParser->getStatementarray($rawDefinitions, 1);
303 list($statementsPerTable, $insertCount) = $this->installToolSqlParser->getCreateTables($statements, 1);
304 // Traverse the tables
305 foreach ($statementsPerTable as $table => $query) {
306 $GLOBALS['TYPO3_DB']->admin_query('DROP TABLE IF EXISTS ' . $table);
307 $GLOBALS['TYPO3_DB']->admin_query($query);
308 if ($insertCount[$table]) {
309 $insertStatements = $this->installToolSqlParser->getTableInsertStatements($statements, $table);
310 foreach ($insertStatements as $statement) {
311 $GLOBALS['TYPO3_DB']->admin_query($statement);
312 }
313 }
314 }
315 }
316
317 /**
318 * Writes the TSstyleconf values to "localconf.php"
319 * Removes the temp_CACHED* files before return.
320 *
321 * @param string $extensionKey Extension key
322 * @param array $newConfiguration Configuration array to write back
323 * @return void
324 */
325 public function writeExtensionTypoScriptStyleConfigurationToLocalconf($extensionKey, $newConfiguration) {
326 $this->configurationManager->setLocalConfigurationValueByPath('EXT/extConf/' . $extensionKey, serialize($newConfiguration));
327 \TYPO3\CMS\Core\Extension\ExtensionManager::removeCacheFiles();
328 }
329
330 /**
331 * Remove an extension (delete the directory)
332 *
333 * @param string $extension
334 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
335 * @return void
336 */
337 public function removeExtension($extension) {
338 $absolutePath = $this->fileHandlingUtility->getAbsoluteExtensionPath($extension);
339 if ($this->fileHandlingUtility->isValidExtensionPath($absolutePath)) {
340 $this->fileHandlingUtility->removeDirectory($absolutePath);
341 } else {
342 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('No valid extension path given.', 1342875724);
343 }
344 }
345
346 /**
347 * Get the data dump for an extension
348 *
349 * @param string $extension
350 * @return array
351 */
352 public function getExtensionSqlDataDump($extension) {
353 $extension = $this->enrichExtensionWithDetails($extension);
354 $filePrefix = PATH_site . $extension['siteRelPath'];
355 $sqlData['extTables'] = $this->getSqlDataDumpForFile($filePrefix . '/ext_tables.sql');
356 $sqlData['staticSql'] = $this->getSqlDataDumpForFile($filePrefix . '/ext_tables_static+adt.sql');
357 return $sqlData;
358 }
359
360 /**
361 * Gets the sql data dump for a specific sql file (for example ext_tables.sql)
362 *
363 * @param string $sqlFile
364 * @return string
365 */
366 protected function getSqlDataDumpForFile($sqlFile) {
367 $sqlData = '';
368 if (file_exists($sqlFile)) {
369 $sqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($sqlFile);
370 $fieldDefinitions = $this->installToolSqlParser->getFieldDefinitions_fileContent($sqlContent);
371 $sqlData = $this->databaseUtility->dumpStaticTables($fieldDefinitions);
372 }
373 return $sqlData;
374 }
375
376 /**
377 * Checks if an update for an extension is available
378 *
379 * @internal
380 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionData
381 * @return boolean
382 */
383 public function isUpdateAvailable(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionData) {
384 // Only check for update for TER extensions
385 $version = $extensionData->getIntegerVersion();
386 /** @var $highestTerVersionExtension \TYPO3\CMS\Extensionmanager\Domain\Model\Extension */
387 $highestTerVersionExtension = $this->extensionRepository->findHighestAvailableVersion($extensionData->getExtensionKey());
388 if ($highestTerVersionExtension instanceof \TYPO3\CMS\Extensionmanager\Domain\Model\Extension) {
389 $highestVersion = $highestTerVersionExtension->getIntegerVersion();
390 if ($highestVersion > $version) {
391 return TRUE;
392 }
393 }
394 return FALSE;
395 }
396
397 }
398
399
400 ?>