‪TYPO3CMS  10.4
ImportExportUtility.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 Psr\EventDispatcher\EventDispatcherInterface;
23 
30 {
34  protected ‪$import;
35 
40 
41  public function ‪__construct(EventDispatcherInterface ‪$eventDispatcher)
42  {
43  $this->eventDispatcher = ‪$eventDispatcher;
44  }
45 
49  public function ‪getImport(): ?‪Import
50  {
51  return ‪$this->import;
52  }
53 
63  public function ‪importT3DFile($file, $pid)
64  {
65  if (!is_string($file)) {
66  throw new \InvalidArgumentException('Input parameter $file has to be of type string', 1377625645);
67  }
68  if (!is_int($pid)) {
69  throw new \InvalidArgumentException('Input parameter $int has to be of type integer', 1377625646);
70  }
71  $this->import = GeneralUtility::makeInstance(Import::class);
72  $this->import->init();
73 
74  $this->eventDispatcher->dispatch(new BeforeImportEvent($this->import));
75 
76  $importResponse = 0;
77  if ($file && @is_file($file)) {
78  if ($this->import->loadFile($file, 1)) {
79  // Import to root page:
80  $this->import->importData($pid);
81  // Get id of first created page:
82  $newPages = $this->import->import_mapId['pages'];
83  $importResponse = (int)reset($newPages);
84  }
85  }
86 
87  // Check for errors during the import process:
88  ‪$errors = $this->import->printErrorLog();
89  if (‪$errors !== '') {
90  $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
91  $logger->warning(‪$errors);
92 
93  if (!$importResponse) {
94  throw new \ErrorException('No page records imported', 1377625537);
95  }
96  }
97  return $importResponse;
98  }
99 }
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility\$import
‪Import null $import
Definition: ImportExportUtility.php:33
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility\importT3DFile
‪int importT3DFile($file, $pid)
Definition: ImportExportUtility.php:61
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility
Definition: ImportExportUtility.php:30
‪TYPO3\CMS\Impexp\Import
Definition: Import.php:42
‪TYPO3\CMS\Impexp\Utility
Definition: ImportExportUtility.php:16
‪TYPO3\CMS\Impexp\Event\BeforeImportEvent
Definition: BeforeImportEvent.php:26
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility\getImport
‪Import null getImport()
Definition: ImportExportUtility.php:47
‪$errors
‪$errors
Definition: annotationChecker.php:121
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: ImportExportUtility.php:37
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility\__construct
‪__construct(EventDispatcherInterface $eventDispatcher)
Definition: ImportExportUtility.php:39