[FEATURE] CLI support for T3D import 77/45977/18
authorMartin Kutschker <masi@typo3.org>
Fri, 15 Jan 2016 21:00:48 +0000 (21:00 +0000)
committerGeorg Ringer <georg.ringer@gmail.com>
Wed, 18 Jan 2017 12:17:46 +0000 (13:17 +0100)
A new CLI command allows to imoprt t3d/xml files for EXT:impexp.

Resolves: #72749
Releases: master
Change-Id: I8775bcc1d39901c25f0c4613d1d5fc3ff3b8868a
Reviewed-on: https://review.typo3.org/45977
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/core/Documentation/Changelog/master/Feature-72749-CLISupportForT3DImport.rst [new file with mode: 0644]
typo3/sysext/impexp/Classes/Command/Exception/ImportFailedException.php [new file with mode: 0644]
typo3/sysext/impexp/Classes/Command/Exception/InvalidFileException.php [new file with mode: 0644]
typo3/sysext/impexp/Classes/Command/Exception/LoadingFileFailedException.php [new file with mode: 0644]
typo3/sysext/impexp/Classes/Command/Exception/PrerequisitesNotMetException.php [new file with mode: 0644]
typo3/sysext/impexp/Classes/Command/ImportCommand.php [new file with mode: 0644]
typo3/sysext/impexp/Configuration/Commands.php [new file with mode: 0644]

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-72749-CLISupportForT3DImport.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-72749-CLISupportForT3DImport.rst
new file mode 100644 (file)
index 0000000..224742b
--- /dev/null
@@ -0,0 +1,37 @@
+.. include:: ../../Includes.txt
+
+============================================
+Feature: #72749 - CLI support for T3D import
+============================================
+
+See :issue:`72749`
+
+
+Description
+===========
+
+The impexp extension now allows to import data files (T3D or XML) via the command line interface through a Symfony
+Command.
+
+Impact
+======
+
+The command line allows the following options:
+
+.. code-block:: text
+
+    Imports a T3D file into a page.
+
+    USAGE:
+     ./typo3/sysext/core/bin/typo3 impexp:import [<options>] <file> <pageId>
+
+    ARGUMENTS:
+      --file      The path / filename to import (.t3d or .xml), the EXT: syntax can be used as well
+      --pageId    The page id where the page should be started from, defaults to "0" if not set
+
+    OPTIONS:
+      --updateRecords   Force updating existing records
+      --ignorePid       Don't correct page ids of updated records
+      --enableLog       log all database action
+
+.. index:: CLI, ext:impexp
diff --git a/typo3/sysext/impexp/Classes/Command/Exception/ImportFailedException.php b/typo3/sysext/impexp/Classes/Command/Exception/ImportFailedException.php
new file mode 100644 (file)
index 0000000..d4ce686
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+namespace TYPO3\CMS\Impexp\Command\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!
+ */
+
+/**
+ * Import failed
+ */
+class ImportFailedException extends \RuntimeException
+{
+}
diff --git a/typo3/sysext/impexp/Classes/Command/Exception/InvalidFileException.php b/typo3/sysext/impexp/Classes/Command/Exception/InvalidFileException.php
new file mode 100644 (file)
index 0000000..53a82b3
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+namespace TYPO3\CMS\Impexp\Command\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!
+ */
+
+/**
+ * File was not found or could not be opened
+ */
+class InvalidFileException extends \InvalidArgumentException
+{
+}
diff --git a/typo3/sysext/impexp/Classes/Command/Exception/LoadingFileFailedException.php b/typo3/sysext/impexp/Classes/Command/Exception/LoadingFileFailedException.php
new file mode 100644 (file)
index 0000000..7abbc8e
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+namespace TYPO3\CMS\Impexp\Command\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!
+ */
+
+/**
+ * File could not be loaded
+ */
+class LoadingFileFailedException extends \RuntimeException
+{
+}
diff --git a/typo3/sysext/impexp/Classes/Command/Exception/PrerequisitesNotMetException.php b/typo3/sysext/impexp/Classes/Command/Exception/PrerequisitesNotMetException.php
new file mode 100644 (file)
index 0000000..3be4e31
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+namespace TYPO3\CMS\Impexp\Command\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!
+ */
+
+/**
+ * Prerequisites are not met
+ */
+class PrerequisitesNotMetException extends \RuntimeException
+{
+}
diff --git a/typo3/sysext/impexp/Classes/Command/ImportCommand.php b/typo3/sysext/impexp/Classes/Command/ImportCommand.php
new file mode 100644 (file)
index 0000000..cf82dca
--- /dev/null
@@ -0,0 +1,115 @@
+<?php
+namespace TYPO3\CMS\Impexp\Command;
+
+/*
+ * 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 Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+use TYPO3\CMS\Core\Core\Bootstrap;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Impexp\Import;
+
+/**
+ * Command for importing T3D/XML data files
+ */
+class ImportCommand extends Command
+{
+    /**
+     * Configure the command by defining the name, options and arguments
+     */
+    protected function configure()
+    {
+        $this
+            ->setDescription('Imports a T3D / XML file with content into a page tree')
+            ->addArgument(
+                'file',
+                InputArgument::REQUIRED,
+                'The path and filename to import (.t3d or .xml)'
+            )
+            ->addArgument(
+                'pageId',
+                InputArgument::OPTIONAL,
+                'The page ID to start from. If empty, the root level (= pageId=0) is used.'
+            )->addOption(
+                'updateRecords',
+                null,
+                InputOption::VALUE_NONE,
+                'If set, existing records with the same UID will be updated instead of inserted'
+            )->addOption(
+                'ignorePid',
+                null,
+                InputOption::VALUE_NONE,
+                'If set, page IDs of updated records are not corrected (only works in conjunction with the updateRecords option)'
+            )->addOption(
+                'enableLog',
+                null,
+                InputOption::VALUE_NONE,
+                'If set, all database actions are logged'
+            );
+    }
+
+    /**
+     * Executes the command for importing a t3d/xml file into the TYPO3 system
+     *
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     * @return void
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $fileName = $input->getArgument('file');
+        $fileName = GeneralUtility::getFileAbsFileName($fileName);
+        if (empty($fileName) || !file_exists($fileName)) {
+            throw new Exception\InvalidFileException('The given filename "' . ($fileName ?? $input->getArgument('file')) . '" could not be found', 1484483040);
+        }
+
+        $io = new SymfonyStyle($input, $output);
+
+        // Ensure the _cli_ user is authenticated
+        Bootstrap::getInstance()->initializeBackendAuthentication();
+
+        $pageId = (int)$input->getArgument('pageId');
+
+        $import = GeneralUtility::makeInstance(Import::class);
+        $import->init();
+        $import->update = (bool)($input->hasOption('updateRecords') && $input->getOption('updateRecords'));
+        // Only used when $updateRecords is "true"
+        $import->global_ignore_pid = (bool)($input->hasOption('ignorePid') && $input->getOption('ignorePid'));
+        // Enables logging of database actions
+        $import->enableLogging = (bool)($input->hasOption('enableLog') && $input->getOption('enableLog'));
+
+        if (!$import->loadFile($fileName, true)) {
+            $io->error($import->errorLog);
+            throw new Exception\LoadingFileFailedException('Loading of the import file failed.', 1484484619);
+        }
+
+        $messages = $import->checkImportPrerequisites();
+        if (!empty($messages)) {
+            $io->error($messages);
+            throw new Exception\PrerequisitesNotMetException('Prerequisites for file import are not met.', 1484484612);
+        }
+
+        $import->importData($pageId);
+        if (!empty($import->errorLog)) {
+            $io->error($import->errorLog);
+            throw new Exception\ImportFailedException('The import has failed.', 1484484613);
+        }
+
+        $io->success('Imported ' . $input->getArgument('file') . ' to page ' . $pageId . ' successfully');
+    }
+}
diff --git a/typo3/sysext/impexp/Configuration/Commands.php b/typo3/sysext/impexp/Configuration/Commands.php
new file mode 100644 (file)
index 0000000..cecbaea
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+/**
+ * Commands to be executed by typo3, where the key of the array
+ * is the name of the command (to be called as the first argument after typo3).
+ * Required parameter is the "class" of the command which needs to be a subclass
+ * of Symfony/Console/Command.
+ */
+return [
+    'impexp:import' => [
+        'class' => \TYPO3\CMS\Impexp\Command\ImportCommand::class
+    ]
+];