5146b0f378ad21495b6a55f03356fa161dc2aa93
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Hooks / FormFileExtensionUpdate.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Form\Hooks;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
19 use TYPO3\CMS\Core\Database\Connection;
20 use TYPO3\CMS\Core\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
22 use TYPO3\CMS\Core\Database\ReferenceIndex;
23 use TYPO3\CMS\Core\Localization\LanguageService;
24 use TYPO3\CMS\Core\Resource\DuplicationBehavior;
25 use TYPO3\CMS\Core\Resource\File;
26 use TYPO3\CMS\Core\Resource\ResourceFactory;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Core\Utility\PathUtility;
29 use TYPO3\CMS\Core\Utility\StringUtility;
30 use TYPO3\CMS\Extbase\Object\ObjectManager;
31 use TYPO3\CMS\Form\Mvc\Configuration\YamlSource;
32 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManager;
33 use TYPO3\CMS\Form\Slot\FilePersistenceSlot;
34 use TYPO3\CMS\Install\Updates\AbstractUpdate;
35
36 /**
37 * Update wizard to migrate all forms currently in use to new ending
38 */
39 class FormFileExtensionUpdate extends AbstractUpdate
40 {
41
42 /**
43 * @var string
44 */
45 protected $title = 'Rename form definition file extension from .yaml to .form.yaml';
46
47 /**
48 * @var FormPersistenceManager
49 */
50 protected $persistenceManager;
51
52 /**
53 * @var YamlSource
54 */
55 protected $yamlSource;
56
57 /**
58 * @var ResourceFactory
59 */
60 protected $resourceFactory;
61
62 /**
63 * @var ReferenceIndex
64 */
65 protected $referenceIndex;
66
67 /**
68 * @var FlexFormTools
69 */
70 protected $flexFormTools;
71
72 /**
73 * @var Connection
74 */
75 protected $connection;
76
77 /**
78 * Checks whether updates are required.
79 *
80 * @param string &$description The description for the update
81 * @return bool Whether an update is required (TRUE) or not (FALSE)
82 */
83 public function checkForUpdate(&$description)
84 {
85 $updateNeeded = false;
86 $information = [];
87
88 $this->persistenceManager = $this->getObjectManager()->get(FormPersistenceManager::class);
89 $this->yamlSource = $this->getObjectManager()->get(YamlSource::class);
90 $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
91
92 foreach ($this->getFormDefinitionsInformation() as $formDefinitionInformation) {
93 if (
94 (
95 $formDefinitionInformation['hasNewFileExtension'] === true
96 && $formDefinitionInformation['hasReferencesForOldFileExtension'] === false
97 && $formDefinitionInformation['hasReferencesForNewFileExtension'] === false
98 )
99 || (
100 $formDefinitionInformation['hasNewFileExtension'] === false
101 && $formDefinitionInformation['location'] === 'extension'
102 && $formDefinitionInformation['hasReferencesForOldFileExtension'] === false
103 && $formDefinitionInformation['hasReferencesForNewFileExtension'] === false
104 )
105 ) {
106 continue;
107 }
108
109 if (
110 $formDefinitionInformation['hasNewFileExtension'] === false
111 && $formDefinitionInformation['location'] === 'storage'
112 ) {
113 $updateNeeded = true;
114 $information['rename'] = 'Form definition files were found that should be migrated to be named .form.yaml.';
115 }
116
117 if (
118 $formDefinitionInformation['hasNewFileExtension']
119 && $formDefinitionInformation['hasReferencesForOldFileExtension']
120 ) {
121 $updateNeeded = true;
122 $information['updateReference'] = 'Referenced form definition files found that should be updated.';
123 }
124
125 if (
126 $formDefinitionInformation['referencesForOldFileExtensionNeedsFlexformUpdates'] === true
127 || $formDefinitionInformation['referencesForNewFileExtensionNeedsFlexformUpdates'] === true
128 ) {
129 $updateNeeded = true;
130 if ($formDefinitionInformation['hasNewFileExtension'] === true) {
131 $information['updateReference'] = 'Referenced form definition files found that should be updated.';
132 } else {
133 if ($formDefinitionInformation['location'] === 'storage') {
134 $information['updateReference'] = 'Referenced form definition files found that should be updated.';
135 } else {
136 $information['manualStepsNeeded'] =
137 'There are references to form definitions which are located in extensions and thus cannot be renamed automatically by this wizard.'
138 . 'This form definitions from extensions that do not end with .form.yaml have to be renamed by hand!'
139 . 'After that you can run this wizard again to migrate the references.';
140 }
141 }
142 }
143 }
144
145 $description = implode('<br>', $information);
146
147 return $updateNeeded;
148 }
149
150 /**
151 * Performs the accordant updates.
152 *
153 * @param array &$dbQueries Queries done in this update
154 * @param string &$customMessage Custom message
155 * @return bool Whether everything went smoothly or not
156 */
157 public function performUpdate(array &$dbQueries, &$customMessage): bool
158 {
159 $messages = [];
160
161 $GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageService::class);
162 $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
163 $filePersistenceSlot = GeneralUtility::makeInstance(FilePersistenceSlot::class);
164
165 $this->connection = $connectionPool->getConnectionForTable('tt_content');
166 $this->persistenceManager = $this->getObjectManager()->get(FormPersistenceManager::class);
167 $this->yamlSource = $this->getObjectManager()->get(YamlSource::class);
168 $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
169 $this->referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class);
170 $this->flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
171
172 $filePersistenceSlot->defineInvocation(
173 FilePersistenceSlot::COMMAND_FILE_RENAME,
174 true
175 );
176
177 $formDefinitionsInformation = $this->getFormDefinitionsInformation();
178 foreach ($formDefinitionsInformation as $currentPersistenceIdentifier => $formDefinitionInformation) {
179 if (
180 (
181 $formDefinitionInformation['hasNewFileExtension'] === true
182 && $formDefinitionInformation['hasReferencesForOldFileExtension'] === false
183 && $formDefinitionInformation['hasReferencesForNewFileExtension'] === false
184 )
185 || (
186 $formDefinitionInformation['hasNewFileExtension'] === false
187 && $formDefinitionInformation['location'] === 'extension'
188 && $formDefinitionInformation['hasReferencesForOldFileExtension'] === false
189 && $formDefinitionInformation['hasReferencesForNewFileExtension'] === false
190 )
191 ) {
192 continue;
193 }
194
195 if (
196 $formDefinitionInformation['hasNewFileExtension'] === true
197 && (
198 $formDefinitionInformation['hasReferencesForOldFileExtension'] === true
199 || $formDefinitionInformation['hasReferencesForNewFileExtension'] === true
200 )
201 ) {
202 foreach ($formDefinitionInformation['referencesForOldFileExtension'] as $referenceForOldFileExtension) {
203 $newFlexformXml = $this->generateNewFlexformForReference(
204 $referenceForOldFileExtension,
205 $referenceForOldFileExtension['sheetIdentifiersWhichNeedsUpdate'],
206 $formDefinitionInformation['persistenceIdentifier']
207 );
208 $this->updateContentReference(
209 $referenceForOldFileExtension['ttContentUid'],
210 $newFlexformXml,
211 true
212 );
213 }
214
215 foreach ($formDefinitionInformation['referencesForNewFileExtension'] as $referenceForNewFileExtension) {
216 $newFlexformXml = $this->generateNewFlexformForReference(
217 $referenceForNewFileExtension,
218 $referenceForNewFileExtension['sheetIdentifiersWhichNeedsUpdate']
219 );
220 $this->updateContentReference(
221 $referenceForNewFileExtension['ttContentUid'],
222 $newFlexformXml
223 );
224 }
225
226 continue;
227 }
228
229 if ($formDefinitionInformation['location'] === 'storage') {
230 $file = $formDefinitionInformation['file'];
231
232 $newPossiblePersistenceIdentifier = $this->persistenceManager->getUniquePersistenceIdentifier(
233 $file->getNameWithoutExtension(),
234 $file->getParentFolder()->getCombinedIdentifier()
235 );
236 $newFileName = PathUtility::pathinfo(
237 $newPossiblePersistenceIdentifier,
238 PATHINFO_BASENAME
239 );
240
241 try {
242 $file->rename($newFileName, DuplicationBehavior::RENAME);
243 $newPersistenceIdentifier = $file->getCombinedIdentifier();
244 } catch (\Exception $e) {
245 $messages[] = sprintf(
246 'Failed to rename form definition "%s" to "%s".',
247 $formDefinitionInformation['persistenceIdentifier'],
248 $newFileName
249 );
250 continue;
251 }
252
253 if (
254 $formDefinitionInformation['hasReferencesForOldFileExtension'] === true
255 || $formDefinitionInformation['hasReferencesForNewFileExtension'] === true
256 ) {
257 foreach ($formDefinitionInformation['referencesForOldFileExtension'] as $referenceForOldFileExtension) {
258 $sheetIdentifiersWhichNeedsUpdate = $this->getSheetIdentifiersWhichNeedsUpdate(
259 $referenceForOldFileExtension['flexform'],
260 $formDefinitionsInformation,
261 $currentPersistenceIdentifier,
262 $formDefinitionInformation['persistenceIdentifier'],
263 $newPersistenceIdentifier
264 );
265 $newFlexformXml = $this->generateNewFlexformForReference(
266 $referenceForOldFileExtension,
267 $sheetIdentifiersWhichNeedsUpdate,
268 $newPersistenceIdentifier
269 );
270 $this->updateContentReference(
271 $referenceForOldFileExtension['ttContentUid'],
272 $newFlexformXml
273 );
274 }
275
276 foreach ($formDefinitionInformation['referencesForNewFileExtension'] as $referenceForNewFileExtension) {
277 $sheetIdentifiersWhichNeedsUpdate = $this->getSheetIdentifiersWhichNeedsUpdate(
278 $referenceForNewFileExtension['flexform'],
279 $formDefinitionsInformation,
280 $currentPersistenceIdentifier,
281 $formDefinitionInformation['persistenceIdentifier'],
282 $newPersistenceIdentifier
283 );
284 $newFlexformXml = $this->generateNewFlexformForReference(
285 $referenceForNewFileExtension,
286 $sheetIdentifiersWhichNeedsUpdate,
287 $newPersistenceIdentifier
288 );
289 $this->updateContentReference(
290 $referenceForNewFileExtension['ttContentUid'],
291 $newFlexformXml
292 );
293 }
294 }
295 } else {
296 $messages[] = sprintf(
297 'Failed to rename form definition "%s" to "%s". You have to be rename it by hand!. '
298 . 'After that you can run this wizard again to migrate the references.',
299 $formDefinitionInformation['persistenceIdentifier'],
300 $this->getNewPersistenceIdentifier($formDefinitionInformation['persistenceIdentifier'])
301 );
302 }
303 }
304
305 $filePersistenceSlot->defineInvocation(
306 FilePersistenceSlot::COMMAND_FILE_RENAME,
307 null
308 );
309
310 if (count($messages) > 0) {
311 $customMessage = 'The following issues occurred during performing updates:'
312 . '<br><ul><li>' . implode('</li><li>', $messages) . '</li></ul>';
313 return false;
314 }
315
316 return true;
317 }
318
319 /**
320 * @return array
321 */
322 protected function getFormDefinitionsInformation(): array
323 {
324 $formDefinitionsInformation = array_merge(
325 $this->getFormDefinitionsInformationFromStorages(),
326 $this->getFormDefinitionsInformationFromExtensions()
327 );
328
329 $formDefinitionsInformation = $this->enrichFormDefinitionsInformationWithDataFromReferences($formDefinitionsInformation);
330
331 return $formDefinitionsInformation;
332 }
333
334 /**
335 * @return array
336 */
337 protected function getFormDefinitionsInformationFromStorages(): array
338 {
339 $formDefinitionsInformation = [];
340
341 foreach ($this->persistenceManager->retrieveYamlFilesFromStorageFolders() as $file) {
342 $persistenceIdentifier = $file->getCombinedIdentifier();
343
344 $formDefinition = $this->getFormDefinition($file);
345 if (empty($formDefinition)) {
346 continue;
347 }
348
349 $formDefinitionsInformation[$persistenceIdentifier] = $this->setFormDefinitionInformationData(
350 $persistenceIdentifier,
351 $formDefinition,
352 $file,
353 'storage'
354 );
355 }
356
357 return $formDefinitionsInformation;
358 }
359
360 /**
361 * @return array
362 */
363 protected function getFormDefinitionsInformationFromExtensions(): array
364 {
365 $formDefinitionsInformation = [];
366
367 foreach ($this->persistenceManager->retrieveYamlFilesFromExtensionFolders() as $persistenceIdentifier => $_) {
368 try {
369 $file = $this->resourceFactory->retrieveFileOrFolderObject($persistenceIdentifier);
370 } catch (\Exception $exception) {
371 continue;
372 }
373
374 $formDefinition = $this->getFormDefinition($file);
375 if (empty($formDefinition)) {
376 continue;
377 }
378
379 $formDefinitionsInformation[$persistenceIdentifier] = $this->setFormDefinitionInformationData(
380 $persistenceIdentifier,
381 $formDefinition,
382 $file,
383 'extension'
384 );
385 }
386
387 return $formDefinitionsInformation;
388 }
389
390 /**
391 * @param string $persistenceIdentifier
392 * @param array $formDefinition
393 * @param File $file
394 * @param string $localtion
395 * @return array
396 */
397 protected function setFormDefinitionInformationData(
398 string $persistenceIdentifier,
399 array $formDefinition,
400 File $file,
401 string $localtion
402 ): array {
403 return [
404 'location' => $localtion,
405 'persistenceIdentifier' => $persistenceIdentifier,
406 'prototypeName' => $formDefinition['prototypeName'],
407 'formIdentifier' => $formDefinition['identifier'],
408 'file' => $file,
409 'referencesForOldFileExtension' => [],
410 'referencesForNewFileExtension' => [],
411 'hasNewFileExtension' => $this->hasNewFileExtension($persistenceIdentifier),
412 'hasReferencesForOldFileExtension' => false,
413 'hasReferencesForNewFileExtension' => false,
414 'referencesForOldFileExtensionNeedsFlexformUpdates' => false,
415 'referencesForNewFileExtensionNeedsFlexformUpdates' => false,
416 ];
417 }
418
419 /**
420 * @param array $formDefinitionsInformation
421 * @return array
422 */
423 protected function enrichFormDefinitionsInformationWithDataFromReferences(array $formDefinitionsInformation): array
424 {
425 foreach ($this->getAllFlexformFieldsFromFormPlugins() as $pluginData) {
426 if (empty($pluginData['pi_flexform'])) {
427 continue;
428 }
429 $flexform = GeneralUtility::xml2array($pluginData['pi_flexform']);
430 $referencedPersistenceIdentifier = $this->getPersistenceIdentifierFromFlexform($flexform);
431 $referenceHasNewFileExtension = $this->hasNewFileExtension($referencedPersistenceIdentifier);
432 $possibleOldReferencedPersistenceIdentifier = $this->getOldPersistenceIdentifier($referencedPersistenceIdentifier);
433 $possibleNewReferencedPersistenceIdentifier = $this->getNewPersistenceIdentifier($referencedPersistenceIdentifier);
434
435 $referenceData = [
436 'scope' => null,
437 'ttContentUid' => (int)$pluginData['uid'],
438 'flexform' => $flexform,
439 'sheetIdentifiersWhichNeedsUpdate' => [],
440 ];
441
442 $targetPersistenceIdentifier = null;
443 if (array_key_exists($referencedPersistenceIdentifier, $formDefinitionsInformation)) {
444 $targetPersistenceIdentifier = $referencedPersistenceIdentifier;
445 if ($referenceHasNewFileExtension) {
446 $referenceData['scope'] = 'referencesForNewFileExtension';
447 } else {
448 $referenceData['scope'] = 'referencesForOldFileExtension';
449 }
450 } else {
451 if ($referenceHasNewFileExtension) {
452 if (array_key_exists($possibleOldReferencedPersistenceIdentifier, $formDefinitionsInformation)) {
453 $targetPersistenceIdentifier = $possibleOldReferencedPersistenceIdentifier;
454 $referenceData['scope'] = 'referencesForNewFileExtension';
455 } else {
456 // There is no existing file for this reference
457 continue;
458 }
459 } else {
460 if (array_key_exists($possibleNewReferencedPersistenceIdentifier, $formDefinitionsInformation)) {
461 $targetPersistenceIdentifier = $possibleNewReferencedPersistenceIdentifier;
462 $referenceData['scope'] = 'referencesForOldFileExtension';
463 } else {
464 // There is no existing file for this reference
465 continue;
466 }
467 }
468 }
469
470 $referenceData['sheetIdentifiersWhichNeedsUpdate'] = $this->getSheetIdentifiersWhichNeedsUpdate(
471 $flexform,
472 $formDefinitionsInformation,
473 $targetPersistenceIdentifier,
474 $possibleOldReferencedPersistenceIdentifier,
475 $possibleNewReferencedPersistenceIdentifier
476 );
477
478 $scope = $referenceData['scope'];
479
480 $formDefinitionsInformation[$targetPersistenceIdentifier][$scope][] = $referenceData;
481 if ($scope === 'referencesForOldFileExtension') {
482 $formDefinitionsInformation[$targetPersistenceIdentifier]['hasReferencesForOldFileExtension'] = true;
483 $formDefinitionsInformation[$targetPersistenceIdentifier]['referencesForOldFileExtensionNeedsFlexformUpdates'] = !empty($referenceData['sheetIdentifiersWhichNeedsUpdate']);
484 } else {
485 $formDefinitionsInformation[$targetPersistenceIdentifier]['hasReferencesForNewFileExtension'] = true;
486 $formDefinitionsInformation[$targetPersistenceIdentifier]['referencesForNewFileExtensionNeedsFlexformUpdates'] = !empty($referenceData['sheetIdentifiersWhichNeedsUpdate']);
487 }
488 }
489
490 return $formDefinitionsInformation;
491 }
492
493 /**
494 * @param array $flexform
495 * @param array $formDefinitionsInformation
496 * @param string $targetPersistenceIdentifier
497 * @param string $possibleOldReferencedPersistenceIdentifier
498 * @param string $possibleNewReferencedPersistenceIdentifier
499 * @return array
500 */
501 protected function getSheetIdentifiersWhichNeedsUpdate(
502 array $flexform,
503 array $formDefinitionsInformation,
504 string $targetPersistenceIdentifier,
505 string $possibleOldReferencedPersistenceIdentifier,
506 string $possibleNewReferencedPersistenceIdentifier
507 ): array {
508 $sheetIdentifiersWhichNeedsUpdate = [];
509
510 $sheetIdentifiers = $this->getSheetIdentifiersForFinisherOverrides($flexform);
511 foreach ($sheetIdentifiers as $currentSheetIdentifier => $finisherIdentifier) {
512 $sheetIdentifierForOldPersistenceIdentifier = $this->buildExpectedSheetIdentifier(
513 $possibleOldReferencedPersistenceIdentifier,
514 $formDefinitionsInformation[$targetPersistenceIdentifier]['prototypeName'],
515 $formDefinitionsInformation[$targetPersistenceIdentifier]['formIdentifier'],
516 $finisherIdentifier
517 );
518
519 $sheetIdentifierForNewPersistenceIdentifier = $this->buildExpectedSheetIdentifier(
520 $possibleNewReferencedPersistenceIdentifier,
521 $formDefinitionsInformation[$targetPersistenceIdentifier]['prototypeName'],
522 $formDefinitionsInformation[$targetPersistenceIdentifier]['formIdentifier'],
523 $finisherIdentifier
524 );
525
526 if (
527 $currentSheetIdentifier === $sheetIdentifierForOldPersistenceIdentifier
528 && !array_key_exists($sheetIdentifierForNewPersistenceIdentifier, $sheetIdentifiers)
529 ) {
530 $sheetIdentifiersWhichNeedsUpdate[$currentSheetIdentifier] = $sheetIdentifierForNewPersistenceIdentifier;
531 }
532 }
533
534 return $sheetIdentifiersWhichNeedsUpdate;
535 }
536
537 /**
538 * @param array $flexform
539 * @return array
540 */
541 protected function getSheetIdentifiersForFinisherOverrides(array $flexform): array
542 {
543 $sheetIdentifiers = [];
544 foreach ($this->getFinisherSheetsFromFlexform($flexform) as $sheetIdentifier => $sheetData) {
545 $firstSheetItemOptionPath = array_shift(array_keys($sheetData['lDEF']));
546 preg_match('#^settings\.finishers\.(.*)\..+$#', $firstSheetItemOptionPath, $matches);
547 if (!isset($matches[1])) {
548 continue;
549 }
550 $finisherIdentifier = $matches[1];
551 $sheetIdentifiers[$sheetIdentifier] = $finisherIdentifier;
552 }
553
554 return $sheetIdentifiers;
555 }
556
557 /**
558 * @param array $flexform
559 * @return array
560 */
561 protected function getFinisherSheetsFromFlexform(array $flexform): array
562 {
563 if (!isset($flexform['data'])) {
564 return [];
565 }
566
567 return array_filter(
568 $flexform['data'],
569 function ($key) {
570 return $key !== 'sDEF' && strlen($key) === 32;
571 },
572 ARRAY_FILTER_USE_KEY
573 );
574 }
575
576 /**
577 * @param array $flexform
578 * @return string
579 */
580 protected function getPersistenceIdentifierFromFlexform(array $flexform): string
581 {
582 return $flexform['data']['sDEF']['lDEF']['settings.persistenceIdentifier']['vDEF'] ?? '';
583 }
584
585 /**
586 * @param array $referenceData
587 * @param array $sheetIdentifiersWhichNeedsUpdate
588 * @param string $newPersistenceIdentifier
589 * @return string
590 */
591 protected function generateNewFlexformForReference(
592 array $referenceData,
593 array $sheetIdentifiersWhichNeedsUpdate,
594 string $newPersistenceIdentifier = ''
595 ): string {
596 $flexform = $referenceData['flexform'];
597 if (!empty($newPersistenceIdentifier)) {
598 $flexform['data']['sDEF']['lDEF']['settings.persistenceIdentifier']['vDEF'] = $newPersistenceIdentifier;
599 }
600
601 foreach ($sheetIdentifiersWhichNeedsUpdate as $oldSheetIdentifier => $newSheetIdentifier) {
602 $flexform['data'][$newSheetIdentifier] = $flexform['data'][$oldSheetIdentifier];
603 unset($flexform['data'][$oldSheetIdentifier]);
604 }
605
606 return $this->flexFormTools->flexArray2Xml($flexform, true);
607 }
608
609 /**
610 * @param string $persistenceIdentifier
611 * @return bool
612 */
613 protected function hasNewFileExtension(string $persistenceIdentifier): bool
614 {
615 return StringUtility::endsWith(
616 $persistenceIdentifier,
617 FormPersistenceManager::FORM_DEFINITION_FILE_EXTENSION
618 );
619 }
620
621 /**
622 * @param array $formDefinition
623 * @return bool
624 */
625 protected function looksLikeAFormDefinition(array $formDefinition): bool
626 {
627 return isset($formDefinition['identifier'], $formDefinition['type']) && $formDefinition['type'] === 'Form';
628 }
629
630 /**
631 * @param string $persistenceIdentifier
632 * @return string
633 */
634 protected function getOldPersistenceIdentifier(string $persistenceIdentifier): string
635 {
636 return preg_replace(
637 '
638 #^(.*)(\.form\.yaml)$#',
639 '${1}.yaml',
640 $persistenceIdentifier
641 );
642 }
643
644 /**
645 * @param string $persistenceIdentifier
646 * @return string
647 */
648 protected function getNewPersistenceIdentifier(string $persistenceIdentifier): string
649 {
650 return preg_replace(
651 '#(?<!\.form).yaml$#',
652 '.form.yaml',
653 $persistenceIdentifier
654 );
655 }
656
657 /**
658 * @param string $persistenceIdentifier
659 * @param string $prototypeName
660 * @param string $formIdentifier
661 * @param string $finisherIdentifier
662 * @return string
663 */
664 protected function buildExpectedSheetIdentifier(
665 string $persistenceIdentifier,
666 string $prototypeName,
667 string $formIdentifier,
668 string $finisherIdentifier
669 ): string {
670 return md5(
671 implode('', [
672 $persistenceIdentifier,
673 $prototypeName,
674 $formIdentifier,
675 $finisherIdentifier
676 ])
677 );
678 }
679
680 /**
681 * @param File $file
682 * @return array
683 */
684 protected function getFormDefinition(File $file): array
685 {
686 try {
687 $formDefinition = $this->yamlSource->load([$file]);
688
689 if (!$this->looksLikeAFormDefinition($formDefinition)) {
690 $formDefinition = [];
691 }
692 } catch (\Exception $exception) {
693 $formDefinition = [];
694 }
695
696 return $formDefinition;
697 }
698
699 /**
700 * @return array
701 */
702 protected function getAllFlexformFieldsFromFormPlugins(): array
703 {
704 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
705 ->getQueryBuilderForTable('tt_content');
706 $queryBuilder->getRestrictions()
707 ->removeAll()
708 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
709
710 $records = $queryBuilder
711 ->select('uid', 'pi_flexform')
712 ->from('tt_content')
713 ->where(
714 $queryBuilder->expr()->eq(
715 'CType',
716 $queryBuilder->createNamedParameter('form_formframework', \PDO::PARAM_STR)
717 )
718 )
719 ->execute()
720 ->fetchAll();
721
722 return $records;
723 }
724
725 /**
726 * @param int $uid
727 * @param string $flexform
728 * @param bool $updateRefindex
729 */
730 protected function updateContentReference(
731 int $uid,
732 string $flexform,
733 bool $updateRefindex = false
734 ): void {
735 $this->connection->update(
736 'tt_content',
737 ['pi_flexform' => $flexform],
738 ['uid' => $uid]
739 );
740
741 if (!$updateRefindex) {
742 return;
743 }
744
745 $this->referenceIndex->updateRefIndexTable(
746 'tt_content',
747 $uid
748 );
749 }
750
751 /**
752 * @return ObjectManager
753 */
754 protected function getObjectManager(): ObjectManager
755 {
756 return GeneralUtility::makeInstance(ObjectManager::class);
757 }
758 }