ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDataSet.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 
24 abstract class ilDataSet
25 {
26  var $dircnt;
27 
28  const EXPORT_NO_INST_ID = 1;
33  const EXPORT_ID = 6;
34  const EXPORT_ID_INVALID = 7;
35 
36 
40  function __construct()
41  {
42  }
43 
56  final public function init($a_entity, $a_schema_version)
57  {
58  $this->entity = $a_entity;
59  $this->schema_version = $a_schema_version;
60  $this->data = array();
61  }
62 
68  abstract public function getSupportedVersions();
69 
78  abstract protected function getTypes($a_entity, $a_version);
79 
84  abstract protected function getXmlNamespace($a_entity, $a_schema_version);
85 
92  abstract function readData($a_entity, $a_version, $a_ids);
93 
100  function setExportDirectories($a_relative, $a_absolute)
101  {
102  $this->relative_export_dir = $a_relative;
103  $this->absolute_export_dir = $a_absolute;
104  }
105 
111  function setImportDirectory($a_val)
112  {
113  $this->import_directory = $a_val;
114  }
115 
122  {
123  return $this->import_directory;
124  }
125 
131  function setDSPrefix($a_val)
132  {
133  $this->var = $a_val;
134  }
135 
141  function getDSPrefix()
142  {
143  return $this->var;
144  }
145 
146  function getDSPrefixString()
147  {
148  if ($this->getDSPrefix() != "")
149  {
150  return $this->getDSPrefix().":";
151  }
152  }
153 
161  function getDirectDataFromQuery($a_query, $a_convert_to_leading_upper = true)
162  {
163  global $ilDB;
164 
165  $set = $ilDB->query($a_query);
166  $this->data = array();
167  while ($rec = $ilDB->fetchAssoc($set))
168  {
169  if ($a_convert_to_leading_upper)
170  {
171  $tmp = array();
172  foreach ($rec as $k => $v)
173  {
174  $tmp[$this->convertToLeadingUpper($k)]
175  = $v;
176  }
177  $rec = $tmp;
178  }
179 
180  $this->data[] = $rec;
181  }
182  }
183 
190  function convertToLeadingUpper($a_str)
191  {
192  $a_str = strtoupper(substr($a_str, 0, 1)).substr($a_str, 1);
193  while (is_int($pos = strpos($a_str, "_")))
194  {
195  $a_str = substr($a_str, 0, $pos).
196  strtoupper(substr($a_str, $pos+1, 1)).
197  substr($a_str, $pos+2);
198  }
199  return $a_str;
200  }
201 
202 
206  final function getJsonRepresentation()
207  {
208  if ($this->version === false)
209  {
210  return false;
211  }
212 
213  $arr["entity"] = $this->getJsonEntityName();
214  $arr["version"] = $this->version;
215  $arr["install_id"] = IL_INST_ID;
216  $arr["install_url"] = ILIAS_HTTP_PATH;
217  $arr["types"] = $this->getJsonTypes();
218  $arr["set"] = array();
219  foreach ($this->data as $d)
220  {
221  $arr["set"][] = $this->getJsonRecord($d);
222  }
223 
224  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
225 
226  return ilJsonUtil::encode($arr);
227  }
228 
250  final function getXmlRepresentation($a_entity, $a_schema_version,
251  $a_ids, $a_field = "", $a_omit_header = false, $a_omit_types = false)
252  {
253  $this->dircnt = 1;
254 
255  // step 1: check target release and supported versions
256 
257 
258 
259  // step 2: init writer
260  include_once "./Services/Xml/classes/class.ilXmlWriter.php";
261  $writer = new ilXmlWriter();
262  if (!$a_omit_header)
263  {
264  $writer->xmlHeader();
265  }
266 
267  // collect namespaces
268  $namespaces = $prefixes = array();
269  $this->getNamespaces($namespaces, $a_entity, $a_schema_version);
270  $atts = array("InstallationId" => IL_INST_ID,
271  "InstallationUrl" => ILIAS_HTTP_PATH, "TopEntity" => $a_entity);
272  $cnt = 1;
273  foreach ($namespaces as $entity => $ns)
274  {
275  $prefix = "ns".$cnt;
276  $prefixes[$entity] = $prefix;
277 // $atts["xmlns:".$prefix] = $ns;
278  $cnt++;
279  }
280 
281  $writer->xmlStartTag($this->getDSPrefixString().'DataSet', $atts);
282 
283  // add types
284  if (!$a_omit_types)
285  {
286  $this->addTypesXml($writer, $a_entity, $a_schema_version);
287  }
288 
289  // add records
290  $this->addRecordsXml($writer, $prefixes, $a_entity, $a_schema_version, $a_ids, $a_field = "");
291 
292 
293  $writer->xmlEndTag($this->getDSPrefixString()."DataSet");
294 //if ($a_entity == "mep")
295 //{
296 // echo "<pre>".htmlentities($writer->xmlDumpMem(true))."</pre>"; exit;
297 //}
298  return $writer->xmlDumpMem(false);
299  }
300 
301 
308  function addRecordsXml($a_writer, $a_prefixes, $a_entity, $a_schema_version, $a_ids, $a_field = "")
309  {
310  $types = $this->getXmlTypes($a_entity, $a_schema_version);
311 
312  $this->readData($a_entity, $a_schema_version, $a_ids, $a_field);
313  if (is_array($this->data))
314  {
315  foreach ($this->data as $d)
316  {
317  $a_writer->xmlStartTag($this->getDSPrefixString()."Rec",
318  array("Entity" => $this->getXmlEntityName($a_entity, $a_schema_version)));
319 
320  // entity tag
321  $a_writer->xmlStartTag($this->getXmlEntityTag($a_entity, $a_schema_version));
322 
323  $rec = $this->getXmlRecord($a_entity, $a_schema_version, $d);
324  foreach ($rec as $f => $c)
325  {
326  switch ($types[$f])
327  {
328  case "directory":
329  if ($this->absolute_export_dir != "" && $this->relative_export_dir != "")
330  {
331  ilUtil::makeDirParents($this->absolute_export_dir."/dsDir_".$this->dircnt);
332  ilUtil::rCopy($c, $this->absolute_export_dir."/dsDir_".$this->dircnt);
333 //echo "<br>copy-".$c."-".$this->absolute_export_dir."/dsDir_".$this->dircnt."-";
334  $c = $this->relative_export_dir."/dsDir_".$this->dircnt;
335  $this->dircnt++;
336  }
337  break;
338  }
339  // this changes schema/dtd
340  //$a_writer->xmlElement($a_prefixes[$a_entity].":".$f,
341  // array(), $c);
342  $a_writer->xmlElement($f, array(), $c);
343  }
344 
345  $a_writer->xmlEndTag($this->getXmlEntityTag($a_entity, $a_schema_version));
346 
347  $a_writer->xmlEndTag($this->getDSPrefixString()."Rec");
348 
349  $this->afterXmlRecordWriting($a_entity, $a_schema_version, $d);
350 
351  // foreach record records of dependent entities (no record)
352  $deps = $this->getDependencies($a_entity, $a_schema_version, $rec, $a_ids);
353  if (is_array($deps))
354  {
355  foreach ($deps as $dp => $par)
356  {
357  $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_schema_version, $par["ids"], $par["field"]);
358  }
359  }
360  }
361  }
362  else if ($this->data === false)
363  {
364  // false -> add records of dependent entities (no record)
365  $deps = $this->getDependencies($a_entity, $a_schema_version, null, $a_ids);
366  if (is_array($deps))
367  {
368  foreach ($deps as $dp => $par)
369  {
370  $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_schema_version, $par["ids"], $par["field"]);
371  }
372  }
373  }
374  }
375 
382  function afterXmlRecordWriting($a_entity, $a_version, $a_set)
383  {
384  }
385 
391  private function addTypesXml($a_writer, $a_entity, $a_schema_version)
392  {
393  $types = $this->getXmlTypes($a_entity, $a_schema_version);
394 
395  // add types of current entity
396  if (is_array($types))
397  {
398  $a_writer->xmlStartTag($this->getDSPrefixString()."Types",
399  array("Entity" => $this->getXmlEntityName($a_entity, $a_schema_version),
400  "SchemaVersion" => $a_schema_version));
401  foreach ($this->getXmlTypes($a_entity, $a_schema_version) as $f => $t)
402  {
403  $a_writer->xmlElement($this->getDSPrefixString().'FieldType',
404  array("Name" => $f, "Type" => $t));
405  }
406  $a_writer->xmlEndTag($this->getDSPrefixString()."Types");
407  }
408 
409  // add types of dependent entities
410  $deps = $this->getDependencies($a_entity, $a_schema_version, null, null);
411  if (is_array($deps))
412  {
413  foreach ($deps as $dp => $w)
414  {
415  $this->addTypesXml($a_writer, $dp, $a_schema_version);
416  }
417  }
418 
419  }
420 
428  function getNamespaces(&$namespaces, $a_entity, $a_schema_version)
429  {
430  $ns = $this->getXmlNamespace($a_entity, $a_schema_version);
431  if ($ns != "")
432  {
433  $namespaces[$a_entity] = $ns;
434  }
435  // add types of dependent entities
436  $deps = $this->getDependencies($a_entity, $a_schema_version, null, null);
437  if (is_array($deps))
438  {
439  foreach ($deps as $dp => $w)
440  {
441  $this->getNamespaces($namespaces, $dp, $a_schema_version);
442  }
443  }
444  }
445 
452  function getXmlRecord($a_entity, $a_version, $a_set)
453  {
454  return $a_set;
455  }
456 
463  function getJsonRecord($a_set)
464  {
465  return $a_set;
466  }
467 
473  function getXmlTypes($a_entity, $a_version)
474  {
475  return $this->getTypes($a_entity, $a_version);
476  }
477 
483  function getJsonTypes($a_entity, $a_version)
484  {
485  return $this->getTypes($a_entity, $a_version);
486  }
487 
494  function getXMLEntityName($a_entity, $a_version)
495  {
496  return $a_entity;
497  }
498 
505  function getXMLEntityTag($a_entity, $a_schema_version)
506  {
507  return $this->convertToLeadingUpper($a_entity);
508  }
509 
514  function getJsonEntityName($a_entity, $a_version)
515  {
516  return $a_entity;
517  }
518 
524  function setImport($a_val)
525  {
526  $this->import = $a_val;
527  }
528 
534  function getImport()
535  {
536  return $this->import;
537  }
538 
546  protected function createObjectExportId($a_type, $a_id)
547  {
548  return "il_".IL_INST_ID."_".$a_type."_".$a_id;
549  }
550 
558  protected function parseObjectExportId($a_id, $a_fallback_id = NULL)
559  {
560  // ilias export id?
561  if(substr($a_id, 0, 3) == "il_")
562  {
563  $parts = explode("_", $a_id);
564  $inst_id = $parts[1];
565  $type = $parts[2];
566  $id = $parts[3];
567 
568  // missing installation ids?
569  if(($inst_id == 0 || IL_INST_ID == 0) && !DEVMODE)
570  {
571  return array("type"=>self::EXPORT_NO_INST_ID, "id"=>$a_fallback_id);
572  }
573 
574  // same installation?
575  if($inst_id == IL_INST_ID)
576  {
577  // still existing?
578  if(ilObject::_lookupType($id) == $type)
579  {
580  return array("type"=>self::EXPORT_ID_ILIAS_LOCAL, "id"=>$id);
581  }
582  // not found
583  else
584  {
585  return array("type"=>self::EXPORT_ID_ILIAS_LOCAL_INVALID, "id"=>$a_fallback_id);
586  }
587  }
588  // different installation
589  else
590  {
591  $id = ilObject::_getIdForImportId($a_id);
592  // matching type?
593  if($id && ilObject::_lookupType($id) == $type)
594  {
595  return array("type"=>self::EXPORT_ID_ILIAS_REMOTE, "id"=>$id);
596  }
597  // not found
598  else
599  {
600  return array("type"=>self::EXPORT_ID_ILIAS_REMOTE_INVALID, "id"=>$a_fallback_id);
601  }
602  }
603  }
604 
605  // external id
606  $id = ilObject::_getIdForImportId($a_id);
607  if($id)
608  {
609  return array("type"=>self::EXPORT_ID, "id"=>$id);
610  }
611  else
612  {
613  return array("type"=>self::EXPORT_ID_INVALID, "id"=>$a_fallback_id);
614  }
615  }
616 }
617 
618 ?>