‪TYPO3CMS  11.5
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  protected ‪Import ‪$import;
36 
38  {
39  $this->import = ‪$import;
40  parent::__construct();
41  }
42 
46  protected function ‪configure(): void
47  {
48  $this
49  ->addArgument(
50  'file',
51  InputArgument::REQUIRED,
52  'The file path to import from (.t3d or .xml).'
53  )
54  ->addArgument(
55  'pid',
56  InputArgument::OPTIONAL,
57  'The page to import to.',
58  0
59  )
60  ->addOption(
61  'update-records',
62  null,
63  InputOption::VALUE_NONE,
64  'If set, existing records with the same UID will be updated instead of inserted.'
65  )
66  ->addOption(
67  'ignore-pid',
68  null,
69  InputOption::VALUE_NONE,
70  'If set, page IDs of updated records are not corrected (only works in conjunction with --update-records).'
71  )
72  ->addOption(
73  'force-uid',
74  null,
75  InputOption::VALUE_NONE,
76  'If set, UIDs from file will be forced.'
77  )
78  ->addOption(
79  'import-mode',
80  null,
81  InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
82  sprintf(
83  'Set the import mode of this specific record. ' . PHP_EOL .
84  'Pattern is "{table}:{record}={mode}". ' . PHP_EOL .
85  'Available modes for new records are "%1$s" and "%3$s" ' .
86  'and for existing records "%2$s", "%4$s", "%5$s" and "%3$s".' . PHP_EOL .
87  'Examples are "pages:987=%1$s", "tt_content:1=%2$s", etc.',
93  )
94  )
95  ->addOption(
96  'enable-log',
97  null,
98  InputOption::VALUE_NONE,
99  'If set, all database actions are logged.'
100  )
101  // @deprecated since v11, will be removed in v12. Drop all options below and look for other fallbacks in the class.
102  ->addOption(
103  'updateRecords',
104  null,
105  InputOption::VALUE_NONE,
106  'Deprecated. Use --update-records instead.'
107  )
108  ->addOption(
109  'ignorePid',
110  null,
111  InputOption::VALUE_NONE,
112  'Deprecated. Use --ignore-pid instead.'
113  )
114  ->addOption(
115  'forceUid',
116  null,
117  InputOption::VALUE_NONE,
118  'Deprecated. Use --force-uid instead.'
119  )
120  ->addOption(
121  'importMode',
122  null,
123  InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
124  'Deprecated. Use --import-mode instead.'
125  )
126  ->addOption(
127  'enableLog',
128  null,
129  InputOption::VALUE_NONE,
130  'Deprecated. Use --enable-log instead.'
131  )
132  ;
133  }
134 
142  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
143  {
144  // @deprecated since v11, will be removed in v12. lowerCameCased options. Also look for other fallbacks in the class.
145  $deprecatedOptions = [
146  '--updateRecords' => '--update-records',
147  '--ignorePid' => '--ignore-pid',
148  '--forceUid' => '--force-uid',
149  '--importMode' => '--import-mode',
150  '--enableLog' => '--enable-log',
151  ];
152  foreach ($deprecatedOptions as $deprecatedName => $actualName) {
153  if ($input->hasParameterOption($deprecatedName, true)) {
154  $this->‪triggerCommandOptionDeprecation($deprecatedName, $actualName);
155  }
156  }
157 
158  // Ensure the _cli_ user is authenticated
160 
161  $io = new SymfonyStyle($input, ‪$output);
162 
163  try {
164  $this->import->setPid((int)$input->getArgument('pid'));
165  $this->import->setUpdate(
166  $input->getOption('updateRecords') ||
167  $input->getOption('update-records')
168  );
169  $this->import->setGlobalIgnorePid(
170  $input->getOption('ignorePid') ||
171  $input->getOption('ignore-pid')
172  );
173  $this->import->setForceAllUids(
174  $input->getOption('forceUid') ||
175  $input->getOption('force-uid')
176  );
177  $this->import->setEnableLogging(
178  $input->getOption('enableLog') ||
179  $input->getOption('enable-log')
180  );
181  $this->import->setImportMode(
182  array_merge(
183  $this->‪parseAssociativeArray($input, 'importMode', '='),
184  $this->‪parseAssociativeArray($input, 'import-mode', '='),
185  )
186  );
187  $this->import->loadFile((string)$input->getArgument('file'), true);
188  $this->import->checkImportPrerequisites();
189  $this->import->importData();
190  $io->success('Importing ' . $input->getArgument('file') . ' to page ' . $input->getArgument('pid') . ' succeeded.');
191  return 0;
192  } catch (\‪Exception $e) {
193  // Since impexp triggers core and DataHandler with potential hooks, and exception could come from "everywhere".
194  $io->error('Importing ' . $input->getArgument('file') . ' to page ' . $input->getArgument('pid') . ' failed.');
195  if ($io->isVerbose()) {
196  $io->writeln($e->getMessage());
197  $io->writeln($this->import->getErrorLog());
198  }
199  return 1;
200  }
201  }
202 
206  protected function ‪triggerCommandOptionDeprecation(string $deprecatedName, string $actualName): void
207  {
208  trigger_error(
209  sprintf(
210  'Command option "impexp:import %s" is deprecated and will be removed in v12. Use "%s" instead.',
211  $deprecatedName,
212  $actualName
213  ),
214  E_USER_DEPRECATED
215  );
216  }
217 
227  protected function ‪parseAssociativeArray(InputInterface &$input, string $optionName, string $separator): array
228  {
229  $array = [];
230 
231  foreach ($input->getOption($optionName) as &$value) {
232  $parts = ‪GeneralUtility::trimExplode($separator, $value, true, 2);
233  if (count($parts) === 2) {
234  $array[$parts[0]] = $parts[1];
235  } else {
236  throw new \InvalidArgumentException(
237  sprintf('Command line option "%s" has invalid entry "%s".', $optionName, $value),
238  1610464090
239  );
240  }
241  }
242 
243  return $array;
244  }
245 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:999
‪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\Core\Core\Bootstrap\initializeBackendAuthentication
‪static initializeBackendAuthentication($proceedIfNoUserIsLoggedIn=false)
Definition: Bootstrap.php:588
‪TYPO3\CMS\Impexp\Command\ImportCommand\configure
‪configure()
Definition: ImportCommand.php:46
‪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\Import
Definition: Import.php:50
‪TYPO3\CMS\Impexp\Command\ImportCommand\execute
‪int execute(InputInterface $input, OutputInterface $output)
Definition: ImportCommand.php:142
‪TYPO3\CMS\Impexp\Command\ImportCommand\$import
‪Import $import
Definition: ImportCommand.php:35
‪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
‪TYPO3\CMS\Impexp\Command\ImportCommand\triggerCommandOptionDeprecation
‪triggerCommandOptionDeprecation(string $deprecatedName, string $actualName)
Definition: ImportCommand.php:206
‪$output
‪$output
Definition: annotationChecker.php:121
‪TYPO3\CMS\Impexp\Command\ImportCommand\__construct
‪__construct(Import $import)
Definition: ImportCommand.php:37
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:70
‪TYPO3\CMS\Impexp\Command\ImportCommand\parseAssociativeArray
‪array parseAssociativeArray(InputInterface &$input, string $optionName, string $separator)
Definition: ImportCommand.php:227
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50