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