ILIAS  release_7 Revision v7.30-3-g800a261c036
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
24abstract class ilDataSet
25{
26 public $dircnt;
28
34 const EXPORT_ID = 6;
36
40 protected $db;
41
45 protected $ds_log;
46
50 protected $ds_namespace = 'ds';
51
55 public function __construct()
56 {
57 global $DIC;
58
59 $this->db = $DIC->database();
60 $this->ds_log = ilLoggerFactory::getLogger('ds');
61 }
62
75 final public function init($a_entity, $a_schema_version)
76 {
77 $this->entity = $a_entity;
78 $this->schema_version = $a_schema_version;
79 $this->data = array();
80 }
81
87 abstract public function getSupportedVersions();
88
97 abstract protected function getTypes($a_entity, $a_version);
98
103 abstract protected function getXmlNamespace($a_entity, $a_schema_version);
104
111 abstract public function readData($a_entity, $a_version, $a_ids);
112
119 public function setExportDirectories($a_relative, $a_absolute)
120 {
121 $this->relative_export_dir = $a_relative;
122 $this->absolute_export_dir = $a_absolute;
123 }
124
130 public function setImportDirectory($a_val)
131 {
132 $this->import_directory = $a_val;
133 }
134
140 public function getImportDirectory()
141 {
142 return $this->import_directory;
143 }
144
150 public function setDSPrefix($a_val)
151 {
152 $this->ds_namespace = $a_val;
153 }
154
160 public function getDSPrefix()
161 {
162 return $this->ds_namespace;
163 }
164
165 public function getDSPrefixString()
166 {
167 if ($this->getDSPrefix() != "") {
168 return $this->getDSPrefix() . ":";
169 }
170 }
171
180 public function getDirectDataFromQuery($a_query, $a_convert_to_leading_upper = true, $a_set = true)
181 {
183
184 $set = $ilDB->query($a_query);
185 $this->data = array();
186 $ret = [];
187 while ($rec = $ilDB->fetchAssoc($set)) {
188 if ($a_convert_to_leading_upper) {
189 $tmp = array();
190 foreach ($rec as $k => $v) {
191 $tmp[$this->convertToLeadingUpper($k)]
192 = $v;
193 }
194 $rec = $tmp;
195 }
196
197 if ($a_set) {
198 $this->data[] = $rec;
199 }
200 $ret[] = $rec;
201 }
202 return $ret;
203 }
204
211 public function convertToLeadingUpper($a_str)
212 {
213 $a_str = strtoupper(substr($a_str, 0, 1)) . substr($a_str, 1);
214 while (is_int($pos = strpos($a_str, "_"))) {
215 $a_str = substr($a_str, 0, $pos) .
216 strtoupper(substr($a_str, $pos + 1, 1)) .
217 substr($a_str, $pos + 2);
218 }
219 return $a_str;
220 }
221
222
226 final public function getJsonRepresentation()
227 {
228 if ($this->version === false) {
229 return false;
230 }
231
232 $arr["entity"] = $this->getJsonEntityName();
233 $arr["version"] = $this->version;
234 $arr["install_id"] = IL_INST_ID;
235 $arr["install_url"] = ILIAS_HTTP_PATH;
236 $arr["types"] = $this->getJsonTypes();
237 $arr["set"] = array();
238 foreach ($this->data as $d) {
239 $arr["set"][] = $this->getJsonRecord($d);
240 }
241
242 include_once("./Services/JSON/classes/class.ilJsonUtil.php");
243
244 return ilJsonUtil::encode($arr);
245 }
246
268 final public function getXmlRepresentation(
269 $a_entity,
270 $a_schema_version,
271 $a_ids,
272 $a_field = "",
273 $a_omit_header = false,
274 $a_omit_types = false
275 ) {
276 $this->dircnt = 1;
277
278 // step 1: check target release and supported versions
279
280
281
282 // step 2: init writer
283 include_once "./Services/Xml/classes/class.ilXmlWriter.php";
284 $writer = new ilXmlWriter();
285 if (!$a_omit_header) {
286 $writer->xmlHeader();
287 }
288
289 // collect namespaces
290 $namespaces = $prefixes = array();
291 $this->getNamespaces($namespaces, $a_entity, $a_schema_version);
292 $atts = array("InstallationId" => IL_INST_ID,
293 "InstallationUrl" => ILIAS_HTTP_PATH, "TopEntity" => $a_entity);
294 $cnt = 1;
295 foreach ($namespaces as $entity => $ns) {
296 $prefix = "ns" . $cnt;
297 $prefixes[$entity] = $prefix;
298 // $atts["xmlns:".$prefix] = $ns;
299 $cnt++;
300 }
301
302 $this->ds_log->debug("Start writing Dataset, entity: " . $a_entity . ", schema version: " . $a_schema_version .
303 ", ids: " . print_r($a_ids, true));
304 $writer->xmlStartTag($this->getDSPrefixString() . 'DataSet', $atts);
305
306 // add types
307 if (!$a_omit_types) {
308 $this->ds_log->debug("...write types");
309 $this->addTypesXml($writer, $a_entity, $a_schema_version);
310 }
311
312 // add records
313 $this->ds_log->debug("...write records");
314 $this->addRecordsXml($writer, $prefixes, $a_entity, $a_schema_version, $a_ids, $a_field);
315
316
317 $writer->xmlEndTag($this->getDSPrefixString() . "DataSet");
318 //if ($a_entity == "mep")
319 //{
320 // echo "<pre>".htmlentities($writer->xmlDumpMem(true))."</pre>"; exit;
321 //}
322 return $writer->xmlDumpMem(false);
323 }
324
325
332 public function addRecordsXml($a_writer, $a_prefixes, $a_entity, $a_schema_version, $a_ids, $a_field = "")
333 {
334 $types = $this->getXmlTypes($a_entity, $a_schema_version);
335
336 $this->ds_log->debug("...read data");
337 $this->readData($a_entity, $a_schema_version, $a_ids, $a_field);
338 $this->ds_log->debug("...data: " . print_r($this->data, true));
339 if (is_array($this->data)) {
340 foreach ($this->data as $d) {
341 $a_writer->xmlStartTag(
342 $this->getDSPrefixString() . "Rec",
343 array("Entity" => $this->getXmlEntityName($a_entity, $a_schema_version))
344 );
345
346 // entity tag
347 $a_writer->xmlStartTag($this->getXmlEntityTag($a_entity, $a_schema_version));
348
349 $rec = $this->getXmlRecord($a_entity, $a_schema_version, $d);
350 foreach ($rec as $f => $c) {
351 switch ($types[$f]) {
352 case "directory":
353 if ($this->absolute_export_dir != "" && $this->relative_export_dir != "") {
354 ilUtil::makeDirParents($this->absolute_export_dir . "/dsDir_" . $this->dircnt);
355 ilUtil::rCopy($c, $this->absolute_export_dir . "/dsDir_" . $this->dircnt);
356 //echo "<br>copy-".$c."-".$this->absolute_export_dir."/dsDir_".$this->dircnt."-";
357 $c = $this->relative_export_dir . "/dsDir_" . $this->dircnt;
358 $this->dircnt++;
359 }
360 break;
361 }
362 // this changes schema/dtd
363 //$a_writer->xmlElement($a_prefixes[$a_entity].":".$f,
364 // array(), $c);
365 $a_writer->xmlElement($f, array(), $c);
366 }
367
368 $a_writer->xmlEndTag($this->getXmlEntityTag($a_entity, $a_schema_version));
369
370 $a_writer->xmlEndTag($this->getDSPrefixString() . "Rec");
371
372 $this->afterXmlRecordWriting($a_entity, $a_schema_version, $d);
373
374 // foreach record records of dependent entities
375 $this->ds_log->debug("...get dependencies");
376 $deps = $this->getDependencies($a_entity, $a_schema_version, $rec, $a_ids);
377 $this->ds_log->debug("...dependencies: " . print_r($deps, true));
378 if (is_array($deps)) {
379 foreach ($deps as $dp => $par) {
380 $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_schema_version, $par["ids"], $par["field"]);
381 }
382 }
383 }
384 } elseif ($this->data === false) {
385 // false -> add records of dependent entities (no record)
386 $this->ds_log->debug("...get dependencies (no record)");
387 $deps = $this->getDependencies($a_entity, $a_schema_version, null, $a_ids);
388 if (is_array($deps)) {
389 foreach ($deps as $dp => $par) {
390 $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_schema_version, $par["ids"], $par["field"]);
391 }
392 }
393 }
394 }
395
402 public function afterXmlRecordWriting($a_entity, $a_version, $a_set)
403 {
404 }
405
411 private function addTypesXml($a_writer, $a_entity, $a_schema_version)
412 {
413 $types = $this->getXmlTypes($a_entity, $a_schema_version);
414
415 // add types of current entity
416 if (is_array($types)) {
417 $a_writer->xmlStartTag(
418 $this->getDSPrefixString() . "Types",
419 array("Entity" => $this->getXmlEntityName($a_entity, $a_schema_version),
420 "SchemaVersion" => $a_schema_version)
421 );
422 foreach ($this->getXmlTypes($a_entity, $a_schema_version) as $f => $t) {
423 $a_writer->xmlElement(
424 $this->getDSPrefixString() . 'FieldType',
425 array("Name" => $f, "Type" => $t)
426 );
427 }
428 $a_writer->xmlEndTag($this->getDSPrefixString() . "Types");
429 }
430
431 // add types of dependent entities
432 $deps = $this->getDependencies($a_entity, $a_schema_version, null, null);
433 if (is_array($deps)) {
434 foreach ($deps as $dp => $w) {
435 $this->addTypesXml($a_writer, $dp, $a_schema_version);
436 }
437 }
438 }
439
447 public function getNamespaces(&$namespaces, $a_entity, $a_schema_version)
448 {
449 $ns = $this->getXmlNamespace($a_entity, $a_schema_version);
450 if ($ns != "") {
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 foreach ($deps as $dp => $w) {
457 $this->getNamespaces($namespaces, $dp, $a_schema_version);
458 }
459 }
460 }
461
468 public function getXmlRecord($a_entity, $a_version, $a_set)
469 {
470 return $a_set;
471 }
472
479 public function getJsonRecord($a_set)
480 {
481 return $a_set;
482 }
483
489 public function getXmlTypes($a_entity, $a_version)
490 {
491 return $this->getTypes($a_entity, $a_version);
492 }
493
499 public function getJsonTypes($a_entity, $a_version)
500 {
501 return $this->getTypes($a_entity, $a_version);
502 }
503
510 public function getXMLEntityName($a_entity, $a_version)
511 {
512 return $a_entity;
513 }
514
521 public function getXMLEntityTag($a_entity, $a_schema_version)
522 {
523 return $this->convertToLeadingUpper($a_entity);
524 }
525
530 public function getJsonEntityName($a_entity, $a_version)
531 {
532 return $a_entity;
533 }
534
540 public function setImport($a_val)
541 {
542 $this->import = $a_val;
543 }
544
550 public function getImport()
551 {
552 return $this->import;
553 }
554
560 public function setCurrentInstallationId($a_val)
561 {
562 $this->current_installation_id = $a_val;
563 }
564
570 public function getCurrentInstallationId()
571 {
573 }
574
582 protected function createObjectExportId($a_type, $a_id)
583 {
584 return "il_" . IL_INST_ID . "_" . $a_type . "_" . $a_id;
585 }
586
594 protected function parseObjectExportId($a_id, $a_fallback_id = null)
595 {
596 // ilias export id?
597 if (substr($a_id, 0, 3) == "il_") {
598 $parts = explode("_", $a_id);
599 $inst_id = $parts[1];
600 $type = $parts[2];
601 $id = $parts[3];
602
603 // missing installation ids?
604 if (($inst_id == 0 || IL_INST_ID == 0) && !DEVMODE) {
605 return array("type" => self::EXPORT_NO_INST_ID, "id" => $a_fallback_id);
606 }
607
608 // same installation?
609 if ($inst_id == IL_INST_ID) {
610 // still existing?
611 if (ilObject::_lookupType($id) == $type) {
612 return array("type" => self::EXPORT_ID_ILIAS_LOCAL, "id" => $id);
613 }
614 // not found
615 else {
616 return array("type" => self::EXPORT_ID_ILIAS_LOCAL_INVALID, "id" => $a_fallback_id);
617 }
618 }
619 // different installation
620 else {
621 $id = ilObject::_getIdForImportId($a_id);
622 // matching type?
623 if ($id && ilObject::_lookupType($id) == $type) {
624 return array("type" => self::EXPORT_ID_ILIAS_REMOTE, "id" => $id);
625 }
626 // not found
627 else {
628 return array("type" => self::EXPORT_ID_ILIAS_REMOTE_INVALID, "id" => $a_fallback_id);
629 }
630 }
631 }
632
633 // external id
634 $id = ilObject::_getIdForImportId($a_id);
635 if ($id) {
636 return array("type" => self::EXPORT_ID, "id" => $id);
637 } else {
638 return array("type" => self::EXPORT_ID_INVALID, "id" => $a_fallback_id);
639 }
640 }
641
642 protected function stripTags(array $rec, array $omit_keys = []) : array
643 {
644 $ret_rec = [];
645 foreach ($rec as $k => $v) {
646 if (in_array($k, $omit_keys, true)) {
647 $ret_rec[$k] = $v;
648 } else {
649 $ret_rec[$k] = ilUtil::stripSlashes($v);
650 }
651 }
652 return $ret_rec;
653 }
654}
An exception for terminatinating execution or to throw for unit testing.
A dataset contains in data in a common structure that can be shared and transformed for different pur...
const EXPORT_ID_ILIAS_REMOTE_INVALID
stripTags(array $rec, array $omit_keys=[])
getXMLEntityTag($a_entity, $a_schema_version)
Get entity tag.
setExportDirectories($a_relative, $a_absolute)
Set export directories.
getXMLEntityName($a_entity, $a_version)
Get entity name for xml (may be overwritten)
const EXPORT_ID_ILIAS_LOCAL_INVALID
getImport()
Get import object.
getNamespaces(&$namespaces, $a_entity, $a_schema_version)
Get xml namespaces.
getTypes($a_entity, $a_version)
Get (abstract) types for (abstract) field names.
getXmlTypes($a_entity, $a_version)
Get xml types.
setDSPrefix($a_val)
Set XML dataset namespace prefix.
getDSPrefix()
Get XML dataset namespace prefix.
__construct()
Constructor.
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 ...
const EXPORT_ID_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...
getImportDirectory()
Get import directory.
afterXmlRecordWriting($a_entity, $a_version, $a_set)
After xml record writing hook record.
getCurrentInstallationId()
Get current installation id.
const EXPORT_NO_INST_ID
const EXPORT_ID_ILIAS_REMOTE
addTypesXml($a_writer, $a_entity, $a_schema_version)
Add types to xml writer.
getJsonEntityName($a_entity, $a_version)
Get entity name for json (may be overwritten)
getSupportedVersions()
Get supported version.
convertToLeadingUpper($a_str)
Make xyz_abc a XyzAbc string.
setImport($a_val)
Set import object.
addRecordsXml($a_writer, $a_prefixes, $a_entity, $a_schema_version, $a_ids, $a_field="")
Add records xml.
readData($a_entity, $a_version, $a_ids)
Read data from DB.
parseObjectExportId($a_id, $a_fallback_id=null)
Parse export id.
setImportDirectory($a_val)
Set import directory.
setCurrentInstallationId($a_val)
Set current installation id.
getJsonRecord($a_set)
Get json record for version.
getJsonTypes($a_entity, $a_version)
Get json types.
init($a_entity, $a_schema_version)
Init.
const EXPORT_ID_ILIAS_LOCAL
getXmlRecord($a_entity, $a_version, $a_set)
Get xml record for version.
getXmlNamespace($a_entity, $a_schema_version)
Get xml namespace.
createObjectExportId($a_type, $a_id)
Build ilias export id.
getJsonRepresentation()
Get json representation.
static encode($mixed, $suppress_native=false)
static getLogger($a_component_id)
Get component logger.
static _lookupType($a_id, $a_reference=false)
lookup object type
static _getIdForImportId($a_import_id)
get current object id for import id (static)
static rCopy($a_sdir, $a_tdir, $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static makeDirParents($a_dir)
Create a new directory and all parent directories.
XML writer class.
$c
Definition: cli.php:37
const IL_INST_ID
Definition: constants.php:38
for( $i=6;$i< 13;$i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
global $DIC
Definition: goto.php:24
$ret
Definition: parser.php:6
$type
global $ilDB