ILIAS  release_8 Revision v8.23
class.ilImport.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
25 class ilImport
26 {
27  protected ilLogger $log;
29 
30  private array $entity_types = [];
31  private ?ilXmlImporter $importer = null;
32  private string $comp = '';
33  private string $current_comp = '';
34  protected string $install_id = "";
35  protected string $install_url = "";
36  protected string $entities = "";
37  protected string $tmp_import_dir = "";
38 
39  protected ?ilImportMapping $mapping = null;
40  protected array $skip_entity = array();
41  protected array $configs = array();
42  protected array $skip_importer = [];
43 
44  public function __construct(int $a_target_id = 0)
45  {
46  global $DIC;
47 
48  $this->objDefinition = $DIC['objDefinition'];
49  $this->mapping = new ilImportMapping();
50  $this->mapping->setTargetId($a_target_id);
51  $this->log = $DIC->logger()->exp();
52  }
53 
57  public function getConfig(string $a_comp): ilImportConfig
58  {
59  // if created, return existing config object
60  if (isset($this->configs[$a_comp])) {
61  return $this->configs[$a_comp];
62  }
63 
64  // create instance of export config object
65  $comp_arr = explode("/", $a_comp);
66  $a_class = "il" . $comp_arr[1] . "ImportConfig";
67  $imp_config = new $a_class();
68  $this->configs[$a_comp] = $imp_config;
69 
70  return $imp_config;
71  }
72 
73  public function getMapping(): ilImportMapping
74  {
75  return $this->mapping;
76  }
77 
78  final public function setEntityTypes(array $a_val): void
79  {
80  $this->entity_types = $a_val;
81  }
82 
83  final public function getEntityTypes(): array
84  {
85  return $this->entity_types;
86  }
87 
91  public function addSkipEntity(string $a_component, string $a_entity, bool $skip = true): void
92  {
93  $this->skip_entity[$a_component][$a_entity] = $skip;
94  }
95 
96  public function addSkipImporter(string $a_component, bool $skip = true): void
97  {
98  $this->skip_importer[$a_component] = $skip;
99  }
100 
104  final public function importEntity(
105  string $a_tmp_file,
106  string $a_filename,
107  string $a_entity,
108  string $a_component,
109  bool $a_copy_file = false
110  ): int {
111  return $this->importObject(null, $a_tmp_file, $a_filename, $a_entity, $a_component, $a_copy_file);
112  }
113 
114  final public function importObject(
115  ?object $a_new_obj,
116  string $a_tmp_file,
117  string $a_filename,
118  string $a_type,
119  string $a_comp = "",
120  bool $a_copy_file = false
121  ): ?int {
122 
123  // create temporary directory
124  $tmpdir = ilFileUtils::ilTempnam();
125  ilFileUtils::makeDir($tmpdir);
126  if ($a_copy_file) {
127  copy($a_tmp_file, $tmpdir . "/" . $a_filename);
128  } else {
129  ilFileUtils::moveUploadedFile($a_tmp_file, $a_filename, $tmpdir . "/" . $a_filename);
130  }
131 
132  $this->log->debug("unzip: " . $tmpdir . "/" . $a_filename);
133  ilFileUtils::unzip($tmpdir . "/" . $a_filename);
134  $dir = $tmpdir . "/" . substr($a_filename, 0, strlen($a_filename) - 4);
135  $this->setTemporaryImportDir($dir);
136  $this->log->debug("dir: " . $dir);
137  $ret = $this->doImportObject($dir, $a_type, $a_comp, $tmpdir);
138  $new_id = null;
139  if (is_array($ret) && array_key_exists('new_id', $ret)) {
140  $new_id = $ret['new_id'];
141  }
142  // delete temporary directory
143  ilFileUtils::delDir($tmpdir);
144  return $new_id;
145  }
146 
147  public function importFromDirectory(string $dir, string $a_type, string $a_comp): ?int
148  {
149  $ret = $this->doImportObject($dir, $a_type, $a_comp);
150  if (is_array($ret)) {
151  return $ret['new_id'];
152  }
153  return null;
154  }
155 
160  protected function setTemporaryImportDir(string $a_val)
161  {
162  $this->tmp_import_dir = $a_val;
163  }
164 
169  public function getTemporaryImportDir(): string
170  {
171  return $this->tmp_import_dir;
172  }
173 
177  protected function doImportObject(
178  string $dir,
179  string $a_type,
180  string $a_component = "",
181  string $a_tmpdir = ""
182  ): array {
183  if ($a_component == "") {
184  $a_component = ilImportExportFactory::getComponentForExport($a_type);
185  }
186  $this->comp = $a_component;
187 
188  // get import class
189  $success = true;
190 
191  // process manifest file
192  if (!is_file($dir . "/manifest.xml")) {
193  $mess = (DEVMODE)
194  ? 'Manifest file not found: "' . $dir . "/manifest.xml" . '".'
195  : 'Manifest file not found: "manifest.xml."';
197  $e->setManifestDir($dir);
198  $e->setTmpDir($a_tmpdir);
199  throw $e;
200  }
201  $parser = new ilManifestParser($dir . "/manifest.xml");
202  $this->mapping->setInstallUrl($parser->getInstallUrl());
203  $this->mapping->setInstallId($parser->getInstallId());
204 
205  // check for correct type
206  if ($parser->getMainEntity() != $a_type) {
208  "Object type does not match. Import file has type '" .
209  $parser->getMainEntity() . "' but import being processed for '" . $a_type . "'."
210  );
211  }
212 
213  // process export files
214  $expfiles = $parser->getExportFiles();
215 
216  $all_importers = array();
217  foreach ($expfiles as $expfile) {
218  $comp = $expfile["component"];
219 
220  if (isset($this->skip_importer[$comp]) && $this->skip_importer[$comp] === true) {
221  continue;
222  }
223 
224  $class = ilImportExportFactory::getImporterClass($comp);
225 
226  // log a warning for inactive page component plugins, but continue import
227  // page content will be imported, but not its additional data
228  // (other plugins throw an exception in ilImportExportFactory)
229  if ($class == '') {
230  $this->log->warning("no class found for component: $comp");
231  continue;
232  }
233 
234  $this->log->debug("create new class = $class");
235 
236  $this->importer = new $class();
237  $this->importer->setImport($this);
238  $all_importers[] = $this->importer;
239  $this->importer->setImportDirectory($dir);
240  $this->importer->init();
241  $this->current_comp = $comp;
242  try {
243  $this->log->debug("Process file: " . $dir . "/" . $expfile["path"]);
244  $parser = new ilExportFileParser($dir . "/" . $expfile["path"], $this, "processItemXml");
245  } catch (Exception $e) {
246  $this->log->error("Import failed: " . $e->getMessage());
247  $this->log->error('XML failed: ' . file_get_contents($dir . '/' . $expfile['path']));
248  throw $e;
249  }
250  }
251 
252  // write import ids before(!) final processing
253  $obj_map = $this->getMapping()->getMappingsOfEntity('Services/Container', 'objs');
254  if (is_array($obj_map)) {
255  foreach ($obj_map as $obj_id_old => $obj_id_new) {
257  (int) $obj_id_new,
258  "il_" . $this->mapping->getInstallId() . "_" . ilObject::_lookupType((int) $obj_id_new) . "_" . $obj_id_old
259  );
260  }
261  }
262 
263  // final processing
264  foreach ($all_importers as $imp) {
265  $this->log->debug("Call finalProcessing for: " . get_class($imp));
266  $imp->finalProcessing($this->mapping);
267  }
268 
269  // we should only get on mapping here
270  $top_mapping = $this->mapping->getMappingsOfEntity($this->comp, $a_type);
271 
272  $new_id = (int) current($top_mapping);
273  return array(
274  'new_id' => $new_id,
275  'importers' => $all_importers
276  );
277  }
278 
282  public function processItemXml(
283  string $a_entity,
284  string $a_schema_version,
285  string $a_id,
286  string $a_xml,
287  string $a_install_id,
288  string $a_install_url
289  ): void {
290  // skip
291  if (isset($this->skip_entity[$this->current_comp][$a_entity]) &&
292  $this->skip_entity[$this->current_comp][$a_entity]) {
293  return;
294  }
295 
296  if ($this->objDefinition->isRBACObject($a_entity) &&
297  $this->getMapping()->getMapping('Services/Container', 'imported', $a_id)) {
298  $this->log->info('Ignoring referenced ' . $a_entity . ' with id ' . $a_id);
299  return;
300  }
301  $this->importer->setInstallId($a_install_id);
302  $this->importer->setInstallUrl($a_install_url);
303  $this->importer->setSchemaVersion($a_schema_version);
304  $this->importer->setSkipEntities($this->skip_entity);
305  $this->importer->importXmlRepresentation($a_entity, $a_id, $a_xml, $this->mapping);
306 
307  // Store information about imported obj_ids in mapping to avoid double imports of references
308  if ($this->objDefinition->isRBACObject($a_entity)) {
309  $this->getMapping()->addMapping('Services/Container', 'imported', $a_id, '1');
310  }
311  }
312 }
doImportObject(string $dir, string $a_type, string $a_component="", string $a_tmpdir="")
Import repository object export file.
array $skip_entity
static _writeImportId(int $obj_id, string $import_id)
write import id to db (static)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setEntityTypes(array $a_val)
ilLogger $log
string $install_url
string $comp
string $entities
importEntity(string $a_tmp_file, string $a_filename, string $a_entity, string $a_component, bool $a_copy_file=false)
Import entity.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $tmp_import_dir
array $skip_importer
__construct(int $a_target_id=0)
addSkipEntity(string $a_component, string $a_entity, bool $skip=true)
Add skip entity.
importObject(?object $a_new_obj, string $a_tmp_file, string $a_filename, string $a_type, string $a_comp="", bool $a_copy_file=false)
static unzip(string $path_to_zip_file, bool $overwrite_existing=false, bool $unpack_flat=false)
global $DIC
Definition: feed.php:28
string $current_comp
ilXmlImporter $importer
parses the objects.xml it handles the xml-description of all ilias objects
getTemporaryImportDir()
Get temporary import directory.
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
ilImportMapping $mapping
getConfig(string $a_comp)
Get configuration (note that configurations are optional, null may be returned!)
addSkipImporter(string $a_component, bool $skip=true)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
array $entity_types
Manifest parser for ILIAS standard export files.
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
setImportDirectory(string $a_val)
processItemXml(string $a_entity, string $a_schema_version, string $a_id, string $a_xml, string $a_install_id, string $a_install_url)
Process item xml.
importFromDirectory(string $dir, string $a_type, string $a_comp)
setTemporaryImportDir(string $a_val)
Set temporary import directory.
string $install_id
static _lookupType(int $id, bool $reference=false)
array $configs
ilObjectDefinition $objDefinition
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...