TYPO3 CMS  TYPO3_8-7
ImportCommand.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
26 
30 class ImportCommand extends Command
31 {
35  protected function configure()
36  {
37  $this
38  ->setDescription('Imports a T3D / XML file with content into a page tree')
39  ->addArgument(
40  'file',
41  InputArgument::REQUIRED,
42  'The path and filename to import (.t3d or .xml)'
43  )
44  ->addArgument(
45  'pageId',
46  InputArgument::OPTIONAL,
47  'The page ID to start from. If empty, the root level (= pageId=0) is used.'
48  )->addOption(
49  'updateRecords',
50  null,
51  InputOption::VALUE_NONE,
52  'If set, existing records with the same UID will be updated instead of inserted'
53  )->addOption(
54  'ignorePid',
55  null,
56  InputOption::VALUE_NONE,
57  'If set, page IDs of updated records are not corrected (only works in conjunction with the updateRecords option)'
58  )->addOption(
59  'enableLog',
60  null,
61  InputOption::VALUE_NONE,
62  'If set, all database actions are logged'
63  );
64  }
65 
72  protected function execute(InputInterface $input, OutputInterface $output)
73  {
74  $fileName = $input->getArgument('file');
75  $fileName = GeneralUtility::getFileAbsFileName($fileName);
76  if (empty($fileName) || !file_exists($fileName)) {
77  throw new Exception\InvalidFileException('The given filename "' . ($fileName ?? $input->getArgument('file')) . '" could not be found', 1484483040);
78  }
79 
80  $io = new SymfonyStyle($input, $output);
81 
82  // Ensure the _cli_ user is authenticated
83  Bootstrap::getInstance()->initializeBackendAuthentication();
84 
85  $pageId = (int)$input->getArgument('pageId');
86 
87  $import = GeneralUtility::makeInstance(Import::class);
88  $import->init();
89  $import->update = (bool)($input->hasOption('updateRecords') && $input->getOption('updateRecords'));
90  // Only used when $updateRecords is "true"
91  $import->global_ignore_pid = (bool)($input->hasOption('ignorePid') && $input->getOption('ignorePid'));
92  // Enables logging of database actions
93  $import->enableLogging = (bool)($input->hasOption('enableLog') && $input->getOption('enableLog'));
94 
95  if (!$import->loadFile($fileName, true)) {
96  $io->error($import->errorLog);
97  throw new Exception\LoadingFileFailedException('Loading of the import file failed.', 1484484619);
98  }
99 
100  $messages = $import->checkImportPrerequisites();
101  if (!empty($messages)) {
102  $io->error($messages);
103  throw new Exception\PrerequisitesNotMetException('Prerequisites for file import are not met.', 1484484612);
104  }
105 
106  $import->importData($pageId);
107  if (!empty($import->errorLog)) {
108  $io->error($import->errorLog);
109  throw new Exception\ImportFailedException('The import has failed.', 1484484613);
110  }
111 
112  $io->success('Imported ' . $input->getArgument('file') . ' to page ' . $pageId . ' successfully');
113  }
114 }
static getFileAbsFileName($filename, $_=null, $_2=null)
static makeInstance($className,... $constructorArguments)
execute(InputInterface $input, OutputInterface $output)