[BUGFIX] Show correct file reference information 58/55158/5
authorNicole Cordes <typo3@cordes.co>
Tue, 19 Dec 2017 10:15:48 +0000 (11:15 +0100)
committerNicole Cordes <typo3@cordes.co>
Tue, 23 Jan 2018 10:11:42 +0000 (11:11 +0100)
This patch corrects how file references (from softrefs) are stored in
sys_refindex table as this was changed with patch
https://review.typo3.org/53219/.

Resolves: #81831
Related: #80991
Related: #80988
Releases: master, 8.7
Change-Id: I776d2db47dffd83217a3c5527860093ddbca58c6
Reviewed-on: https://review.typo3.org/55158
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Benjamin Kluge <b.kluge@neusta.de>
Tested-by: Benjamin Kluge <b.kluge@neusta.de>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Reviewed-by: Reiner Teubner <reiner.teubner@me.com>
Tested-by: Reiner Teubner <reiner.teubner@me.com>
Reviewed-by: Henning Liebe <h.liebe@neusta.de>
Tested-by: Henning Liebe <h.liebe@neusta.de>
Reviewed-by: Sascha Rademacher <sascha.rademacher+typo3@gmail.com>
Tested-by: Sascha Rademacher <sascha.rademacher+typo3@gmail.com>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
typo3/sysext/core/Classes/Database/SoftReferenceIndex.php
typo3/sysext/install/Classes/Updates/FileReferenceUpdate.php [new file with mode: 0644]
typo3/sysext/install/ext_localconf.php

index b3ed3dc..8409a1b 100644 (file)
@@ -272,8 +272,8 @@ class SoftReferenceIndex
                             $linkTags[$key] = str_replace($matches[1], '{softref:' . $token . '}', $linkTags[$key]);
                             $elements[$key] = $linkDetails;
                             $elements[$key]['subst'] = [
                             $linkTags[$key] = str_replace($matches[1], '{softref:' . $token . '}', $linkTags[$key]);
                             $elements[$key] = $linkDetails;
                             $elements[$key]['subst'] = [
-                                'type' => $linkDetails['type'],
-                                'relFileName' => $fileIdMatch[1],
+                                'type' => 'db',
+                                'recordRef' => 'sys_file:' . $fileIdMatch[1],
                                 'tokenID' => $token,
                                 'tokenValue' => 'file:' . ($linkDetails['file'] instanceof File ? $linkDetails['file']->getUid() : $fileIdMatch[1])
                             ];
                                 'tokenID' => $token,
                                 'tokenValue' => 'file:' . ($linkDetails['file'] instanceof File ? $linkDetails['file']->getUid() : $fileIdMatch[1])
                             ];
diff --git a/typo3/sysext/install/Classes/Updates/FileReferenceUpdate.php b/typo3/sysext/install/Classes/Updates/FileReferenceUpdate.php
new file mode 100644 (file)
index 0000000..b6833ea
--- /dev/null
@@ -0,0 +1,123 @@
+<?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\Resource\Exception;
+use TYPO3\CMS\Core\Resource\File;
+use TYPO3\CMS\Core\Resource\ResourceFactory;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Core\Utility\MathUtility;
+
+/**
+ * Migrate file references that are stored in a wrong way to correct scheme
+ */
+class FileReferenceUpdate extends AbstractUpdate
+{
+    /**
+     * @var string
+     */
+    protected $title = 'Migrate file references that are stored in a wrong way to correct scheme';
+
+    /**
+     * 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)
+    {
+        if ($this->isWizardDone()) {
+            return false;
+        }
+
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
+        $count = $queryBuilder->count('hash')
+            ->from('sys_refindex')
+            ->where(
+                $queryBuilder->expr()->eq('ref_table', $queryBuilder->createNamedParameter('_FILE', \PDO::PARAM_STR)),
+                $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('typolink_tag', \PDO::PARAM_STR)),
+                $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
+            )
+            ->execute()->fetchColumn(0);
+
+        if ($count) {
+            $description = 'File references were saved in a wrong way and references aren\'t shown correctly in file list module.';
+        }
+
+        return (bool)$count;
+    }
+
+    /**
+     * Performs the database update
+     *
+     * @param array &$databaseQueries Queries done in this update
+     * @param string &$customMessage Custom message
+     * @return bool
+     */
+    public function performUpdate(array &$databaseQueries, &$customMessage)
+    {
+        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_refindex');
+        $queryBuilder = $connection->createQueryBuilder();
+        $statement = $queryBuilder->select('*')
+            ->from('sys_refindex')
+            ->where(
+                $queryBuilder->expr()->eq('ref_table', $queryBuilder->createNamedParameter('_FILE', \PDO::PARAM_STR)),
+                $queryBuilder->expr()->eq('softref_key', $queryBuilder->createNamedParameter('typolink_tag', \PDO::PARAM_STR)),
+                $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
+            )
+            ->execute();
+        while ($record = $statement->fetch()) {
+            $fileReference = 0;
+            if (MathUtility::canBeInterpretedAsInteger($record['ref_string'])) {
+                $fileReference = $record['ref_string'];
+            } else {
+                try {
+                    $fileObject = ResourceFactory::getInstance()->retrieveFileOrFolderObject($record['ref_string']);
+                    if ($fileObject instanceof File) {
+                        $fileReference = $fileObject->getUid();
+                    }
+                } catch (Exception $e) {
+                }
+            }
+
+            $updateQueryBuilder = $connection->createQueryBuilder();
+            $updateQueryBuilder->update('sys_refindex')
+                ->where(
+                    $updateQueryBuilder->expr()->eq(
+                        'hash',
+                        $updateQueryBuilder->createNamedParameter($record['hash'], \PDO::PARAM_STR)
+                    )
+                );
+
+            if ($fileReference) {
+                $updateQueryBuilder->set('ref_table', 'sys_file')
+                    ->set('ref_uid', $fileReference)
+                    ->set('ref_string', '');
+            } else {
+                $updateQueryBuilder->set('deleted', 1);
+            }
+
+            $databaseQueries[] = $updateQueryBuilder->getSQL();
+            $updateQueryBuilder->execute();
+        }
+
+        $this->markWizardAsDone();
+
+        return true;
+    }
+}
index 6742cde..f6dedfa 100644 (file)
@@ -28,6 +28,8 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\In
     = \TYPO3\CMS\Install\Updates\UploadContentElementUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\MigrateFscStaticTemplateUpdate::class]
     = \TYPO3\CMS\Install\Updates\MigrateFscStaticTemplateUpdate::class;
     = \TYPO3\CMS\Install\Updates\UploadContentElementUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\MigrateFscStaticTemplateUpdate::class]
     = \TYPO3\CMS\Install\Updates\MigrateFscStaticTemplateUpdate::class;
+$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\FileReferenceUpdate::class]
+    = \TYPO3\CMS\Install\Updates\FileReferenceUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate::class]
     = \TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['compatibility7Extension']
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update'][\TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate::class]
     = \TYPO3\CMS\Install\Updates\MigrateFeSessionDataUpdate::class;
 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['compatibility7Extension']