ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 {
13  protected $install_id = "";
14  protected $install_url = "";
15  protected $entities = "";
16 
17  protected $mapping = null;
18 
25  function __construct($a_target_id = 0)
26  {
27  include_once("./Services/Export/classes/class.ilImportMapping.php");
28  $this->mapping = new ilImportMapping();
29  $this->mapping->setTagetId($a_target_id);
30  }
31 
36  public function getMapping()
37  {
38  return $this->mapping;
39  }
40 
46  final function setEntityTypes($a_val)
47  {
48  $this->entity_types = $a_val;
49  }
50 
56  final function getEntityTypes()
57  {
58  return $this->entity_types;
59  }
60 
66  function setCurrentDataset($a_val)
67  {
68  $this->current_dataset = $a_val;
69  }
70 
76  function getCurrentDataset()
77  {
78  return $this->current_dataset;
79  }
80 
84  function afterEntityTypes()
85  {
86  $this->getCurrentDataset()->setImport($this);
87  }
88 
94  function importRecord($a_entity, $a_types, $a_record)
95  {
96  $this->getCurrentDataset()->importRecord($a_entity, $a_types, $a_record);
97  }
98 
99 
105  final public function importObject($a_new_obj, $a_tmp_file, $a_filename, $a_type)
106  {
107  // create temporary directory
108  $tmpdir = ilUtil::ilTempnam();
109  ilUtil::makeDir($tmpdir);
110  ilUtil::moveUploadedFile($a_tmp_file, $a_filename, $tmpdir."/".$a_filename);
111  ilUtil::unzip($tmpdir."/".$a_filename);
112  $dir = $tmpdir."/".substr($a_filename, 0, strlen($a_filename) - 4);
113 
114  $GLOBALS['ilLog']->write(__METHOD__.': do import with dir '.$dir);
115  $new_id = $this->doImportObject($dir, $a_type);
116 
117  // delete temporary directory
118  ilUtil::delDir($tmpdir);
119 
120  return $new_id;
121  }
122 
123 
129  protected function doImportObject($dir,$a_type)
130  {
131  global $objDefinition, $tpl;
132 
133  $comp = $objDefinition->getComponentForType($a_type);
134  $class = $objDefinition->getClassName($a_type);
135 
136  $this->comp = $comp;
137 
138  // get import class
139  $success = true;
140 
141  // process manifest file
142  include_once("./Services/Export/classes/class.ilManifestParser.php");
143  $parser = new ilManifestParser($dir."/manifest.xml");
144  $this->mapping->setInstallUrl($parser->getInstallUrl());
145  $this->mapping->setInstallId($parser->getInstallId());
146 
147  // process export files
148  $expfiles = $parser->getExportFiles();
149 
150  include_once("./Services/Export/classes/class.ilExportFileParser.php");
151  $all_importers = array();
152  foreach ($expfiles as $expfile)
153  {
154  $comp = $expfile["component"];
155  $comp_arr = explode("/", $comp);
156  $import_class_file = "./".$comp."/classes/class.il".$comp_arr[1]."Importer.php";
157  $class = "il".$comp_arr[1]."Importer";
158  include_once($import_class_file);
159  $this->importer = new $class();
160  $all_importers[] = $this->importer;
161  $this->importer->setImportDirectory($dir);
162  $this->importer->init();
163 
164  $parser = new ilExportFileParser($dir."/".$expfile["path"],
165  $this, "processItemXml");
166  }
167 
168  // final processing
169  foreach ($all_importers as $imp)
170  {
171  $imp->finalProcessing($this->mapping);
172  }
173 
174  // we should only get on mapping here
175  $top_mapping = $this->mapping->getMappingsOfEntity($this->comp, $a_type);
176  $new_id = (int) current($top_mapping);
177 //echo "end of ilImport::importObject()."; exit;
178  return $new_id;
179  }
180 
186  function processItemXml($a_entity, $a_schema_version, $a_id, $a_xml,$a_install_id, $a_install_url)
187  {
188  global $objDefinition;
189 
190  if($this->getMapping()->getMapping('Services/Container', 'imported', $a_id))
191  {
192  $GLOBALS['ilLog']->write(__METHOD__.': Ignoring referenced '.$a_entity.' with id '.$a_id);
193  return;
194  }
195  $this->importer->setInstallId($a_install_id);
196  $this->importer->setInstallUrl($a_install_url);
197  $this->importer->setSchemaVersion($a_schema_version);
198 
199  $new_id = $this->importer->importXmlRepresentation($a_entity, $a_id, $a_xml, $this->mapping);
200 
201  // Store information about imported obj_ids in mapping to avoid double imports of references
202  if($objDefinition->isRBACObject($a_entity))
203  {
204  $this->getMapping()->addMapping('Services/Container', 'imported', $a_id, 1);
205  }
206 
207  // @TODO new id is not always set
208  if($new_id)
209  {
210  $this->mapping->addMapping($this->comp ,$a_entity, $a_id, $new_id);
211  }
212  }
213 
214 }
215 ?>