[TASK] Doctrine: Migrate a simple upgrade wizard 26/49426/7
authorChristian Kuhn <lolli@schwarzbu.ch>
Mon, 8 Aug 2016 21:58:49 +0000 (23:58 +0200)
committerMorton Jonuschat <m.jonuschat@mojocode.de>
Tue, 9 Aug 2016 13:24:44 +0000 (15:24 +0200)
Migrate a simple install tool upgrade wizard that performs
an update query on be_users.
The upgrade wizard have a feature to display performed queries.
Usually, with doctrine, values would be prepared statement
values and getSQL() would display a placeholder only.
This is worked around here giving set() the third parameter
boolean false and quoting manually.

For the wizards itself, there are two nearly identical ones,
the patch merges both and improves the detection if an
execution is needed an no longer relies on "was executed once"
flag.

Change-Id: Ifcfcf75731076ed9f4b20981ea2be71ec672f47f
Resolves: #77446
Releases: master
Reviewed-on: https://review.typo3.org/49426
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
typo3/sysext/install/Classes/Updates/BackendUserStartModuleUpdate.php
typo3/sysext/install/Classes/Updates/BackendUserStartModuleUpdateAboutModule.php [deleted file]
typo3/sysext/install/ext_localconf.php

index 4cda88c..3cef0d6 100644 (file)
@@ -14,6 +14,9 @@ namespace TYPO3\CMS\Install\Updates;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * Update backend user setting startModule if set to "help_aboutmodules"
  */
@@ -32,18 +35,31 @@ class BackendUserStartModuleUpdate extends AbstractUpdate
      */
     public function checkForUpdate(&$description)
     {
-        $backendUsersCount = $this->getDatabaseConnection()->exec_SELECTcountRows('uid', 'be_users');
-        if ($this->isWizardDone() || $backendUsersCount === 0) {
-            return false;
+        $statement = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getConnectionForTable('be_users')
+            ->select(['uid', 'uc'], 'be_users', []);
+        $needsExecution = false;
+        while ($backendUser = $statement->fetch()) {
+            if ($backendUser['uc'] !== null) {
+                $userConfig = unserialize($backendUser['uc']);
+                if ($userConfig['startModule'] === 'help_aboutmodules'
+                    || $userConfig['startModule'] === 'help_AboutmodulesAboutmodules'
+                ) {
+                    $needsExecution = true;
+                    break;
+                }
+            }
         }
-
-        $description = 'The backend user setting startModule is changed for the extension aboutmodules. Update all backend users that use ext:aboutmodules as startModule.';
-
-        return true;
+        if ($needsExecution) {
+            $description = 'The backend user setting startModule is changed for the extension aboutmodules. Update all'
+                . ' backend users that use ext:aboutmodules as startModule.';
+        }
+        return $needsExecution;
     }
 
     /**
-     * Performs the database update if backend user's startmodule is help_aboutmodules
+     * Performs the database update if backend user's startmodule is
+     * "help_aboutmodules" or "help_AboutmodulesAboutmodules"
      *
      * @param array &$databaseQueries Queries done in this update
      * @param mixed &$customMessages Custom messages
@@ -51,28 +67,26 @@ class BackendUserStartModuleUpdate extends AbstractUpdate
      */
     public function performUpdate(array &$databaseQueries, &$customMessages)
     {
-        $db = $this->getDatabaseConnection();
-        $backendUsers = $db->exec_SELECTgetRows('uid,uc', 'be_users', '1=1');
-        if (!empty($backendUsers)) {
-            foreach ($backendUsers as $backendUser) {
-                if ($backendUser['uc'] !== null) {
-                    $userConfig = unserialize($backendUser['uc']);
-                    if ($userConfig['startModule'] === 'help_aboutmodules') {
-                        $userConfig['startModule'] = 'help_AboutmodulesAboutmodules';
-                        $db->exec_UPDATEquery(
-                            'be_users',
-                            'uid=' . (int)$backendUser['uid'],
-                            array(
-                                'uc' => serialize($userConfig),
-                            )
-                        );
-                        $databaseQueries[] = $db->debug_lastBuiltQuery;
-                    }
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
+        $statement = $queryBuilder->select('uid', 'uc')->from('be_users')->execute();
+        while ($backendUser = $statement->fetch()) {
+            if ($backendUser['uc'] !== null) {
+                $userConfig = unserialize($backendUser['uc']);
+                if ($userConfig['startModule'] === 'help_aboutmodules'
+                    || $userConfig['startModule'] === 'help_AboutmodulesAboutmodules'
+                ) {
+                    $userConfig['startModule'] = 'help_AboutAboutmodules';
+                    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
+                    $queryBuilder->update('be_users')
+                        ->where($queryBuilder->expr()->eq('uid', (int)$backendUser['uid']))
+                        // Manual quoting and false as third parameter to have the final
+                        // value in $databaseQueries and not a statement placeholder
+                        ->set('uc', $queryBuilder->quote(serialize($userConfig)), false);
+                    $databaseQueries[] = $queryBuilder->getSQL();
+                    $queryBuilder->execute();
                 }
             }
         }
-
-        $this->markWizardAsDone();
         return true;
     }
 }
diff --git a/typo3/sysext/install/Classes/Updates/BackendUserStartModuleUpdateAboutModule.php b/typo3/sysext/install/Classes/Updates/BackendUserStartModuleUpdateAboutModule.php
deleted file mode 100644 (file)
index 934eb8c..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-<?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!
- */
-
-/**
- * Update backend user setting startModule if set to "help_aboutmodules"
- */
-class BackendUserStartModuleUpdateAboutModule extends AbstractUpdate
-{
-    /**
-     * @var string
-     */
-    protected $title = 'Update backend user setting "startModule"';
-
-    /**
-     * 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)
-    {
-        $backendUsersCount = $this->getDatabaseConnection()->exec_SELECTcountRows('uid', 'be_users');
-        if ($this->isWizardDone() || $backendUsersCount === 0) {
-            return false;
-        }
-
-        $description = 'The backend user setting startModule is changed for the extension aboutmodules. Update all '
-            . 'backend users that use ext:aboutmodules as startModule.';
-
-        return true;
-    }
-
-    /**
-     * Performs the database update if backend user's startmodule is help_aboutmodules
-     *
-     * @param array &$databaseQueries Queries done in this update
-     * @param mixed &$customMessages Custom messages
-     * @return bool
-     */
-    public function performUpdate(array &$databaseQueries, &$customMessages)
-    {
-        $db = $this->getDatabaseConnection();
-        $backendUsers = $db->exec_SELECTgetRows('uid,uc', 'be_users', '1=1');
-        if (!empty($backendUsers)) {
-            foreach ($backendUsers as $backendUser) {
-                if ($backendUser['uc'] !== null) {
-                    $userConfig = unserialize($backendUser['uc']);
-                    if ($userConfig['startModule'] === 'help_AboutmodulesAboutmodules') {
-                        $userConfig['startModule'] = 'help_AboutAboutmodules';
-                        $db->exec_UPDATEquery(
-                            'be_users',
-                            'uid=' . (int)$backendUser['uid'],
-                            array(
-                                'uc' => serialize($userConfig),
-                            )
-                        );
-                        $databaseQueries[] = $db->debug_lastBuiltQuery;
-                    }
-                }
-            }
-        }
-
-        $this->markWizardAsDone();
-        return true;
-    }
-}
index 837b1af..7787c0b 100644 (file)
@@ -3,7 +3,6 @@ defined('TYPO3_MODE') or die();
 
 // TYPO3 CMS 7
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['accessRightParameters'] = \TYPO3\CMS\Install\Updates\AccessRightParametersUpdate::class;
-$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['backendUserStartModule'] = \TYPO3\CMS\Install\Updates\BackendUserStartModuleUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['languageIsoCode'] = \TYPO3\CMS\Install\Updates\LanguageIsoCodeUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['PageShortcutParent'] = \TYPO3\CMS\Install\Updates\PageShortcutParentUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['backendShortcuts'] = \TYPO3\CMS\Install\Updates\MigrateShortcutUrlsAgainUpdate::class;
@@ -40,7 +39,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\In
 // Add update wizards below this line
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\WizardDoneToRegistry::class]
     = \TYPO3\CMS\Install\Updates\WizardDoneToRegistry::class;
-$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\BackendUserStartModuleUpdateAboutModule::class]
-    = \TYPO3\CMS\Install\Updates\BackendUserStartModuleUpdateAboutModule::class;
+$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\BackendUserStartModuleUpdate::class]
+    = \TYPO3\CMS\Install\Updates\BackendUserStartModuleUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\FrontendUserImageUpdateWizard::class]
     = \TYPO3\CMS\Install\Updates\FrontendUserImageUpdateWizard::class;