[TASK] Migrate form upgrade wizard to new API 08/58308/4
authorSusanne Moog <susanne.moog@typo3.org>
Mon, 17 Sep 2018 18:59:47 +0000 (20:59 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Tue, 18 Sep 2018 14:54:23 +0000 (16:54 +0200)
Resolves: #86275
Releases: master
Change-Id: I767f02c75b5f505e9b7ec78e933513d427dba2ae
Reviewed-on: https://review.typo3.org/58308
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Tobi Kretschmann <tobi@tobishome.de>
Tested-by: Tobi Kretschmann <tobi@tobishome.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/form/Classes/Hooks/FormFileExtensionUpdate.php
typo3/sysext/form/Tests/Functional/Hooks/FormFileExtensionUpdateTest.php
typo3/sysext/install/Classes/Command/UpgradeWizardRunCommand.php
typo3/sysext/install/Classes/Updates/ReferenceIndexUpdatedPrerequisite.php

index 5146b0f..2eab469 100644 (file)
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Form\Hooks;
  * The TYPO3 project - inspiring people to share!
  */
 
+use Symfony\Component\Console\Output\OutputInterface;
 use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
 use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\ConnectionPool;
@@ -31,18 +32,20 @@ use TYPO3\CMS\Extbase\Object\ObjectManager;
 use TYPO3\CMS\Form\Mvc\Configuration\YamlSource;
 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManager;
 use TYPO3\CMS\Form\Slot\FilePersistenceSlot;
-use TYPO3\CMS\Install\Updates\AbstractUpdate;
+use TYPO3\CMS\Install\Updates\ChattyInterface;
+use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
+use TYPO3\CMS\Install\Updates\ReferenceIndexUpdatedPrerequisite;
+use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
 
 /**
  * Update wizard to migrate all forms currently in use to new ending
  */
-class FormFileExtensionUpdate extends AbstractUpdate
+class FormFileExtensionUpdate implements ChattyInterface, UpgradeWizardInterface
 {
-
     /**
-     * @var string
+     * @var OutputInterface
      */
-    protected $title = 'Rename form definition file extension from .yaml to .form.yaml';
+    protected $output;
 
     /**
      * @var FormPersistenceManager
@@ -75,15 +78,65 @@ class FormFileExtensionUpdate extends AbstractUpdate
     protected $connection;
 
     /**
+     * Return the identifier for this wizard
+     * This should be the same string as used in the ext_localconf class registration
+     *
+     * @return string
+     */
+    public function getIdentifier(): string
+    {
+        return 'formFileExtension';
+    }
+
+    /**
+     * Return the speaking name of this wizard
+     *
+     * @return string
+     */
+    public function getTitle(): string
+    {
+        return 'Rename form definition file extension from .yaml to .form.yaml';
+    }
+
+    /**
+     * Return the description for this wizard
+     *
+     * @return string
+     */
+    public function getDescription(): string
+    {
+        return 'Form definition files need to be named *.form.yaml to have a way of distinguishing form yaml ' .
+               'configuration files from other yaml configuration files. This wizard will analyze and rename found files.';
+    }
+
+    /**
+     * Returns an array of class names of Prerequisite classes
+     * This way a wizard can define dependencies like "database up-to-date" or
+     * "reference index updated"
+     *
+     * @return string[]
+     */
+    public function getPrerequisites(): array
+    {
+        return [
+            ReferenceIndexUpdatedPrerequisite::class,
+            DatabaseUpdatedPrerequisite::class
+        ];
+    }
+
+    public function setOutput(OutputInterface $output): void
+    {
+        $this->output = $output;
+    }
+
+    /**
      * Checks whether updates are required.
      *
-     * @param string &$description The description for the update
      * @return bool Whether an update is required (TRUE) or not (FALSE)
      */
-    public function checkForUpdate(&$description)
+    public function updateNecessary(): bool
     {
         $updateNeeded = false;
-        $information = [];
 
         $this->persistenceManager = $this->getObjectManager()->get(FormPersistenceManager::class);
         $this->yamlSource = $this->getObjectManager()->get(YamlSource::class);
@@ -111,7 +164,7 @@ class FormFileExtensionUpdate extends AbstractUpdate
                 && $formDefinitionInformation['location'] === 'storage'
             ) {
                 $updateNeeded = true;
-                $information['rename'] = 'Form definition files were found that should be migrated to be named .form.yaml.';
+                $this->output->writeln('Form definition files were found that should be migrated to be named .form.yaml.');
             }
 
             if (
@@ -119,7 +172,7 @@ class FormFileExtensionUpdate extends AbstractUpdate
                 && $formDefinitionInformation['hasReferencesForOldFileExtension']
             ) {
                 $updateNeeded = true;
-                $information['updateReference'] = 'Referenced form definition files found that should be updated.';
+                $this->output->writeln('Referenced form definition files found that should be updated.');
             }
 
             if (
@@ -128,35 +181,30 @@ class FormFileExtensionUpdate extends AbstractUpdate
             ) {
                 $updateNeeded = true;
                 if ($formDefinitionInformation['hasNewFileExtension'] === true) {
-                    $information['updateReference'] = 'Referenced form definition files found that should be updated.';
+                    $this->output->writeln('Referenced form definition files found that should be updated.');
+                } elseif ($formDefinitionInformation['location'] === 'storage') {
+                    $this->output->writeln('Referenced form definition files found that should be updated.');
                 } else {
-                    if ($formDefinitionInformation['location'] === 'storage') {
-                        $information['updateReference'] = 'Referenced form definition files found that should be updated.';
-                    } else {
-                        $information['manualStepsNeeded'] =
-                            'There are references to form definitions which are located in extensions and thus cannot be renamed automatically by this wizard.'
-                          . 'This form definitions from extensions that do not end with .form.yaml have to be renamed by hand!'
-                          . 'After that you can run this wizard again to migrate the references.';
-                    }
+                    $this->output->writeln(
+                        '<warning>There are references to form definitions which are located in extensions and thus cannot be renamed automatically by this wizard.'
+                      . 'This form definitions from extensions that do not end with .form.yaml have to be renamed by hand!'
+                      . 'After that you can run this wizard again to migrate the references.</warning>'
+                    );
                 }
             }
         }
 
-        $description = implode('<br>', $information);
-
         return $updateNeeded;
     }
 
     /**
      * Performs the accordant updates.
      *
-     * @param array &$dbQueries Queries done in this update
-     * @param string &$customMessage Custom message
      * @return bool Whether everything went smoothly or not
      */
-    public function performUpdate(array &$dbQueries, &$customMessage): bool
+    public function executeUpdate(): bool
     {
-        $messages = [];
+        $success = true;
 
         $GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageService::class);
         $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
@@ -242,11 +290,12 @@ class FormFileExtensionUpdate extends AbstractUpdate
                     $file->rename($newFileName, DuplicationBehavior::RENAME);
                     $newPersistenceIdentifier = $file->getCombinedIdentifier();
                 } catch (\Exception $e) {
-                    $messages[] = sprintf(
-                        'Failed to rename form definition "%s" to "%s".',
+                    $this->output->writeln(sprintf(
+                        '<error>Failed to rename form definition "%s" to "%s".</error>',
                         $formDefinitionInformation['persistenceIdentifier'],
                         $newFileName
-                    );
+                    ));
+                    $success = false;
                     continue;
                 }
 
@@ -293,12 +342,13 @@ class FormFileExtensionUpdate extends AbstractUpdate
                     }
                 }
             } else {
-                $messages[] = sprintf(
-                    'Failed to rename form definition "%s" to "%s". You have to be rename it by hand!. '
-                  . 'After that you can run this wizard again to migrate the references.',
+                $success = false;
+                $this->output->writeln(sprintf(
+                    '<error>Failed to rename form definition "%s" to "%s". You have to be rename it by hand!. '
+                  . 'After that you can run this wizard again to migrate the references.</error>',
                     $formDefinitionInformation['persistenceIdentifier'],
                     $this->getNewPersistenceIdentifier($formDefinitionInformation['persistenceIdentifier'])
-                );
+                ));
             }
         }
 
@@ -307,13 +357,7 @@ class FormFileExtensionUpdate extends AbstractUpdate
             null
         );
 
-        if (count($messages) > 0) {
-            $customMessage = 'The following issues occurred during performing updates:'
-                . '<br><ul><li>' . implode('</li><li>', $messages) . '</li></ul>';
-            return false;
-        }
-
-        return true;
+        return $success;
     }
 
     /**
@@ -542,13 +586,13 @@ class FormFileExtensionUpdate extends AbstractUpdate
     {
         $sheetIdentifiers = [];
         foreach ($this->getFinisherSheetsFromFlexform($flexform) as $sheetIdentifier => $sheetData) {
-            $firstSheetItemOptionPath = array_shift(array_keys($sheetData['lDEF']));
+            $itemOptionPath = array_keys($sheetData['lDEF']);
+            $firstSheetItemOptionPath = array_shift($itemOptionPath);
             preg_match('#^settings\.finishers\.(.*)\..+$#', $firstSheetItemOptionPath, $matches);
             if (!isset($matches[1])) {
                 continue;
             }
-            $finisherIdentifier = $matches[1];
-            $sheetIdentifiers[$sheetIdentifier] = $finisherIdentifier;
+            $sheetIdentifiers[$sheetIdentifier] = $matches[1];
         }
 
         return $sheetIdentifiers;
index d4a8d70..09a90ae 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Form\Tests\Functional\Hooks;
  */
 
 use Doctrine\DBAL\FetchMode;
+use Symfony\Component\Console\Output\NullOutput;
 use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
 use TYPO3\CMS\Core\Core\Bootstrap;
 use TYPO3\CMS\Core\Database\ConnectionPool;
@@ -84,7 +85,9 @@ class FormFileExtensionUpdateTest extends FunctionalTestCase
             $storage->getFolder($folderIdentifier)->delete(true);
         }
 
+        $output = new NullOutput();
         $this->subject = GeneralUtility::makeInstance(FormFileExtensionUpdate::class);
+        $this->subject->setOutput($output);
         $this->slot = GeneralUtility::makeInstance(FilePersistenceSlot::class);
         $this->flexForm = GeneralUtility::makeInstance(FlexFormTools::class);
         $this->referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class);
@@ -106,8 +109,7 @@ class FormFileExtensionUpdateTest extends FunctionalTestCase
      */
     private function invokeCheckForUpdate(): bool
     {
-        $description = '';
-        return $this->subject->checkForUpdate($description);
+        return $this->subject->updateNecessary();
     }
 
     /**
@@ -253,12 +255,7 @@ class FormFileExtensionUpdateTest extends FunctionalTestCase
 
     private function invokePerformUpdate(): bool
     {
-        $queries = [];
-        $messages = '';
-        return $this->subject->performUpdate(
-            $queries,
-            $messages
-        );
+        return $this->subject->executeUpdate();
     }
 
     /**
index a84ec1f..45cdfdf 100644 (file)
@@ -106,8 +106,12 @@ class UpgradeWizardRunCommand extends Command
                 $className = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][$wizardToExecute];
                 $upgradeWizard = $this->getWizard($className, $wizardToExecute);
                 if ($upgradeWizard !== null) {
-                    $this->handlePrerequisites([$upgradeWizard]);
-                    $result = $this->runSingleWizard($upgradeWizard);
+                    $prerequisitesFulfilled = $this->handlePrerequisites([$upgradeWizard]);
+                    if ($prerequisitesFulfilled === true) {
+                        $result = $this->runSingleWizard($upgradeWizard);
+                    } else {
+                        $result = 1;
+                    }
                 }
             } else {
                 $this->output->error('No such wizard: ' . $wizardToExecute);
index d7ebc5e..0017e6d 100644 (file)
@@ -79,7 +79,7 @@ class ReferenceIndexUpdatedPrerequisite implements PrerequisiteInterface, Chatty
     {
         $this->referenceIndex->enableRuntimeCache();
         $result = $this->referenceIndex->updateIndex(true, false);
-        return $result['errorCount'] === 0;
+        return $result[2] === 0;
     }
 
     /**