ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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  protected $current_installation_id = "";
28 
29  const EXPORT_NO_INST_ID = 1;
34  const EXPORT_ID = 6;
35  const EXPORT_ID_INVALID = 7;
36 
37 
41  function __construct()
42  {
43  $this->ds_log = ilLoggerFactory::getLogger('ds');
44  }
45 
58  final public function init($a_entity, $a_schema_version)
59  {
60  $this->entity = $a_entity;
61  $this->schema_version = $a_schema_version;
62  $this->data = array();
63  }
64 
70  abstract public function getSupportedVersions();
71 
80  abstract protected function getTypes($a_entity, $a_version);
81 
86  abstract protected function getXmlNamespace($a_entity, $a_schema_version);
87 
94  abstract function readData($a_entity, $a_version, $a_ids);
95 
102  function setExportDirectories($a_relative, $a_absolute)
103  {
104  $this->relative_export_dir = $a_relative;
105  $this->absolute_export_dir = $a_absolute;
106  }
107 
113  function setImportDirectory($a_val)
114  {
115  $this->import_directory = $a_val;
116  }
117 
124  {
125  return $this->import_directory;
126  }
127 
133  function setDSPrefix($a_val)
134  {
135  $this->var = $a_val;
136  }
137 
143  function getDSPrefix()
144  {
145  return $this->var;
146  }
147 
148  function getDSPrefixString()
149  {
150  if ($this->getDSPrefix() != "")
151  {
152  return $this->getDSPrefix().":";
153  }
154  }
155 
163  function getDirectDataFromQuery($a_query, $a_convert_to_leading_upper = true)
164  {
165  global $ilDB;
166 
167  $set = $ilDB->query($a_query);
168  $this->data = array();
169  while ($rec = $ilDB->fetchAssoc($set))
170  {
171  if ($a_convert_to_leading_upper)
172  {
173  $tmp = array();
174  foreach ($rec as $k => $v)
175  {
176  $tmp[$this->convertToLeadingUpper($k)]
177  = $v;
178  }
179  $rec = $tmp;
180  }
181 
182  $this->data[] = $rec;
183  }
184  }
185 
192  function convertToLeadingUpper($a_str)
193  {
194  $a_str = strtoupper(substr($a_str, 0, 1)).substr($a_str, 1);
195  while (is_int($pos = strpos($a_str, "_")))
196  {
197  $a_str = substr($a_str, 0, $pos).
198  strtoupper(substr($a_str, $pos+1, 1)).
199  substr($a_str, $pos+2);
200  }
201  return $a_str;
202  }
203 
204 
208  final function getJsonRepresentation()
209  {
210  if ($this->version === false)
211  {
212  return false;
213  }
214 
215  $arr["entity"] = $this->getJsonEntityName();
216  $arr["version"] = $this->version;
217  $arr["install_id"] = IL_INST_ID;
218  $arr["install_url"] = ILIAS_HTTP_PATH;
219  $arr["types"] = $this->getJsonTypes();
220  $arr["set"] = array();
221  foreach ($this->data as $d)
222  {
223  $arr["set"][] = $this->getJsonRecord($d);
224  }
225 
226  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
227 
228  return ilJsonUtil::encode($arr);
229  }
230 
252  final function getXmlRepresentation($a_entity, $a_schema_version,
253  $a_ids, $a_field = "", $a_omit_header = false, $a_omit_types = false)
254  {
255  $this->dircnt = 1;
256 
257  // step 1: check target release and supported versions
258 
259 
260 
261  // step 2: init writer
262  include_once "./Services/Xml/classes/class.ilXmlWriter.php";
263  $writer = new ilXmlWriter();
264  if (!$a_omit_header)
265  {
266  $writer->xmlHeader();
267  }
268 
269  // collect namespaces
270  $namespaces = $prefixes = array();
271  $this->getNamespaces($namespaces, $a_entity, $a_schema_version);
272  $atts = array("InstallationId" => IL_INST_ID,
273  "InstallationUrl" => ILIAS_HTTP_PATH, "TopEntity" => $a_entity);
274  $cnt = 1;
275  foreach ($namespaces as $entity => $ns)
276  {
277  $prefix = "ns".$cnt;
278  $prefixes[$entity] = $prefix;
279 // $atts["xmlns:".$prefix] = $ns;
280  $cnt++;
281  }
282 
283  $this->ds_log->debug("Start writing Dataset, entity: ".$a_entity.", schema version: ".$a_schema_version.
284  ", ids: ".print_r($a_ids, true));
285  $writer->xmlStartTag($this->getDSPrefixString().'DataSet', $atts);
286 
287  // add types
288  if (!$a_omit_types)
289  {
290  $this->ds_log->debug("...write types");
291  $this->addTypesXml($writer, $a_entity, $a_schema_version);
292  }
293 
294  // add records
295  $this->ds_log->debug("...write records");
296  $this->addRecordsXml($writer, $prefixes, $a_entity, $a_schema_version, $a_ids, $a_field);
297 
298 
299  $writer->xmlEndTag($this->getDSPrefixString()."DataSet");
300 //if ($a_entity == "mep")
301 //{
302 // echo "<pre>".htmlentities($writer->xmlDumpMem(true))."</pre>"; exit;
303 //}
304  return $writer->xmlDumpMem(false);
305  }
306 
307 
314  function addRecordsXml($a_writer, $a_prefixes, $a_entity, $a_schema_version, $a_ids, $a_field = "")
315  {
316  $types = $this->getXmlTypes($a_entity, $a_schema_version);
317 
318  $this->ds_log->debug("...read data");
319  $this->readData($a_entity, $a_schema_version, $a_ids, $a_field);
320  $this->ds_log->debug("...data: ".print_r($this->data, true));
321  if (is_array($this->data))
322  {
323  foreach ($this->data as $d)
324  {
325  $a_writer->xmlStartTag($this->getDSPrefixString()."Rec",
326  array("Entity" => $this->getXmlEntityName($a_entity, $a_schema_version)));
327 
328  // entity tag
329  $a_writer->xmlStartTag($this->getXmlEntityTag($a_entity, $a_schema_version));
330 
331  $rec = $this->getXmlRecord($a_entity, $a_schema_version, $d);
332  foreach ($rec as $f => $c)
333  {
334  switch ($types[$f])
335  {
336  case "directory":
337  if ($this->absolute_export_dir != "" && $this->relative_export_dir != "")
338  {
339  ilUtil::makeDirParents($this->absolute_export_dir."/dsDir_".$this->dircnt);
340  ilUtil::rCopy($c, $this->absolute_export_dir."/dsDir_".$this->dircnt);
341 //echo "<br>copy-".$c."-".$this->absolute_export_dir."/dsDir_".$this->dircnt."-";
342  $c = $this->relative_export_dir."/dsDir_".$this->dircnt;
343  $this->dircnt++;
344  }
345  break;
346  }
347  // this changes schema/dtd
348  //$a_writer->xmlElement($a_prefixes[$a_entity].":".$f,
349  // array(), $c);
350  $a_writer->xmlElement($f, array(), $c);
351  }
352 
353  $a_writer->xmlEndTag($this->getXmlEntityTag($a_entity, $a_schema_version));
354 
355  $a_writer->xmlEndTag($this->getDSPrefixString()."Rec");
356 
357  $this->afterXmlRecordWriting($a_entity, $a_schema_version, $d);
358 
359  // foreach record records of dependent entities
360  $this->ds_log->debug("...get dependencies");
361  $deps = $this->getDependencies($a_entity, $a_schema_version, $rec, $a_ids);
362  $this->ds_log->debug("...dependencies: ".print_r($deps, true));
363  if (is_array($deps))
364  {
365  foreach ($deps as $dp => $par)
366  {
367  $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_schema_version, $par["ids"], $par["field"]);
368  }
369  }
370  }
371  }
372  else if ($this->data === false)
373  {
374  // false -> add records of dependent entities (no record)
375  $this->ds_log->debug("...get dependencies (no record)");
376  $deps = $this->getDependencies($a_entity, $a_schema_version, null, $a_ids);
377  if (is_array($deps))
378  {
379  foreach ($deps as $dp => $par)
380  {
381  $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_schema_version, $par["ids"], $par["field"]);
382  }
383  }
384  }
385  }
386 
393  function afterXmlRecordWriting($a_entity, $a_version, $a_set)
394  {
395  }
396 
402  private function addTypesXml($a_writer, $a_entity, $a_schema_version)
403  {
404  $types = $this->getXmlTypes($a_entity, $a_schema_version);
405 
406  // add types of current entity
407  if (is_array($types))
408  {
409  $a_writer->xmlStartTag($this->getDSPrefixString()."Types",
410  array("Entity" => $this->getXmlEntityName($a_entity, $a_schema_version),
411  "SchemaVersion" => $a_schema_version));
412  foreach ($this->getXmlTypes($a_entity, $a_schema_version) as $f => $t)
413  {
414  $a_writer->xmlElement($this->getDSPrefixString().'FieldType',
415  array("Name" => $f, "Type" => $t));
416  }
417  $a_writer->xmlEndTag($this->getDSPrefixString()."Types");
418  }
419 
420  // add types of dependent entities
421  $deps = $this->getDependencies($a_entity, $a_schema_version, null, null);
422  if (is_array($deps))
423  {
424  foreach ($deps as $dp => $w)
425  {
426  $this->addTypesXml($a_writer, $dp, $a_schema_version);
427  }
428  }
429 
430  }
431 
439  function getNamespaces(&$namespaces, $a_entity, $a_schema_version)
440  {
441  $ns = $this->getXmlNamespace($a_entity, $a_schema_version);
442  if ($ns != "")
443  {
444  $namespaces[$a_entity] = $ns;
445  }
446  // add types of dependent entities
447  $deps = $this->getDependencies($a_entity, $a_schema_version, null, null);
448  if (is_array($deps))
449  {
450  foreach ($deps as $dp => $w)
451  {
452  $this->getNamespaces($namespaces, $dp, $a_schema_version);
453  }
454  }
455  }
456 
463  function getXmlRecord($a_entity, $a_version, $a_set)
464  {
465  return $a_set;
466  }
467 
474  function getJsonRecord($a_set)
475  {
476  return $a_set;
477  }
478 
484  function getXmlTypes($a_entity, $a_version)
485  {
486  return $this->getTypes($a_entity, $a_version);
487  }
488 
494  function getJsonTypes($a_entity, $a_version)
495  {
496  return $this->getTypes($a_entity, $a_version);
497  }
498 
505  function getXMLEntityName($a_entity, $a_version)
506  {
507  return $a_entity;
508  }
509 
516  function getXMLEntityTag($a_entity, $a_schema_version)
517  {
518  return $this->convertToLeadingUpper($a_entity);
519  }
520 
525  function getJsonEntityName($a_entity, $a_version)
526  {
527  return $a_entity;
528  }
529 
535  function setImport($a_val)
536  {
537  $this->import = $a_val;
538  }
539 
545  function getImport()
546  {
547  return $this->import;
548  }
549 
555  function setCurrentInstallationId($a_val)
556  {
557  $this->current_installation_id = $a_val;
558  }
559 
566  {
568  }
569 
577  protected function createObjectExportId($a_type, $a_id)
578  {
579  return "il_".IL_INST_ID."_".$a_type."_".$a_id;
580  }
581 
589  protected function parseObjectExportId($a_id, $a_fallback_id = NULL)
590  {
591  // ilias export id?
592  if(substr($a_id, 0, 3) == "il_")
593  {
594  $parts = explode("_", $a_id);
595  $inst_id = $parts[1];
596  $type = $parts[2];
597  $id = $parts[3];
598 
599  // missing installation ids?
600  if(($inst_id == 0 || IL_INST_ID == 0) && !DEVMODE)
601  {
602  return array("type"=>self::EXPORT_NO_INST_ID, "id"=>$a_fallback_id);
603  }
604 
605  // same installation?
606  if($inst_id == IL_INST_ID)
607  {
608  // still existing?
609  if(ilObject::_lookupType($id) == $type)
610  {
611  return array("type"=>self::EXPORT_ID_ILIAS_LOCAL, "id"=>$id);
612  }
613  // not found
614  else
615  {
616  return array("type"=>self::EXPORT_ID_ILIAS_LOCAL_INVALID, "id"=>$a_fallback_id);
617  }
618  }
619  // different installation
620  else
621  {
622  $id = ilObject::_getIdForImportId($a_id);
623  // matching type?
624  if($id && ilObject::_lookupType($id) == $type)
625  {
626  return array("type"=>self::EXPORT_ID_ILIAS_REMOTE, "id"=>$id);
627  }
628  // not found
629  else
630  {
631  return array("type"=>self::EXPORT_ID_ILIAS_REMOTE_INVALID, "id"=>$a_fallback_id);
632  }
633  }
634  }
635 
636  // external id
637  $id = ilObject::_getIdForImportId($a_id);
638  if($id)
639  {
640  return array("type"=>self::EXPORT_ID, "id"=>$id);
641  }
642  else
643  {
644  return array("type"=>self::EXPORT_ID_INVALID, "id"=>$a_fallback_id);
645  }
646  }
647 }
648 
649 ?>
getJsonEntityName($a_entity, $a_version)
Get entity name for json (may be overwritten)
static makeDirParents($a_dir)
Create a new directory and all parent directories.
init($a_entity, $a_schema_version)
Init.
const EXPORT_ID_ILIAS_REMOTE
setExportDirectories($a_relative, $a_absolute)
Set export directories.
setImportDirectory($a_val)
Set import directory.
getTypes($a_entity, $a_version)
Get (abstract) types for (abstract) field names.
static rCopy($a_sdir, $a_tdir, $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
const EXPORT_ID_ILIAS_LOCAL_INVALID
createObjectExportId($a_type, $a_id)
Build ilias export id.
XML writer class.
_getIdForImportId($a_import_id)
get current object id for import id (static)
afterXmlRecordWriting($a_entity, $a_version, $a_set)
After xml record writing hook record.
addTypesXml($a_writer, $a_entity, $a_schema_version)
Add types to xml writer.
getDSPrefix()
Get XML dataset namespace prefix.
getImportDirectory()
Get import directory.
readData($a_entity, $a_version, $a_ids)
Read data from DB.
const EXPORT_NO_INST_ID
__construct()
Constructor.
getJsonTypes($a_entity, $a_version)
Get json types.
parseObjectExportId($a_id, $a_fallback_id=NULL)
Parse export id.
getXMLEntityName($a_entity, $a_version)
Get entity name for xml (may be overwritten)
convertToLeadingUpper($a_str)
Make xyz_abc a XyzAbc string.
$w
getXmlRecord($a_entity, $a_version, $a_set)
Get xml record for version.
getImport()
Get import object.
const EXPORT_ID_INVALID
const EXPORT_ID_ILIAS_REMOTE_INVALID
static encode($mixed, $suppress_native=false)
const EXPORT_ID_ILIAS_LOCAL
getXmlNamespace($a_entity, $a_schema_version)
Get xml namespace.
getDirectDataFromQuery($a_query, $a_convert_to_leading_upper=true)
Get data from query.This is a standard procedure, all db field names are directly mapped to abstract ...
getJsonRepresentation()
Get json representation.
getSupportedVersions()
Get supported version.
getNamespaces(&$namespaces, $a_entity, $a_schema_version)
Get xml namespaces.
setCurrentInstallationId($a_val)
Set current installation id.
getCurrentInstallationId()
Get current installation id.
getJsonRecord($a_set)
Get json record for version.
static _lookupType($a_id, $a_reference=false)
lookup object type
getXmlTypes($a_entity, $a_version)
Get xml types.
global $ilDB
getXmlRepresentation($a_entity, $a_schema_version, $a_ids, $a_field="", $a_omit_header=false, $a_omit_types=false)
Get xml representation <dataset install_id="123" install_url="..."> <types entity="table_name" versio...
setImport($a_val)
Set import object.
static getLogger($a_component_id)
Get component logger.
getXMLEntityTag($a_entity, $a_schema_version)
Get entity tag.
A dataset contains in data in a common structure that can be shared and transformed for different pur...
setDSPrefix($a_val)
Set XML dataset namespace prefix.
addRecordsXml($a_writer, $a_prefixes, $a_entity, $a_schema_version, $a_ids, $a_field="")
Add records xml.