[BUGFIX] Add upgrade wizard to keep sorting of sys_language records 23/56123/2
authorGeorg Ringer <georg.ringer@gmail.com>
Tue, 27 Feb 2018 20:40:12 +0000 (21:40 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 12 Mar 2018 23:08:30 +0000 (00:08 +0100)
To keep the sorting after an upgrade from 7.6 to 8.7 and later, an
upgrade wizard is added.

Resolves: #82538
Releases: master, 8.7
Change-Id: If1b5fbc56d445239bc6e4eaecb695889aeb9819b
Reviewed-on: https://review.typo3.org/56123
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/install/Classes/Updates/LanguageSortingUpdate.php [new file with mode: 0644]
typo3/sysext/install/ext_localconf.php

diff --git a/typo3/sysext/install/Classes/Updates/LanguageSortingUpdate.php b/typo3/sysext/install/Classes/Updates/LanguageSortingUpdate.php
new file mode 100644 (file)
index 0000000..fcf01f4
--- /dev/null
@@ -0,0 +1,105 @@
+<?php
+declare(strict_types = 1);
+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\Database\ConnectionPool;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Update sys_language records to use the newly sorting column,
+ * set default sorting from title
+ */
+class LanguageSortingUpdate extends AbstractUpdate
+{
+    /**
+     * @var string
+     */
+    protected $title = 'Update sorting of sys_language records';
+
+    /**
+     * 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): bool
+    {
+        if ($this->isWizardDone()) {
+            $this->markWizardAsDone();
+
+            return false;
+        }
+
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('sys_language');
+        $hasAffectedRows = (bool)$queryBuilder->count('uid')
+            ->from('sys_language')
+            ->where(
+                $queryBuilder->expr()->eq('sorting', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)),
+                $queryBuilder->expr()->isNotNull('sorting')
+            )
+            ->execute()
+            ->fetchColumn(0);
+
+        if ($hasAffectedRows === true) {
+            $description = 'The sys_language records have unsorted rows. '
+                . ' This upgrade wizard adds values depending on the language title';
+        }
+
+        return $hasAffectedRows;
+    }
+
+    /**
+     * Performs the database update if the sorting field is 0 or null
+     *
+     * @param array &$databaseQueries Queries done in this update
+     * @param string &$customMessage Custom message
+     *
+     * @return bool
+     */
+    public function performUpdate(array &$databaseQueries, &$customMessage): bool
+    {
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+            ->getQueryBuilderForTable('sys_language');
+        $statement = $queryBuilder->select('uid')
+            ->from('sys_language')
+            ->where(
+                $queryBuilder->expr()->eq('sorting', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
+            )
+            ->orderBy('title')
+            ->execute();
+        $sortCounter = 128;
+        while ($languageRecord = $statement->fetch()) {
+            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
+                ->getQueryBuilderForTable('sys_language');
+            $queryBuilder->update('sys_language')
+                ->where(
+                    $queryBuilder->expr()->eq(
+                        'uid',
+                        $queryBuilder->createNamedParameter($languageRecord['uid'], \PDO::PARAM_INT)
+                    )
+                )
+                ->set('sorting', $sortCounter);
+            $databaseQueries[] = $queryBuilder->getSQL();
+            $queryBuilder->execute();
+            $sortCounter *= 2;
+        }
+        $this->markWizardAsDone();
+
+        return true;
+    }
+}
index 026596a..4dc8c98 100644 (file)
@@ -73,3 +73,5 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['formLegacyEx
     = \TYPO3\CMS\Install\Updates\FormLegacyExtractionUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['rtehtmlareaExtension']
     = \TYPO3\CMS\Install\Updates\RteHtmlAreaExtractionUpdate::class;
+$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['sysLanguageSorting']
+    = \TYPO3\CMS\Install\Updates\LanguageSortingUpdate::class;