‪TYPO3CMS  ‪main
ImportExportUtility.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 Psr\EventDispatcher\EventDispatcherInterface;
25 
33 {
34  protected ?‪Import ‪$import = null;
35  protected EventDispatcherInterface ‪$eventDispatcher;
36 
37  public function ‪__construct(EventDispatcherInterface ‪$eventDispatcher)
38  {
39  $this->eventDispatcher = ‪$eventDispatcher;
40  }
41 
42  public function ‪getImport(): ?‪Import
43  {
44  return ‪$this->import;
45  }
46 
55  public function ‪importT3DFile(string $file, int $pid): int
56  {
57  $this->import = GeneralUtility::makeInstance(Import::class);
58  $this->import->setPid($pid);
59 
60  $this->eventDispatcher->dispatch(new ‪BeforeImportEvent($this->import, $file));
61 
62  try {
63  $this->import->loadFile($file);
64  $this->import->importData();
65  } catch (\‪Exception $e) {
66  $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
67  $logger->warning(
68  $e->getMessage() . PHP_EOL . implode(PHP_EOL, $this->import->getErrorLog())
69  );
70  }
71 
72  // Get id of first created page:
73  $importResponse = 0;
74  $importMapId = $this->import->getImportMapId();
75  if (isset($importMapId['pages'])) {
76  $newPages = $importMapId['pages'];
77  $importResponse = (int)reset($newPages);
78  }
79 
80  // Check for errors during the import process:
81  if ($this->import->hasErrors()) {
82  if (!$importResponse) {
83  throw new \ErrorException('No page records imported', 1377625537);
84  }
85  }
86  return $importResponse;
87  }
88 }
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility\importT3DFile
‪int importT3DFile(string $file, int $pid)
Definition: ImportExportUtility.php:55
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility\getImport
‪getImport()
Definition: ImportExportUtility.php:42
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility
Definition: ImportExportUtility.php:33
‪TYPO3\CMS\Impexp\Import
Definition: Import.php:51
‪TYPO3\CMS\Impexp\Utility
Definition: ImportExportUtility.php:18
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility\$import
‪Import $import
Definition: ImportExportUtility.php:34
‪TYPO3\CMS\Impexp\Event\BeforeImportEvent
Definition: BeforeImportEvent.php:26
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: ImportExportUtility.php:35
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Impexp\Utility\ImportExportUtility\__construct
‪__construct(EventDispatcherInterface $eventDispatcher)
Definition: ImportExportUtility.php:37