forked from FluidTYPO3/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynFlexMigration.php
292 lines (264 loc) · 10.7 KB
/
DynFlexMigration.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
<?php
/**
* Class for migrating user extensions using FED page and fce template
* feature to flux
* @TODO make this a base class that can be used by other migrations?
* maybe even implement something like "migration recipes"?
*/
class DynFlexMigration {
const CONFIG_EXT = 'extension';
const CONFIG_MODE = 'mode';
const CONFIG_MODE_AUTO = 'auto';
const CONFIG_MODE_INTERACTIVE = 'interactive';
const CONFIG_MODE_DRY = 'dry';
const CONFIG_TARGET = 'target';
const CONFIG_TARGET_TEMPLATES = 'templates';
const CONFIG_TARGET_DATABASE = 'database';
const CONFIG_TEMPLATE_TYPES = 'fileextensions';
const CONFIG_TEMPLATE_DIRECTORIES = 'templatedirs';
const CONFIG_OMIT_BACKUP_WARNING = 'omitbackupwarning';
/**
* @var t3lib_cli
*/
protected $cli;
/**
* @var Tx_Extbase_Object_ObjectManager
*/
protected $objectManager;
/**
*
* @var Tx_Flux_Configuration_ConfigurationManager
*/
protected $configurationManager;
/**
* @var Tx_Flux_Service_FlexForm
*/
protected $flexformService;
/**
* @var Tx_Flux_Provider_ConfigurationService
*/
protected $configurationService;
/**
* @var array migration configuration
*/
protected $migrationConfiguration;
/**
* CONSTRUCTOR
* @var t3lib_cli $cli command line i/f
*/
public function __construct(t3lib_cli $cli) {
$this->cli = $cli;
$this->objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
$this->configurationManager = $this->objectManager->get('Tx_Flux_Configuration_ConfigurationManager');
$this->flexformService = $this->objectManager->get('Tx_Flux_Service_FlexForm');
$this->configurationService = $this->objectManager->get('Tx_Flux_Provider_ConfigurationService');
}
/**
* @param array $configuration
* @return void
*/
public function migrate($configuration) {
$this->migrationConfiguration = $configuration;
$this->cli->cli_echo('Migration settings:' . PHP_EOL);
$this->cli->cli_echo(' Extension to parse: ' . ($configuration[self::CONFIG_EXT] == '*' ? '*all*' : $configuration[self::CONFIG_EXT]) . PHP_EOL);
$this->cli->cli_echo(' Template extensions to parse: ' . join(', ', $configuration[self::CONFIG_TEMPLATE_TYPES]) . PHP_EOL);
$this->cli->cli_echo(' Template directories to parse: ' . join(', ', $configuration[self::CONFIG_TEMPLATE_DIRECTORIES]) . PHP_EOL . PHP_EOL);
if($configuration[self::CONFIG_MODE] != self::CONFIG_MODE_DRY && !$configuration[self::CONFIG_OMIT_BACKUP_WARNING]) {
$this->cli->cli_echo(PHP_EOL . 'WARNING! There is no BACKUP mechanism in place - make sure to take care on your own!' . PHP_EOL, TRUE);
if(!$this->cli->cli_keyboardInput_yes('Continue')) {
exit;
}
}
if ($configuration[self::CONFIG_TARGET][self::CONFIG_TARGET_TEMPLATES]) {
$this->migrateTemplates();
}
if ($configuration[self::CONFIG_TARGET][self::CONFIG_TARGET_DATABASE]) {
$this->migrateDatabase();
}
if (!$configuration[self::CONFIG_MODE_DRY]) {
$this->clearCaches();
}
}
/**
* do template migration
* @return void
*/
protected function migrateTemplates() {
$cli = $this->cli;
$cli->cli_echo(PHP_EOL . 'MIGRATING TEMPLATES' . PHP_EOL);
// retrieve absolute and relative directories
$absoluteDirectories = $relaviveDirectories = array();
foreach ($this->migrationConfiguration[self::CONFIG_TEMPLATE_DIRECTORIES] as $directory) {
if (strpos($directory, DIRECTORY_SEPARATOR) === 0){
//echo 'FOUND ABSOLUTE';
$absoluteDirectories[] = substr($directory, 1);
} else {
$relaviveDirectories[] = $directory;
}
}
// parse absolute direcories
$cli->cli_echo(PHP_EOL . 'Parsing absolute directories' . PHP_EOL);
foreach($absoluteDirectories as $directory) {
$cli->cli_echo($cli->cli_indent('Parsing ' . $directory . '...' . PHP_EOL, 2));
$files = t3lib_div::getAllFilesAndFoldersInPath(array(), $directory . '/', join(',', $this->migrationConfiguration[self::CONFIG_TEMPLATE_TYPES]));
foreach ($files as $file) {
$this->migrateFile($file);
}
}
// parse extensions
if(($ext = $this->migrationConfiguration[self::CONFIG_EXT]) == '*') {
$this->migrationConfiguration[self::CONFIG_EXT] = t3lib_div::get_dirs('typo3conf/ext/');
} else {
$this->migrationConfiguration[self::CONFIG_EXT] = array($ext);
}
foreach($this->migrationConfiguration[self::CONFIG_EXT] as $ext) {
if (t3lib_extMgm::isLoaded($ext) === FALSE) {
continue;
}
$cli->cli_echo(PHP_EOL . 'Parsing ' . $ext . '...' . PHP_EOL);
foreach ($relaviveDirectories as $directory) {
$cli->cli_echo($cli->cli_indent(PHP_EOL . 'Parsing ' . $directory . '...' . PHP_EOL, 2));
$path = t3lib_extMgm::siteRelPath($ext) . $directory . '/';
$files = t3lib_div::getAllFilesAndFoldersInPath(array(), $path, join(',', $this->migrationConfiguration[self::CONFIG_TEMPLATE_TYPES]));//, TRUE);
foreach ($files as $file) {
$this->migrateFile($file, $path);
}
}
}
}
protected function migrateFile($fileName, $stripPath = NULL) {
$cli = $this->cli;
$oldTags = array(
array('fed:flexform.group', 'fed:page.group', 'fed:fce.group'),
array('fed:fce', 'fed:flexform'),
array('fed:page.field.'),
array('fed:page '),
array('<\/fed:page>')
);
$newTags = array(
'flux:flexform.sheet',
'flux:flexform',
'flux:flexform.field.',
'flux:flexform ',
'</flux:flexform>'
);
$fileInfo = pathinfo($fileName);
if ($stripPath !== NULL) {
$displayFilename = str_replace($stripPath, '', $fileName);
} else {
$displayFilename = $fileInfo['basename'];
}
$cli->cli_echo($cli->cli_indent('Parsing ' . $displayFilename . PHP_EOL, 2));
$file = file_get_contents($fileName);
$fileBackup = $file;
// replace tags
$i=0;
foreach ($newTags as $newTag) {
$file = preg_replace('/(' . implode('|', $oldTags[$i++]) . ')/um', $newTag, $file);
}
// add flux namespace if it does not exist and is required
$fluxNamespace = '{namespace flux=Tx_Flux_ViewHelpers}';
$fluxTag = 'flux:';
if ((strpos($file, $fluxNamespace) === FALSE) && (strpos($file, $fluxTag) !== FALSE)) {
$file = $fluxNamespace . LF . $file;
}
// add flux:widget.grid if required
$previewSection = NULL;
$previewEndingTagPosition = FALSE;
$previewOpeningTagPosition = strpos($file, '<f:section name="Preview"');
if ($previewOpeningTagPosition === FALSE) {
$previewOpeningTagPosition = strpos($file, "<f:section name='Preview'");
}
if ($previewOpeningTagPosition !== FALSE) {
$previewEndingTagPosition = strpos($file, "</f:section", $previewOpeningTagPosition);
} elseif (strpos($file, 'flux:flexform.content') !== FALSE && strpos($file, 'flux:widget.grid') === FALSE) {
$previewSection = "<f:section name=\"Preview\">\n\t<flux:widget.grid />\n</f:section>\n\n";
} elseif ($previewEndTagPosition === FALSE) {
$previewSection = '<f:section name="Preview"></f:section>' . LF;
}
$splitPoint = FALSE;
if ($previewSection) {
// completely new Preview section - add it after the Configuration section
$configurationSectionOpeningTagPosition = strpos($file, '<f:section name="Configuration"');
if ($configurationSectionOpeningTagPosition === FALSE) {
$configurationSectionOpeningTagPosition = strpos($file, "<f:section name='Configuration'");
}
$configurationSectionClosingTagPosition = strpos($file, '</f:section', $configurationSectionOpeningTagPosition);
$splitPoint = $configurationSectionClosingTagPosition;
} elseif ($previewEndingTagPosition !== FALSE && strpos($file, 'flux:flexform.content') !== FALSE && strpos($file, 'flux:widget.grid') === FALSE) {
// preview section only needs the grid Widget, set splitPoint and merged "section" markup accordingly
$splitPoint = $previewEndTagPosition - 12;
$previewSection = "\t<flux:widget.grid />" . LF;
}
if ($previewSection !== NULL && $splitPoint !== FALSE) {
$before = substr($file, 0, $splitPoint);
$after = substr($file, $splitPoint);
$file = $before . $previewSection . $after;
}
// display / store changes
if ($file != $fileBackup) {
$file = trim($file);
$doStoreTemplate = FALSE;
$cli->cli_echo($cli->cli_indent('file modified!' . PHP_EOL, 4), TRUE);
//if ($this->migrationConfiguration[self::CONFIG_MODE] == self::CONFIG_MODE_DRY || $this->migrationConfiguration[self::CONFIG_MODE] == self::CONFIG_MODE_DRY) {
$tempFileLeft = PATH_site . 'typo3temp/' . uniqid('fluxmigrateTempFile_');
$tempFileRight = PATH_site . 'typo3temp/' . uniqid('fluxmigrateTempFile_');
t3lib_div::writeFile($tempFileLeft, $fileBackup);
t3lib_div::writeFile($tempFileRight, $file);
$command = 'diff ' . $tempFileLeft . ' ' . $tempFileRight;
$output = shell_exec($command);
unlink($tempFileLeft);
unlink($tempFileRight);
$cli->cli_echo($cli->cli_indent($output . LF, 0));
//}
if ($this->migrationConfiguration[self::CONFIG_MODE] == self::CONFIG_MODE_INTERACTIVE){
if($cli->cli_keyboardInput_yes('Save modifications')){
$doStoreTemplate = TRUE;
}
}
if ($this->migrationConfiguration[self::CONFIG_MODE] == self::CONFIG_MODE_AUTO || $doStoreTemplate) {
file_put_contents($fileName, $file);
}
}
}
/**
* do database migration
* @return void
*/
protected function migrateDatabase() {
$this->cli->cli_echo(PHP_EOL . 'MIGRATING DATABASE' . PHP_EOL);
$doDbUpdate = FALSE;
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid, tx_fed_fcecontentarea', 'tt_content', 'deleted=0 AND tx_fed_fcecontentarea <> "" AND tx_flux_column = ""');
$numberOfRows = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
if($numberOfRows) {
$this->cli->cli_echo($numberOfRows . ' tt_content entries need to get updated' . PHP_EOL);
if($this->migrationConfiguration[self::CONFIG_MODE] == self::CONFIG_MODE_INTERACTIVE) {
if($this->cli->cli_keyboardInput_yes('Update DB entries')) {
$doDbUpdate = TRUE;
}
}
} else {
$this->cli->cli_echo('tt_content entries are up to date' . PHP_EOL);
}
if ($numberOfRows && ($this->migrationConfiguration[self::CONFIG_MODE] == self::CONFIG_MODE_AUTO || $doDbUpdate)){
// update DB if needed
$this->cli->cli_echo('updateing tt_content entries...' . PHP_EOL);
while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tt_content', 'uid='.$row['uid'], array('tx_flux_column' => $row['tx_fed_fcecontentarea']));
}
// sanity check
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid, tx_fed_fcecontentarea', 'tt_content', 'deleted=0 AND tx_fed_fcecontentarea <> "" AND tx_flux_column = ""');
$numberOfFailedRows = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
if ($numberOfFailedRows) {
$this->cli->cli_echo('WARNING! Failed updating ' . $numberOfFailedRows . ' tt_content entries' . PHP_EOL, TRUE);
} else {
$this->cli->cli_echo('SUCCESS...' . PHP_EOL);
}
}
}
/**
*
*/
protected function clearCaches() {
}
}