[TASK] Make Core\Configuration\ConfigurationManager non static
[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 * @return void
161 */
162 protected function unloadExtension($extensionKey) {
163 \TYPO3\CMS\Core\Extension\ExtensionManager::unloadExtension($extensionKey);
164 }
165
166 /**
167 * Helper function to install an extension
168 * also processes db updates and clears the cache if the extension asks for it
169 *
170 * @param string $extensionKey
171 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
172 * @return void
173 */
174 public function install($extensionKey) {
175 $extension = $this->enrichExtensionWithDetails($extensionKey);
176 $this->processDatabaseUpdates($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 * @internal
200 * @return mixed
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 * Gets the content of the ext_tables.sql and ext_tables_static+adt.sql files
216 * Additionally adds the table definitions for the cache tables
217 *
218 * @param string $extension
219 * @return void
220 */
221 public function processDatabaseUpdates($extension) {
222 $extTablesSqlFile = PATH_site . $extension['siteRelPath'] . '/ext_tables.sql';
223 if (file_exists($extTablesSqlFile)) {
224 $extTablesSqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extTablesSqlFile);
225 // @TODO: This should probably moved to TYPO3\CMS\Core\Cache\Cache->getDatabaseTableDefinitions ?!
226 $GLOBALS['typo3CacheManager']->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
227 $extTablesSqlContent .= \TYPO3\CMS\Core\Cache\Cache::getDatabaseTableDefinitions();
228 $this->updateDbWithExtTablesSql($extTablesSqlContent);
229 }
230 $extTablesStaticSqlFile = PATH_site . $extension['siteRelPath'] . '/ext_tables_static+adt.sql';
231 if (file_exists($extTablesStaticSqlFile)) {
232 $extTablesStaticSqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extTablesStaticSqlFile);
233 $this->importStaticSql($extTablesStaticSqlContent);
234 }
235 }
236
237 /**
238 * Reload Cache files and Typo3LoadedExtensions
239 *
240 * @return void
241 */
242 public function reloadCaches() {
243 \TYPO3\CMS\Core\Extension\ExtensionManager::removeCacheFiles();
244 // Set new extlist / extlistArray for extension load changes at runtime
245 $localConfiguration = $this->configurationManager->getLocalConfiguration();
246 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'] = $localConfiguration['EXT']['extList'];
247 $GLOBALS['TYPO3_CONF_VARS']['EXT']['extListArray'] = $localConfiguration['EXT']['extListArray'];
248 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->populateTypo3LoadedExtGlobal(FALSE)->loadAdditionalConfigurationFromExtensions(FALSE);
249 }
250
251 /**
252 * Save default configuration of an extension
253 *
254 * @param string $extensionKey
255 * @return void
256 */
257 protected function saveDefaultConfiguration($extensionKey) {
258 /** @var $configUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
259 $configUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
260 $configUtility->saveDefaultConfiguration($extensionKey);
261 }
262
263 /**
264 * Update database / process db updates from ext_tables
265 *
266 * @param string $rawDefinitions The raw SQL statements from ext_tables.sql
267 * @return void
268 */
269 public function updateDbWithExtTablesSql($rawDefinitions) {
270 $fieldDefinitionsFromFile = $this->installToolSqlParser->getFieldDefinitions_fileContent($rawDefinitions);
271 if (count($fieldDefinitionsFromFile)) {
272 $fieldDefinitionsFromCurrentDatabase = $this->installToolSqlParser->getFieldDefinitions_database();
273 $diff = $this->installToolSqlParser->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase);
274 $updateStatements = $this->installToolSqlParser->getUpdateSuggestions($diff);
275 foreach ((array) $updateStatements['add'] as $string) {
276 $GLOBALS['TYPO3_DB']->admin_query($string);
277 }
278 foreach ((array) $updateStatements['change'] as $string) {
279 $GLOBALS['TYPO3_DB']->admin_query($string);
280 }
281 foreach ((array) $updateStatements['create_table'] as $string) {
282 $GLOBALS['TYPO3_DB']->admin_query($string);
283 }
284 }
285 }
286
287 /**
288 * Import static SQL data (normally used for ext_tables_static+adt.sql)
289 *
290 * @param string $rawDefinitions
291 * @return void
292 */
293 public function importStaticSql($rawDefinitions) {
294 $statements = $this->installToolSqlParser->getStatementarray($rawDefinitions, 1);
295 list($statementsPerTable, $insertCount) = $this->installToolSqlParser->getCreateTables($statements, 1);
296 // Traverse the tables
297 foreach ($statementsPerTable as $table => $query) {
298 $GLOBALS['TYPO3_DB']->admin_query('DROP TABLE IF EXISTS ' . $table);
299 $GLOBALS['TYPO3_DB']->admin_query($query);
300 if ($insertCount[$table]) {
301 $insertStatements = $this->installToolSqlParser->getTableInsertStatements($statements, $table);
302 foreach ($insertStatements as $statement) {
303 $GLOBALS['TYPO3_DB']->admin_query($statement);
304 }
305 }
306 }
307 }
308
309 /**
310 * Writes the TSstyleconf values to "localconf.php"
311 * Removes the temp_CACHED* files before return.
312 *
313 * @param string $extensionKey Extension key
314 * @param array $newConfiguration Configuration array to write back
315 * @return void
316 */
317 public function writeExtensionTypoScriptStyleConfigurationToLocalconf($extensionKey, $newConfiguration) {
318 $this->configurationManager->setLocalConfigurationValueByPath('EXT/extConf/' . $extensionKey, serialize($newConfiguration));
319 \TYPO3\CMS\Core\Extension\ExtensionManager::removeCacheFiles();
320 }
321
322 /**
323 * Remove an extension (delete the directory)
324 *
325 * @param string $extension
326 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
327 * @return void
328 */
329 public function removeExtension($extension) {
330 $absolutePath = $this->filehandlingUtility->getAbsoluteExtensionPath($extension);
331 if ($this->filehandlingUtility->isValidExtensionPath($absolutePath)) {
332 $this->filehandlingUtility->removeDirectory($absolutePath);
333 } else {
334 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('No valid extension path given.', 1342875724);
335 }
336 }
337
338 /**
339 * Get the data dump for an extension
340 *
341 * @param string $extension
342 * @return array
343 */
344 public function getExtensionSqlDataDump($extension) {
345 $extension = $this->enrichExtensionWithDetails($extension);
346 $filePrefix = PATH_site . $extension['siteRelPath'];
347 $sqlData['extTables'] = $this->getSqlDataDumpForFile($filePrefix . '/ext_tables.sql');
348 $sqlData['staticSql'] = $this->getSqlDataDumpForFile($filePrefix . '/ext_tables_static+adt.sql');
349 return $sqlData;
350 }
351
352 /**
353 * Gets the sql data dump for a specific sql file (for example ext_tables.sql)
354 *
355 * @param string $sqlFile
356 * @return string
357 */
358 protected function getSqlDataDumpForFile($sqlFile) {
359 $sqlData = '';
360 if (file_exists($sqlFile)) {
361 $sqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($sqlFile);
362 $fieldDefinitions = $this->installToolSqlParser->getFieldDefinitions_fileContent($sqlContent);
363 $sqlData = $this->databaseUtility->dumpStaticTables($fieldDefinitions);
364 }
365 return $sqlData;
366 }
367
368 /**
369 * Checks if an update for an extension is available
370 *
371 * @internal
372 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionData
373 * @return boolean
374 */
375 public function isUpdateAvailable(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionData) {
376 // Only check for update for TER extensions
377 $version = $extensionData->getIntegerVersion();
378 /** @var $highestTerVersionExtension \TYPO3\CMS\Extensionmanager\Domain\Model\Extension */
379 $highestTerVersionExtension = $this->extensionRepository->findHighestAvailableVersion($extensionData->getExtensionKey());
380 if ($highestTerVersionExtension instanceof \TYPO3\CMS\Extensionmanager\Domain\Model\Extension) {
381 $highestVersion = $highestTerVersionExtension->getIntegerVersion();
382 if ($highestVersion > $version) {
383 return TRUE;
384 }
385 }
386 return FALSE;
387 }
388
389 }
390
391
392 ?>