TYPO3 CMS  TYPO3_6-2
InstallUtility.php
Go to the documentation of this file.
1 <?php
3 
19 
26 
32 
38 
43  protected $dependencyUtility;
44 
50 
55  protected $listUtility;
56 
61  protected $databaseUtility;
62 
68 
73  protected $packageManager;
74 
79  protected $cacheManager;
80 
86 
91  protected $registry;
92 
101  public function install($extensionKey) {
102  $extension = $this->enrichExtensionWithDetails($extensionKey, FALSE);
103  $this->ensureConfiguredDirectoriesExist($extension);
104  if (!$this->isLoaded($extensionKey)) {
105  $this->loadExtension($extensionKey);
106  }
107  if (!empty($extension['clearcacheonload']) || !empty($extension['clearCacheOnLoad'])) {
108  $this->cacheManager->flushCaches();
109  } else {
110  $this->cacheManager->flushCachesInGroup('system');
111  }
112  $this->reloadCaches();
113 
114  $this->importInitialFiles($extension['siteRelPath'], $extensionKey);
115  $this->processDatabaseUpdates($extension);
116  $this->processRuntimeDatabaseUpdates($extensionKey);
117  $this->saveDefaultConfiguration($extension['key']);
118  }
119 
127  public function uninstall($extensionKey) {
128  $dependentExtensions = $this->dependencyUtility->findInstalledExtensionsThatDependOnMe($extensionKey);
129  if (is_array($dependentExtensions) && count($dependentExtensions) > 0) {
130  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(
131  \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
132  'extensionList.uninstall.dependencyError',
133  'extensionmanager',
134  array($extensionKey, implode(',', $dependentExtensions))
135  ),
136  1342554622
137  );
138  } else {
139  $this->unloadExtension($extensionKey);
140  }
141  }
142 
149  public function isLoaded($extensionKey) {
150  return $this->packageManager->isPackageActive($extensionKey);
151  }
152 
159  protected function loadExtension($extensionKey) {
160  $this->packageManager->activatePackage($extensionKey);
161  }
162 
169  protected function unloadExtension($extensionKey) {
170  $this->packageManager->deactivatePackage($extensionKey);
171  $this->cacheManager->flushCachesInGroup('system');
172  }
173 
180  public function isAvailable($extensionKey) {
181  return $this->packageManager->isPackageAvailable($extensionKey);
182  }
183 
193  public function enrichExtensionWithDetails($extensionKey, $loadTerInformation = TRUE) {
194  $availableExtensions = $this->listUtility->getAvailableExtensions();
195  if (isset($availableExtensions[$extensionKey])) {
196  $extension = $availableExtensions[$extensionKey];
197  } else {
198  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Extension ' . $extensionKey . ' is not available', 1342864081);
199  }
200  if (!$loadTerInformation) {
201  $availableAndInstalledExtensions = $this->listUtility->enrichExtensionsWithEmConfInformation(array($extensionKey => $extension));
202  } else {
203  $availableAndInstalledExtensions = $this->listUtility->enrichExtensionsWithEmConfAndTerInformation(array($extensionKey => $extension));
204  }
205 
206  if (!isset($availableAndInstalledExtensions[$extensionKey])) {
207  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(
208  'Please check your uploaded extension "' . $extensionKey . '". The configuration file "ext_emconf.php" seems to be invalid.',
209  1391432222
210  );
211  }
212 
213  return $availableAndInstalledExtensions[$extensionKey];
214  }
215 
221  protected function ensureConfiguredDirectoriesExist(array $extension) {
222  $this->fileHandlingUtility->ensureConfiguredDirectoriesExist($extension);
223  }
224 
231  public function processDatabaseUpdates(array $extension) {
232  $extTablesSqlFile = PATH_site . $extension['siteRelPath'] . 'ext_tables.sql';
233  $extTablesSqlContent = '';
234  if (file_exists($extTablesSqlFile)) {
235  $extTablesSqlContent .= \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extTablesSqlFile);
236  }
237  if ($extTablesSqlContent !== '') {
238  $this->updateDbWithExtTablesSql($extTablesSqlContent);
239  }
240 
241  $this->importStaticSqlFile($extension['siteRelPath']);
242  $this->importT3DFile($extension['siteRelPath']);
243  }
244 
251  protected function processRuntimeDatabaseUpdates($extensionKey) {
252  $sqlString = $this->emitTablesDefinitionIsBeingBuiltSignal($extensionKey);
253  if (!empty($sqlString)) {
254  $this->updateDbWithExtTablesSql(implode(LF . LF . LF . LF, $sqlString));
255  }
256  }
257 
265  protected function emitTablesDefinitionIsBeingBuiltSignal($extensionKey) {
266  $signalReturn = $this->signalSlotDispatcher->dispatch(__CLASS__, 'tablesDefinitionIsBeingBuilt', array(array(), $extensionKey));
267  // This is important to support old associated returns
268  $signalReturn = array_values($signalReturn);
269  $sqlString = $signalReturn[0];
270  if (!is_array($sqlString)) {
271  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(
272  sprintf(
273  'The signal %s of class %s returned a value of type %s, but array was expected.',
274  'tablesDefinitionIsBeingBuilt',
275  __CLASS__,
276  gettype($sqlString)
277  ),
278  1382360258
279  );
280  }
281  return $sqlString;
282  }
283 
289  public function reloadCaches() {
291  // Reload class aliases defined in Migrations/Code/ClassAliasMap.php
292  \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->getEarlyInstance('TYPO3\\CMS\\Core\\Core\\ClassLoader')
293  ->setPackages($this->packageManager->getActivePackages());
294  \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::loadExtLocalconf(FALSE);
295  \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->loadExtensionTables(FALSE);
296  }
297 
304  protected function saveDefaultConfiguration($extensionKey) {
306  $configUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
307  $configUtility->saveDefaultConfiguration($extensionKey);
308  }
309 
316  public function updateDbWithExtTablesSql($rawDefinitions) {
317  $fieldDefinitionsFromFile = $this->installToolSqlParser->getFieldDefinitions_fileContent($rawDefinitions);
318  if (count($fieldDefinitionsFromFile)) {
319  $fieldDefinitionsFromCurrentDatabase = $this->installToolSqlParser->getFieldDefinitions_database();
320  $diff = $this->installToolSqlParser->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase);
321  $updateStatements = $this->installToolSqlParser->getUpdateSuggestions($diff);
322  $db = $this->getDatabaseConnection();
323  foreach ((array) $updateStatements['add'] as $string) {
324  $db->admin_query($string);
325  }
326  foreach ((array) $updateStatements['change'] as $string) {
327  $db->admin_query($string);
328  }
329  foreach ((array) $updateStatements['create_table'] as $string) {
330  $db->admin_query($string);
331  }
332  }
333  }
334 
341  public function importStaticSql($rawDefinitions) {
342  $statements = $this->installToolSqlParser->getStatementarray($rawDefinitions, 1);
343  list($statementsPerTable, $insertCount) = $this->installToolSqlParser->getCreateTables($statements, 1);
344  $db = $this->getDatabaseConnection();
345  // Traverse the tables
346  foreach ($statementsPerTable as $table => $query) {
347  $db->admin_query('DROP TABLE IF EXISTS ' . $table);
348  $db->admin_query($query);
349  if ($insertCount[$table]) {
350  $insertStatements = $this->installToolSqlParser->getTableInsertStatements($statements, $table);
351  foreach ($insertStatements as $statement) {
352  $db->admin_query($statement);
353  }
354  }
355  }
356  }
357 
365  public function removeExtension($extension) {
366  $absolutePath = $this->fileHandlingUtility->getAbsoluteExtensionPath($extension);
367  if ($this->fileHandlingUtility->isValidExtensionPath($absolutePath)) {
368  if ($this->packageManager->isPackageAvailable($extension)) {
369  // Package manager deletes the extension and removes the entry from PackageStates.php
370  $this->packageManager->deletePackage($extension);
371  } else {
372  // The extension is not listed in PackageStates.php, we can safely remove it
373  $this->fileHandlingUtility->removeDirectory($absolutePath);
374  }
375  } else {
376  throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('No valid extension path given.', 1342875724);
377  }
378  }
379 
386  public function getExtensionSqlDataDump($extension) {
387  $extension = $this->enrichExtensionWithDetails($extension);
388  $filePrefix = PATH_site . $extension['siteRelPath'];
389  $sqlData['extTables'] = $this->getSqlDataDumpForFile($filePrefix . 'ext_tables.sql');
390  $sqlData['staticSql'] = $this->getSqlDataDumpForFile($filePrefix . 'ext_tables_static+adt.sql');
391  return $sqlData;
392  }
393 
400  protected function getSqlDataDumpForFile($sqlFile) {
401  $sqlData = '';
402  if (file_exists($sqlFile)) {
403  $sqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($sqlFile);
404  $fieldDefinitions = $this->installToolSqlParser->getFieldDefinitions_fileContent($sqlContent);
405  $sqlData = $this->databaseUtility->dumpStaticTables($fieldDefinitions);
406  }
407  return $sqlData;
408  }
409 
410 
418  public function isUpdateAvailable(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionData) {
419  return (bool)$this->getUpdateableVersion($extensionData);
420  }
421 
430  public function getUpdateableVersion(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionData) {
431  // Only check for update for TER extensions
432  $version = $extensionData->getIntegerVersion();
433 
435  $extensionUpdates = $this->extensionRepository->findByVersionRangeAndExtensionKeyOrderedByVersion(
436  $extensionData->getExtensionKey(),
437  $version,
438  0,
439  FALSE
440  );
441  if ($extensionUpdates->count() > 0) {
442  foreach ($extensionUpdates as $extensionUpdate) {
443  try {
444  $this->dependencyUtility->checkDependencies($extensionUpdate);
445  if (!$this->dependencyUtility->hasDependencyErrors()) {
446  return $extensionUpdate;
447  }
448  } catch (ExtensionManagerException $e) {
449  }
450  }
451  }
452  return FALSE;
453  }
454 
462  protected function importT3DFile($extensionSiteRelPath) {
463  $registryKeysToCheck = array(
464  $extensionSiteRelPath . 'Initialisation/data.t3d',
465  $extensionSiteRelPath . 'Initialisation/dataImported',
466  );
467  foreach ($registryKeysToCheck as $registryKeyToCheck) {
468  if ($this->registry->get('extensionDataImport', $registryKeyToCheck)) {
469  // Data was imported before => early return
470  return;
471  }
472  }
473  $importFileToUse = NULL;
474  $possibleImportFiles = array(
475  $extensionSiteRelPath . 'Initialisation/data.t3d',
476  $extensionSiteRelPath . 'Initialisation/data.xml'
477  );
478  foreach ($possibleImportFiles as $possibleImportFile) {
479  if (!file_exists(PATH_site . $possibleImportFile)) {
480  continue;
481  }
482  $importFileToUse = $possibleImportFile;
483  }
484  if ($importFileToUse !== NULL) {
486  $importExportUtility = $this->objectManager->get('TYPO3\\CMS\\Impexp\\Utility\\ImportExportUtility');
487  try {
488  $importResult = $importExportUtility->importT3DFile(PATH_site . $importFileToUse, 0);
489  $this->registry->set('extensionDataImport', $extensionSiteRelPath . 'Initialisation/dataImported', 1);
490  $this->emitAfterExtensionT3DImportSignal($importFileToUse, $importResult);
491  } catch (\ErrorException $e) {
493  $logger = $this->objectManager->get('TYPO3\\CMS\\Core\\Log\\LogManager')->getLogger(__CLASS__);
494  $logger->log(\TYPO3\CMS\Core\Log\LogLevel::WARNING, $e->getMessage());
495  }
496  }
497  }
498 
505  protected function emitAfterExtensionT3DImportSignal($importFileToUse, $importResult) {
506  $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionT3DImport', array($importFileToUse, $importResult, $this));
507  }
508 
516  protected function importStaticSqlFile($extensionSiteRelPath) {
517  $extTablesStaticSqlRelFile = $extensionSiteRelPath . 'ext_tables_static+adt.sql';
518  if (!$this->registry->get('extensionDataImport', $extTablesStaticSqlRelFile)) {
519  $extTablesStaticSqlFile = PATH_site . $extTablesStaticSqlRelFile;
520  if (file_exists($extTablesStaticSqlFile)) {
521  $extTablesStaticSqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($extTablesStaticSqlFile);
522  $this->importStaticSql($extTablesStaticSqlContent);
523  }
524  $this->registry->set('extensionDataImport', $extTablesStaticSqlRelFile, 1);
525  $this->emitAfterExtensionStaticSqlImportSignal($extTablesStaticSqlRelFile);
526  }
527  }
528 
534  protected function emitAfterExtensionStaticSqlImportSignal($extTablesStaticSqlRelFile) {
535  $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionStaticSqlImport', array($extTablesStaticSqlRelFile, $this));
536  }
537 
545  protected function importInitialFiles($extensionSiteRelPath, $extensionKey) {
546  $importRelFolder = $extensionSiteRelPath . 'Initialisation/Files';
547  if (!$this->registry->get('extensionDataImport', $importRelFolder)) {
548  $importFolder = PATH_site . $importRelFolder;
549  if (file_exists($importFolder)) {
550  $destinationRelPath = $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'] . $extensionKey;
551  $destinationAbsolutePath = PATH_site . $destinationRelPath;
552  if (!file_exists($destinationAbsolutePath) &&
554  ) {
555  \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($destinationAbsolutePath);
556  }
557  \TYPO3\CMS\Core\Utility\GeneralUtility::copyDirectory($importRelFolder, $destinationRelPath);
558  $this->registry->set('extensionDataImport', $importRelFolder, 1);
559  $this->emitAfterExtensionFileImportSignal($destinationAbsolutePath);
560  }
561  }
562  }
563 
569  protected function emitAfterExtensionFileImportSignal($destinationAbsolutePath) {
570  $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionFileImport', array($destinationAbsolutePath, $this));
571  }
572 
576  protected function getDatabaseConnection() {
577  return $GLOBALS['TYPO3_DB'];
578  }
579 }
emitAfterExtensionFileImportSignal($destinationAbsolutePath)
static copyDirectory($source, $destination)
isUpdateAvailable(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionData)
emitAfterExtensionStaticSqlImportSignal($extTablesStaticSqlRelFile)
static getUrl($url, $includeHeader=0, $requestHeaders=FALSE, &$report=NULL)
importInitialFiles($extensionSiteRelPath, $extensionKey)
enrichExtensionWithDetails($extensionKey, $loadTerInformation=TRUE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
emitAfterExtensionT3DImportSignal($importFileToUse, $importResult)