[BUGFIX] Show SQL errors during extension installation 97/54497/4
authorNicole Cordes <typo3@cordes.co>
Sun, 29 Oct 2017 19:30:12 +0000 (20:30 +0100)
committerBenni Mack <benni@typo3.org>
Fri, 25 May 2018 12:23:48 +0000 (14:23 +0200)
This patch shows collected SQL errors that occurred during an extension
installation as flash message.

Resolves: #82245
Releases: 7.6
Change-Id: Ifdfd738045022109ce33b33846e9673898077fc2
Reviewed-on: https://review.typo3.org/54497
Reviewed-by: Alexander Opitz <opitz.alexander@googlemail.com>
Tested-by: Alexander Opitz <opitz.alexander@googlemail.com>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/extensionmanager/Classes/Controller/UploadExtensionFileController.php
typo3/sysext/extensionmanager/Classes/Exception/SqlErrorException.php [new file with mode: 0644]
typo3/sysext/extensionmanager/Classes/Utility/InstallUtility.php

index 9502b35..e4b0f2b 100644 (file)
@@ -19,6 +19,7 @@ use TYPO3\CMS\Core\Messaging\FlashMessage;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extensionmanager\Exception\DependencyConfigurationNotFoundException;
 use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
+use TYPO3\CMS\Extensionmanager\Exception\SqlErrorException;
 
 /**
  * Controller for handling upload of a local extension file
@@ -175,6 +176,8 @@ class UploadExtensionFileController extends AbstractController
             throw $exception;
         } catch (DependencyConfigurationNotFoundException $exception) {
             $this->addFlashMessage($exception->getMessage(), '', FlashMessage::ERROR);
+        } catch (SqlErrorException $exception) {
+            $this->addFlashMessage($exception->getMessage(), '', FlashMessage::ERROR);
         } catch (\Exception $exception) {
             $this->removeExtensionAndRestoreFromBackup($fileName);
             $this->addFlashMessage($exception->getMessage(), '', FlashMessage::ERROR);
diff --git a/typo3/sysext/extensionmanager/Classes/Exception/SqlErrorException.php b/typo3/sysext/extensionmanager/Classes/Exception/SqlErrorException.php
new file mode 100644 (file)
index 0000000..f17c50a
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+namespace TYPO3\CMS\Extensionmanager\Exception;
+
+/*
+ * 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!
+ */
+
+/**
+ * An exception when an SQL error in ext_tables.sql or ext_tables_static+adt.sql occurred
+ * @internal Only internal usage in TYPO3 v7, not in TYPO3 v8 and up.
+ */
+class SqlErrorException extends ExtensionManagerException
+{
+}
index 25ddc60..796c9be 100644 (file)
@@ -18,6 +18,7 @@ use TYPO3\CMS\Core\Service\OpcodeCacheService;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
 use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
+use TYPO3\CMS\Extensionmanager\Exception\SqlErrorException;
 use TYPO3\CMS\Impexp\Utility\ImportExportUtility;
 
 /**
@@ -471,6 +472,7 @@ class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface
      * Update database / process db updates from ext_tables
      *
      * @param string $rawDefinitions The raw SQL statements from ext_tables.sql
+     * @throws ExtensionManagerException
      * @return void
      */
     public function updateDbWithExtTablesSql($rawDefinitions)
@@ -481,14 +483,27 @@ class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface
             $diff = $this->installToolSqlParser->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase);
             $updateStatements = $this->installToolSqlParser->getUpdateSuggestions($diff);
             $db = $this->getDatabaseConnection();
+            $errors = [];
             foreach ((array)$updateStatements['add'] as $string) {
-                $db->admin_query($string);
+                $res = $db->admin_query($string);
+                if ($res === false) {
+                    $errors[] = 'Error: You have an error in your SQL syntax;' . $string;
+                }
             }
             foreach ((array)$updateStatements['change'] as $string) {
-                $db->admin_query($string);
+                $res = $db->admin_query($string);
+                if ($res === false) {
+                    $errors[] = 'Error: You have an error in your SQL syntax;' . $string;
+                }
             }
             foreach ((array)$updateStatements['create_table'] as $string) {
-                $db->admin_query($string);
+                $res = $db->admin_query($string);
+                if ($res === false) {
+                    $errors[] = 'Error: You have an error in your SQL syntax;' . $string;
+                }
+            }
+            if (!empty($errors)) {
+                throw new SqlErrorException(implode(PHP_EOL, $errors), 1509303611);
             }
         }
     }
@@ -497,6 +512,7 @@ class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface
      * Import static SQL data (normally used for ext_tables_static+adt.sql)
      *
      * @param string $rawDefinitions
+     * @throws ExtensionManagerException
      * @return void
      */
     public function importStaticSql($rawDefinitions)
@@ -504,17 +520,23 @@ class InstallUtility implements \TYPO3\CMS\Core\SingletonInterface
         $statements = $this->installToolSqlParser->getStatementarray($rawDefinitions, 1);
         list($statementsPerTable, $insertCount) = $this->installToolSqlParser->getCreateTables($statements, 1);
         $db = $this->getDatabaseConnection();
+        $errors = [];
         // Traverse the tables
         foreach ($statementsPerTable as $table => $query) {
             $db->admin_query('DROP TABLE IF EXISTS ' . $table);
-            $db->admin_query($query);
-            if ($insertCount[$table]) {
+            $res = $db->admin_query($query);
+            if ($res === false) {
+                $errors[] = 'Error: You have an error in your SQL syntax;' . PHP_EOL . $string;
+            } elseif ($insertCount[$table]) {
                 $insertStatements = $this->installToolSqlParser->getTableInsertStatements($statements, $table);
                 foreach ($insertStatements as $statement) {
                     $db->admin_query($statement);
                 }
             }
         }
+        if (!empty($errors)) {
+            throw new SqlErrorException(implode(PHP_EOL, $errors), 1509303783);
+        }
     }
 
     /**