‪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\Command\Command;
21 use Symfony\Component\Console\Input\InputArgument;
22 use Symfony\Component\Console\Input\InputInterface;
23 use Symfony\Component\Console\Input\InputOption;
24 use Symfony\Component\Console\Output\OutputInterface;
25 use Symfony\Component\Console\Style\SymfonyStyle;
29 
33 class ‪ImportCommand extends Command
34 {
35  public function ‪__construct(protected readonly ‪Import $import)
36  {
37  parent::__construct();
38  }
39 
43  protected function ‪configure(): void
44  {
45  $this
46  ->addArgument(
47  'file',
48  InputArgument::REQUIRED,
49  'The file path to import from (.t3d or .xml).'
50  )
51  ->addArgument(
52  'pid',
53  InputArgument::OPTIONAL,
54  'The page to import to.',
55  0
56  )
57  ->addOption(
58  'update-records',
59  null,
60  InputOption::VALUE_NONE,
61  'If set, existing records with the same UID will be updated instead of inserted.'
62  )
63  ->addOption(
64  'ignore-pid',
65  null,
66  InputOption::VALUE_NONE,
67  'If set, page IDs of updated records are not corrected (only works in conjunction with --update-records).'
68  )
69  ->addOption(
70  'force-uid',
71  null,
72  InputOption::VALUE_NONE,
73  'If set, UIDs from file will be forced.'
74  )
75  ->addOption(
76  'import-mode',
77  null,
78  InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
79  sprintf(
80  'Set the import mode of this specific record. ' . PHP_EOL .
81  'Pattern is "{table}:{record}={mode}". ' . PHP_EOL .
82  'Available modes for new records are "%1$s" and "%3$s" ' .
83  'and for existing records "%2$s", "%4$s", "%5$s" and "%3$s".' . PHP_EOL .
84  'Examples are "pages:987=%1$s", "tt_content:1=%2$s", etc.',
90  )
91  )
92  ->addOption(
93  'enable-log',
94  null,
95  InputOption::VALUE_NONE,
96  'If set, all database actions are logged.'
97  )
98  ;
99  }
100 
104  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
105  {
106  // Ensure the _cli_ user is authenticated
108 
109  $io = new SymfonyStyle($input, ‪$output);
110 
111  try {
112  $this->import->setPid((int)$input->getArgument('pid'));
113  $this->import->setUpdate($input->getOption('update-records'));
114  $this->import->setGlobalIgnorePid($input->getOption('ignore-pid'));
115  $this->import->setForceAllUids($input->getOption('force-uid'));
116  $this->import->setEnableLogging($input->getOption('enable-log'));
117  $this->import->setImportMode($this->‪parseAssociativeArray($input, 'import-mode', '='));
118  $this->import->loadFile((string)$input->getArgument('file'), true);
119  $this->import->checkImportPrerequisites();
120  $this->import->importData();
121  $io->success('Importing ' . $input->getArgument('file') . ' to page ' . $input->getArgument('pid') . ' succeeded.');
122  return Command::SUCCESS;
123  } catch (\‪Exception $e) {
124  // Since impexp triggers core and DataHandler with potential hooks, and exception could come from "everywhere".
125  $io->error('Importing ' . $input->getArgument('file') . ' to page ' . $input->getArgument('pid') . ' failed.');
126  if ($io->isVerbose()) {
127  $io->writeln($e->getMessage());
128  $io->writeln($this->import->getErrorLog());
129  }
130  return Command::FAILURE;
131  }
132  }
133 
138  protected function ‪parseAssociativeArray(InputInterface &$input, string $optionName, string $separator): array
139  {
140  $array = [];
141 
142  foreach ($input->getOption($optionName) as &$value) {
143  $parts = GeneralUtility::trimExplode($separator, $value, true, 2);
144  if (count($parts) === 2) {
145  $array[$parts[0]] = $parts[1];
146  } else {
147  throw new \InvalidArgumentException(
148  sprintf('Command line option "%s" has invalid entry "%s".', $optionName, $value),
149  1610464090
150  );
151  }
152  }
153 
154  return $array;
155  }
156 }
‪TYPO3\CMS\Impexp\Command
Definition: ExportCommand.php:18
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_AS_NEW
‪const IMPORT_MODE_AS_NEW
Definition: Import.php:52
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Impexp\Command\ImportCommand
Definition: ImportCommand.php:34
‪TYPO3\CMS\Impexp\Command\ImportCommand\configure
‪configure()
Definition: ImportCommand.php:43
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_FORCE_UID
‪const IMPORT_MODE_FORCE_UID
Definition: Import.php:51
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_EXCLUDE
‪const IMPORT_MODE_EXCLUDE
Definition: Import.php:53
‪TYPO3\CMS\Impexp\Command\ImportCommand\__construct
‪__construct(protected readonly Import $import)
Definition: ImportCommand.php:35
‪TYPO3\CMS\Impexp\Import
Definition: Import.php:50
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_RESPECT_PID
‪const IMPORT_MODE_RESPECT_PID
Definition: Import.php:55
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_IGNORE_PID
‪const IMPORT_MODE_IGNORE_PID
Definition: Import.php:54
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Impexp\Command\ImportCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: ImportCommand.php:104
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:64
‪TYPO3\CMS\Impexp\Command\ImportCommand\parseAssociativeArray
‪parseAssociativeArray(InputInterface &$input, string $optionName, string $separator)
Definition: ImportCommand.php:138
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Core\Bootstrap\initializeBackendAuthentication
‪static initializeBackendAuthentication()
Definition: Bootstrap.php:529