[TASK] Rename ExtensionManager class Part 2
[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 install an extension
142 * also processes db updates and clears the cache if the extension asks for it
143 *
144 * @param string $extensionKey
145 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
146 * @return void
147 */
148 public function install($extensionKey) {
149 $extension = $this->enrichExtensionWithDetails($extensionKey);
150 $this->processDatabaseUpdates($extension);
151 $this->ensureConfiguredDirectoriesExist($extension);
152 if ($extension['clearcacheonload']) {
153 $GLOBALS['typo3CacheManager']->flushCaches();
154 }
155 if (!$this->isLoaded($extensionKey)) {
156 $this->loadExtension($extensionKey);
157 }
158 $this->reloadCaches();
159 $this->saveDefaultConfiguration($extension['key']);
160 }
161
162 /**
163 * Helper function to uninstall an extension
164 *
165 * @param string $extensionKey
166 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
167 * @return void
168 */
169 public function uninstall($extensionKey) {
170 $dependentExtensions = $this->dependencyUtility->findInstalledExtensionsThatDependOnMe($extensionKey);
171 if (is_array($dependentExtensions) && count($dependentExtensions) > 0) {
172 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Cannot deactivate extension ' . $extensionKey . ' - The extension(s) ' . implode(',', $dependentExtensions) . ' depend on it', 1342554622);
173 } else {
174 $this->unloadExtension($extensionKey);
175 }
176 }
177
178 /**
179 * Wrapper function to check for loaded extensions
180 *
181 * @param string $extensionKey
182 * @return boolean TRUE if extension is loaded
183 */
184 public function isLoaded($extensionKey) {
185 return \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded($extensionKey);
186 }
187
188 /**
189 * Wrapper function for loading extensions
190 *
191 * @param string $extensionKey
192 * @return void
193 */
194 protected function loadExtension($extensionKey) {
195 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::loadExtension($extensionKey);
196 }
197
198 /**
199 * Wrapper function for unloading extensions
200 *
201 * @param string $extensionKey
202 * @return void
203 */
204 protected function unloadExtension($extensionKey) {
205 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::unloadExtension($extensionKey);
206 }
207
208 /**
209 * Checks if an extension is available in the system
210 *
211 * @param $extensionKey
212 * @return boolean
213 */
214 public function isAvailable($extensionKey) {
215 $availableExtensions = $this->listUtility->getAvailableExtensions();
216 return array_key_exists($extensionKey, $availableExtensions);
217 }
218
219 /**
220 * Fetch additional information for an extension key
221 *
222 * @param string $extensionKey
223 * @access private
224 * @return array
225 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
226 */
227 public function enrichExtensionWithDetails($extensionKey) {
228 $availableExtensions = $this->listUtility->getAvailableExtensions();
229 if (isset($availableExtensions[$extensionKey])) {
230 $extension = $availableExtensions[$extensionKey];
231 } else {
232 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Extension ' . $extensionKey . ' is not available', 1342864081);
233 }
234 $availableAndInstalledExtensions = $this->listUtility->enrichExtensionsWithEmConfAndTerInformation(array($extensionKey => $extension));
235 return $availableAndInstalledExtensions[$extensionKey];
236 }
237
238 /**
239 * Creates directories as requested in ext_emconf.php
240 *
241 * @param array $extension
242 */
243 protected function ensureConfiguredDirectoriesExist(array $extension) {
244 $this->fileHandlingUtility->ensureConfiguredDirectoriesExist($extension);
245 }
246
247 /**
248 * Gets the content of the ext_tables.sql and ext_tables_static+adt.sql files
249 * Additionally adds the table definitions for the cache tables
250 *
251 * @param array $extension
252 */
253 public function processDatabaseUpdates(array $extension) {
254 $extTablesSqlFile = PATH_site . $extension['siteRelPath'] . '/ext_tables.sql';
255 if (file_exists($extTablesSqlFile)) {
256 $extTablesSqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extTablesSqlFile);
257 // @TODO: This should probably moved to TYPO3\CMS\Core\Cache\Cache->getDatabaseTableDefinitions ?!
258 $GLOBALS['typo3CacheManager']->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
259 $extTablesSqlContent .= \TYPO3\CMS\Core\Cache\Cache::getDatabaseTableDefinitions();
260 $this->updateDbWithExtTablesSql($extTablesSqlContent);
261 }
262 $extTablesStaticSqlFile = PATH_site . $extension['siteRelPath'] . '/ext_tables_static+adt.sql';
263 if (file_exists($extTablesStaticSqlFile)) {
264 $extTablesStaticSqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extTablesStaticSqlFile);
265 $this->importStaticSql($extTablesStaticSqlContent);
266 }
267 }
268
269 /**
270 * Reload Cache files and Typo3LoadedExtensions
271 *
272 * @return void
273 */
274 public function reloadCaches() {
275 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::removeCacheFiles();
276 // Set new extlist / extlistArray for extension load changes at runtime
277 $localConfiguration = $this->configurationManager->getLocalConfiguration();
278 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'] = $localConfiguration['EXT']['extList'];
279 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extListArray'] = $localConfiguration['EXT']['extListArray'];
280 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->populateTypo3LoadedExtGlobal(FALSE)->loadAdditionalConfigurationFromExtensions(FALSE);
281 }
282
283 /**
284 * Save default configuration of an extension
285 *
286 * @param string $extensionKey
287 * @return void
288 */
289 protected function saveDefaultConfiguration($extensionKey) {
290 /** @var $configUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
291 $configUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
292 $configUtility->saveDefaultConfiguration($extensionKey);
293 }
294
295 /**
296 * Update database / process db updates from ext_tables
297 *
298 * @param string $rawDefinitions The raw SQL statements from ext_tables.sql
299 * @return void
300 */
301 public function updateDbWithExtTablesSql($rawDefinitions) {
302 $fieldDefinitionsFromFile = $this->installToolSqlParser->getFieldDefinitions_fileContent($rawDefinitions);
303 if (count($fieldDefinitionsFromFile)) {
304 $fieldDefinitionsFromCurrentDatabase = $this->installToolSqlParser->getFieldDefinitions_database();
305 $diff = $this->installToolSqlParser->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase);
306 $updateStatements = $this->installToolSqlParser->getUpdateSuggestions($diff);
307 foreach ((array) $updateStatements['add'] as $string) {
308 $GLOBALS['TYPO3_DB']->admin_query($string);
309 }
310 foreach ((array) $updateStatements['change'] as $string) {
311 $GLOBALS['TYPO3_DB']->admin_query($string);
312 }
313 foreach ((array) $updateStatements['create_table'] as $string) {
314 $GLOBALS['TYPO3_DB']->admin_query($string);
315 }
316 }
317 }
318
319 /**
320 * Import static SQL data (normally used for ext_tables_static+adt.sql)
321 *
322 * @param string $rawDefinitions
323 * @return void
324 */
325 public function importStaticSql($rawDefinitions) {
326 $statements = $this->installToolSqlParser->getStatementarray($rawDefinitions, 1);
327 list($statementsPerTable, $insertCount) = $this->installToolSqlParser->getCreateTables($statements, 1);
328 // Traverse the tables
329 foreach ($statementsPerTable as $table => $query) {
330 $GLOBALS['TYPO3_DB']->admin_query('DROP TABLE IF EXISTS ' . $table);
331 $GLOBALS['TYPO3_DB']->admin_query($query);
332 if ($insertCount[$table]) {
333 $insertStatements = $this->installToolSqlParser->getTableInsertStatements($statements, $table);
334 foreach ($insertStatements as $statement) {
335 $GLOBALS['TYPO3_DB']->admin_query($statement);
336 }
337 }
338 }
339 }
340
341 /**
342 * Writes the TSstyleconf values to "localconf.php"
343 * Removes the temp_CACHED* files before return.
344 *
345 * @param string $extensionKey Extension key
346 * @param array $newConfiguration Configuration array to write back
347 * @return void
348 */
349 public function writeExtensionTypoScriptStyleConfigurationToLocalconf($extensionKey, $newConfiguration) {
350 $this->configurationManager->setLocalConfigurationValueByPath('EXT/extConf/' . $extensionKey, serialize($newConfiguration));
351 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::removeCacheFiles();
352 }
353
354 /**
355 * Remove an extension (delete the directory)
356 *
357 * @param string $extension
358 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
359 * @return void
360 */
361 public function removeExtension($extension) {
362 $absolutePath = $this->fileHandlingUtility->getAbsoluteExtensionPath($extension);
363 if ($this->fileHandlingUtility->isValidExtensionPath($absolutePath)) {
364 $this->fileHandlingUtility->removeDirectory($absolutePath);
365 } else {
366 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('No valid extension path given.', 1342875724);
367 }
368 }
369
370 /**
371 * Get the data dump for an extension
372 *
373 * @param string $extension
374 * @return array
375 */
376 public function getExtensionSqlDataDump($extension) {
377 $extension = $this->enrichExtensionWithDetails($extension);
378 $filePrefix = PATH_site . $extension['siteRelPath'];
379 $sqlData['extTables'] = $this->getSqlDataDumpForFile($filePrefix . '/ext_tables.sql');
380 $sqlData['staticSql'] = $this->getSqlDataDumpForFile($filePrefix . '/ext_tables_static+adt.sql');
381 return $sqlData;
382 }
383
384 /**
385 * Gets the sql data dump for a specific sql file (for example ext_tables.sql)
386 *
387 * @param string $sqlFile
388 * @return string
389 */
390 protected function getSqlDataDumpForFile($sqlFile) {
391 $sqlData = '';
392 if (file_exists($sqlFile)) {
393 $sqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($sqlFile);
394 $fieldDefinitions = $this->installToolSqlParser->getFieldDefinitions_fileContent($sqlContent);
395 $sqlData = $this->databaseUtility->dumpStaticTables($fieldDefinitions);
396 }
397 return $sqlData;
398 }
399
400 /**
401 * Checks if an update for an extension is available
402 *
403 * @internal
404 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionData
405 * @return boolean
406 */
407 public function isUpdateAvailable(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionData) {
408 // Only check for update for TER extensions
409 $version = $extensionData->getIntegerVersion();
410 /** @var $highestTerVersionExtension \TYPO3\CMS\Extensionmanager\Domain\Model\Extension */
411 $highestTerVersionExtension = $this->extensionRepository->findHighestAvailableVersion($extensionData->getExtensionKey());
412 if ($highestTerVersionExtension instanceof \TYPO3\CMS\Extensionmanager\Domain\Model\Extension) {
413 $highestVersion = $highestTerVersionExtension->getIntegerVersion();
414 if ($highestVersion > $version) {
415 return TRUE;
416 }
417 }
418 return FALSE;
419 }
420
421 }
422
423
424 ?>