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