forked from lochmueller/calendarize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginUpdater.php
More file actions
282 lines (243 loc) Β· 10.7 KB
/
Copy pathPluginUpdater.php
File metadata and controls
282 lines (243 loc) Β· 10.7 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
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
<?php
declare(strict_types=1);
namespace HDNET\Calendarize\Updates;
use TYPO3\CMS\Core\Attribute\UpgradeWizard;
use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
/**
* This Upgrade wizard should have already been run previously. It will no longer work in TYPO3 v14 because
* the tt_content.list_type database field is missing. In TYPO3 v13, it should run before PluginListTypeToCTypeUpdate.
*/
#[UpgradeWizard('calendarize_pluginUpdater')]
class PluginUpdater extends AbstractUpdate
{
protected string $title = 'Calendarize Plugin Updater';
protected string $description = 'This wizard migrates the switchableControllerActions in all existing ' .
'plugins to the new list types. The permissions in BE groups are updated as well to allow all new ' .
'list types where necessary.';
protected array $migrationMap = [
'Calendar->list;Calendar->detail' => 'calendarize_listdetail',
'Calendar->list' => 'calendarize_list',
'Calendar->detail' => 'calendarize_detail',
'Calendar->search' => 'calendarize_search',
'Calendar->result' => 'calendarize_result',
'Calendar->latest' => 'calendarize_latest',
'Calendar->single' => 'calendarize_single',
'Calendar->year' => 'calendarize_year',
'Calendar->quarter' => 'calendarize_quarter',
'Calendar->month' => 'calendarize_month',
'Calendar->week' => 'calendarize_week',
'Calendar->day' => 'calendarize_day',
'Calendar->past' => 'calendarize_past',
'Booking->booking;Booking->send' => 'calendarize_booking',
];
public function __construct(
protected readonly QueryBuilder $contentElementsQueryBuilder,
protected readonly QueryBuilder $backendGroupsQueryBuilder,
protected readonly FlexFormTools $flexFormTools,
protected readonly Typo3Version $typo3Version
) {}
/**
* @var string
*/
public const OLD_LIST_TYPE = 'calendarize_calendar';
public function executeUpdate(): bool
{
$this->migratePlugins();
$this->migrateBackendUserGroupPermissions();
return true;
}
protected function migratePlugins(): void
{
$contentElements = $this->getContentElementsToMigrate();
$this->output->writeln('Start migration of ' . \count($contentElements) . ' plugins.');
foreach ($contentElements as $contentElement) {
$flexForm = $this->flexFormTools->convertFlexFormContentToArray($contentElement['pi_flexform']);
$newListType = $this->getNewListType($flexForm['switchableControllerActions'] ?? '');
$flexFormData = $this->removeFlexFormSettingsNotForListType($contentElement, $newListType);
if (\count($flexFormData['data']) > 0) {
$newFlexform = $this->array2xml($flexFormData);
} else {
$newFlexform = '';
}
$this->updateContentElement($contentElement['uid'], $newListType, $newFlexform);
}
$this->output->writeln('');
$this->output->writeln('');
$this->output->writeln('Migration of plugins finished.');
}
protected function getContentElementsToMigrate(): array
{
$this->contentElementsQueryBuilder->getRestrictions()->removeAll()->add(
GeneralUtility::makeInstance(DeletedRestriction::class),
);
$constraintsForActions = [];
foreach ($this->migrationMap as $action => $_) {
$constraintsForActions[] = $this->contentElementsQueryBuilder->expr()->like(
'pi_flexform',
$this->contentElementsQueryBuilder->createNamedParameter(
'%>' . $this->contentElementsQueryBuilder->escapeLikeWildcards(htmlspecialchars($action)) . '<%',
),
);
}
return $this->contentElementsQueryBuilder
->select('uid', 'list_type', 'pi_flexform', 'pid', 'CType')
->from('tt_content')
->where(
$this->contentElementsQueryBuilder->expr()->eq(
'CType',
$this->contentElementsQueryBuilder->createNamedParameter('list'),
),
$this->contentElementsQueryBuilder->expr()->eq(
'list_type',
$this->contentElementsQueryBuilder->createNamedParameter(self::OLD_LIST_TYPE),
),
$this->contentElementsQueryBuilder->expr()->or(...$constraintsForActions),
)
->executeQuery()
->fetchAllAssociative();
}
protected function getNewListType(string $actions): string
{
return $this->migrationMap[$actions] ?? self::OLD_LIST_TYPE;
}
protected function removeFlexFormSettingsNotForListType(array $contentElement, string $newListType): array
{
// Update record with new list_type (this is needed because FlexFormTools
// looks up those values in the given record and assumes they're up-to-date)
$contentElement['list_type'] = $newListType;
$newFlexform = $this->flexFormTools->cleanFlexFormXML('tt_content', 'pi_flexform', $contentElement);
$flexFormData = GeneralUtility::xml2array($newFlexform);
return $this->removeEmptyFlexFormSheets($flexFormData);
}
protected function removeEmptyFlexFormSheets(array $flexFormData): array
{
foreach ($flexFormData['data'] as $sheetKey => $sheetData) {
// Remove empty sheets
if (!\is_array($flexFormData['data'][$sheetKey]['lDEF']) || !\count($flexFormData['data'][$sheetKey]['lDEF']) > 0) {
unset($flexFormData['data'][$sheetKey]);
}
}
return $flexFormData;
}
protected function array2xml(array $input = []): string
{
$options = [
'parentTagMap' => [
'data' => 'sheet',
'sheet' => 'language',
'language' => 'field',
'el' => 'field',
'field' => 'value',
'field:el' => 'el',
'el:_IS_NUM' => 'section',
'section' => 'itemType',
],
'disableTypeAttrib' => 2,
];
$spaceInd = 4;
$output = GeneralUtility::array2xml($input, '', 0, 'T3FlexForms', $spaceInd, $options);
return '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>' . "\n" . $output;
}
protected function updateContentElement(int $uid, string $newListType, string $flexform): void
{
$this->contentElementsQueryBuilder->update('tt_content')
->set('list_type', $newListType)
->set('pi_flexform', $flexform)
->where(
$this->contentElementsQueryBuilder->expr()->in(
'uid',
$this->contentElementsQueryBuilder->createNamedParameter($uid, Connection::PARAM_INT),
),
)
->executeStatement();
}
protected function migrateBackendUserGroupPermissions(): void
{
$groups = $this->getBackendUserGroupsToMigrate();
$this->output->writeln('');
$this->output->writeln('');
$this->output->writeln('Start migration of ' . \count($groups) . ' BE groups.');
foreach ($groups as $group) {
$this->updateBackendUserGroup($group);
}
$this->output->writeln('');
$this->output->writeln('');
$this->output->writeln('Migration of backend groups finished.');
}
protected function getBackendUserGroupsToMigrate(): array
{
$this->backendGroupsQueryBuilder->getRestrictions()->removeAll()->add(
GeneralUtility::makeInstance(DeletedRestriction::class),
);
return $this->backendGroupsQueryBuilder
->select('uid', 'explicit_allowdeny')
->from('be_groups')
->where(
$this->backendGroupsQueryBuilder->expr()->like(
'explicit_allowdeny',
$this->backendGroupsQueryBuilder->createNamedParameter(
'%' . $this->backendGroupsQueryBuilder->escapeLikeWildcards($this->getOldListTypeForGroupPermissions()) . '%',
),
),
$this->backendGroupsQueryBuilder->expr()->notLike(
'explicit_allowdeny',
$this->backendGroupsQueryBuilder->createNamedParameter(
'%' . $this->backendGroupsQueryBuilder->escapeLikeWildcards($this->getNewListTypesForGroupPermissions()) . '%',
),
),
)
->executeQuery()
->fetchAllAssociative();
}
protected function updateBackendUserGroup(array $group): void
{
$default = $this->getNewListTypesForGroupPermissions();
$searchReplace = [
$this->getOldListTypeForGroupPermissions() . ':ALLOW' => $default,
$this->getOldListTypeForGroupPermissions() . ':DENY' => '',
$this->getOldListTypeForGroupPermissions() => $default,
];
$newList = str_replace(array_keys($searchReplace), array_values($searchReplace), $group['explicit_allowdeny']);
$this->backendGroupsQueryBuilder->update('be_groups')
->set('explicit_allowdeny', $newList)
->where(
$this->backendGroupsQueryBuilder->expr()->in(
'uid',
$this->backendGroupsQueryBuilder->createNamedParameter($group['uid'], Connection::PARAM_INT),
),
)
->executeStatement();
}
protected function getOldListTypeForGroupPermissions(): string
{
return 'tt_content:list_type:' . self::OLD_LIST_TYPE;
}
protected function getNewListTypesForGroupPermissions(): string
{
$groupPermissions = $this->getOldListTypeForGroupPermissions();
foreach ($this->migrationMap as $newListType) {
$groupPermissions .= ',tt_content:list_type:' . $newListType;
}
return $groupPermissions;
}
public function updateNecessary(): bool
{
// in TYPO3 v14 it is no longer possible to perform this wizard as the list_type field is missing!
if ($this->typo3Version->getMajorVersion() >= 14) {
return false;
}
return \count($this->getContentElementsToMigrate()) > 0 || \count($this->getBackendUserGroupsToMigrate()) > 0;
}
public function getPrerequisites(): array
{
return [
DatabaseUpdatedPrerequisite::class,
];
}
}