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