[BUGFIX] Hide update icon for incompatible extensions
[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-2013 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 text file 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 */
34 class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface {
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
38 * @inject
39 */
40 public $objectManager;
41
42 /**
43 * @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService
44 * @inject
45 */
46 public $installToolSqlParser;
47
48 /**
49 * @var \TYPO3\CMS\Extensionmanager\Utility\DependencyUtility
50 * @inject
51 */
52 protected $dependencyUtility;
53
54 /**
55 * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
56 * @inject
57 */
58 protected $fileHandlingUtility;
59
60 /**
61 * @var \TYPO3\CMS\Extensionmanager\Utility\ListUtility
62 * @inject
63 */
64 protected $listUtility;
65
66 /**
67 * @var \TYPO3\CMS\Extensionmanager\Utility\DatabaseUtility
68 * @inject
69 */
70 protected $databaseUtility;
71
72 /**
73 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
74 * @inject
75 */
76 public $extensionRepository;
77
78 /**
79 * @var \TYPO3\CMS\Core\Package\PackageManager
80 * @inject
81 */
82 protected $packageManager;
83
84 /**
85 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
86 * @inject
87 */
88 protected $signalSlotDispatcher;
89
90 /**
91 * @var \TYPO3\CMS\Core\Registry
92 * @inject
93 */
94 protected $registry;
95
96 /**
97 * __construct
98 */
99 public function __construct() {
100 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
101 /** @var $installToolSqlParser \TYPO3\CMS\Install\Service\SqlSchemaMigrationService */
102 $this->installToolSqlParser = $this->objectManager->get('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService');
103 $this->dependencyUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\DependencyUtility');
104 }
105
106 /**
107 * Helper function to install an extension
108 * also processes db updates and clears the cache if the extension asks for it
109 *
110 * @param string $extensionKey
111 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
112 * @return void
113 */
114 public function install($extensionKey) {
115 $extension = $this->enrichExtensionWithDetails($extensionKey);
116 $this->processDatabaseUpdates($extension);
117 $this->ensureConfiguredDirectoriesExist($extension);
118 $this->importInitialFiles($extension['siteRelPath'], $extensionKey);
119 if (!$this->isLoaded($extensionKey)) {
120 $this->loadExtension($extensionKey);
121 }
122 $this->reloadCaches();
123 $this->processCachingFrameworkUpdates();
124 $this->saveDefaultConfiguration($extension['key']);
125 if ($extension['clearcacheonload']) {
126 $GLOBALS['typo3CacheManager']->flushCaches();
127 }
128 }
129
130 /**
131 * Helper function to uninstall an extension
132 *
133 * @param string $extensionKey
134 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
135 * @return void
136 */
137 public function uninstall($extensionKey) {
138 $dependentExtensions = $this->dependencyUtility->findInstalledExtensionsThatDependOnMe($extensionKey);
139 if (is_array($dependentExtensions) && count($dependentExtensions) > 0) {
140 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(
141 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
142 'extensionList.uninstall.dependencyError',
143 'extensionmanager',
144 array($extensionKey, implode(',', $dependentExtensions))
145 ),
146 1342554622
147 );
148 } else {
149 $this->unloadExtension($extensionKey);
150 }
151 }
152
153 /**
154 * Wrapper function to check for loaded extensions
155 *
156 * @param string $extensionKey
157 * @return boolean TRUE if extension is loaded
158 */
159 public function isLoaded($extensionKey) {
160 return $this->packageManager->isPackageActive($extensionKey);
161 }
162
163 /**
164 * Wrapper function for loading extensions
165 *
166 * @param string $extensionKey
167 * @return void
168 */
169 protected function loadExtension($extensionKey) {
170 $this->packageManager->activatePackage($extensionKey);
171 }
172
173 /**
174 * Wrapper function for unloading extensions
175 *
176 * @param string $extensionKey
177 * @return void
178 */
179 protected function unloadExtension($extensionKey) {
180 $this->packageManager->deactivatePackage($extensionKey);
181 $this->reloadCaches();
182 }
183
184 /**
185 * Checks if an extension is available in the system
186 *
187 * @param $extensionKey
188 * @return boolean
189 */
190 public function isAvailable($extensionKey) {
191 return $this->packageManager->isPackageAvailable($extensionKey);
192 }
193
194 /**
195 * Fetch additional information for an extension key
196 *
197 * @param string $extensionKey
198 * @access private
199 * @return array
200 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
201 */
202 public function enrichExtensionWithDetails($extensionKey) {
203 $availableExtensions = $this->listUtility->getAvailableExtensions();
204 if (isset($availableExtensions[$extensionKey])) {
205 $extension = $availableExtensions[$extensionKey];
206 } else {
207 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Extension ' . $extensionKey . ' is not available', 1342864081);
208 }
209 $availableAndInstalledExtensions = $this->listUtility->enrichExtensionsWithEmConfAndTerInformation(array($extensionKey => $extension));
210 return $availableAndInstalledExtensions[$extensionKey];
211 }
212
213 /**
214 * Creates directories as requested in ext_emconf.php
215 *
216 * @param array $extension
217 */
218 protected function ensureConfiguredDirectoriesExist(array $extension) {
219 $this->fileHandlingUtility->ensureConfiguredDirectoriesExist($extension);
220 }
221
222 /**
223 * Gets the content of the ext_tables.sql and ext_tables_static+adt.sql files
224 * Additionally adds the table definitions for the cache tables
225 *
226 * @param array $extension
227 */
228 public function processDatabaseUpdates(array $extension) {
229 $extTablesSqlFile = PATH_site . $extension['siteRelPath'] . '/ext_tables.sql';
230 $extTablesSqlContent = '';
231 if (file_exists($extTablesSqlFile)) {
232 $extTablesSqlContent .= \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extTablesSqlFile);
233 }
234 if ($extTablesSqlContent !== '') {
235 $this->updateDbWithExtTablesSql($extTablesSqlContent);
236 }
237
238 $this->importStaticSqlFile($extension['siteRelPath']);
239 $this->importT3DFile($extension['siteRelPath']);
240 }
241
242 /**
243 * Gets all registered caches and creates required caching framework tables.
244 *
245 * @return void
246 */
247 protected function processCachingFrameworkUpdates() {
248 $extTablesSqlContent = '';
249
250 // @TODO: This should probably moved to TYPO3\CMS\Core\Cache\Cache->getDatabaseTableDefinitions ?!
251 $GLOBALS['typo3CacheManager']->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
252 $extTablesSqlContent .= \TYPO3\CMS\Core\Cache\Cache::getDatabaseTableDefinitions();
253
254 if ($extTablesSqlContent !== '') {
255 $this->updateDbWithExtTablesSql($extTablesSqlContent);
256 }
257 }
258
259 /**
260 * Reload Cache files and Typo3LoadedExtensions
261 *
262 * @return void
263 */
264 public function reloadCaches() {
265 $GLOBALS['typo3CacheManager']->flushCachesInGroup('system');
266 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->reloadTypo3LoadedExtAndClassLoaderAndExtLocalconf();
267 }
268
269 /**
270 * Save default configuration of an extension
271 *
272 * @param string $extensionKey
273 * @return void
274 */
275 protected function saveDefaultConfiguration($extensionKey) {
276 /** @var $configUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
277 $configUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
278 $configUtility->saveDefaultConfiguration($extensionKey);
279 }
280
281 /**
282 * Update database / process db updates from ext_tables
283 *
284 * @param string $rawDefinitions The raw SQL statements from ext_tables.sql
285 * @return void
286 */
287 public function updateDbWithExtTablesSql($rawDefinitions) {
288 $fieldDefinitionsFromFile = $this->installToolSqlParser->getFieldDefinitions_fileContent($rawDefinitions);
289 if (count($fieldDefinitionsFromFile)) {
290 $fieldDefinitionsFromCurrentDatabase = $this->installToolSqlParser->getFieldDefinitions_database();
291 $diff = $this->installToolSqlParser->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase);
292 $updateStatements = $this->installToolSqlParser->getUpdateSuggestions($diff);
293 foreach ((array) $updateStatements['add'] as $string) {
294 $GLOBALS['TYPO3_DB']->admin_query($string);
295 }
296 foreach ((array) $updateStatements['change'] as $string) {
297 $GLOBALS['TYPO3_DB']->admin_query($string);
298 }
299 foreach ((array) $updateStatements['create_table'] as $string) {
300 $GLOBALS['TYPO3_DB']->admin_query($string);
301 }
302 }
303 }
304
305 /**
306 * Import static SQL data (normally used for ext_tables_static+adt.sql)
307 *
308 * @param string $rawDefinitions
309 * @return void
310 */
311 public function importStaticSql($rawDefinitions) {
312 $statements = $this->installToolSqlParser->getStatementarray($rawDefinitions, 1);
313 list($statementsPerTable, $insertCount) = $this->installToolSqlParser->getCreateTables($statements, 1);
314 // Traverse the tables
315 foreach ($statementsPerTable as $table => $query) {
316 $GLOBALS['TYPO3_DB']->admin_query('DROP TABLE IF EXISTS ' . $table);
317 $GLOBALS['TYPO3_DB']->admin_query($query);
318 if ($insertCount[$table]) {
319 $insertStatements = $this->installToolSqlParser->getTableInsertStatements($statements, $table);
320 foreach ($insertStatements as $statement) {
321 $GLOBALS['TYPO3_DB']->admin_query($statement);
322 }
323 }
324 }
325 }
326
327 /**
328 * Remove an extension (delete the directory)
329 *
330 * @param string $extension
331 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
332 * @return void
333 */
334 public function removeExtension($extension) {
335 $absolutePath = $this->fileHandlingUtility->getAbsoluteExtensionPath($extension);
336 if ($this->fileHandlingUtility->isValidExtensionPath($absolutePath)) {
337 $this->fileHandlingUtility->removeDirectory($absolutePath);
338 } else {
339 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('No valid extension path given.', 1342875724);
340 }
341 }
342
343 /**
344 * Get the data dump for an extension
345 *
346 * @param string $extension
347 * @return array
348 */
349 public function getExtensionSqlDataDump($extension) {
350 $extension = $this->enrichExtensionWithDetails($extension);
351 $filePrefix = PATH_site . $extension['siteRelPath'];
352 $sqlData['extTables'] = $this->getSqlDataDumpForFile($filePrefix . '/ext_tables.sql');
353 $sqlData['staticSql'] = $this->getSqlDataDumpForFile($filePrefix . '/ext_tables_static+adt.sql');
354 return $sqlData;
355 }
356
357 /**
358 * Gets the sql data dump for a specific sql file (for example ext_tables.sql)
359 *
360 * @param string $sqlFile
361 * @return string
362 */
363 protected function getSqlDataDumpForFile($sqlFile) {
364 $sqlData = '';
365 if (file_exists($sqlFile)) {
366 $sqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($sqlFile);
367 $fieldDefinitions = $this->installToolSqlParser->getFieldDefinitions_fileContent($sqlContent);
368 $sqlData = $this->databaseUtility->dumpStaticTables($fieldDefinitions);
369 }
370 return $sqlData;
371 }
372
373 /**
374 * Checks if an update for an extension is available
375 *
376 * @internal
377 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionData
378 * @return boolean
379 */
380 public function isUpdateAvailable(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionData) {
381 $isUpdateAvailable = FALSE;
382 // Only check for update for TER extensions
383 $version = $extensionData->getIntegerVersion();
384 /** @var $highestTerVersionExtension \TYPO3\CMS\Extensionmanager\Domain\Model\Extension */
385 $highestTerVersionExtension = $this->extensionRepository->findHighestAvailableVersion($extensionData->getExtensionKey());
386 if ($highestTerVersionExtension instanceof \TYPO3\CMS\Extensionmanager\Domain\Model\Extension) {
387 $highestVersion = $highestTerVersionExtension->getIntegerVersion();
388 if ($highestVersion > $version) {
389 try {
390 $this->dependencyUtility->buildExtensionDependenciesTree($highestTerVersionExtension);
391 $isUpdateAvailable = TRUE;
392 } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
393 }
394 }
395 }
396 return $isUpdateAvailable;
397 }
398
399 /**
400 * Uses the export import extension to import a T3DFile to PID 0
401 * Execution state is saved in the this->registry, so it only happens once
402 *
403 * @param string $extensionSiteRelPath
404 * @return void
405 */
406 protected function importT3DFile($extensionSiteRelPath) {
407 $t3dImportRelFile = $extensionSiteRelPath . '/Initialisation/data.t3d';
408 if (!$this->registry->get('extensionDataImport', $t3dImportRelFile)) {
409 $t3dImportFile = PATH_site . $t3dImportRelFile;
410 if (file_exists($t3dImportFile)) {
411 $importExportUtility = $this->objectManager->get('TYPO3\\CMS\\Impexp\\Utility\\ImportExportUtility');
412 try {
413 $importResult = $importExportUtility->importT3DFile($t3dImportFile, 0);
414 $this->registry->set('extensionDataImport', $t3dImportRelFile, 1);
415 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionT3DImport', array($t3dImportRelFile, $importResult, $this));
416 } catch (\ErrorException $e) {
417 /** @var \TYPO3\CMS\Core\Log\Logger $logger */
418 $logger = $this->objectManager->get('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger(__CLASS__);
419 $logger->log(\TYPO3\CMS\Core\Log\LogLevel::WARNING, $e->getMessage());
420 }
421 }
422 }
423 }
424
425 /**
426 * Imports a static tables SQL File (ext_tables_static+adt)
427 * Execution state is saved in the this->registry, so it only happens once
428 *
429 * @param string $extensionSiteRelPath
430 * @return void
431 */
432 protected function importStaticSqlFile($extensionSiteRelPath) {
433 $extTablesStaticSqlRelFile = $extensionSiteRelPath . '/ext_tables_static+adt.sql';
434 if (!$this->registry->get('extensionDataImport', $extTablesStaticSqlRelFile)) {
435 $extTablesStaticSqlFile = PATH_site . $extTablesStaticSqlRelFile;
436 if (file_exists($extTablesStaticSqlFile)) {
437 $extTablesStaticSqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extTablesStaticSqlFile);
438 $this->importStaticSql($extTablesStaticSqlContent);
439 }
440 $this->registry->set('extensionDataImport', $extTablesStaticSqlRelFile, 1);
441 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionStaticSqlImport', array($extTablesStaticSqlRelFile, $this));
442 }
443 }
444
445 /**
446 * Imports files from Initialisation/Files to fileadmin
447 * via lowlevel copy directory method
448 *
449 * @param string $extensionSiteRelPath relative path to extension dir
450 * @param string $extensionKey
451 */
452 protected function importInitialFiles($extensionSiteRelPath, $extensionKey) {
453 $importRelFolder = $extensionSiteRelPath . '/Initialisation/Files';
454 if (!$this->registry->get('extensionDataImport', $importRelFolder)) {
455 $importFolder = PATH_site . $importRelFolder;
456 if (file_exists($importFolder)) {
457 $destinationRelPath = $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'] . $extensionKey;
458 $destinationAbsolutePath = PATH_site . $destinationRelPath;
459 if (!file_exists($destinationAbsolutePath) &&
460 \TYPO3\CMS\Core\Utility\GeneralUtility::isAllowedAbsPath($destinationAbsolutePath)
461 ) {
462 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($destinationAbsolutePath);
463 }
464 \TYPO3\CMS\Core\Utility\GeneralUtility::copyDirectory($importRelFolder, $destinationRelPath);
465 $this->registry->set('extensionDataImport', $importRelFolder, 1);
466 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionFileImport', array($destinationAbsolutePath, $this));
467 }
468 }
469 }
470 }