ILIAS  release_4-3 Revision
 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  protected $skip_entity = array();
19 
26  function __construct($a_target_id = 0)
27  {
28  include_once("./Services/Export/classes/class.ilImportMapping.php");
29  $this->mapping = new ilImportMapping();
30  $this->mapping->setTagetId($a_target_id);
31  }
32 
37  public function getMapping()
38  {
39  return $this->mapping;
40  }
41 
47  final function setEntityTypes($a_val)
48  {
49  $this->entity_types = $a_val;
50  }
51 
57  final function getEntityTypes()
58  {
59  return $this->entity_types;
60  }
61 
68  function addSkipEntity($a_component, $a_entity, $skip = true)
69  {
70  $this->skip_entity[$a_component][$a_entity] = $skip;
71  }
72 
78  function setCurrentDataset($a_val)
79  {
80  $this->current_dataset = $a_val;
81  }
82 
89  function getCurrentDataset()
90  {
91  return $this->current_dataset;
92  }
93 
97  function afterEntityTypes()
98  {
99  $this->getCurrentDataset()->setImport($this);
100  }
101 
107  function importRecord($a_entity, $a_types, $a_record)
108  {
109  $this->getCurrentDataset()->importRecord($a_entity, $a_types, $a_record);
110  }
111 
112 
116  final public function importEntity($a_tmp_file, $a_filename,
117  $a_entity, $a_component, $a_copy_file = false)
118  {
119  $this->importObject(null, $a_tmp_file, $a_filename, $a_entity, $a_component, $a_copy_file);
120  }
121 
122 
128  final public function importObject($a_new_obj, $a_tmp_file, $a_filename, $a_type,
129  $a_comp = "", $a_copy_file = false)
130  {
131  // create temporary directory
132  $tmpdir = ilUtil::ilTempnam();
133  ilUtil::makeDir($tmpdir);
134  if ($a_copy_file)
135  {
136  copy($a_tmp_file, $tmpdir."/".$a_filename);
137  }
138  else
139  {
140  ilUtil::moveUploadedFile($a_tmp_file, $a_filename, $tmpdir."/".$a_filename);
141  }
142  ilUtil::unzip($tmpdir."/".$a_filename);
143  $dir = $tmpdir."/".substr($a_filename, 0, strlen($a_filename) - 4);
144 
145  $GLOBALS['ilLog']->write(__METHOD__.': do import with dir '.$dir);
146  $new_id = $this->doImportObject($dir, $a_type, $a_comp, $tmpdir);
147 
148  // delete temporary directory
149  ilUtil::delDir($tmpdir);
150 
151  return $new_id;
152  }
153 
154 
160  protected function doImportObject($dir, $a_type, $a_component = "", $a_tmpdir = "")
161  {
162  global $objDefinition, $tpl;
163 
164  if ($a_component == "")
165  {
166  $comp = $objDefinition->getComponentForType($a_type);
167  $class = $objDefinition->getClassName($a_type);
168  }
169  else
170  {
171  $comp = $a_component;
172  $c = explode("/", $comp);
173  $class = $c[count($c) - 1];
174  }
175 
176  $this->comp = $comp;
177 
178  // get import class
179  $success = true;
180 
181  // process manifest file
182  include_once("./Services/Export/classes/class.ilManifestParser.php");
183  if (!is_file($dir."/manifest.xml"))
184  {
185  include_once("./Services/Export/exceptions/class.ilManifestFileNotFoundImportException.php");
186  $e = new ilManifestFileNotFoundImportException('Manifest file not found: "'.$dir."/manifest.xml".'".');
187  $e->setManifestDir($dir);
188  $e->setTmpDir($a_tmpdir);
189  throw $e;
190  }
191  $parser = new ilManifestParser($dir."/manifest.xml");
192  $this->mapping->setInstallUrl($parser->getInstallUrl());
193  $this->mapping->setInstallId($parser->getInstallId());
194 
195  // process export files
196  $expfiles = $parser->getExportFiles();
197 
198  include_once("./Services/Export/classes/class.ilExportFileParser.php");
199  $all_importers = array();
200  foreach ($expfiles as $expfile)
201  {
202  $comp = $expfile["component"];
203  $comp_arr = explode("/", $comp);
204  $import_class_file = "./".$comp."/classes/class.il".$comp_arr[1]."Importer.php";
205  $class = "il".$comp_arr[1]."Importer";
206  include_once($import_class_file);
207  $this->importer = new $class();
208  $all_importers[] = $this->importer;
209  $this->importer->setImportDirectory($dir);
210  $this->importer->init();
211  $this->current_comp = $comp;
212 
213  try {
214  $parser = new ilExportFileParser($dir."/".$expfile["path"],$this, "processItemXml");
215  }
216  catch(Exception $e)
217  {
218  $GLOBALS['ilLog']->write(__METHOD__.': Import failed with message: '.$e->getMessage());
219  #$GLOBALS['ilLog']->write(__METHOD__.': '.file_get_contents($dir.'/'.$expfile['path']));
220  throw $e;
221  }
222  }
223 
224  // final processing
225  foreach ($all_importers as $imp)
226  {
227  $imp->finalProcessing($this->mapping);
228  }
229 
230  // we should only get on mapping here
231  $top_mapping = $this->mapping->getMappingsOfEntity($this->comp, $a_type);
232  $new_id = (int) current($top_mapping);
233 
234  return $new_id;
235  }
236 
242  function processItemXml($a_entity, $a_schema_version, $a_id, $a_xml,$a_install_id, $a_install_url)
243  {
244  global $objDefinition;
245 
246  // skip
247  if ($this->skip_entity[$this->current_comp][$a_entity])
248  {
249  return;
250  }
251 
252  if($objDefinition->isRBACObject($a_entity) &&
253  $this->getMapping()->getMapping('Services/Container', 'imported', $a_id))
254  {
255  $GLOBALS['ilLog']->write(__METHOD__.': Ignoring referenced '.$a_entity.' with id '.$a_id);
256  return;
257  }
258  $this->importer->setInstallId($a_install_id);
259  $this->importer->setInstallUrl($a_install_url);
260  $this->importer->setSchemaVersion($a_schema_version);
261  $this->importer->setSkipEntities($this->skip_entity);
262  $new_id = $this->importer->importXmlRepresentation($a_entity, $a_id, $a_xml, $this->mapping);
263 
264  // Store information about imported obj_ids in mapping to avoid double imports of references
265  if($objDefinition->isRBACObject($a_entity))
266  {
267  $this->getMapping()->addMapping('Services/Container', 'imported', $a_id, 1);
268  }
269 
270  // @TODO new id is not always set
271  if($new_id)
272  {
273  $this->mapping->addMapping($this->comp ,$a_entity, $a_id, $new_id);
274  }
275  }
276 
277 }
278 ?>