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
16namespace TYPO3\CMS\Impexp\Utility;
17
18use Psr\EventDispatcher\EventDispatcherInterface;
19use TYPO3\CMS\Core\Log\LogManager;
20use TYPO3\CMS\Core\Utility\GeneralUtility;
21use TYPO3\CMS\Impexp\Event\BeforeImportEvent;
22use TYPO3\CMS\Impexp\Import;
23
24/**
25 * Utility for import / export
26 * Can be used for API access for simple importing of files
27 * @internal
28 */
29class ImportExportUtility
30{
31    /**
32     * @var Import|null
33     */
34    protected $import;
35
36    /**
37     * @var EventDispatcherInterface
38     */
39    protected $eventDispatcher;
40
41    public function __construct(EventDispatcherInterface $eventDispatcher)
42    {
43        $this->eventDispatcher = $eventDispatcher;
44    }
45
46    /**
47     * @return Import|null
48     */
49    public function getImport(): ?Import
50    {
51        return $this->import;
52    }
53
54    /**
55     * Import a T3D file directly
56     *
57     * @param string $file The full absolute path to the file
58     * @param int $pid The pid under which the t3d file should be imported
59     * @throws \ErrorException
60     * @throws \InvalidArgumentException
61     * @return int
62     */
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}
100