-
Notifications
You must be signed in to change notification settings - Fork 638
/
InstallUtility.php
666 lines (609 loc) · 23.2 KB
/
InstallUtility.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
<?php
namespace TYPO3\CMS\Extensionmanager\Utility;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Database\Schema\SchemaMigrator;
use TYPO3\CMS\Core\Database\Schema\SqlReader;
use TYPO3\CMS\Core\Service\OpcodeCacheService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
use TYPO3\CMS\Impexp\Utility\ImportExportUtility;
/**
* Extension Manager Install Utility
*/
class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface
{
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
public $objectManager;
/**
* @var \TYPO3\CMS\Extensionmanager\Utility\DependencyUtility
*/
protected $dependencyUtility;
/**
* @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
*/
protected $fileHandlingUtility;
/**
* @var \TYPO3\CMS\Extensionmanager\Utility\ListUtility
*/
protected $listUtility;
/**
* @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
*/
public $extensionRepository;
/**
* @var \TYPO3\CMS\Core\Package\PackageManager
*/
protected $packageManager;
/**
* @var \TYPO3\CMS\Core\Cache\CacheManager
*/
protected $cacheManager;
/**
* @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
*/
protected $signalSlotDispatcher;
/**
* @var \TYPO3\CMS\Core\Registry
*/
protected $registry;
/**
* @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
*/
public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
/**
* @param \TYPO3\CMS\Extensionmanager\Utility\DependencyUtility $dependencyUtility
*/
public function injectDependencyUtility(\TYPO3\CMS\Extensionmanager\Utility\DependencyUtility $dependencyUtility)
{
$this->dependencyUtility = $dependencyUtility;
}
/**
* @param \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility
*/
public function injectFileHandlingUtility(\TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility)
{
$this->fileHandlingUtility = $fileHandlingUtility;
}
/**
* @param \TYPO3\CMS\Extensionmanager\Utility\ListUtility $listUtility
*/
public function injectListUtility(\TYPO3\CMS\Extensionmanager\Utility\ListUtility $listUtility)
{
$this->listUtility = $listUtility;
}
/**
* @param \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository
*/
public function injectExtensionRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository)
{
$this->extensionRepository = $extensionRepository;
}
/**
* @param \TYPO3\CMS\Core\Package\PackageManager $packageManager
*/
public function injectPackageManager(\TYPO3\CMS\Core\Package\PackageManager $packageManager)
{
$this->packageManager = $packageManager;
}
/**
* @param \TYPO3\CMS\Core\Cache\CacheManager $cacheManager
*/
public function injectCacheManager(\TYPO3\CMS\Core\Cache\CacheManager $cacheManager)
{
$this->cacheManager = $cacheManager;
}
/**
* @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
*/
public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
{
$this->signalSlotDispatcher = $signalSlotDispatcher;
}
/**
* @param \TYPO3\CMS\Core\Registry $registry
*/
public function injectRegistry(\TYPO3\CMS\Core\Registry $registry)
{
$this->registry = $registry;
}
/**
* Helper function to install an extension
* also processes db updates and clears the cache if the extension asks for it
*
* @param string $extensionKey
* @throws ExtensionManagerException
*/
public function install($extensionKey)
{
$extension = $this->enrichExtensionWithDetails($extensionKey, false);
$this->loadExtension($extensionKey);
if (!empty($extension['clearcacheonload']) || !empty($extension['clearCacheOnLoad'])) {
$this->cacheManager->flushCaches();
} else {
$this->cacheManager->flushCachesInGroup('system');
}
// TODO: Should be possible to move this call to processExtensionSetup.
// TODO: We need to check why our acceptance test on postgress fails then
$this->saveDefaultConfiguration($extensionKey);
$this->reloadCaches();
$this->processExtensionSetup($extensionKey);
$this->emitAfterExtensionInstallSignal($extensionKey);
}
/**
* @param string $extensionKey
*/
public function processExtensionSetup($extensionKey)
{
$extension = $this->enrichExtensionWithDetails($extensionKey, false);
$this->ensureConfiguredDirectoriesExist($extension);
$this->importInitialFiles($extension['siteRelPath'], $extensionKey);
$this->processDatabaseUpdates($extension);
$this->processRuntimeDatabaseUpdates($extensionKey);
}
/**
* Helper function to uninstall an extension
*
* @param string $extensionKey
* @throws ExtensionManagerException
*/
public function uninstall($extensionKey)
{
$dependentExtensions = $this->dependencyUtility->findInstalledExtensionsThatDependOnMe($extensionKey);
if (is_array($dependentExtensions) && !empty($dependentExtensions)) {
throw new ExtensionManagerException(
\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
'extensionList.uninstall.dependencyError',
'extensionmanager',
[$extensionKey, implode(',', $dependentExtensions)]
),
1342554622
);
}
$this->unloadExtension($extensionKey);
}
/**
* Wrapper function to check for loaded extensions
*
* @param string $extensionKey
* @return bool TRUE if extension is loaded
*/
public function isLoaded($extensionKey)
{
return $this->packageManager->isPackageActive($extensionKey);
}
/**
* Reset and reload the available extensions
*/
public function reloadAvailableExtensions()
{
$this->listUtility->reloadAvailableExtensions();
}
/**
* Wrapper function for loading extensions
*
* @param string $extensionKey
*/
protected function loadExtension($extensionKey)
{
$this->packageManager->activatePackage($extensionKey);
}
/**
* Wrapper function for unloading extensions
*
* @param string $extensionKey
*/
protected function unloadExtension($extensionKey)
{
$this->packageManager->deactivatePackage($extensionKey);
$this->emitAfterExtensionUninstallSignal($extensionKey);
$this->cacheManager->flushCachesInGroup('system');
}
/**
* Emits a signal after an extension has been installed
*
* @param string $extensionKey
*/
protected function emitAfterExtensionInstallSignal($extensionKey)
{
$this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionInstall', [$extensionKey, $this]);
}
/**
* Emits a signal after an extension has been uninstalled
*
* @param string $extensionKey
*/
protected function emitAfterExtensionUninstallSignal($extensionKey)
{
$this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionUninstall', [$extensionKey, $this]);
}
/**
* Checks if an extension is available in the system
*
* @param string $extensionKey
* @return bool
*/
public function isAvailable($extensionKey)
{
return $this->packageManager->isPackageAvailable($extensionKey);
}
/**
* Reloads the package information, if the package is already registered
*
* @param string $extensionKey
* @throws \TYPO3\CMS\Core\Package\Exception\InvalidPackageStateException if the package isn't available
* @throws \TYPO3\CMS\Core\Package\Exception\InvalidPackageKeyException if an invalid package key was passed
* @throws \TYPO3\CMS\Core\Package\Exception\InvalidPackagePathException if an invalid package path was passed
* @throws \TYPO3\CMS\Core\Package\Exception\InvalidPackageManifestException if no extension configuration file could be found
*/
public function reloadPackageInformation($extensionKey)
{
if ($this->packageManager->isPackageAvailable($extensionKey)) {
$this->reloadOpcache();
$this->packageManager->reloadPackageInformation($extensionKey);
}
}
/**
* Fetch additional information for an extension key
*
* @param string $extensionKey
* @param bool $loadTerInformation
* @access private
* @return array
* @throws ExtensionManagerException
*/
public function enrichExtensionWithDetails($extensionKey, $loadTerInformation = true)
{
$extension = $this->getExtensionArray($extensionKey);
if (!$loadTerInformation) {
$availableAndInstalledExtensions = $this->listUtility->enrichExtensionsWithEmConfInformation([$extensionKey => $extension]);
} else {
$availableAndInstalledExtensions = $this->listUtility->enrichExtensionsWithEmConfAndTerInformation([$extensionKey => $extension]);
}
if (!isset($availableAndInstalledExtensions[$extensionKey])) {
throw new ExtensionManagerException(
'Please check your uploaded extension "' . $extensionKey . '". The configuration file "ext_emconf.php" seems to be invalid.',
1391432222
);
}
return $availableAndInstalledExtensions[$extensionKey];
}
/**
* @param string $extensionKey
* @return array
* @throws ExtensionManagerException
*/
protected function getExtensionArray($extensionKey)
{
$availableExtensions = $this->listUtility->getAvailableExtensions();
if (isset($availableExtensions[$extensionKey])) {
return $availableExtensions[$extensionKey];
}
throw new ExtensionManagerException('Extension ' . $extensionKey . ' is not available', 1342864081);
}
/**
* Creates directories as requested in ext_emconf.php
*
* @param array $extension
*/
protected function ensureConfiguredDirectoriesExist(array $extension)
{
$this->fileHandlingUtility->ensureConfiguredDirectoriesExist($extension);
}
/**
* Gets the content of the ext_tables.sql and ext_tables_static+adt.sql files
* Additionally adds the table definitions for the cache tables
*
* @param array $extension
*/
public function processDatabaseUpdates(array $extension)
{
$extTablesSqlFile = PATH_site . $extension['siteRelPath'] . 'ext_tables.sql';
$extTablesSqlContent = '';
if (file_exists($extTablesSqlFile)) {
$extTablesSqlContent .= file_get_contents($extTablesSqlFile);
}
if ($extTablesSqlContent !== '') {
try {
$this->updateDbWithExtTablesSql($extTablesSqlContent);
} catch (\TYPO3\CMS\Core\Database\Schema\Exception\StatementException $e) {
throw new ExtensionManagerException(
$e->getMessage(),
1476340371
);
}
}
$this->importStaticSqlFile($extension['siteRelPath']);
$this->importT3DFile($extension['siteRelPath']);
}
/**
* Gets all database updates due to runtime configuration, like caching framework or
* category api for example
*
* @param string $extensionKey
*/
protected function processRuntimeDatabaseUpdates($extensionKey)
{
$sqlString = $this->emitTablesDefinitionIsBeingBuiltSignal($extensionKey);
if (!empty($sqlString)) {
$this->updateDbWithExtTablesSql(implode(LF . LF . LF . LF, $sqlString));
}
}
/**
* Emits a signal to manipulate the tables definitions
*
* @param string $extensionKey
* @return mixed
* @throws ExtensionManagerException
*/
protected function emitTablesDefinitionIsBeingBuiltSignal($extensionKey)
{
$signalReturn = $this->signalSlotDispatcher->dispatch(__CLASS__, 'tablesDefinitionIsBeingBuilt', [[], $extensionKey]);
// This is important to support old associated returns
$signalReturn = array_values($signalReturn);
$sqlString = $signalReturn[0];
if (!is_array($sqlString)) {
throw new ExtensionManagerException(
sprintf(
'The signal %s of class %s returned a value of type %s, but array was expected.',
'tablesDefinitionIsBeingBuilt',
__CLASS__,
gettype($sqlString)
),
1382360258
);
}
return $sqlString;
}
/**
* Reload Cache files and Typo3LoadedExtensions
*/
public function reloadCaches()
{
$this->reloadOpcache();
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::loadExtLocalconf(false);
\TYPO3\CMS\Core\Core\Bootstrap::loadBaseTca(false);
\TYPO3\CMS\Core\Core\Bootstrap::loadExtTables(false);
}
/**
* Reloads PHP opcache
*/
protected function reloadOpcache()
{
GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive();
}
/**
* Save default configuration of an extension
*
* @param string $extensionKey
*/
protected function saveDefaultConfiguration($extensionKey)
{
$extensionConfiguration = $this->objectManager->get(ExtensionConfiguration::class);
$extensionConfiguration->synchronizeExtConfTemplateWithLocalConfiguration($extensionKey);
}
/**
* Update database / process db updates from ext_tables
*
* @param string $rawDefinitions The raw SQL statements from ext_tables.sql
*/
public function updateDbWithExtTablesSql($rawDefinitions)
{
$sqlReader = GeneralUtility::makeInstance(SqlReader::class);
$statements = $sqlReader->getCreateTableStatementArray($rawDefinitions);
if (count($statements) !== 0) {
$schemaMigrationService = GeneralUtility::makeInstance(SchemaMigrator::class);
$schemaMigrationService->install($statements);
}
}
/**
* Import static SQL data (normally used for ext_tables_static+adt.sql)
*
* @param string $rawDefinitions
*/
public function importStaticSql($rawDefinitions)
{
$sqlReader = GeneralUtility::makeInstance(SqlReader::class);
$statements = $sqlReader->getStatementArray($rawDefinitions);
$schemaMigrationService = GeneralUtility::makeInstance(SchemaMigrator::class);
$schemaMigrationService->importStaticData($statements, true);
}
/**
* Remove an extension (delete the directory)
*
* @param string $extension
* @throws ExtensionManagerException
*/
public function removeExtension($extension)
{
$absolutePath = $this->fileHandlingUtility->getAbsoluteExtensionPath($extension);
if ($this->fileHandlingUtility->isValidExtensionPath($absolutePath)) {
if ($this->packageManager->isPackageAvailable($extension)) {
// Package manager deletes the extension and removes the entry from PackageStates.php
$this->packageManager->deletePackage($extension);
} else {
// The extension is not listed in PackageStates.php, we can safely remove it
$this->fileHandlingUtility->removeDirectory($absolutePath);
}
} else {
throw new ExtensionManagerException('No valid extension path given.', 1342875724);
}
}
/**
* Checks if an update for an extension is available which also resolves dependencies.
*
* @internal
* @param Extension $extensionData
* @return bool
*/
public function isUpdateAvailable(Extension $extensionData)
{
return (bool)$this->getUpdateableVersion($extensionData);
}
/**
* Returns the updateable version for an extension which also resolves dependencies.
*
* @internal
* @param Extension $extensionData
* @return bool|Extension FALSE if no update available otherwise latest possible update
*/
public function getUpdateableVersion(Extension $extensionData)
{
// Only check for update for TER extensions
$version = $extensionData->getIntegerVersion();
/** @var $extensionUpdates[] \TYPO3\CMS\Extensionmanager\Domain\Model\Extension */
$extensionUpdates = $this->extensionRepository->findByVersionRangeAndExtensionKeyOrderedByVersion(
$extensionData->getExtensionKey(),
$version,
0,
false
);
if ($extensionUpdates->count() > 0) {
foreach ($extensionUpdates as $extensionUpdate) {
try {
$this->dependencyUtility->checkDependencies($extensionUpdate);
if (!$this->dependencyUtility->hasDependencyErrors()) {
return $extensionUpdate;
}
} catch (ExtensionManagerException $e) {
}
}
}
return false;
}
/**
* Uses the export import extension to import a T3D or XML file to PID 0
* Execution state is saved in the this->registry, so it only happens once
*
* @param string $extensionSiteRelPath
*/
protected function importT3DFile($extensionSiteRelPath)
{
$registryKeysToCheck = [
$extensionSiteRelPath . 'Initialisation/data.t3d',
$extensionSiteRelPath . 'Initialisation/dataImported',
];
foreach ($registryKeysToCheck as $registryKeyToCheck) {
if ($this->registry->get('extensionDataImport', $registryKeyToCheck)) {
// Data was imported before => early return
return;
}
}
$importFileToUse = null;
$possibleImportFiles = [
$extensionSiteRelPath . 'Initialisation/data.t3d',
$extensionSiteRelPath . 'Initialisation/data.xml'
];
foreach ($possibleImportFiles as $possibleImportFile) {
if (!file_exists(PATH_site . $possibleImportFile)) {
continue;
}
$importFileToUse = $possibleImportFile;
}
if ($importFileToUse !== null) {
/** @var ImportExportUtility $importExportUtility */
$importExportUtility = $this->objectManager->get(ImportExportUtility::class);
try {
$importResult = $importExportUtility->importT3DFile(PATH_site . $importFileToUse, 0);
$this->registry->set('extensionDataImport', $extensionSiteRelPath . 'Initialisation/dataImported', 1);
$this->emitAfterExtensionT3DImportSignal($importFileToUse, $importResult);
} catch (\ErrorException $e) {
/** @var \TYPO3\CMS\Core\Log\Logger $logger */
$logger = $this->objectManager->get(\TYPO3\CMS\Core\Log\LogManager::class)->getLogger(__CLASS__);
$logger->log(\TYPO3\CMS\Core\Log\LogLevel::WARNING, $e->getMessage());
}
}
}
/**
* Emits a signal after an t3d file was imported
*
* @param string $importFileToUse
* @param int $importResult
*/
protected function emitAfterExtensionT3DImportSignal($importFileToUse, $importResult)
{
$this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionT3DImport', [$importFileToUse, $importResult, $this]);
}
/**
* Imports a static tables SQL File (ext_tables_static+adt)
* Execution state is saved in the this->registry, so it only happens once
*
* @param string $extensionSiteRelPath
*/
protected function importStaticSqlFile($extensionSiteRelPath)
{
$extTablesStaticSqlRelFile = $extensionSiteRelPath . 'ext_tables_static+adt.sql';
if (!$this->registry->get('extensionDataImport', $extTablesStaticSqlRelFile)) {
$extTablesStaticSqlFile = PATH_site . $extTablesStaticSqlRelFile;
$shortFileHash = '';
if (file_exists($extTablesStaticSqlFile)) {
$extTablesStaticSqlContent = file_get_contents($extTablesStaticSqlFile);
$shortFileHash = md5($extTablesStaticSqlContent);
$this->importStaticSql($extTablesStaticSqlContent);
}
$this->registry->set('extensionDataImport', $extTablesStaticSqlRelFile, $shortFileHash);
$this->emitAfterExtensionStaticSqlImportSignal($extTablesStaticSqlRelFile);
}
}
/**
* Emits a signal after a static sql file was imported
*
* @param string $extTablesStaticSqlRelFile
*/
protected function emitAfterExtensionStaticSqlImportSignal($extTablesStaticSqlRelFile)
{
$this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionStaticSqlImport', [$extTablesStaticSqlRelFile, $this]);
}
/**
* Imports files from Initialisation/Files to fileadmin
* via lowlevel copy directory method
*
* @param string $extensionSiteRelPath relative path to extension dir
* @param string $extensionKey
*/
protected function importInitialFiles($extensionSiteRelPath, $extensionKey)
{
$importRelFolder = $extensionSiteRelPath . 'Initialisation/Files';
if (!$this->registry->get('extensionDataImport', $importRelFolder)) {
$importFolder = PATH_site . $importRelFolder;
if (file_exists($importFolder)) {
$destinationRelPath = $GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'] . $extensionKey;
$destinationAbsolutePath = PATH_site . $destinationRelPath;
if (!file_exists($destinationAbsolutePath) &&
GeneralUtility::isAllowedAbsPath($destinationAbsolutePath)
) {
GeneralUtility::mkdir($destinationAbsolutePath);
}
GeneralUtility::copyDirectory($importRelFolder, $destinationRelPath);
$this->registry->set('extensionDataImport', $importRelFolder, 1);
$this->emitAfterExtensionFileImportSignal($destinationAbsolutePath);
}
}
}
/**
* Emits a signal after extension files were imported
*
* @param string $destinationAbsolutePath
*/
protected function emitAfterExtensionFileImportSignal($destinationAbsolutePath)
{
$this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionFileImport', [$destinationAbsolutePath, $this]);
}
}