ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
164  function getDirectDataFromQuery($a_query, $a_convert_to_leading_upper = true, $a_set = true)
165  {
166  global $ilDB;
167 
168  $set = $ilDB->query($a_query);
169  $this->data = array();
170  $ret = [];
171  while ($rec = $ilDB->fetchAssoc($set))
172  {
173  if ($a_convert_to_leading_upper)
174  {
175  $tmp = array();
176  foreach ($rec as $k => $v)
177  {
178  $tmp[$this->convertToLeadingUpper($k)]
179  = $v;
180  }
181  $rec = $tmp;
182  }
183 
184  if ($a_set)
185  {
186  $this->data[] = $rec;
187  }
188  $ret[] = $rec;
189  }
190  return $ret;
191  }
192 
199  function convertToLeadingUpper($a_str)
200  {
201  $a_str = strtoupper(substr($a_str, 0, 1)).substr($a_str, 1);
202  while (is_int($pos = strpos($a_str, "_")))
203  {
204  $a_str = substr($a_str, 0, $pos).
205  strtoupper(substr($a_str, $pos+1, 1)).
206  substr($a_str, $pos+2);
207  }
208  return $a_str;
209  }
210 
211 
215  final function getJsonRepresentation()
216  {
217  if ($this->version === false)
218  {
219  return false;
220  }
221 
222  $arr["entity"] = $this->getJsonEntityName();
223  $arr["version"] = $this->version;
224  $arr["install_id"] = IL_INST_ID;
225  $arr["install_url"] = ILIAS_HTTP_PATH;
226  $arr["types"] = $this->getJsonTypes();
227  $arr["set"] = array();
228  foreach ($this->data as $d)
229  {
230  $arr["set"][] = $this->getJsonRecord($d);
231  }
232 
233  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
234 
235  return ilJsonUtil::encode($arr);
236  }
237 
259  final function getXmlRepresentation($a_entity, $a_schema_version,
260  $a_ids, $a_field = "", $a_omit_header = false, $a_omit_types = false)
261  {
262  $this->dircnt = 1;
263 
264  // step 1: check target release and supported versions
265 
266 
267 
268  // step 2: init writer
269  include_once "./Services/Xml/classes/class.ilXmlWriter.php";
270  $writer = new ilXmlWriter();
271  if (!$a_omit_header)
272  {
273  $writer->xmlHeader();
274  }
275 
276  // collect namespaces
277  $namespaces = $prefixes = array();
278  $this->getNamespaces($namespaces, $a_entity, $a_schema_version);
279  $atts = array("InstallationId" => IL_INST_ID,
280  "InstallationUrl" => ILIAS_HTTP_PATH, "TopEntity" => $a_entity);
281  $cnt = 1;
282  foreach ($namespaces as $entity => $ns)
283  {
284  $prefix = "ns".$cnt;
285  $prefixes[$entity] = $prefix;
286 // $atts["xmlns:".$prefix] = $ns;
287  $cnt++;
288  }
289 
290  $this->ds_log->debug("Start writing Dataset, entity: ".$a_entity.", schema version: ".$a_schema_version.
291  ", ids: ".print_r($a_ids, true));
292  $writer->xmlStartTag($this->getDSPrefixString().'DataSet', $atts);
293 
294  // add types
295  if (!$a_omit_types)
296  {
297  $this->ds_log->debug("...write types");
298  $this->addTypesXml($writer, $a_entity, $a_schema_version);
299  }
300 
301  // add records
302  $this->ds_log->debug("...write records");
303  $this->addRecordsXml($writer, $prefixes, $a_entity, $a_schema_version, $a_ids, $a_field);
304 
305 
306  $writer->xmlEndTag($this->getDSPrefixString()."DataSet");
307 //if ($a_entity == "mep")
308 //{
309 // echo "<pre>".htmlentities($writer->xmlDumpMem(true))."</pre>"; exit;
310 //}
311  return $writer->xmlDumpMem(false);
312  }
313 
314 
321  function addRecordsXml($a_writer, $a_prefixes, $a_entity, $a_schema_version, $a_ids, $a_field = "")
322  {
323  $types = $this->getXmlTypes($a_entity, $a_schema_version);
324 
325  $this->ds_log->debug("...read data");
326  $this->readData($a_entity, $a_schema_version, $a_ids, $a_field);
327  $this->ds_log->debug("...data: ".print_r($this->data, true));
328  if (is_array($this->data))
329  {
330  foreach ($this->data as $d)
331  {
332  $a_writer->xmlStartTag($this->getDSPrefixString()."Rec",
333  array("Entity" => $this->getXmlEntityName($a_entity, $a_schema_version)));
334 
335  // entity tag
336  $a_writer->xmlStartTag($this->getXmlEntityTag($a_entity, $a_schema_version));
337 
338  $rec = $this->getXmlRecord($a_entity, $a_schema_version, $d);
339  foreach ($rec as $f => $c)
340  {
341  switch ($types[$f])
342  {
343  case "directory":
344  if ($this->absolute_export_dir != "" && $this->relative_export_dir != "")
345  {
346  ilUtil::makeDirParents($this->absolute_export_dir."/dsDir_".$this->dircnt);
347  ilUtil::rCopy($c, $this->absolute_export_dir."/dsDir_".$this->dircnt);
348 //echo "<br>copy-".$c."-".$this->absolute_export_dir."/dsDir_".$this->dircnt."-";
349  $c = $this->relative_export_dir."/dsDir_".$this->dircnt;
350  $this->dircnt++;
351  }
352  break;
353  }
354  // this changes schema/dtd
355  //$a_writer->xmlElement($a_prefixes[$a_entity].":".$f,
356  // array(), $c);
357  $a_writer->xmlElement($f, array(), $c);
358  }
359 
360  $a_writer->xmlEndTag($this->getXmlEntityTag($a_entity, $a_schema_version));
361 
362  $a_writer->xmlEndTag($this->getDSPrefixString()."Rec");
363 
364  $this->afterXmlRecordWriting($a_entity, $a_schema_version, $d);
365 
366  // foreach record records of dependent entities
367  $this->ds_log->debug("...get dependencies");
368  $deps = $this->getDependencies($a_entity, $a_schema_version, $rec, $a_ids);
369  $this->ds_log->debug("...dependencies: ".print_r($deps, true));
370  if (is_array($deps))
371  {
372  foreach ($deps as $dp => $par)
373  {
374  $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_schema_version, $par["ids"], $par["field"]);
375  }
376  }
377  }
378  }
379  else if ($this->data === false)
380  {
381  // false -> add records of dependent entities (no record)
382  $this->ds_log->debug("...get dependencies (no record)");
383  $deps = $this->getDependencies($a_entity, $a_schema_version, null, $a_ids);
384  if (is_array($deps))
385  {
386  foreach ($deps as $dp => $par)
387  {
388  $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_schema_version, $par["ids"], $par["field"]);
389  }
390  }
391  }
392  }
393 
400  function afterXmlRecordWriting($a_entity, $a_version, $a_set)
401  {
402  }
403 
409  private function addTypesXml($a_writer, $a_entity, $a_schema_version)
410  {
411  $types = $this->getXmlTypes($a_entity, $a_schema_version);
412 
413  // add types of current entity
414  if (is_array($types))
415  {
416  $a_writer->xmlStartTag($this->getDSPrefixString()."Types",
417  array("Entity" => $this->getXmlEntityName($a_entity, $a_schema_version),
418  "SchemaVersion" => $a_schema_version));
419  foreach ($this->getXmlTypes($a_entity, $a_schema_version) as $f => $t)
420  {
421  $a_writer->xmlElement($this->getDSPrefixString().'FieldType',
422  array("Name" => $f, "Type" => $t));
423  }
424  $a_writer->xmlEndTag($this->getDSPrefixString()."Types");
425  }
426 
427  // add types of dependent entities
428  $deps = $this->getDependencies($a_entity, $a_schema_version, null, null);
429  if (is_array($deps))
430  {
431  foreach ($deps as $dp => $w)
432  {
433  $this->addTypesXml($a_writer, $dp, $a_schema_version);
434  }
435  }
436 
437  }
438 
446  function getNamespaces(&$namespaces, $a_entity, $a_schema_version)
447  {
448  $ns = $this->getXmlNamespace($a_entity, $a_schema_version);
449  if ($ns != "")
450  {
451  $namespaces[$a_entity] = $ns;
452  }
453  // add types of dependent entities
454  $deps = $this->getDependencies($a_entity, $a_schema_version, null, null);
455  if (is_array($deps))
456  {
457  foreach ($deps as $dp => $w)
458  {
459  $this->getNamespaces($namespaces, $dp, $a_schema_version);
460  }
461  }
462  }
463 
470  function getXmlRecord($a_entity, $a_version, $a_set)
471  {
472  return $a_set;
473  }
474 
481  function getJsonRecord($a_set)
482  {
483  return $a_set;
484  }
485 
491  function getXmlTypes($a_entity, $a_version)
492  {
493  return $this->getTypes($a_entity, $a_version);
494  }
495 
501  function getJsonTypes($a_entity, $a_version)
502  {
503  return $this->getTypes($a_entity, $a_version);
504  }
505 
512  function getXMLEntityName($a_entity, $a_version)
513  {
514  return $a_entity;
515  }
516 
523  function getXMLEntityTag($a_entity, $a_schema_version)
524  {
525  return $this->convertToLeadingUpper($a_entity);
526  }
527 
532  function getJsonEntityName($a_entity, $a_version)
533  {
534  return $a_entity;
535  }
536 
542  function setImport($a_val)
543  {
544  $this->import = $a_val;
545  }
546 
552  function getImport()
553  {
554  return $this->import;
555  }
556 
562  function setCurrentInstallationId($a_val)
563  {
564  $this->current_installation_id = $a_val;
565  }
566 
573  {
575  }
576 
584  protected function createObjectExportId($a_type, $a_id)
585  {
586  return "il_".IL_INST_ID."_".$a_type."_".$a_id;
587  }
588 
596  protected function parseObjectExportId($a_id, $a_fallback_id = NULL)
597  {
598  // ilias export id?
599  if(substr($a_id, 0, 3) == "il_")
600  {
601  $parts = explode("_", $a_id);
602  $inst_id = $parts[1];
603  $type = $parts[2];
604  $id = $parts[3];
605 
606  // missing installation ids?
607  if(($inst_id == 0 || IL_INST_ID == 0) && !DEVMODE)
608  {
609  return array("type"=>self::EXPORT_NO_INST_ID, "id"=>$a_fallback_id);
610  }
611 
612  // same installation?
613  if($inst_id == IL_INST_ID)
614  {
615  // still existing?
616  if(ilObject::_lookupType($id) == $type)
617  {
618  return array("type"=>self::EXPORT_ID_ILIAS_LOCAL, "id"=>$id);
619  }
620  // not found
621  else
622  {
623  return array("type"=>self::EXPORT_ID_ILIAS_LOCAL_INVALID, "id"=>$a_fallback_id);
624  }
625  }
626  // different installation
627  else
628  {
629  $id = ilObject::_getIdForImportId($a_id);
630  // matching type?
631  if($id && ilObject::_lookupType($id) == $type)
632  {
633  return array("type"=>self::EXPORT_ID_ILIAS_REMOTE, "id"=>$id);
634  }
635  // not found
636  else
637  {
638  return array("type"=>self::EXPORT_ID_ILIAS_REMOTE_INVALID, "id"=>$a_fallback_id);
639  }
640  }
641  }
642 
643  // external id
644  $id = ilObject::_getIdForImportId($a_id);
645  if($id)
646  {
647  return array("type"=>self::EXPORT_ID, "id"=>$id);
648  }
649  else
650  {
651  return array("type"=>self::EXPORT_ID_INVALID, "id"=>$a_fallback_id);
652  }
653  }
654 }
655 
656 ?>
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.
Add some data
const EXPORT_ID_ILIAS_REMOTE
setExportDirectories($a_relative, $a_absolute)
Set export directories.
setImportDirectory($a_val)
Set import directory.
getDirectDataFromQuery($a_query, $a_convert_to_leading_upper=true, $a_set=true)
Get data from query.This is a standard procedure, all db field names are directly mapped to abstract ...
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.
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.
for($col=0; $col< 50; $col++) $d
getImport()
Get import object.
const EXPORT_ID_INVALID
const EXPORT_ID_ILIAS_REMOTE_INVALID
$a_type
Definition: workflow.php:93
static encode($mixed, $suppress_native=false)
const EXPORT_ID_ILIAS_LOCAL
getXmlNamespace($a_entity, $a_schema_version)
Get xml namespace.
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.
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
getXmlTypes($a_entity, $a_version)
Get xml types.
global $ilDB
$ret
Definition: parser.php:6
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.
static _getIdForImportId($a_import_id)
get current object id for import id (static)