‪TYPO3CMS  ‪main
ImportCommand.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Symfony\Component\Console\Attribute\AsCommand;
21 use Symfony\Component\Console\Command\Command;
22 use Symfony\Component\Console\Input\InputArgument;
23 use Symfony\Component\Console\Input\InputInterface;
24 use Symfony\Component\Console\Input\InputOption;
25 use Symfony\Component\Console\Output\OutputInterface;
26 use Symfony\Component\Console\Style\SymfonyStyle;
30 
34 #[AsCommand('impexp:import', 'Imports a T3D / XML file with content into a page tree')]
35 class ‪ImportCommand extends Command
36 {
37  public function ‪__construct(protected readonly ‪Import $import)
38  {
39  parent::__construct();
40  }
41 
45  protected function ‪configure(): void
46  {
47  $this
48  ->addArgument(
49  'file',
50  InputArgument::REQUIRED,
51  'The file path to import from (.t3d or .xml).'
52  )
53  ->addArgument(
54  'pid',
55  InputArgument::OPTIONAL,
56  'The page to import to.',
57  0
58  )
59  ->addOption(
60  'update-records',
61  null,
62  InputOption::VALUE_NONE,
63  'If set, existing records with the same UID will be updated instead of inserted.'
64  )
65  ->addOption(
66  'ignore-pid',
67  null,
68  InputOption::VALUE_NONE,
69  'If set, page IDs of updated records are not corrected (only works in conjunction with --update-records).'
70  )
71  ->addOption(
72  'force-uid',
73  null,
74  InputOption::VALUE_NONE,
75  'If set, UIDs from file will be forced.'
76  )
77  ->addOption(
78  'import-mode',
79  null,
80  InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
81  sprintf(
82  'Set the import mode of this specific record. ' . PHP_EOL .
83  'Pattern is "{table}:{record}={mode}". ' . PHP_EOL .
84  'Available modes for new records are "%1$s" and "%3$s" ' .
85  'and for existing records "%2$s", "%4$s", "%5$s" and "%3$s".' . PHP_EOL .
86  'Examples are "pages:987=%1$s", "tt_content:1=%2$s", etc.',
92  )
93  )
94  ->addOption(
95  'enable-log',
96  null,
97  InputOption::VALUE_NONE,
98  'If set, all database actions are logged.'
99  )
100  ;
101  }
102 
106  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
107  {
108  // Ensure the _cli_ user is authenticated
110 
111  $io = new SymfonyStyle($input, ‪$output);
112 
113  try {
114  $this->import->setPid((int)$input->getArgument('pid'));
115  $this->import->setUpdate($input->getOption('update-records'));
116  $this->import->setGlobalIgnorePid($input->getOption('ignore-pid'));
117  $this->import->setForceAllUids($input->getOption('force-uid'));
118  $this->import->setEnableLogging($input->getOption('enable-log'));
119  $this->import->setImportMode($this->‪parseAssociativeArray($input, 'import-mode', '='));
120  $this->import->loadFile((string)$input->getArgument('file'));
121  $this->import->checkImportPrerequisites();
122  $this->import->importData();
123  $io->success('Importing ' . $input->getArgument('file') . ' to page ' . $input->getArgument('pid') . ' succeeded.');
124  return Command::SUCCESS;
125  } catch (\‪Exception $e) {
126  // Since impexp triggers core and DataHandler with potential hooks, and exception could come from "everywhere".
127  $io->error('Importing ' . $input->getArgument('file') . ' to page ' . $input->getArgument('pid') . ' failed.');
128  if ($io->isVerbose()) {
129  $io->writeln($e->getMessage());
130  $io->writeln($this->import->getErrorLog());
131  }
132  return Command::FAILURE;
133  }
134  }
135 
140  protected function ‪parseAssociativeArray(InputInterface &$input, string $optionName, string $separator): array
141  {
142  $array = [];
143 
144  foreach ($input->getOption($optionName) as &$value) {
145  $parts = ‪GeneralUtility::trimExplode($separator, $value, true, 2);
146  if (count($parts) === 2) {
147  $array[$parts[0]] = $parts[1];
148  } else {
149  throw new \InvalidArgumentException(
150  sprintf('Command line option "%s" has invalid entry "%s".', $optionName, $value),
151  1610464090
152  );
153  }
154  }
155 
156  return $array;
157  }
158 }
‪TYPO3\CMS\Impexp\Command
Definition: ExportCommand.php:18
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_AS_NEW
‪const IMPORT_MODE_AS_NEW
Definition: Import.php:53
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Impexp\Command\ImportCommand
Definition: ImportCommand.php:36
‪TYPO3\CMS\Impexp\Command\ImportCommand\configure
‪configure()
Definition: ImportCommand.php:45
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_FORCE_UID
‪const IMPORT_MODE_FORCE_UID
Definition: Import.php:52
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_EXCLUDE
‪const IMPORT_MODE_EXCLUDE
Definition: Import.php:54
‪TYPO3\CMS\Impexp\Command\ImportCommand\__construct
‪__construct(protected readonly Import $import)
Definition: ImportCommand.php:37
‪TYPO3\CMS\Impexp\Import
Definition: Import.php:51
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_RESPECT_PID
‪const IMPORT_MODE_RESPECT_PID
Definition: Import.php:56
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_IGNORE_PID
‪const IMPORT_MODE_IGNORE_PID
Definition: Import.php:55
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Impexp\Command\ImportCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: ImportCommand.php:106
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:62
‪TYPO3\CMS\Impexp\Command\ImportCommand\parseAssociativeArray
‪parseAssociativeArray(InputInterface &$input, string $optionName, string $separator)
Definition: ImportCommand.php:140
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Core\Bootstrap\initializeBackendAuthentication
‪static initializeBackendAuthentication()
Definition: Bootstrap.php:527
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822