ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilExport.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
13 class ilExport
14 {
15  public static $new_file_structure = array('cat','exc','crs','sess','file','grp','frm', 'usr', 'catr', 'crsr');
16 
17  // this should be part of module.xml and be parsed in the future
18  static $export_implementer = array("tst", "lm", "glo");
19 
24  public function __construct()
25  {
26 
27  }
28 
29 
34  static function _getValidExportSubItems($a_ref_id)
35  {
36  global $tree;
37 
38  $valid_items = array();
39  $sub_items = $tree->getSubTree($tree->getNodeData($a_ref_id));
40  foreach ($sub_items as $sub_item)
41  {
42  if (in_array($sub_item["type"], self::$export_implementer))
43  {
44  $valid_items[] = array("type" => $sub_item["type"],
45  "title" => $sub_item["title"], "ref_id" => $sub_item["child"],
46  "obj_id" => $sub_item["obj_id"],
47  "timestamp" =>
48  ilExport::_getLastExportFileDate($sub_item["obj_id"], "xml", $sub_item["type"]));
49  }
50  }
51  return $valid_items;
52  }
53 
61  static function _getLastExportFileDate($a_obj_id, $a_type = "", $a_obj_type = "")
62  {
63  $files = ilExport::_getExportFiles($a_obj_id, $a_type, $a_obj_type);
64  if (is_array($files))
65  {
66  $files = ilUtil::sortArray($files, "timestamp", "desc");
67  return $files[0]["timestamp"];
68  }
69  return false;
70  }
71 
79  static function _getLastExportFileInformation($a_obj_id, $a_type = "", $a_obj_type = "")
80  {
81  $files = ilExport::_getExportFiles($a_obj_id, $a_type, $a_obj_type);
82  if (is_array($files))
83  {
84  $files = ilUtil::sortArray($files, "timestamp", "desc");
85  return $files[0];
86  }
87  return false;
88  }
89 
99  public static function _getExportDirectory($a_obj_id, $a_type = "xml", $a_obj_type = "", $a_entity = "")
100  {
101  global $objDefinition;
102 
103  $ent = ($a_entity == "")
104  ? ""
105  : "_".$a_entity;
106 
107 
108  if ($a_obj_type == "")
109  {
110  $a_obj_type = ilObject::_lookupType($a_obj_id);
111  }
112 
113  if(in_array($a_obj_type, self::$new_file_structure))
114  {
115  include_once './Services/FileSystem/classes/class.ilFileSystemStorage.php';
116  $dir = ilUtil::getDataDir().DIRECTORY_SEPARATOR;
117  $dir .= 'il'.$objDefinition->getClassName($a_obj_type).$ent.DIRECTORY_SEPARATOR;
118  $dir .= ilFileSystemStorage::_createPathFromId($a_obj_id, $a_obj_type).DIRECTORY_SEPARATOR;
119  $dir .= ($a_type == 'xml' ? 'export' : 'export_'.$a_type);
120  return $dir;
121  }
122 
123  include_once './Services/Export/classes/class.ilImportExportFactory.php';
124  $exporter_class = ilImportExportFactory::getExporterClass($a_obj_type);
125  $export_dir = call_user_func(array($exporter_class,'lookupExportDirectory'),$a_obj_type,$a_obj_id,$a_type,$a_entity);
126 
127  $GLOBALS['ilLog']->write(__METHOD__.': Export dir is '.$export_dir);
128  return $export_dir;
129  }
130 
134  function _getExportFiles($a_obj_id, $a_export_types = "", $a_obj_type = "")
135  {
136  $GLOBALS['ilLog']->write(__METHOD__);
137 
138  if ($a_obj_type == "")
139  {
140  $a_obj_type = ilObject::_lookupType($a_obj_id);
141  }
142 
143  if ($a_export_types == "")
144  {
145  $a_export_types = array("xml");
146  }
147  if (!is_array($a_export_types))
148  {
149  $a_export_types = array($a_export_types);
150  }
151 
152  // initialize array
153  $file = array();
154 
155  $types = $a_export_types;
156 
157  foreach($types as $type)
158  {
159  $dir = ilExport::_getExportDirectory($a_obj_id, $type, $a_obj_type);
160 
161  // quit if import dir not available
162  if (!@is_dir($dir) or
163  !is_writeable($dir))
164  {
165  continue;
166  }
167 
168  // open directory
169  $h_dir = dir($dir);
170 
171  // get files and save the in the array
172  while ($entry = $h_dir->read())
173  {
174  if ($entry != "." and
175  $entry != ".." and
176  substr($entry, -4) == ".zip" and
177  ereg("^[0-9]{10}_{2}[0-9]+_{2}(".$a_obj_type."_)*[0-9]+\.zip\$", $entry))
178  {
179  $ts = substr($entry, 0, strpos($entry, "__"));
180  $file[$entry.$type] = array("type" => $type, "file" => $entry,
181  "size" => filesize($dir."/".$entry),
182  "timestamp" => $ts);
183  }
184  }
185 
186  // close import directory
187  $h_dir->close();
188  }
189 
190  // sort files
191  ksort ($file);
192  reset ($file);
193  return $file;
194  }
195 
199  function _createExportDirectory($a_obj_id, $a_export_type = "xml", $a_obj_type = "")
200  {
201  global $ilErr;
202 
203  if ($a_obj_type == "")
204  {
205  $a_obj_type = ilObject::_lookupType($a_obj_id);
206  }
207 
208  $edir = ilExport::_getExportDirectory($a_obj_id,$a_export_type,$a_obj_type);
209  ilUtil::makeDirParents($edir);
210  return true;
211  }
212 
217  function _generateIndexFile($a_filename, $a_obj_id, $a_files, $a_type = "")
218  {
219  global $lng;
220 
221  $lng->loadLanguageModule("export");
222 
223  if ($a_type == "")
224  {
225  $a_type = ilObject::_lookupType($a_obj_id);
226  }
227  $a_tpl = new ilTemplate("tpl.main.html", true, true);
228  $location_stylesheet = ilUtil::getStyleSheetLocation();
229  $a_tpl->setVariable("LOCATION_STYLESHEET",$location_stylesheet);
230  $a_tpl->getStandardTemplate();
231  $a_tpl->setTitle(ilObject::_lookupTitle($a_obj_id));
232  $a_tpl->setDescription($lng->txt("export_export_date").": ".
233  date('Y-m-d H:i:s', time())." (".date_default_timezone_get().")");
234  $f_tpl = new ilTemplate("tpl.export_list.html", true, true, "Services/Export");
235  foreach ($a_files as $file)
236  {
237  $f_tpl->setCurrentBlock("file_row");
238  $f_tpl->setVariable("TITLE", $file["title"]);
239  $f_tpl->setVariable("TYPE", $lng->txt("obj_".$file["type"]));
240  $f_tpl->setVariable("FILE", $file["file"]);
241  $f_tpl->parseCurrentBlock();
242  }
243  $a_tpl->setContent($f_tpl->get());
244  $index_content = $a_tpl->get("DEFAULT", false, false, false, true, false, false);
245 
246  $f = fopen ($a_filename, "w");
247  fwrite($f, $index_content);
248  fclose($f);
249  }
250 
254 
255  /***
256  *
257  * - Walk through sequence
258  * - Each step in sequence creates one xml file,
259  * e.g. Services/Mediapool/set_1.xml
260  * - manifest.xml lists all files
261  *
262  * <manifest>
263  * <xmlfile path="Services/Mediapool/set_1.xml"/>
264  * ...
265  * </manifest
266  *
267  *
268  */
269 
279  function exportObject($a_type, $a_id, $a_target_release)
280  {
281  global $objDefinition, $tpl;
282 
283  $comp = $objDefinition->getComponentForType($a_type);
284  $c = explode("/", $comp);
285  $class = "il".$c[1]."Exporter";
286 
287  // manifest writer
288  include_once "./Services/Xml/classes/class.ilXmlWriter.php";
289  $this->manifest_writer = new ilXmlWriter();
290  $this->manifest_writer->xmlHeader();
291  $this->manifest_writer->xmlStartTag(
292  'Manifest',
293  array(
294  "MainEntity" => $a_type,
295  "Title" => ilObject::_lookupTitle($a_id),
296  "TargetRelease" => $a_target_release,
297  "InstallationId" => IL_INST_ID,
298  "InstallationUrl" => ILIAS_HTTP_PATH));
299 
300  // get export class
301  ilExport::_createExportDirectory($a_id, "xml", $a_type);
302  $export_dir = ilExport::_getExportDirectory($a_id, "xml", $a_type);
303  $ts = time();
304 
305  // Workaround for test assessment
306  $sub_dir = $ts.'__'.IL_INST_ID.'__'.$a_type.'_'.$a_id;
307  $new_file = $sub_dir.'.zip';
308 
309  $this->export_run_dir = $export_dir."/".$sub_dir;
310  ilUtil::makeDirParents($this->export_run_dir);
311 
312  $this->cnt = array();
313 
314  $success = $this->processExporter($comp, $class, $a_type, $a_target_release, $a_id);
315 
316  $this->manifest_writer->xmlEndTag('Manifest');
317 
318  $this->manifest_writer->xmlDumpFile($this->export_run_dir."/manifest.xml", false);
319 
320  // zip the file
321  ilUtil::zip($this->export_run_dir, $export_dir."/".$new_file);
322  ilUtil::delDir($this->export_run_dir);
323 
324  // Store info about export
325  if($success)
326  {
327  include_once './Services/Export/classes/class.ilExportFileInfo.php';
328  $exp = new ilExportFileInfo($a_id);
329  $exp->setVersion($a_target_release);
330  $exp->setCreationDate(new ilDateTime($ts,IL_CAL_UNIX));
331  $exp->setExportType('xml');
332  $exp->setFilename($new_file);
333  $exp->create();
334  }
335 
336  return array(
337  "success" => $success,
338  "file" => $new_file,
339  "directory" => $export_dir
340  );
341  }
342 
353  function exportEntity($a_entity, $a_id, $a_target_release,
354  $a_component, $a_title, $a_export_dir, $a_type_for_file = "")
355  {
356  global $objDefinition, $tpl;
357 
358  if ($a_type_for_file == "")
359  {
360  $a_type_for_file = $a_entity;
361  }
362 
363  $comp = $a_component;
364  $c = explode("/", $comp);
365  $class = "il".$c[1]."Exporter";
366 
367  // manifest writer
368  include_once "./Services/Xml/classes/class.ilXmlWriter.php";
369  $this->manifest_writer = new ilXmlWriter();
370  $this->manifest_writer->xmlHeader();
371  $this->manifest_writer->xmlStartTag(
372  'Manifest',
373  array(
374  "MainEntity" => $a_entity,
375  "Title" => $a_title,
376  "TargetRelease" => $a_target_release,
377  "InstallationId" => IL_INST_ID,
378  "InstallationUrl" => ILIAS_HTTP_PATH));
379 
380  $export_dir = $a_export_dir;
381  $ts = time();
382 
383  // determine file name and subdirectory
384  $sub_dir = $ts.'__'.IL_INST_ID.'__'.$a_type_for_file.'_'.$a_id;
385  $new_file = $sub_dir.'.zip';
386 
387  $this->export_run_dir = $export_dir."/".$sub_dir;
388  ilUtil::makeDirParents($this->export_run_dir);
389 
390  $this->cnt = array();
391 
392  $success = $this->processExporter($comp, $class, $a_entity, $a_target_release, $a_id);
393 
394  $this->manifest_writer->xmlEndTag('Manifest');
395 
396  $this->manifest_writer->xmlDumpFile($this->export_run_dir."/manifest.xml", false);
397 
398  // zip the file
399  ilUtil::zip($this->export_run_dir, $export_dir."/".$new_file);
400  ilUtil::delDir($this->export_run_dir);
401 
402  return array(
403  "success" => $success,
404  "file" => $new_file,
405  "directory" => $export_dir
406  );
407  }
408 
415  function processExporter($a_comp, $a_class, $a_entity, $a_target_release, $a_id)
416  {
417  $success = true;
418 
419  if (!is_array($a_id))
420  {
421  if ($a_id == "")
422  {
423  return;
424  }
425  $a_id = array($a_id);
426  }
427 
428  // get exporter object
429  $export_class_file = "./".$a_comp."/classes/class.".$a_class.".php";
430 //echo "1-".$export_class_file."-"; exit;
431  if (!is_file($export_class_file))
432  {
433 echo "1-not found:".$export_class_file."-"; exit;
434  return false;
435  }
436  include_once($export_class_file);
437  $exp = new $a_class();
438  if (!isset($this->cnt[$a_comp]))
439  {
440  $this->cnt[$a_comp] = 1;
441  }
442  else
443  {
444  $this->cnt[$a_comp]++;
445  }
446  $set_dir_relative = $a_comp."/set_".$this->cnt[$a_comp];
447  $set_dir_absolute = $this->export_run_dir."/".$set_dir_relative;
448  ilUtil::makeDirParents($set_dir_absolute);
449  $exp->init();
450 
451  $sv = $exp->determineSchemaVersion($a_entity, $a_target_release);
452 
453  // process head dependencies
454  $sequence = $exp->getXmlExportHeadDependencies($a_entity, $a_target_release, $a_id);
455  foreach ($sequence as $s)
456  {
457  $comp = explode("/", $s["component"]);
458  $exp_class = "il".$comp[1]."Exporter";
459  $s = $this->processExporter($s["component"], $exp_class,
460  $s["entity"], $a_target_release, $s["ids"]);
461  if (!$s)
462  {
463  $success = false;
464  }
465  }
466 
467  // write export.xml file
468  $export_writer = new ilXmlWriter();
469  $export_writer->xmlHeader();
470 
471  $attribs = array("InstallationId" => IL_INST_ID,
472  "InstallationUrl" => ILIAS_HTTP_PATH,
473  "Entity" => $a_entity, "SchemaVersion" => $sv["schema_version"], "TargetRelease" => $a_target_release,
474  "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
475  "xmlns:exp" => "http://www.ilias.de/Services/Export/exp/4_1",
476  "xsi:schemaLocation" => "http://www.ilias.de/Services/Export/exp/4_1 ".ILIAS_HTTP_PATH."/xml/ilias_export_4_1.xsd"
477  );
478  if ($sv["namespace"] != "" && $sv["xsd_file"] != "")
479  {
480  $attribs["xsi:schemaLocation"].= " ".$sv["namespace"]." ".
481  ILIAS_HTTP_PATH."/xml/".$sv["xsd_file"];
482  $attribs["xmlns"] = $sv["namespace"];
483  }
484  if ($sv["uses_dataset"])
485  {
486  $attribs["xsi:schemaLocation"].= " ".
487  "http://www.ilias.de/Services/DataSet/ds/4_3 ".ILIAS_HTTP_PATH."/xml/ilias_ds_4_3.xsd";
488  $attribs["xmlns:ds"] = "http://www.ilias.de/Services/DataSet/ds/4_3";
489  }
490 
491 
492  $export_writer->xmlStartTag('exp:Export', $attribs);
493 
494  $dir_cnt = 1;
495  foreach ($a_id as $id)
496  {
497  $exp->setExportDirectories($set_dir_relative."/expDir_".$dir_cnt,
498  $set_dir_absolute."/expDir_".$dir_cnt);
499  $export_writer->xmlStartTag('exp:ExportItem', array("Id" => $id));
500  //$xml = $exp->getXmlRepresentation($a_entity, $a_target_release, $id);
501  $xml = $exp->getXmlRepresentation($a_entity, $sv["schema_version"], $id);
502  $export_writer->appendXml($xml);
503  $export_writer->xmlEndTag('exp:ExportItem');
504  $dir_cnt++;
505  }
506 
507  $export_writer->xmlEndTag('exp:Export');
508  $export_writer->xmlDumpFile($set_dir_absolute."/export.xml", false);
509 
510  $this->manifest_writer->xmlElement("ExportFile",
511  array("Component" => $a_comp, "Path" => $set_dir_relative."/export.xml"));
512 
513  // process tail dependencies
514  $sequence = $exp->getXmlExportTailDependencies($a_entity, $a_target_release, $a_id);
515  foreach ($sequence as $s)
516  {
517  $comp = explode("/", $s["component"]);
518  $exp_class = "il".$comp[1]."Exporter";
519  $s = $this->processExporter($s["component"], $exp_class,
520  $s["entity"], $a_target_release, $s["ids"]);
521  if (!$s)
522  {
523  $success = false;
524  }
525  }
526 
527  return $success;
528  }
529 }
530 ?>