‪TYPO3CMS  10.4
ImportCommand.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Symfony\Component\Console\Command\Command;
19 use Symfony\Component\Console\Input\InputArgument;
20 use Symfony\Component\Console\Input\InputInterface;
21 use Symfony\Component\Console\Input\InputOption;
22 use Symfony\Component\Console\Output\OutputInterface;
23 use Symfony\Component\Console\Style\SymfonyStyle;
31 
35 class ‪ImportCommand extends Command
36 {
40  protected function ‪configure()
41  {
42  $this
43  ->setDescription('Imports a T3D / XML file with content into a page tree')
44  ->addArgument(
45  'file',
46  InputArgument::REQUIRED,
47  'The path and filename to import (.t3d or .xml)'
48  )
49  ->addArgument(
50  'pageId',
51  InputArgument::OPTIONAL,
52  'The page ID to start from. If empty, the root level (= pageId=0) is used.'
53  )->addOption(
54  'updateRecords',
55  null,
56  InputOption::VALUE_NONE,
57  'If set, existing records with the same UID will be updated instead of inserted'
58  )->addOption(
59  'ignorePid',
60  null,
61  InputOption::VALUE_NONE,
62  'If set, page IDs of updated records are not corrected (only works in conjunction with the updateRecords option)'
63  )->addOption(
64  'forceUid',
65  null,
66  InputOption::VALUE_NONE,
67  'If set, UIDs from file will be forced.'
68  )->addOption(
69  'enableLog',
70  null,
71  InputOption::VALUE_NONE,
72  'If set, all database actions are logged'
73  );
74  }
75 
83  protected function ‪execute(InputInterface $input, OutputInterface ‪$output)
84  {
85  $fileName = (string)$input->getArgument('file');
86  $fileName = GeneralUtility::getFileAbsFileName($fileName);
87  if ($fileName === '' || !file_exists($fileName)) {
88  throw new ‪InvalidFileException('The given filename "' . $fileName . '" could not be found', 1484483040);
89  }
90 
91  $io = new SymfonyStyle($input, ‪$output);
92 
93  // Ensure the _cli_ user is authenticated
95 
96  $pageId = (int)$input->getArgument('pageId');
97 
98  $import = GeneralUtility::makeInstance(Import::class);
99  $import->init();
100  $import->update = (bool)($input->hasOption('updateRecords') && $input->getOption('updateRecords'));
101  // Only used when $updateRecords is "true"
102  $import->global_ignore_pid = (bool)($input->hasOption('ignorePid') && $input->getOption('ignorePid'));
103  // Force using UIDs from File
104  $import->force_all_UIDS = (bool)($input->hasOption('forceUid') && $input->getOption('forceUid'));
105  // Enables logging of database actions
106  $import->enableLogging = (bool)($input->hasOption('enableLog') && $input->getOption('enableLog'));
107 
108  if (!$import->loadFile($fileName, true)) {
109  $io->error($import->errorLog);
110  throw new ‪LoadingFileFailedException('Loading of the import file failed.', 1484484619);
111  }
112 
113  $messages = $import->checkImportPrerequisites();
114  if (!empty($messages)) {
115  $io->error($messages);
116  throw new ‪PrerequisitesNotMetException('Prerequisites for file import are not met.', 1484484612);
117  }
118 
119  $import->importData($pageId);
120  if (!empty($import->errorLog)) {
121  $io->error($import->errorLog);
122  throw new ‪ImportFailedException('The import has failed.', 1484484613);
123  }
124 
125  $io->success('Imported ' . $input->getArgument('file') . ' to page ' . $pageId . ' successfully');
126  return 0;
127  }
128 }
‪TYPO3\CMS\Impexp\Command
‪TYPO3\CMS\Impexp\Command\ImportCommand
Definition: ImportCommand.php:36
‪TYPO3\CMS\Impexp\Command\Exception\ImportFailedException
Definition: ImportFailedException.php:22
‪TYPO3\CMS\Core\Core\Bootstrap\initializeBackendAuthentication
‪static initializeBackendAuthentication($proceedIfNoUserIsLoggedIn=false)
Definition: Bootstrap.php:607
‪TYPO3\CMS\Impexp\Command\ImportCommand\configure
‪configure()
Definition: ImportCommand.php:40
‪TYPO3\CMS\Impexp\Command\Exception\LoadingFileFailedException
Definition: LoadingFileFailedException.php:22
‪TYPO3\CMS\Impexp\Command\Exception\InvalidFileException
Definition: InvalidFileException.php:22
‪TYPO3\CMS\Impexp\Import
Definition: Import.php:42
‪TYPO3\CMS\Impexp\Command\ImportCommand\execute
‪int execute(InputInterface $input, OutputInterface $output)
Definition: ImportCommand.php:83
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Impexp\Command\Exception\PrerequisitesNotMetException
Definition: PrerequisitesNotMetException.php:22
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46