[CLEANUP] ImportExport classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / impexp / Classes / Utility / ImportExportUtility.php
1 <?php
2 namespace TYPO3\CMS\Impexp\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Log\LogManager;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
20 use TYPO3\CMS\Impexp\ImportExport;
21
22 /**
23 * Utility for import / export
24 * Can be used for API access for simple importing of files
25 *
26 */
27 class ImportExportUtility
28 {
29 /**
30 * Import a T3D file directly
31 *
32 * @param string $file The full absolute path to the file
33 * @param int $pid The pid under which the t3d file should be imported
34 * @throws \ErrorException
35 * @throws \InvalidArgumentException
36 * @return int
37 */
38 public function importT3DFile($file, $pid)
39 {
40 if (!is_string($file)) {
41 throw new \InvalidArgumentException('Input parameter $file has to be of type string', 1377625645);
42 }
43 if (!is_int($pid)) {
44 throw new \InvalidArgumentException('Input parameter $int has to be of type integer', 1377625646);
45 }
46 /** @var $import ImportExport */
47 $import = GeneralUtility::makeInstance(ImportExport::class);
48 $import->init(0, 'import');
49
50 $this->emitAfterImportExportInitialisationSignal($import);
51
52 $importResponse = 0;
53 if ($file && @is_file($file)) {
54 if ($import->loadFile($file, 1)) {
55 // Import to root page:
56 $import->importData($pid);
57 // Get id of first created page:
58 $newPages = $import->import_mapId['pages'];
59 $importResponse = (int)reset($newPages);
60 }
61 }
62
63 // Check for errors during the import process:
64 $errors = $import->printErrorLog();
65 if ($errors !== '') {
66 /** @var \TYPO3\CMS\Core\Log\Logger $logger */
67 $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
68 $logger->warning($errors);
69
70 if (!$importResponse) {
71 throw new \ErrorException('No page records imported', 1377625537);
72 }
73 }
74 return $importResponse;
75 }
76
77 /**
78 * Get the SignalSlot dispatcher
79 *
80 * @return Dispatcher
81 */
82 protected function getSignalSlotDispatcher()
83 {
84 return GeneralUtility::makeInstance(Dispatcher::class);
85 }
86
87 /**
88 * Emits a signal after initialization
89 *
90 * @param ImportExport $import
91 * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException
92 * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException
93 */
94 protected function emitAfterImportExportInitialisationSignal(ImportExport $import)
95 {
96 $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'afterImportExportInitialisation', array($import));
97 }
98 }