[TASK] Migrate CTypes text, image and textpic to textmedia 60/40960/17
authorPatrick Broens <patrick@patrickbroens.nl>
Wed, 15 Jul 2015 17:43:08 +0000 (19:43 +0200)
committerAndreas Fernandez <typo3@scripting-base.de>
Fri, 25 Sep 2015 13:10:25 +0000 (15:10 +0200)
With EXT:fluid_styled_content CE image and textpic is dropped in favor
of CType textmedia.

This change introduces an Upgrade Wizard to migrate all existing text,
image and textpic CE to type textmedia.

Resolves: #67954
Releases: master
Change-Id: Ie86812718e34026ed3f00121c510d386b5bcdff6
Reviewed-on: http://review.typo3.org/40960
Reviewed-by: Sascha Egerer <sascha@sascha-egerer.de>
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
typo3/sysext/core/Documentation/Changelog/master/Important-67954-MigrateCTypesTextImageAndTextpicToTextmedia.rst [new file with mode: 0644]
typo3/sysext/install/Classes/Updates/ContentTypesToTextMediaUpdate.php [new file with mode: 0644]
typo3/sysext/install/Tests/Unit/Updates/ContentTypesToTextMediaUpdateTest.php [new file with mode: 0644]
typo3/sysext/install/ext_localconf.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Important-67954-MigrateCTypesTextImageAndTextpicToTextmedia.rst b/typo3/sysext/core/Documentation/Changelog/master/Important-67954-MigrateCTypesTextImageAndTextpicToTextmedia.rst
new file mode 100644 (file)
index 0000000..691a47a
--- /dev/null
@@ -0,0 +1,33 @@
+=======================================================================
+Important: #67954 - Migrate CTypes text, image and textpic to textmedia
+=======================================================================
+
+Description
+===========
+
+EXT:fluid_styled_content simplifies the available CTypes which results in types ``text``, ``image`` and ``textpic``
+being dropped in favor of type ``textmedia``. The relation field is changed from ``image`` to ``media_references``.
+
+
+Impact
+======
+
+When EXT:fluid_styled_content is and EXT:css_styled_content isn't installed an "Upgrade Wizard" is available in the
+Install Tool to migrate all CE of type ``text``, ``image`` or ``textpic`` to type ``textmedia``.
+Furthermore the relations to field ``image`` will be adjusted to ``media_references`` for the migrated CE's.
+
+The frontend rendering has to be adjusted so the new type is rendered after the migration is done.
+Migration will not happen automatically but has to be triggered manually.
+
+
+Affected Installations
+======================
+
+All installations where EXT:fluid_styled_content is and EXT:css_styled_content isn't installed.
+
+
+Migration
+=========
+
+First un-install EXT:css_styled_content and install EXT:fluid_styled_content. After that an "Upgrade Wizard" will be
+available in the install tool to migrate all existing CE elements of type ``text``, ``image`` or ``textpic`` to type ``textmedia``.
\ No newline at end of file
diff --git a/typo3/sysext/install/Classes/Updates/ContentTypesToTextMediaUpdate.php b/typo3/sysext/install/Classes/Updates/ContentTypesToTextMediaUpdate.php
new file mode 100644 (file)
index 0000000..42798ab
--- /dev/null
@@ -0,0 +1,119 @@
+<?php
+namespace TYPO3\CMS\Install\Updates;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
+
+/**
+ * Migrate CTypes 'text', 'image' and 'textpic' to 'textmedia' for extension 'frontend'
+ */
+class ContentTypesToTextMediaUpdate extends AbstractUpdate {
+
+       /**
+        * @var string
+        */
+       protected $title = 'Migrate CTypes text, image and textpic to textmedia and move file relations from "image" to "media_references"';
+
+       /**
+        * Checks if an update is needed
+        *
+        * @param string &$description The description for the update
+        * @return bool Whether an update is needed (TRUE) or not (FALSE)
+        */
+       public function checkForUpdate(&$description) {
+               $updateNeeded = TRUE;
+
+               if (
+                       !ExtensionManagementUtility::isLoaded('fluid_styled_content')
+                       || ExtensionManagementUtility::isLoaded('css_styled_content')
+                       || $this->isWizardDone()
+               ) {
+                       $updateNeeded = FALSE;
+               } else {
+                       $nonTextmediaCount = $this->getDatabaseConnection()->exec_SELECTcountRows(
+                               'uid',
+                               'tt_content',
+                               'CType IN (\'text\', \'image\', \'textpic\')'
+                       );
+
+                       if ($nonTextmediaCount === 0) {
+                               $updateNeeded = FALSE;
+                       }
+               }
+
+               $description = 'The extension "fluid_styled_content" is using a new CType, textmedia, ' .
+                       'which replaces the CTypes text, image and textpic. ' .
+                       'This update wizard migrates these old CTypes to the new one in the database.';
+
+               return $updateNeeded;
+       }
+
+       /**
+        * Performs the database update if old CTypes are available
+        *
+        * @param array &$databaseQueries Queries done in this update
+        * @param mixed &$customMessages Custom messages
+        * @return bool
+        */
+       public function performUpdate(array &$databaseQueries, &$customMessages) {
+               $databaseConnection = $this->getDatabaseConnection();
+
+               // Update 'text' records
+               $query = '
+                       UPDATE tt_content
+                       SET tt_content.CType = \'textmedia\'
+                       WHERE
+                       tt_content.CType = \'text\'
+               ';
+               $databaseConnection->sql_query($query);
+
+               // Store last executed query
+               $databaseQueries[] = str_replace(chr(10), ' ', $query);
+               // Check for errors
+               if ($databaseConnection->sql_error()) {
+                       $customMessages = 'SQL-ERROR: ' . htmlspecialchars($databaseConnection->sql_error());
+                       return FALSE;
+               }
+
+               // Update 'textpic' and 'image' records
+               $query = '
+                       UPDATE tt_content
+                       LEFT JOIN sys_file_reference
+                       ON sys_file_reference.uid_foreign = tt_content.uid
+                       AND sys_file_reference.tablenames =\'tt_content\'
+                       AND sys_file_reference.fieldname = \'image\'
+                       SET tt_content.CType = \'textmedia\',
+                       tt_content.media = image,
+                       tt_content.image = \'\',
+                       sys_file_reference.fieldname = \'media\'
+                       WHERE
+                       tt_content.CType = \'textpic\'
+                       OR tt_content.CType = \'image\'
+               ';
+               $databaseConnection->sql_query($query);
+
+               // Store last executed query
+               $databaseQueries[] = str_replace(chr(10), ' ', $query);
+               // Check for errors
+               if ($databaseConnection->sql_error()) {
+                       $customMessages = 'SQL-ERROR: ' . htmlspecialchars($databaseConnection->sql_error());
+                       return FALSE;
+               }
+
+               $this->markWizardAsDone();
+
+               return TRUE;
+       }
+}
diff --git a/typo3/sysext/install/Tests/Unit/Updates/ContentTypesToTextMediaUpdateTest.php b/typo3/sysext/install/Tests/Unit/Updates/ContentTypesToTextMediaUpdateTest.php
new file mode 100644 (file)
index 0000000..9a06b69
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+namespace TYPO3\CMS\Install\Tests\Unit\Updates;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use Prophecy\Argument;
+use Prophecy\Prophecy\ObjectProphecy;
+use Prophecy\Prophet;
+use TYPO3\CMS\Core\Tests\Unit\Resource\BaseTestCase;
+use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
+use TYPO3\CMS\Install\Updates\ContentTypesToTextMediaUpdate as UpdateWizard;
+use TYPO3\CMS\Core\Package\PackageManager;
+
+/**
+ * Test Class for ContentTypesToTextMediaUpdate
+ */
+class ContentTypesToTextMediaUpdateTest extends BaseTestCase {
+
+       /**
+        * @var ObjectProphecy
+        */
+       protected $packageManagerProphecy;
+
+       /**
+        * @var ObjectProphecy
+        */
+       protected $dbProphecy;
+
+       /**
+        * @var ObjectProphecy
+        */
+       protected $updateWizard;
+
+       public function setUp() {
+               unset($GLOBALS['TYPO3_CONF_VARS']['INSTALL']['wizardDone']);
+               $prophet = new Prophet();
+               $this->packageManagerProphecy = $prophet->prophesize(PackageManager::class);
+               $this->dbProphecy = $prophet->prophesize(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
+               $GLOBALS['TYPO3_DB'] = $this->dbProphecy->reveal();
+               $this->updateWizard = new UpdateWizard();
+               ExtensionManagementUtility::setPackageManager($this->packageManagerProphecy->reveal());
+       }
+
+       public function tearDown() {
+               ExtensionManagementUtility::setPackageManager(new PackageManager());
+       }
+
+       /**
+        * @test
+        * @return void
+        */
+       public function updateWizardDoesNotRunIfCssStyledContentIsInstalled() {
+               $this->packageManagerProphecy->isPackageActive('fluid_styled_content')->willReturn(TRUE);
+               $this->packageManagerProphecy->isPackageActive('css_styled_content')->willReturn(TRUE);
+
+               $description = '';
+               $this->assertFalse($this->updateWizard->checkForUpdate($description));
+       }
+
+       /**
+        * @test
+        * @return void
+        */
+       public function updateWizardDoesRunIfCssStyledContentIsNotInstalledAndDataToUpdate() {
+               $this->packageManagerProphecy->isPackageActive('fluid_styled_content')->willReturn(TRUE);
+               $this->packageManagerProphecy->isPackageActive('css_styled_content')->willReturn(FALSE);
+               $this->dbProphecy->exec_SELECTcountRows(Argument::cetera())->willReturn(1);
+
+               $description = '';
+               $this->assertTrue($this->updateWizard->checkForUpdate($description));
+       }
+
+}
index 725fb45..157a657 100644 (file)
@@ -10,6 +10,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['processedFil
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['filesReplacePermission'] = \TYPO3\CMS\Install\Updates\FilesReplacePermissionUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['tableCType'] = \TYPO3\CMS\Install\Updates\TableFlexFormToTtContentFieldsUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\FileListIsStartModuleUpdate::class] = \TYPO3\CMS\Install\Updates\FileListIsStartModuleUpdate::class;
+$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['textmediaCType'] = \TYPO3\CMS\Install\Updates\ContentTypesToTextMediaUpdate::class;
 
 $signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
 $signalSlotDispatcher->connect(