ILIAS  release_8 Revision v8.24
class.ilDataSet.php
Go to the documentation of this file.
1<?php
2
36abstract class ilDataSet
37{
38 public const EXPORT_NO_INST_ID = 1;
39 public const EXPORT_ID_ILIAS_LOCAL = 2;
41 public const EXPORT_ID_ILIAS_REMOTE = 4;
43 public const EXPORT_ID = 6;
44 public const EXPORT_ID_INVALID = 7;
45
46 public int $dircnt = 0;
47 protected string $current_installation_id = "";
48 protected array $data = [];
49 protected ilDBInterface $db;
50 protected ilLogger $ds_log;
51 protected string $import_directory = "";
52 protected string $entity = "";
53 protected string $schema_version = "";
54 protected string $relative_export_dir = "";
55 protected string $absolute_export_dir = "";
56 protected string $ds_prefix = "ds";
57 protected string $version = "";
59
60 public function __construct()
61 {
62 global $DIC;
63
64 $this->db = $DIC->database();
65 $this->ds_log = ilLoggerFactory::getLogger('ds');
66 }
67
80 final public function init(string $a_entity, string $a_schema_version): void
81 {
82 $this->entity = $a_entity;
83 $this->schema_version = $a_schema_version;
84 $this->data = array();
85 }
86
87 abstract public function getSupportedVersions(): array;
88
96 abstract protected function getTypes(string $a_entity, string $a_version): array;
97
98 abstract protected function getXmlNamespace(string $a_entity, string $a_schema_version): string;
99
104 abstract public function readData(
105 string $a_entity,
106 string $a_version,
107 array $a_ids
108 ): void;
109
110 public function setExportDirectories(string $a_relative, string $a_absolute): void
111 {
112 $this->relative_export_dir = $a_relative;
113 $this->absolute_export_dir = $a_absolute;
114 }
115
116 public function setImportDirectory(string $a_val): void
117 {
118 $this->import_directory = $a_val;
119 }
120
121 public function getImportDirectory(): string
122 {
124 }
125
126 public function setDSPrefix(string $a_val): void
127 {
128 $this->ds_prefix = $a_val;
129 }
130
131 public function getDSPrefix(): string
132 {
133 return $this->ds_prefix;
134 }
135
136 public function getDSPrefixString(): string
137 {
138 if ($this->getDSPrefix() !== "") {
139 return $this->getDSPrefix() . ":";
140 }
141 return "";
142 }
143
148 public function getDirectDataFromQuery(
149 string $a_query,
150 bool $a_convert_to_leading_upper = true,
151 bool $a_set = true
152 ): array {
153 $ilDB = $this->db;
154
155 $set = $ilDB->query($a_query);
156 $this->data = array();
157 $ret = [];
158 while ($rec = $ilDB->fetchAssoc($set)) {
159 if ($a_convert_to_leading_upper) {
160 $tmp = array();
161 foreach ($rec as $k => $v) {
162 $tmp[$this->convertToLeadingUpper($k)]
163 = $v;
164 }
165 $rec = $tmp;
166 }
167
168 if ($a_set) {
169 $this->data[] = $rec;
170 }
171 $ret[] = $rec;
172 }
173 return $ret;
174 }
175
179 public function convertToLeadingUpper(string $a_str): string
180 {
181 $a_str = strtoupper(substr($a_str, 0, 1)) . substr($a_str, 1);
182 while (($pos = strpos($a_str, "_")) !== false) {
183 $a_str = substr($a_str, 0, $pos) .
184 strtoupper(substr($a_str, $pos + 1, 1)) .
185 substr($a_str, $pos + 2);
186 }
187 return $a_str;
188 }
189
190
212 final public function getXmlRepresentation(
213 string $a_entity,
214 string $a_schema_version,
215 ?array $a_ids,
216 string $a_field = "",
217 bool $a_omit_header = false,
218 bool $a_omit_types = false
219 ): string {
220 $this->dircnt = 1;
221
222 // init writer
223 $writer = new ilXmlWriter();
224 if (!$a_omit_header) {
225 $writer->xmlHeader();
226 }
227
228 // collect namespaces
229 $namespaces = $prefixes = array();
230 $this->getNamespaces($namespaces, $a_entity, $a_schema_version);
231 $atts = array("InstallationId" => IL_INST_ID,
232 "InstallationUrl" => ILIAS_HTTP_PATH, "TopEntity" => $a_entity);
233 $cnt = 1;
234 foreach ($namespaces as $entity => $ns) {
235 $prefix = "ns" . $cnt;
236 $prefixes[$entity] = $prefix;
237 // $atts["xmlns:".$prefix] = $ns;
238 $cnt++;
239 }
240
241 $this->ds_log->debug("Start writing Dataset, entity: " . $a_entity . ", schema version: " . $a_schema_version .
242 ", ids: " . print_r($a_ids, true));
243 $writer->xmlStartTag($this->getDSPrefixString() . 'DataSet', $atts);
244
245 // add types
246 if (!$a_omit_types) {
247 $this->ds_log->debug("...write types");
248 $this->addTypesXml($writer, $a_entity, $a_schema_version);
249 }
250
251 // add records
252 $this->ds_log->debug("...write records");
253 $this->addRecordsXml($writer, $prefixes, $a_entity, $a_schema_version, $a_ids, $a_field);
254
255 $writer->xmlEndTag($this->getDSPrefixString() . "DataSet");
256
257 return $writer->xmlDumpMem(false);
258 }
259
260
261 public function addRecordsXml(
262 ilXmlWriter $a_writer,
263 array $a_prefixes,
264 string $a_entity,
265 string $a_schema_version,
266 array $a_ids,
267 ?string $a_field = ""
268 ): void {
269 $types = $this->getXmlTypes($a_entity, $a_schema_version);
270
271 $this->ds_log->debug("...read data");
272 $this->readData($a_entity, $a_schema_version, $a_ids);
273 $this->ds_log->debug("...data: " . print_r($this->data, true));
274 foreach ($this->data as $d) {
275 $a_writer->xmlStartTag(
276 $this->getDSPrefixString() . "Rec",
277 array("Entity" => $this->getXMLEntityName($a_entity, $a_schema_version))
278 );
279 $a_writer->xmlStartTag($this->getXMLEntityTag($a_entity, ''));
280 $rec = $this->getXmlRecord($a_entity, $a_schema_version, $d);
281 foreach ($rec as $f => $c) {
282 if ((($types[$f] ?? "") == "directory") && $this->absolute_export_dir !== "" && $this->relative_export_dir !== "") {
283 ilFileUtils::makeDirParents($this->absolute_export_dir . "/dsDir_" . $this->dircnt);
284 $sdir = realpath($c);
285 $tdir = realpath($this->absolute_export_dir . "/dsDir_" . $this->dircnt);
286 try {
287 ilFileUtils::rCopy($sdir, $tdir);
288 } catch (\ILIAS\Filesystem\Exception\FileNotFoundException $e) {
289 $this->ds_log->error($e->getMessage());
290 }
291 $c = $this->relative_export_dir . "/dsDir_" . $this->dircnt;
292 $this->dircnt++;
293 }
294 // this changes schema/dtd
295 //$a_writer->xmlElement($a_prefixes[$a_entity].":".$f,
296 // array(), $c);
297 $a_writer->xmlElement($f, array(), $c);
298 }
299
300 $a_writer->xmlEndTag($this->getXMLEntityTag($a_entity, ''));
301 $a_writer->xmlEndTag($this->getDSPrefixString() . "Rec");
302
303 $this->afterXmlRecordWriting($a_entity, $a_schema_version, $d);
304
305 // foreach record records of dependent entities
306 $this->ds_log->debug("...get dependencies");
307 $deps = $this->getDependencies($a_entity, $a_schema_version, $rec, $a_ids);
308 $this->ds_log->debug("...dependencies: " . print_r($deps, true));
309 foreach ($deps as $dp => $par) {
310 $ids = !is_array($par["ids"])
311 ? [$par["ids"]]
312 : $par["ids"];
313 $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_schema_version, $ids, $par["field"] ?? null);
314 }
315 }
316 }
317
318 protected function getDependencies(
319 string $a_entity,
320 string $a_version,
321 ?array $a_rec = null,
322 ?array $a_ids = null
323 ): array {
324 return [];
325 }
326
327 // After xml record writing hook record
328 public function afterXmlRecordWriting(string $a_entity, string $a_version, array $a_set): void
329 {
330 }
331
332 // Add types to xml writer
333 private function addTypesXml(ilXmlWriter $a_writer, string $a_entity, string $a_schema_version): void
334 {
335 $types = $this->getXmlTypes($a_entity, $a_schema_version);
336
337 // add types of current entity
338 if (count($types) > 0) {
339 $a_writer->xmlStartTag(
340 $this->getDSPrefixString() . "Types",
341 array("Entity" => $this->getXMLEntityName($a_entity, $a_schema_version),
342 "SchemaVersion" => $a_schema_version)
343 );
344 foreach ($this->getXmlTypes($a_entity, $a_schema_version) as $f => $t) {
345 $a_writer->xmlElement(
346 $this->getDSPrefixString() . 'FieldType',
347 array("Name" => $f, "Type" => $t)
348 );
349 }
350 $a_writer->xmlEndTag($this->getDSPrefixString() . "Types");
351 }
352
353 // add types of dependent entities
354 $deps = $this->getDependencies($a_entity, $a_schema_version, null, null);
355 foreach ($deps as $dp => $w) {
356 $this->addTypesXml($a_writer, $dp, $a_schema_version);
357 }
358 }
359
360 // Get xml namespaces
361 public function getNamespaces(array &$namespaces, string $a_entity, string $a_schema_version): void
362 {
363 $ns = $this->getXmlNamespace($a_entity, $a_schema_version);
364 if ($ns !== "") {
365 $namespaces[$a_entity] = $ns;
366 }
367 // add types of dependent entities
368 $deps = $this->getDependencies($a_entity, $a_schema_version, null, null);
369 foreach ($deps as $dp => $w) {
370 $this->getNamespaces($namespaces, $dp, $a_schema_version);
371 }
372 }
373
378 public function getXmlRecord(string $a_entity, string $a_version, array $a_set): array
379 {
380 return $a_set;
381 }
382
387 public function getXmlTypes(string $a_entity, string $a_version): array
388 {
389 return $this->getTypes($a_entity, $a_version);
390 }
391
396 public function getXMLEntityName(string $a_entity, string $a_version): string
397 {
398 return $a_entity;
399 }
400
404 public function getXMLEntityTag(string $a_entity, string $a_schema_version): string
405 {
406 return $this->convertToLeadingUpper($a_entity);
407 }
408
409 public function setImport(ilSurveyImporter $a_val): void
410 {
411 $this->import = $a_val;
412 }
413
414 public function getImport(): ilSurveyImporter
415 {
416 return $this->import;
417 }
418
419 public function setCurrentInstallationId(string $a_val): void
420 {
421 $this->current_installation_id = $a_val;
422 }
423
424 public function getCurrentInstallationId(): string
425 {
426 return $this->current_installation_id;
427 }
428
432 protected function createObjectExportId(string $a_type, string $a_id): string
433 {
434 return "il_" . IL_INST_ID . "_" . $a_type . "_" . $a_id;
435 }
436
441 protected function parseObjectExportId(string $a_id, ?string $a_fallback_id = null): array
442 {
443 // ilias export id?
444 if (strpos($a_id, "il_") === 0) {
445 $parts = explode("_", $a_id);
446 if (count($parts) !== 4) {
447 throw new ilException("Invalid import ID '" . $a_id . "'.");
448 }
449 $inst_id = $parts[1];
450 $type = $parts[2];
451 $id = $parts[3];
452
453 // missing installation ids?
454 if (($inst_id == 0 || IL_INST_ID === "0") && !DEVMODE) {
455 return array("type" => self::EXPORT_NO_INST_ID, "id" => $a_fallback_id);
456 }
457
458 // same installation?
459 if ($inst_id == IL_INST_ID) {
460 // still existing?
462 return array("type" => self::EXPORT_ID_ILIAS_LOCAL, "id" => $id);
463 }
464 // not found
465 else {
466 return array("type" => self::EXPORT_ID_ILIAS_LOCAL_INVALID, "id" => $a_fallback_id);
467 }
468 }
469 // different installation
470 else {
472 // matching type?
473 if ($id && ilObject::_lookupType($id) === $type) {
474 return array("type" => self::EXPORT_ID_ILIAS_REMOTE, "id" => $id);
475 }
476 // not found
477 else {
478 return array("type" => self::EXPORT_ID_ILIAS_REMOTE_INVALID, "id" => $a_fallback_id);
479 }
480 }
481 } else {
482 // external id
484 if ($id) {
485 return array("type" => self::EXPORT_ID, "id" => $id);
486 } else {
487 return array("type" => self::EXPORT_ID_INVALID, "id" => $a_fallback_id);
488 }
489 }
490 }
491
495 public function importRecord(
496 string $a_entity,
497 array $a_types,
498 array $a_rec,
499 ilImportMapping $a_mapping,
500 string $a_schema_version
501 ): void {
502 }
503
504 protected function stripTags(array $rec, array $omit_keys = []): array
505 {
506 $ret_rec = [];
507 foreach ($rec as $k => $v) {
508 if (in_array($k, $omit_keys, true)) {
509 $ret_rec[$k] = $v;
510 } else {
511 $ret_rec[$k] = ilUtil::stripSlashes($v);
512 }
513 }
514 return $ret_rec;
515 }
516}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static return function(ContainerConfigurator $containerConfigurator)
Definition: basic_rector.php:9
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const EXPORT_ID_ILIAS_REMOTE_INVALID
getTypes(string $a_entity, string $a_version)
Get (abstract) types for (abstract) field names.
getXMLEntityName(string $a_entity, string $a_version)
Get entity name for xml (may be overwritten)
importRecord(string $a_entity, array $a_types, array $a_rec, ilImportMapping $a_mapping, string $a_schema_version)
Needs to be overwritten for import use case.
convertToLeadingUpper(string $a_str)
Make xyz_abc a XyzAbc string.
addRecordsXml(ilXmlWriter $a_writer, array $a_prefixes, string $a_entity, string $a_schema_version, array $a_ids, ?string $a_field="")
getXmlTypes(string $a_entity, string $a_version)
Get xml types.
const EXPORT_ID_ILIAS_LOCAL_INVALID
getNamespaces(array &$namespaces, string $a_entity, string $a_schema_version)
string $ds_prefix
string $current_installation_id
readData(string $a_entity, string $a_version, array $a_ids)
Read data from DB.
getDirectDataFromQuery(string $a_query, bool $a_convert_to_leading_upper=true, bool $a_set=true)
Get data from query.This is a standard procedure, all db field names are directly mapped to abstract ...
setDSPrefix(string $a_val)
init(string $a_entity, string $a_schema_version)
Init.
parseObjectExportId(string $a_id, ?string $a_fallback_id=null)
Parse export id.
createObjectExportId(string $a_type, string $a_id)
Build ilias export id.
const EXPORT_ID_INVALID
addTypesXml(ilXmlWriter $a_writer, string $a_entity, string $a_schema_version)
getCurrentInstallationId()
ilDBInterface $db
getXmlRepresentation(string $a_entity, string $a_schema_version, ?array $a_ids, string $a_field="", bool $a_omit_header=false, bool $a_omit_types=false)
Get xml representation <dataset install_id="123" install_url="..."> <types entity="table_name" versio...
string $schema_version
const EXPORT_NO_INST_ID
getDependencies(string $a_entity, string $a_version, ?array $a_rec=null, ?array $a_ids=null)
const EXPORT_ID_ILIAS_REMOTE
getXmlNamespace(string $a_entity, string $a_schema_version)
getXMLEntityTag(string $a_entity, string $a_schema_version)
Get entity tag.
getSupportedVersions()
string $version
setExportDirectories(string $a_relative, string $a_absolute)
ilLogger $ds_log
setImport(ilSurveyImporter $a_val)
afterXmlRecordWriting(string $a_entity, string $a_version, array $a_set)
setImportDirectory(string $a_val)
ilSurveyImporter $import
const EXPORT_ID_ILIAS_LOCAL
setCurrentInstallationId(string $a_val)
string $absolute_export_dir
string $entity
getXmlRecord(string $a_entity, string $a_version, array $a_set)
Get xml record for version.
string $import_directory
string $relative_export_dir
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getLogger(string $a_component_id)
Get component logger.
Component logger with individual log levels by component id.
static _lookupType(int $id, bool $reference=false)
static _getIdForImportId(string $import_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlEndTag(string $tag)
Writes an endtag.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
$c
Definition: cli.php:38
const IL_INST_ID
Definition: constants.php:40
for( $i=6;$i< 13;$i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
return['3gp', '7z', 'ai', 'aif', 'aifc', 'aiff', 'au', 'arw', 'avi', 'backup', 'bak', 'bas', 'bpmn', 'bpmn2', 'bmp', 'bib', 'bibtex', 'bz', 'bz2', 'c', 'c++', 'cc', 'cct', 'cdf', 'cer', 'class', 'cls', 'conf', 'cpp', 'crt', 'crs', 'crw', 'cr2', 'css', 'cst', 'csv', 'cur', 'db', 'dcr', 'des', 'dng', 'doc', 'docx', 'dot', 'dotx', 'dtd', 'dvi', 'el', 'eps', 'epub', 'f', 'f77', 'f90', 'flv', 'for', 'g3', 'gif', 'gl', 'gan', 'ggb', 'gsd', 'gsm', 'gtar', 'gz', 'gzip', 'h', 'hpp', 'htm', 'html', 'htmls', 'ibooks', 'ico', 'ics', 'ini', 'ipynb', 'java', 'jbf', 'jpeg', 'jpg', 'js', 'jsf', 'jso', 'json', 'latex', 'lang', 'less', 'log', 'lsp', 'ltx', 'm1v', 'm2a', 'm2v', 'm3u', 'm4a', 'm4v', 'markdown', 'm', 'mat', 'md', 'mdl', 'mdown', 'mid', 'min', 'midi', 'mobi', 'mod', 'mov', 'movie', 'mp2', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'mph', 'mpga', 'mpp', 'mpt', 'mpv', 'mpx', 'mv', 'mw', 'mv4', 'nb', 'nbp', 'nef', 'nif', 'niff', 'obj', 'obm', 'odt', 'ods', 'odp', 'odg', 'odf', 'oga', 'ogg', 'ogv', 'old', 'p', 'pas', 'pbm', 'pcl', 'pct', 'pcx', 'pdf', 'pgm', 'pic', 'pict', 'png', 'por', 'pov', 'project', 'properties', 'ppa', 'ppm', 'pps', 'ppsx', 'ppt', 'pptx', 'ppz', 'ps', 'psd', 'pwz', 'qt', 'qtc', 'qti', 'qtif', 'r', 'ra', 'ram', 'rar', 'rast', 'rda', 'rev', 'rexx', 'ris', 'rf', 'rgb', 'rm', 'rmd', 'rmi', 'rmm', 'rmp', 'rt', 'rtf', 'rtx', 'rv', 's', 's3m', 'sav', 'sbs', 'sec', 'sdml', 'sgm', 'sgml', 'smi', 'smil', 'srt', 'sps', 'spv', 'stl', 'svg', 'swa', 'swf', 'swz', 'tar', 'tex', 'texi', 'texinfo', 'text', 'tgz', 'tif', 'tiff', 'ttf', 'txt', 'tmp', 'uvproj', 'vdf', 'vimeo', 'viv', 'vivo', 'vrml', 'vsdx', 'wav', 'webm', 'wmv', 'wmx', 'wmz', 'woff', 'wwd', 'xhtml', 'xif', 'xls', 'xlsx', 'xmind', 'xml', 'xsl', 'xsd', 'zip']
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:64
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
Class ChatMainBarProvider \MainMenu\Provider.
$type