ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
11 class ilImport
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 
33  function __construct($a_target_id = 0)
34  {
35  include_once("./Services/Export/classes/class.ilImportMapping.php");
36  $this->mapping = new ilImportMapping();
37  $this->mapping->setTargetId($a_target_id);
38  $this->log = ilLoggerFactory::getLogger('exp');
39  }
40 
48  function getConfig($a_comp)
49  {
50  // if created, return existing config object
51  if (isset($this->configs[$a_comp]))
52  {
53  return $this->configs[$a_comp];
54  }
55 
56  // create instance of export config object
57  $comp_arr = explode("/", $a_comp);
58  $a_class = "il".$comp_arr[1]."ImportConfig";
59  $import_config_file = "./".$a_comp."/classes/class.".$a_class.".php";
60  if (!is_file($import_config_file))
61  {
62  include_once("./Services/Export/exceptions/class.ilImportException.php");
63  throw new ilImportException('Component "'.$a_comp.'" does not provide ImportConfig class.');
64  }
65  include_once($import_config_file);
66  $imp_config = new $a_class();
67  $this->configs[$a_comp] = $imp_config;
68 
69  return $imp_config;
70  }
71 
76  public function getMapping()
77  {
78  return $this->mapping;
79  }
80 
86  final function setEntityTypes($a_val)
87  {
88  $this->entity_types = $a_val;
89  }
90 
96  final function getEntityTypes()
97  {
98  return $this->entity_types;
99  }
100 
107  function addSkipEntity($a_component, $a_entity, $skip = true)
108  {
109  $this->skip_entity[$a_component][$a_entity] = $skip;
110  }
111 
117  function setCurrentDataset($a_val)
118  {
119  $this->current_dataset = $a_val;
120  }
121 
128  function getCurrentDataset()
129  {
130  return $this->current_dataset;
131  }
132 
136  function afterEntityTypes()
137  {
138  $this->getCurrentDataset()->setImport($this);
139  }
140 
146  function importRecord($a_entity, $a_types, $a_record)
147  {
148  $this->getCurrentDataset()->importRecord($a_entity, $a_types, $a_record);
149  }
150 
151 
155  final public function importEntity($a_tmp_file, $a_filename,
156  $a_entity, $a_component, $a_copy_file = false)
157  {
158  $this->importObject(null, $a_tmp_file, $a_filename, $a_entity, $a_component, $a_copy_file);
159  }
160 
161 
167  final public function importObject($a_new_obj, $a_tmp_file, $a_filename, $a_type,
168  $a_comp = "", $a_copy_file = false)
169  {
170 
171  // create temporary directory
172  $tmpdir = ilUtil::ilTempnam();
173  ilUtil::makeDir($tmpdir);
174  if ($a_copy_file)
175  {
176  copy($a_tmp_file, $tmpdir."/".$a_filename);
177  }
178  else
179  {
180  ilUtil::moveUploadedFile($a_tmp_file, $a_filename, $tmpdir."/".$a_filename);
181  }
182 
183  $this->log->debug("unzip: ".$tmpdir."/".$a_filename);
184 
185  ilUtil::unzip($tmpdir."/".$a_filename);
186  $dir = $tmpdir."/".substr($a_filename, 0, strlen($a_filename) - 4);
187 
188  $this->setTemporaryImportDir($dir);
189 
190  $this->log->debug("dir: ".$dir);
191 
192  $ret = $this->doImportObject($dir, $a_type, $a_comp, $tmpdir);
193  $new_id = null;
194  if(is_array($ret))
195  {
196  $new_id = $ret['new_id'];
197  }
198 
199  // delete temporary directory
200  ilUtil::delDir($tmpdir);
201  return $new_id;
202  }
203 
210  function importFromDirectory($dir, $a_type, $a_comp)
211  {
212  $ret = $this->doImportObject($dir, $a_type, $a_comp);
213 
214  if(is_array($ret))
215  {
216  return $ret['new_id'];
217  }
218  return null;
219  }
220 
221 
227  protected function setTemporaryImportDir($a_val)
228  {
229  $this->tmp_import_dir = $a_val;
230  }
231 
237  public function getTemporaryImportDir()
238  {
239  return $this->tmp_import_dir;
240  }
241 
247  protected function doImportObject($dir, $a_type, $a_component = "", $a_tmpdir = "")
248  {
249  if ($a_component == "")
250  {
251  include_once("./Services/Export/classes/class.ilImportExportFactory.php");
252  $a_component = ilImportExportFactory::getComponentForExport($a_type);
253  }
254  $this->comp = $a_component;
255 
256  // get import class
257  $success = true;
258 
259  // process manifest file
260  include_once("./Services/Export/classes/class.ilManifestParser.php");
261  if (!is_file($dir."/manifest.xml"))
262  {
263  include_once("./Services/Export/exceptions/class.ilManifestFileNotFoundImportException.php");
264  $mess = (DEVMODE)
265  ? 'Manifest file not found: "'.$dir."/manifest.xml".'".'
266  : 'Manifest file not found: "manifest.xml."';
268  $e->setManifestDir($dir);
269  $e->setTmpDir($a_tmpdir);
270  throw $e;
271  }
272  $parser = new ilManifestParser($dir."/manifest.xml");
273  $this->mapping->setInstallUrl($parser->getInstallUrl());
274  $this->mapping->setInstallId($parser->getInstallId());
275 
276  // check for correct type
277  if ($parser->getMainEntity() != $a_type)
278  {
279  include_once("./Services/Export/exceptions/class.ilImportObjectTypeMismatchException.php");
280  $e = new ilImportObjectTypeMismatchException("Object type does not match. Import file has type '".$parser->getMainEntity()."' but import being processed for '".$a_type."'.");
281  throw $e;
282  }
283 
284  // process export files
285  $expfiles = $parser->getExportFiles();
286 
287  include_once("./Services/Export/classes/class.ilExportFileParser.php");
288  include_once("./Services/Export/classes/class.ilImportExportFactory.php");
289  $all_importers = array();
290  foreach ($expfiles as $expfile)
291  {
292  $comp = $expfile["component"];
293  $class = ilImportExportFactory::getImporterClass($comp);
294 
295  $this->log->debug("create new class = $class");
296 
297  $this->importer = new $class();
298  $this->importer->setImport($this);
299  $all_importers[] = $this->importer;
300  $this->importer->setImportDirectory($dir);
301  $this->importer->init();
302  $this->current_comp = $comp;
303  try {
304  $this->log->debug("Process file: ".$dir."/".$expfile["path"]);
305  $parser = new ilExportFileParser($dir."/".$expfile["path"],$this, "processItemXml");
306  }
307  catch(Exception $e)
308  {
309  $this->log->error("Import failed: ".$e->getMessage());
310  throw $e;
311  }
312  }
313 
314  // write import ids before(!) final processing
315  $obj_map = $this->getMapping()->getMappingsOfEntity('Services/Container', 'objs');
316  if (is_array($obj_map))
317  {
318  foreach ($obj_map as $obj_id_old => $obj_id_new)
319  {
320  ilObject::_writeImportId($obj_id_new, "il_".$this->mapping->getInstallId()."_".ilObject::_lookupType($obj_id_new)."_".$obj_id_old);
321  }
322  }
323 
324  // final processing
325  foreach ($all_importers as $imp)
326  {
327  $this->log->debug("Call finalProcessing for: ".get_class($imp));
328  $imp->finalProcessing($this->mapping);
329  }
330 
331  // we should only get on mapping here
332  $top_mapping = $this->mapping->getMappingsOfEntity($this->comp, $a_type);
333  $new_id = (int) current($top_mapping);
334  return array(
335  'new_id' => $new_id,
336  'importers' => (array) $all_importers
337  );
338  }
339 
345  function processItemXml($a_entity, $a_schema_version, $a_id, $a_xml,$a_install_id, $a_install_url)
346  {
347  global $objDefinition;
348 
349  // skip
350  if ($this->skip_entity[$this->current_comp][$a_entity])
351  {
352  return;
353  }
354 
355  if($objDefinition->isRBACObject($a_entity) &&
356  $this->getMapping()->getMapping('Services/Container', 'imported', $a_id))
357  {
358  $this->log->info('Ignoring referenced '.$a_entity.' with id '.$a_id);
359  return;
360  }
361  $this->importer->setInstallId($a_install_id);
362  $this->importer->setInstallUrl($a_install_url);
363  $this->importer->setSchemaVersion($a_schema_version);
364  $this->importer->setSkipEntities($this->skip_entity);
365  $new_id = $this->importer->importXmlRepresentation($a_entity, $a_id, $a_xml, $this->mapping);
366 
367  // Store information about imported obj_ids in mapping to avoid double imports of references
368  if($objDefinition->isRBACObject($a_entity))
369  {
370  $this->getMapping()->addMapping('Services/Container', 'imported', $a_id, 1);
371  }
372 
373  // @TODO new id is not always set
374  if($new_id)
375  {
376  $this->mapping->addMapping($this->comp ,$a_entity, $a_id, $new_id);
377  }
378  }
379 
380 }
381 ?>
setEntityTypes($a_val)
Set entity types.
doImportObject($dir, $a_type, $a_component="", $a_tmpdir="")
Import repository object export file.
importObject($a_new_obj, $a_tmp_file, $a_filename, $a_type, $a_comp="", $a_copy_file=false)
Import repository object export file.
afterEntityTypes()
After entity types are parsed.
getMapping()
Get mapping object.
processItemXml($a_entity, $a_schema_version, $a_id, $a_xml, $a_install_id, $a_install_url)
Process item xml.
static unzip($a_file, $overwrite=false, $a_flat=false)
unzip file
importEntity($a_tmp_file, $a_filename, $a_entity, $a_component, $a_copy_file=false)
Import entity.
Import class.
importFromDirectory($dir, $a_type, $a_comp)
Import from directory.
addSkipEntity($a_component, $a_entity, $skip=true)
Add skip entity.
$a_type
Definition: workflow.php:93
getTemporaryImportDir()
Get temporary import directory.
$success
Definition: Utf8Test.php:86
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
General import exception.
manifest.xml file not found-exception for import
setCurrentDataset($a_val)
Set currrent dataset.
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
getConfig($a_comp)
Get configuration (note that configurations are optional, null may be returned!)
Manifest parser for ILIAS standard export files.
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
getEntityTypes()
Get entity types.
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
$parser
Definition: BPMN2Parser.php:24
static _writeImportId($a_obj_id, $a_import_id)
write import id to db (static)
$ret
Definition: parser.php:6
static getLogger($a_component_id)
Get component logger.
importRecord($a_entity, $a_types, $a_record)
After entity types are parsed.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
setTemporaryImportDir($a_val)
Set temporary import directory.
getCurrentDataset()
Get currrent dataset.
__construct($a_target_id=0)
Constructor.