ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  public $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 
40  protected $db;
41 
45  protected $ds_log;
46 
50  public function __construct()
51  {
52  global $DIC;
53 
54  $this->db = $DIC->database();
55  $this->ds_log = ilLoggerFactory::getLogger('ds');
56  }
57 
70  final public function init($a_entity, $a_schema_version)
71  {
72  $this->entity = $a_entity;
73  $this->schema_version = $a_schema_version;
74  $this->data = array();
75  }
76 
82  abstract public function getSupportedVersions();
83 
92  abstract protected function getTypes($a_entity, $a_version);
93 
98  abstract protected function getXmlNamespace($a_entity, $a_schema_version);
99 
106  abstract public function readData($a_entity, $a_version, $a_ids);
107 
114  public function setExportDirectories($a_relative, $a_absolute)
115  {
116  $this->relative_export_dir = $a_relative;
117  $this->absolute_export_dir = $a_absolute;
118  }
119 
125  public function setImportDirectory($a_val)
126  {
127  $this->import_directory = $a_val;
128  }
129 
135  public function getImportDirectory()
136  {
137  return $this->import_directory;
138  }
139 
145  public function setDSPrefix($a_val)
146  {
147  $this->var = $a_val;
148  }
149 
155  public function getDSPrefix()
156  {
157  return $this->var;
158  }
159 
160  public function getDSPrefixString()
161  {
162  if ($this->getDSPrefix() != "") {
163  return $this->getDSPrefix() . ":";
164  }
165  }
166 
175  public function getDirectDataFromQuery($a_query, $a_convert_to_leading_upper = true, $a_set = true)
176  {
177  $ilDB = $this->db;
178 
179  $set = $ilDB->query($a_query);
180  $this->data = array();
181  $ret = [];
182  while ($rec = $ilDB->fetchAssoc($set)) {
183  if ($a_convert_to_leading_upper) {
184  $tmp = array();
185  foreach ($rec as $k => $v) {
186  $tmp[$this->convertToLeadingUpper($k)]
187  = $v;
188  }
189  $rec = $tmp;
190  }
191 
192  if ($a_set) {
193  $this->data[] = $rec;
194  }
195  $ret[] = $rec;
196  }
197  return $ret;
198  }
199 
206  public function convertToLeadingUpper($a_str)
207  {
208  $a_str = strtoupper(substr($a_str, 0, 1)) . substr($a_str, 1);
209  while (is_int($pos = strpos($a_str, "_"))) {
210  $a_str = substr($a_str, 0, $pos) .
211  strtoupper(substr($a_str, $pos+1, 1)) .
212  substr($a_str, $pos+2);
213  }
214  return $a_str;
215  }
216 
217 
221  final public function getJsonRepresentation()
222  {
223  if ($this->version === false) {
224  return false;
225  }
226 
227  $arr["entity"] = $this->getJsonEntityName();
228  $arr["version"] = $this->version;
229  $arr["install_id"] = IL_INST_ID;
230  $arr["install_url"] = ILIAS_HTTP_PATH;
231  $arr["types"] = $this->getJsonTypes();
232  $arr["set"] = array();
233  foreach ($this->data as $d) {
234  $arr["set"][] = $this->getJsonRecord($d);
235  }
236 
237  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
238 
239  return ilJsonUtil::encode($arr);
240  }
241 
263  final public function getXmlRepresentation(
264  $a_entity,
265  $a_schema_version,
266  $a_ids,
267  $a_field = "",
268  $a_omit_header = false,
269  $a_omit_types = false
270  ) {
271  $this->dircnt = 1;
272 
273  // step 1: check target release and supported versions
274 
275 
276 
277  // step 2: init writer
278  include_once "./Services/Xml/classes/class.ilXmlWriter.php";
279  $writer = new ilXmlWriter();
280  if (!$a_omit_header) {
281  $writer->xmlHeader();
282  }
283 
284  // collect namespaces
285  $namespaces = $prefixes = array();
286  $this->getNamespaces($namespaces, $a_entity, $a_schema_version);
287  $atts = array("InstallationId" => IL_INST_ID,
288  "InstallationUrl" => ILIAS_HTTP_PATH, "TopEntity" => $a_entity);
289  $cnt = 1;
290  foreach ($namespaces as $entity => $ns) {
291  $prefix = "ns" . $cnt;
292  $prefixes[$entity] = $prefix;
293  // $atts["xmlns:".$prefix] = $ns;
294  $cnt++;
295  }
296 
297  $this->ds_log->debug("Start writing Dataset, entity: " . $a_entity . ", schema version: " . $a_schema_version .
298  ", ids: " . print_r($a_ids, true));
299  $writer->xmlStartTag($this->getDSPrefixString() . 'DataSet', $atts);
300 
301  // add types
302  if (!$a_omit_types) {
303  $this->ds_log->debug("...write types");
304  $this->addTypesXml($writer, $a_entity, $a_schema_version);
305  }
306 
307  // add records
308  $this->ds_log->debug("...write records");
309  $this->addRecordsXml($writer, $prefixes, $a_entity, $a_schema_version, $a_ids, $a_field);
310 
311 
312  $writer->xmlEndTag($this->getDSPrefixString() . "DataSet");
313  //if ($a_entity == "mep")
314  //{
315  // echo "<pre>".htmlentities($writer->xmlDumpMem(true))."</pre>"; exit;
316  //}
317  return $writer->xmlDumpMem(false);
318  }
319 
320 
327  public function addRecordsXml($a_writer, $a_prefixes, $a_entity, $a_schema_version, $a_ids, $a_field = "")
328  {
329  $types = $this->getXmlTypes($a_entity, $a_schema_version);
330 
331  $this->ds_log->debug("...read data");
332  $this->readData($a_entity, $a_schema_version, $a_ids, $a_field);
333  $this->ds_log->debug("...data: " . print_r($this->data, true));
334  if (is_array($this->data)) {
335  foreach ($this->data as $d) {
336  $a_writer->xmlStartTag(
337  $this->getDSPrefixString() . "Rec",
338  array("Entity" => $this->getXmlEntityName($a_entity, $a_schema_version))
339  );
340 
341  // entity tag
342  $a_writer->xmlStartTag($this->getXmlEntityTag($a_entity, $a_schema_version));
343 
344  $rec = $this->getXmlRecord($a_entity, $a_schema_version, $d);
345  foreach ($rec as $f => $c) {
346  switch ($types[$f]) {
347  case "directory":
348  if ($this->absolute_export_dir != "" && $this->relative_export_dir != "") {
349  ilUtil::makeDirParents($this->absolute_export_dir . "/dsDir_" . $this->dircnt);
350  ilUtil::rCopy($c, $this->absolute_export_dir . "/dsDir_" . $this->dircnt);
351  //echo "<br>copy-".$c."-".$this->absolute_export_dir."/dsDir_".$this->dircnt."-";
352  $c = $this->relative_export_dir . "/dsDir_" . $this->dircnt;
353  $this->dircnt++;
354  }
355  break;
356  }
357  // this changes schema/dtd
358  //$a_writer->xmlElement($a_prefixes[$a_entity].":".$f,
359  // array(), $c);
360  $a_writer->xmlElement($f, array(), $c);
361  }
362 
363  $a_writer->xmlEndTag($this->getXmlEntityTag($a_entity, $a_schema_version));
364 
365  $a_writer->xmlEndTag($this->getDSPrefixString() . "Rec");
366 
367  $this->afterXmlRecordWriting($a_entity, $a_schema_version, $d);
368 
369  // foreach record records of dependent entities
370  $this->ds_log->debug("...get dependencies");
371  $deps = $this->getDependencies($a_entity, $a_schema_version, $rec, $a_ids);
372  $this->ds_log->debug("...dependencies: " . print_r($deps, true));
373  if (is_array($deps)) {
374  foreach ($deps as $dp => $par) {
375  $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_schema_version, $par["ids"], $par["field"]);
376  }
377  }
378  }
379  } elseif ($this->data === false) {
380  // false -> add records of dependent entities (no record)
381  $this->ds_log->debug("...get dependencies (no record)");
382  $deps = $this->getDependencies($a_entity, $a_schema_version, null, $a_ids);
383  if (is_array($deps)) {
384  foreach ($deps as $dp => $par) {
385  $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_schema_version, $par["ids"], $par["field"]);
386  }
387  }
388  }
389  }
390 
397  public function afterXmlRecordWriting($a_entity, $a_version, $a_set)
398  {
399  }
400 
406  private function addTypesXml($a_writer, $a_entity, $a_schema_version)
407  {
408  $types = $this->getXmlTypes($a_entity, $a_schema_version);
409 
410  // add types of current entity
411  if (is_array($types)) {
412  $a_writer->xmlStartTag(
413  $this->getDSPrefixString() . "Types",
414  array("Entity" => $this->getXmlEntityName($a_entity, $a_schema_version),
415  "SchemaVersion" => $a_schema_version)
416  );
417  foreach ($this->getXmlTypes($a_entity, $a_schema_version) as $f => $t) {
418  $a_writer->xmlElement(
419  $this->getDSPrefixString() . 'FieldType',
420  array("Name" => $f, "Type" => $t)
421  );
422  }
423  $a_writer->xmlEndTag($this->getDSPrefixString() . "Types");
424  }
425 
426  // add types of dependent entities
427  $deps = $this->getDependencies($a_entity, $a_schema_version, null, null);
428  if (is_array($deps)) {
429  foreach ($deps as $dp => $w) {
430  $this->addTypesXml($a_writer, $dp, $a_schema_version);
431  }
432  }
433  }
434 
442  public function getNamespaces(&$namespaces, $a_entity, $a_schema_version)
443  {
444  $ns = $this->getXmlNamespace($a_entity, $a_schema_version);
445  if ($ns != "") {
446  $namespaces[$a_entity] = $ns;
447  }
448  // add types of dependent entities
449  $deps = $this->getDependencies($a_entity, $a_schema_version, null, null);
450  if (is_array($deps)) {
451  foreach ($deps as $dp => $w) {
452  $this->getNamespaces($namespaces, $dp, $a_schema_version);
453  }
454  }
455  }
456 
463  public function getXmlRecord($a_entity, $a_version, $a_set)
464  {
465  return $a_set;
466  }
467 
474  public function getJsonRecord($a_set)
475  {
476  return $a_set;
477  }
478 
484  public function getXmlTypes($a_entity, $a_version)
485  {
486  return $this->getTypes($a_entity, $a_version);
487  }
488 
494  public function getJsonTypes($a_entity, $a_version)
495  {
496  return $this->getTypes($a_entity, $a_version);
497  }
498 
505  public function getXMLEntityName($a_entity, $a_version)
506  {
507  return $a_entity;
508  }
509 
516  public function getXMLEntityTag($a_entity, $a_schema_version)
517  {
518  return $this->convertToLeadingUpper($a_entity);
519  }
520 
525  public function getJsonEntityName($a_entity, $a_version)
526  {
527  return $a_entity;
528  }
529 
535  public function setImport($a_val)
536  {
537  $this->import = $a_val;
538  }
539 
545  public function getImport()
546  {
547  return $this->import;
548  }
549 
555  public function setCurrentInstallationId($a_val)
556  {
557  $this->current_installation_id = $a_val;
558  }
559 
565  public function getCurrentInstallationId()
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  $parts = explode("_", $a_id);
594  $inst_id = $parts[1];
595  $type = $parts[2];
596  $id = $parts[3];
597 
598  // missing installation ids?
599  if (($inst_id == 0 || IL_INST_ID == 0) && !DEVMODE) {
600  return array("type"=>self::EXPORT_NO_INST_ID, "id"=>$a_fallback_id);
601  }
602 
603  // same installation?
604  if ($inst_id == IL_INST_ID) {
605  // still existing?
606  if (ilObject::_lookupType($id) == $type) {
607  return array("type"=>self::EXPORT_ID_ILIAS_LOCAL, "id"=>$id);
608  }
609  // not found
610  else {
611  return array("type"=>self::EXPORT_ID_ILIAS_LOCAL_INVALID, "id"=>$a_fallback_id);
612  }
613  }
614  // different installation
615  else {
617  // matching type?
618  if ($id && ilObject::_lookupType($id) == $type) {
619  return array("type"=>self::EXPORT_ID_ILIAS_REMOTE, "id"=>$id);
620  }
621  // not found
622  else {
623  return array("type"=>self::EXPORT_ID_ILIAS_REMOTE_INVALID, "id"=>$a_fallback_id);
624  }
625  }
626  }
627 
628  // external id
630  if ($id) {
631  return array("type"=>self::EXPORT_ID, "id"=>$id);
632  } else {
633  return array("type"=>self::EXPORT_ID_INVALID, "id"=>$a_fallback_id);
634  }
635  }
636 }
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.
$type
static rCopy($a_sdir, $a_tdir, $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
global $DIC
Definition: saml.php:7
const EXPORT_ID_ILIAS_LOCAL_INVALID
if(!array_key_exists('StateId', $_REQUEST)) $id
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.
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
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...
$a_type
Definition: workflow.php:92
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
setImport($a_val)
Set import object.
static getLogger($a_component_id)
Get component logger.
parseObjectExportId($a_id, $a_fallback_id=null)
Parse export id.
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.
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
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)