-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAbstractSiteUpgrade.php
More file actions
88 lines (78 loc) · 2.73 KB
/
AbstractSiteUpgrade.php
File metadata and controls
88 lines (78 loc) · 2.73 KB
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
<?php
declare(strict_types=1);
namespace In2code\Lux\Update\AddSites;
use In2code\Lux\Domain\Service\SiteService;
use In2code\Lux\Utility\DatabaseUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
abstract class AbstractSiteUpgrade
{
/**
* [
* 123 => 'site 1',
* 124 => 'site 2',
* ]
*
* @var array
*/
protected array $mapping = [];
protected SiteService $siteService;
public function __construct()
{
$this->siteService = GeneralUtility::makeInstance(SiteService::class);
}
public function run(): void
{
$connection = $this->getConnectionPool()->getConnectionForTable(static::TABLE_NAME);
$queryBuilder = $this->getPreparedQueryBuilder();
$result = $queryBuilder
->select('page')
->groupBy('page')
->where(
$queryBuilder->expr()->eq('site', $queryBuilder->createNamedParameter(''))
)
->executeQuery();
while ($record = $result->fetchAssociative()) {
$siteIdentifier = $this->getSiteIdentifierFromPage($record['page']);
if ($siteIdentifier !== '') {
$connection->update(
static::TABLE_NAME,
['site' => $siteIdentifier],
['page' => (int)$record['page']]
);
} else {
$connection->update(
static::TABLE_NAME,
['deleted' => 1],
['page' => (int)$record['page']]
);
}
}
}
protected function getSiteIdentifierFromPage(int $pageIdentifier): string
{
if (array_key_exists($pageIdentifier, $this->mapping)) {
return $this->mapping[$pageIdentifier];
}
$siteIdentifier = '';
$site = $this->siteService->getSiteFromPageIdentifier($pageIdentifier);
if ($site !== null) {
$siteIdentifier = $site->getIdentifier();
}
$this->mapping[$pageIdentifier] = $siteIdentifier;
return $siteIdentifier;
}
protected function getPreparedQueryBuilder(): QueryBuilder
{
$queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable(static::TABLE_NAME);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$queryBuilder->from(static::TABLE_NAME);
return $queryBuilder;
}
protected function getConnectionPool(): ConnectionPool
{
return GeneralUtility::makeInstance(ConnectionPool::class);
}
}