From f172cc218420eeb537a382ea56ad974ce853dd49 Mon Sep 17 00:00:00 2001
From: Nicole Cordes <typo3@cordes.co>
Date: Fri, 13 Jan 2017 20:16:20 +0100
Subject: [PATCH] [TASK] Add button to reload static database data of an
 extension

This patch re-adds the possibility to manually trigger a reload of
static database data an extension might ships. In the EM list view, an
inconspicuous button is added for each extension which has an own
ext_tables_static+adt.sql file. Furthermore the import is changed, so
that a md5 hash of the file content is stored in the database. If the
extension is updated and the content if the file changes, a more visible
button is shown to indicate an update should be considered.

Resolves: #60748
Releases: master, 7.6
Change-Id: I8ccb01d7e1f98a4adfc70a4d1aa80db4e76bbc9d
Reviewed-on: https://review.typo3.org/51310
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Matthes
Tested-by: Christian Matthes
Tested-by: Joerg Kummer <service@enobe.de>
Reviewed-by: Helmut Hummel <typo3@helhum.io>
Tested-by: Helmut Hummel <typo3@helhum.io>
---
 .../Classes/Controller/ActionController.php   | 21 +++++
 .../Classes/Utility/InstallUtility.php        |  4 +-
 .../ViewHelpers/ReloadSqlDataViewHelper.php   | 83 +++++++++++++++++++
 .../Resources/Private/Language/locallang.xlf  |  6 ++
 .../Private/Templates/List/Index.html         |  3 +-
 typo3/sysext/extensionmanager/ext_tables.php  |  2 +-
 6 files changed, 116 insertions(+), 3 deletions(-)
 create mode 100644 typo3/sysext/extensionmanager/Classes/ViewHelpers/ReloadSqlDataViewHelper.php

diff --git a/typo3/sysext/extensionmanager/Classes/Controller/ActionController.php b/typo3/sysext/extensionmanager/Classes/Controller/ActionController.php
index fa16187010f7..fc0e8ca032aa 100644
--- a/typo3/sysext/extensionmanager/Classes/Controller/ActionController.php
+++ b/typo3/sysext/extensionmanager/Classes/Controller/ActionController.php
@@ -14,6 +14,9 @@ namespace TYPO3\CMS\Extensionmanager\Controller;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Registry;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 /**
  * Controller for handling extension related actions like
  * installing, removing, downloading of data or files
@@ -174,4 +177,22 @@ class ActionController extends AbstractController
         }
         $this->fileHandlingUtility->sendSqlDumpFileToBrowserAndDelete($filePath, $fileName);
     }
+
+    /**
+     * Reloads the static SQL data of an extension
+     *
+     * @param string $extension
+     */
+    protected function reloadExtensionDataAction($extension)
+    {
+        $extension = $this->installUtility->enrichExtensionWithDetails($extension, false);
+        $registryKey = $extension['siteRelPath'] . 'ext_tables_static+adt.sql';
+
+        $registry = GeneralUtility::makeInstance(Registry::class);
+        $registry->remove('extensionDataImport', $registryKey);
+
+        $this->installUtility->processDatabaseUpdates($extension);
+
+        $this->redirect('index', 'List');
+    }
 }
diff --git a/typo3/sysext/extensionmanager/Classes/Utility/InstallUtility.php b/typo3/sysext/extensionmanager/Classes/Utility/InstallUtility.php
index 6e0275991654..ec665cf48eb0 100644
--- a/typo3/sysext/extensionmanager/Classes/Utility/InstallUtility.php
+++ b/typo3/sysext/extensionmanager/Classes/Utility/InstallUtility.php
@@ -675,11 +675,13 @@ class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface
         $extTablesStaticSqlRelFile = $extensionSiteRelPath . 'ext_tables_static+adt.sql';
         if (!$this->registry->get('extensionDataImport', $extTablesStaticSqlRelFile)) {
             $extTablesStaticSqlFile = PATH_site . $extTablesStaticSqlRelFile;
+            $shortFileHash = '';
             if (file_exists($extTablesStaticSqlFile)) {
                 $extTablesStaticSqlContent = file_get_contents($extTablesStaticSqlFile);
+                $shortFileHash = md5($extTablesStaticSqlContent);
                 $this->importStaticSql($extTablesStaticSqlContent);
             }
-            $this->registry->set('extensionDataImport', $extTablesStaticSqlRelFile, 1);
+            $this->registry->set('extensionDataImport', $extTablesStaticSqlRelFile, $shortFileHash);
             $this->emitAfterExtensionStaticSqlImportSignal($extTablesStaticSqlRelFile);
         }
     }
diff --git a/typo3/sysext/extensionmanager/Classes/ViewHelpers/ReloadSqlDataViewHelper.php b/typo3/sysext/extensionmanager/Classes/ViewHelpers/ReloadSqlDataViewHelper.php
new file mode 100644
index 000000000000..631c4269f88d
--- /dev/null
+++ b/typo3/sysext/extensionmanager/Classes/ViewHelpers/ReloadSqlDataViewHelper.php
@@ -0,0 +1,83 @@
+<?php
+namespace TYPO3\CMS\Extensionmanager\ViewHelpers;
+
+/*
+ * 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\Imaging\Icon;
+use TYPO3\CMS\Core\Imaging\IconFactory;
+use TYPO3\CMS\Core\Registry;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
+
+/**
+ * View helper for update script link
+ */
+class ReloadSqlDataViewHelper extends Link\ActionViewHelper
+{
+    /**
+     * @var string
+     */
+    protected static $registryNamespace = 'extensionDataImport';
+
+    public function initializeArguments()
+    {
+        parent::initializeArguments();
+        $this->registerArgument('extension', 'array', 'Extension key', true);
+    }
+
+    /**
+     * Renders a link to re-import the static SQL data of an extension
+     *
+     * @return string The rendered a tag
+     */
+    public function render()
+    {
+        $extension = $this->arguments['extension'];
+        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
+
+        $staticSqlDataFile = $extension['siteRelPath'] . 'ext_tables_static+adt.sql';
+        if (!file_exists(PATH_site . $staticSqlDataFile)) {
+            return '<span class="btn btn-default disabled">' . $iconFactory->getIcon('empty-empty', Icon::SIZE_SMALL)->render() . '</span>';
+        }
+
+        $registry = GeneralUtility::makeInstance(Registry::class);
+        $oldMd5Hash = $registry->get(static::$registryNamespace, $staticSqlDataFile);
+
+        $md5HashIsEqual = true;
+        // We used to only store "1" in the database when data was imported
+        // No need to compare file content here and just show the reload icon
+        if (!empty($oldMd5Hash) && $oldMd5Hash !== 1) {
+            $currentMd5Hash = md5_file(PATH_site . $staticSqlDataFile);
+            $md5HashIsEqual = $oldMd5Hash === $currentMd5Hash;
+        }
+
+        if ($md5HashIsEqual) {
+            $iconIdentifier = 'actions-database-reload';
+            $languageKey = 'extensionList.databaseReload';
+        } else {
+            $iconIdentifier = 'actions-database-import';
+            $languageKey = 'extensionList.databaseImport';
+        }
+
+        $uriBuilder = $this->controllerContext->getUriBuilder();
+        $uriBuilder->reset();
+        $uri = $uriBuilder->uriFor('reloadExtensionData', ['extension' => $extension['key']], 'Action');
+        $this->tag->addAttribute('href', $uri);
+        $this->tag->addAttribute('class', 'downloadExtensionData btn btn-default');
+        $this->tag->addAttribute('title', LocalizationUtility::translate($languageKey, 'extensionmanager'));
+        $this->tag->setContent($iconFactory->getIcon($iconIdentifier, Icon::SIZE_SMALL)->render());
+
+        return $this->tag->render();
+    }
+}
diff --git a/typo3/sysext/extensionmanager/Resources/Private/Language/locallang.xlf b/typo3/sysext/extensionmanager/Resources/Private/Language/locallang.xlf
index fb92dc2291c9..db981428a69d 100644
--- a/typo3/sysext/extensionmanager/Resources/Private/Language/locallang.xlf
+++ b/typo3/sysext/extensionmanager/Resources/Private/Language/locallang.xlf
@@ -216,6 +216,12 @@
 			<trans-unit id="extensionList.updateConfirmation.message">
 				<source>Update Comments:</source>
 			</trans-unit>
+			<trans-unit id="extensionList.databaseImport">
+				<source>The static database data has changed. You should re-import the data.</source>
+			</trans-unit>
+			<trans-unit id="extensionList.databaseReload">
+				<source>Nothing has changed since last import. You might want to reload static database data.</source>
+			</trans-unit>
 			<trans-unit id="extensionList.updateConfirmation.questionVersionComments">
 				<source>Version Comments</source>
 			</trans-unit>
diff --git a/typo3/sysext/extensionmanager/Resources/Private/Templates/List/Index.html b/typo3/sysext/extensionmanager/Resources/Private/Templates/List/Index.html
index 8032050fbba5..fe48054805d8 100644
--- a/typo3/sysext/extensionmanager/Resources/Private/Templates/List/Index.html
+++ b/typo3/sysext/extensionmanager/Resources/Private/Templates/List/Index.html
@@ -91,6 +91,7 @@
 								</f:link.action>
 							</span>
 							<em:downloadExtensionData class="btn btn-default" extension="{extension}" />
+							<em:reloadSqlData class="btn btn-default" extension="{extension}" />
 						</em:processAvailableActions>
 					</div>
 				</td>
@@ -99,4 +100,4 @@
 		</tbody>
 	</table>
 	</div>
-</f:section>
\ No newline at end of file
+</f:section>
diff --git a/typo3/sysext/extensionmanager/ext_tables.php b/typo3/sysext/extensionmanager/ext_tables.php
index 1172044ba448..40c69b3f418d 100644
--- a/typo3/sysext/extensionmanager/ext_tables.php
+++ b/typo3/sysext/extensionmanager/ext_tables.php
@@ -7,7 +7,7 @@ if (TYPO3_MODE === 'BE') {
         'tools',
         'extensionmanager', '', [
             'List' => 'index,unresolvedDependencies,ter,showAllVersions,distributions',
-            'Action' => 'toggleExtensionInstallationState,installExtensionWithoutSystemDependencyCheck,removeExtension,downloadExtensionZip,downloadExtensionData',
+            'Action' => 'toggleExtensionInstallationState,installExtensionWithoutSystemDependencyCheck,removeExtension,downloadExtensionZip,downloadExtensionData,reloadExtensionData',
             'Configuration' => 'showConfigurationForm,save,saveAndClose',
             'Download' => 'checkDependencies,installFromTer,installExtensionWithoutSystemDependencyCheck,installDistribution,updateExtension,updateCommentForUpdatableVersions',
             'UpdateScript' => 'show',
-- 
GitLab