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