ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilImport.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
12{
16 protected $log;
17
18 protected $install_id = "";
19 protected $install_url = "";
20 protected $entities = "";
21 protected $tmp_import_dir = "";
22
23 protected $mapping = null;
24 protected $skip_entity = array();
25 protected $configs = array();
26
27 protected $skip_importer = [];
28
35 public function __construct($a_target_id = 0)
36 {
37 include_once("./Services/Export/classes/class.ilImportMapping.php");
38 $this->mapping = new ilImportMapping();
39 $this->mapping->setTargetId($a_target_id);
40 $this->log = ilLoggerFactory::getLogger('exp');
41 }
42
50 public function getConfig($a_comp)
51 {
52 // if created, return existing config object
53 if (isset($this->configs[$a_comp])) {
54 return $this->configs[$a_comp];
55 }
56
57 // create instance of export config object
58 $comp_arr = explode("/", $a_comp);
59 $a_class = "il" . $comp_arr[1] . "ImportConfig";
60 $imp_config = new $a_class();
61 $this->configs[$a_comp] = $imp_config;
62
63 return $imp_config;
64 }
65
70 public function getMapping()
71 {
72 return $this->mapping;
73 }
74
80 final public function setEntityTypes($a_val)
81 {
82 $this->entity_types = $a_val;
83 }
84
90 final public function getEntityTypes()
91 {
92 return $this->entity_types;
93 }
94
101 public function addSkipEntity($a_component, $a_entity, $skip = true)
102 {
103 $this->skip_entity[$a_component][$a_entity] = $skip;
104 }
105
112 public function addSkipImporter($a_component, $skip = true)
113 {
114 $this->skip_importer[$a_component] = $skip;
115 }
116
122 public function setCurrentDataset($a_val)
123 {
124 $this->current_dataset = $a_val;
125 }
126
133 public function getCurrentDataset()
134 {
135 return $this->current_dataset;
136 }
137
141 public function afterEntityTypes()
142 {
143 $this->getCurrentDataset()->setImport($this);
144 }
145
151 public function importRecord($a_entity, $a_types, $a_record)
152 {
153 $this->getCurrentDataset()->importRecord($a_entity, $a_types, $a_record);
154 }
155
156
160 final public function importEntity(
161 $a_tmp_file,
162 $a_filename,
163 $a_entity,
164 $a_component,
165 $a_copy_file = false
166 ) {
167 $this->importObject(null, $a_tmp_file, $a_filename, $a_entity, $a_component, $a_copy_file);
168 }
169
170
176 final public function importObject(
177 $a_new_obj,
178 $a_tmp_file,
179 $a_filename,
180 $a_type,
181 $a_comp = "",
182 $a_copy_file = false
183 ) {
184
185 // create temporary directory
186 $tmpdir = ilUtil::ilTempnam();
187 ilUtil::makeDir($tmpdir);
188 if ($a_copy_file) {
189 copy($a_tmp_file, $tmpdir . "/" . $a_filename);
190 } else {
191 ilUtil::moveUploadedFile($a_tmp_file, $a_filename, $tmpdir . "/" . $a_filename);
192 }
193
194 $this->log->debug("unzip: " . $tmpdir . "/" . $a_filename);
195
196 ilUtil::unzip($tmpdir . "/" . $a_filename);
197 $dir = $tmpdir . "/" . substr($a_filename, 0, strlen($a_filename) - 4);
198
199 $this->setTemporaryImportDir($dir);
200
201 $this->log->debug("dir: " . $dir);
202
203 $ret = $this->doImportObject($dir, $a_type, $a_comp, $tmpdir);
204 $new_id = null;
205 if (is_array($ret)) {
206 $new_id = $ret['new_id'];
207 }
208
209 // delete temporary directory
210 ilUtil::delDir($tmpdir);
211 return $new_id;
212 }
213
220 public function importFromDirectory($dir, $a_type, $a_comp)
221 {
222 $ret = $this->doImportObject($dir, $a_type, $a_comp);
223
224 if (is_array($ret)) {
225 return $ret['new_id'];
226 }
227 return null;
228 }
229
230
236 protected function setTemporaryImportDir($a_val)
237 {
238 $this->tmp_import_dir = $a_val;
239 }
240
246 public function getTemporaryImportDir()
247 {
249 }
250
256 protected function doImportObject($dir, $a_type, $a_component = "", $a_tmpdir = "")
257 {
258 if ($a_component == "") {
259 include_once("./Services/Export/classes/class.ilImportExportFactory.php");
260 $a_component = ilImportExportFactory::getComponentForExport($a_type);
261 }
262 $this->comp = $a_component;
263
264 // get import class
265 $success = true;
266
267 // process manifest file
268 include_once("./Services/Export/classes/class.ilManifestParser.php");
269 if (!is_file($dir . "/manifest.xml")) {
270 include_once("./Services/Export/exceptions/class.ilManifestFileNotFoundImportException.php");
271 $mess = (DEVMODE)
272 ? 'Manifest file not found: "' . $dir . "/manifest.xml" . '".'
273 : 'Manifest file not found: "manifest.xml."';
275 $e->setManifestDir($dir);
276 $e->setTmpDir($a_tmpdir);
277 throw $e;
278 }
279 $parser = new ilManifestParser($dir . "/manifest.xml");
280 $this->mapping->setInstallUrl($parser->getInstallUrl());
281 $this->mapping->setInstallId($parser->getInstallId());
282
283 // check for correct type
284 if ($parser->getMainEntity() != $a_type) {
285 include_once("./Services/Export/exceptions/class.ilImportObjectTypeMismatchException.php");
286 $e = new ilImportObjectTypeMismatchException("Object type does not match. Import file has type '" . $parser->getMainEntity() . "' but import being processed for '" . $a_type . "'.");
287 throw $e;
288 }
289
290 // process export files
291 $expfiles = $parser->getExportFiles();
292
293 include_once("./Services/Export/classes/class.ilExportFileParser.php");
294 include_once("./Services/Export/classes/class.ilImportExportFactory.php");
295 $all_importers = array();
296 foreach ($expfiles as $expfile) {
297 $comp = $expfile["component"];
298
299 if (isset($this->skip_importer[$comp]) && $this->skip_importer[$comp] === true) {
300 continue;
301 }
302
303 $class = ilImportExportFactory::getImporterClass($comp);
304
305 // log a warning for inactive page component plugins, but continue import
306 // page content will be imported, but not its additional data
307 // (other plugins throw an exception in ilImportExportFactory)
308 if ($class == '') {
309 $this->log->warning("no class found for component: $comp");
310 continue;
311 }
312
313 $this->log->debug("create new class = $class");
314
315 $this->importer = new $class();
316 $this->importer->setImport($this);
317 $all_importers[] = $this->importer;
318 $this->importer->setImportDirectory($dir);
319 $this->importer->init();
320 $this->current_comp = $comp;
321 try {
322 $this->log->debug("Process file: " . $dir . "/" . $expfile["path"]);
323 $parser = new ilExportFileParser($dir . "/" . $expfile["path"], $this, "processItemXml");
324 } catch (Exception $e) {
325 $this->log->error("Import failed: " . $e->getMessage());
326 throw $e;
327 }
328 }
329
330 // write import ids before(!) final processing
331 $obj_map = $this->getMapping()->getMappingsOfEntity('Services/Container', 'objs');
332 if (is_array($obj_map)) {
333 foreach ($obj_map as $obj_id_old => $obj_id_new) {
334 ilObject::_writeImportId($obj_id_new, "il_" . $this->mapping->getInstallId() . "_" . ilObject::_lookupType($obj_id_new) . "_" . $obj_id_old);
335 }
336 }
337
338 // final processing
339 foreach ($all_importers as $imp) {
340 $this->log->debug("Call finalProcessing for: " . get_class($imp));
341 $imp->finalProcessing($this->mapping);
342 }
343
344 // we should only get on mapping here
345 $top_mapping = $this->mapping->getMappingsOfEntity($this->comp, $a_type);
346 $new_id = (int) current($top_mapping);
347 return array(
348 'new_id' => $new_id,
349 'importers' => (array) $all_importers
350 );
351 }
352
358 public function processItemXml($a_entity, $a_schema_version, $a_id, $a_xml, $a_install_id, $a_install_url)
359 {
360 global $DIC;
361
362 $objDefinition = $DIC['objDefinition'];
363
364 // skip
365 if ($this->skip_entity[$this->current_comp][$a_entity]) {
366 return;
367 }
368
369 if ($objDefinition->isRBACObject($a_entity) &&
370 $this->getMapping()->getMapping('Services/Container', 'imported', $a_id)) {
371 $this->log->info('Ignoring referenced ' . $a_entity . ' with id ' . $a_id);
372 return;
373 }
374 $this->importer->setInstallId($a_install_id);
375 $this->importer->setInstallUrl($a_install_url);
376 $this->importer->setSchemaVersion($a_schema_version);
377 $this->importer->setSkipEntities($this->skip_entity);
378 $new_id = $this->importer->importXmlRepresentation($a_entity, $a_id, $a_xml, $this->mapping);
379
380 // Store information about imported obj_ids in mapping to avoid double imports of references
381 if ($objDefinition->isRBACObject($a_entity)) {
382 $this->getMapping()->addMapping('Services/Container', 'imported', $a_id, 1);
383 }
384
385 // @TODO new id is not always set
386 if ($new_id && $new_id !== true) {
387 $this->mapping->addMapping($this->comp, $a_entity, $a_id, $new_id);
388 }
389 }
390}
$parser
Definition: BPMN2Parser.php:23
$success
Definition: Utf8Test.php:86
An exception for terminatinating execution or to throw for unit testing.
Import class.
importFromDirectory($dir, $a_type, $a_comp)
Import from directory.
setEntityTypes($a_val)
Set entity types.
getEntityTypes()
Get entity types.
processItemXml($a_entity, $a_schema_version, $a_id, $a_xml, $a_install_id, $a_install_url)
Process item xml.
getTemporaryImportDir()
Get temporary import directory.
__construct($a_target_id=0)
Constructor.
addSkipEntity($a_component, $a_entity, $skip=true)
Add skip entity.
getMapping()
Get mapping object.
setTemporaryImportDir($a_val)
Set temporary import directory.
getConfig($a_comp)
Get configuration (note that configurations are optional, null may be returned!)
addSkipImporter($a_component, $skip=true)
Add skip entity.
importObject( $a_new_obj, $a_tmp_file, $a_filename, $a_type, $a_comp="", $a_copy_file=false)
Import repository object export file.
getCurrentDataset()
Get currrent dataset.
afterEntityTypes()
After entity types are parsed.
importRecord($a_entity, $a_types, $a_record)
After entity types are parsed.
setCurrentDataset($a_val)
Set currrent dataset.
doImportObject($dir, $a_type, $a_component="", $a_tmpdir="")
Import repository object export file.
importEntity( $a_tmp_file, $a_filename, $a_entity, $a_component, $a_copy_file=false)
Import entity.
static getLogger($a_component_id)
Get component logger.
manifest.xml file not found-exception for import
Manifest parser for ILIAS standard export files.
static _writeImportId($a_obj_id, $a_import_id)
write import id to db (static)
static _lookupType($a_id, $a_reference=false)
lookup object type
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static unzip($a_file, $overwrite=false, $a_flat=false)
unzip file
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
$ret
Definition: parser.php:6
$a_type
Definition: workflow.php:92
$DIC
Definition: xapitoken.php:46