3declare(strict_types=1);
30 protected string $target_encoding =
'UTF-8';
31 protected array $whitelist = array();
32 protected array $blacklist = array();
33 protected array $tables = array();
34 protected array $filter = array();
39 protected array $allowed_attributes = [];
40 protected array $fields = [];
54 $this->allowed_attributes =
$ilDB->getAllowedAttributes();
61 public static function lookupAbstractedTables(): array
66 $query =
"SELECT DISTINCT(table_name) FROM abstraction_progress ";
70 $names[] = $row->table_name;
76 $abs_tables = array_merge($names, array(
78 'acc_user_access_key',
81 'qpl_question_orderinghorizontal',
82 'qpl_question_fileupload',
85 'style_template_class',
89 'page_editor_settings',
100 'qpl_qst_fileupload',
104 'qpl_qst_javaapplet',
110 'qpl_qst_textsubset',
142 public function setTargetEncoding(
string $a_encoding): void
144 $this->target_encoding = $a_encoding;
147 public function getTargetEncoding(): string
149 return $this->target_encoding;
157 public function setBlackList(array $a_blacklist): void
159 $this->blacklist = $a_blacklist;
162 public function getBlackList(): array
164 return $this->blacklist;
173 public function setWhiteList(array $a_whitelist): void
175 $this->whitelist = $a_whitelist;
178 public function getWhiteList(): array
180 return $this->whitelist;
183 public function setFilter(
string $a_filter,
string $a_value): void
185 $this->
filter[$a_filter] = $a_value;
191 public function getTables(): array
193 return $this->tables = $this->manager->listTables();
199 public function checkProcessing(
string $a_table): bool
202 if (in_array($a_table, $this->blacklist,
true)) {
207 if (count($this->whitelist) > 0 && !in_array($a_table, $this->whitelist,
true)) {
217 protected function openFile(
string $a_path)
220 $file = fopen($a_path,
'wb');
221 $start .=
"\t" .
'global $ilDB;' .
"\n\n";
222 fwrite($file, $start);
230 protected function closeFile($fp): void
239 public function buildDBGenerationScript(
string $a_filename =
""): void
241 if (@is_dir($a_filename)) {
250 if ($a_filename !==
"" && !$is_dir) {
251 $file = fopen($a_filename,
'wb');
253 $start =
'<?php' .
"\n" .
'function setupILIASDatabase()' .
"\n{\n";
254 $start .=
"\t" .
'global $ilDB;' .
"\n\n";
255 fwrite($file, $start);
260 foreach ($this->getTables() as $table) {
261 if ($this->checkProcessing($table)) {
262 if ($a_filename !==
"") {
267 $file = $this->openFile(
$path .
'/' . $table);
271 $this->buildCreateTableStatement($table, $file);
274 $this->buildAddPrimaryKeyStatement($table, $file);
277 $this->buildAddIndexStatements($table, $file);
280 $this->buildAddUniqueConstraintStatements($table, $file);
283 $this->buildCreateSequenceStatement($table, $file);
285 if (in_array($table, array(
'usr_session_stats',
'usr_session_raw',
'il_plugin'))) {
291 $this->buildInsertStatement($table,
$path);
292 #$this->buildInsertStatementsXML($table,$path);
294 $this->buildInsertStatements($table, $file);
298 $this->closeFile($file);
300 } elseif ($a_filename !==
"") {
301 echo
"<br><b>missing: " . $table .
"</b>";
307 $this->buildSingularSequenceStatement($file);
309 if ($a_filename ===
"") {
311 } elseif (!$is_dir) {
313 $ok = fwrite($file, $end);
323 public function buildCreateTableStatement(
string $a_table, $a_file =
null): void
325 $fields = $this->analyzer->getFieldInformation($a_table,
true);
327 $create_st =
"\n\n//\n// " . $a_table .
"\n//\n";
328 $create_st .=
'$fields = array (' .
"\n";
330 foreach ($fields as
$f => $def) {
331 $create_st .=
"\t" . $f_sep .
'"' .
$f .
'" => array (' .
"\n";
334 foreach ($def as $k => $v) {
335 if ($k !==
"nativetype" && $k !==
"alt_types" && $k !==
"autoincrement" && !is_null($v)) {
340 $v = $v ?
"true" :
"false";
351 $create_st .=
"\t\t" . $a_sep .
'"' . $k .
'" => ' . $v .
"\n";
355 $create_st .=
"\t" .
')' .
"\n";
357 $create_st .=
');' .
"\n";
358 $create_st .=
'$ilDB->createTable("' . $a_table .
'", $fields);' .
"\n";
360 if ($a_file ===
null) {
363 fwrite($a_file, $create_st);
370 public function buildAddPrimaryKeyStatement(
string $a_table, $a_file =
null): void
372 $pk = $this->analyzer->getPrimaryKeyInformation($a_table);
374 if (is_array($pk[
"fields"]) && count($pk[
"fields"]) > 0) {
375 $pk_st =
"\n" .
'$pk_fields = array(';
377 foreach ($pk[
"fields"] as
$f => $pos) {
378 $pk_st .= $sep .
'"' .
$f .
'"';
382 $pk_st .=
'$ilDB->addPrimaryKey("' . $a_table .
'", $pk_fields);' .
"\n";
384 if ($a_file ===
null) {
387 fwrite($a_file, $pk_st);
395 public function buildAddIndexStatements(
string $a_table, $a_file =
null): void
397 $ind = $this->analyzer->getIndicesInformation($a_table,
true);
399 if (is_array($ind)) {
400 foreach ($ind as
$i) {
401 if (
$i[
"fulltext"]) {
406 $in_st =
"\n" .
'$in_fields = array(';
408 foreach (
$i[
"fields"] as
$f => $pos) {
409 $in_st .= $sep .
'"' .
$f .
'"';
413 $in_st .=
'$ilDB->addIndex("' . $a_table .
'", $in_fields, "' .
$i[
"name"] .
'"' . $ft .
');' .
"\n";
415 if ($a_file ===
null) {
418 fwrite($a_file, $in_st);
427 private function printOrWrite(
string $string, $file_handle =
null): void
429 if ($file_handle ===
null) {
432 fwrite($file_handle, $string);
439 public function buildAddUniqueConstraintStatements(
string $a_table, $file_handle =
null): void
441 $con = $this->analyzer->getConstraintsInformation($a_table,
true);
443 if (is_array($con)) {
445 foreach ($con as
$i) {
446 $in_st =
"\n" .
'$in_fields = array(';
448 foreach (
$i[
"fields"] as
$f => $pos) {
449 $in_st .= $sep .
'"' .
$f .
'"';
453 $in_st .=
'$ilDB->addUniqueConstraint("' . $a_table .
'", $in_fields, "' .
$i[
"name"] .
'");' .
"\n";
455 $this->printOrWrite($in_st, $file_handle);
463 public function buildCreateSequenceStatement(
string $a_table, $file_handle =
null): void
465 $seq = $this->analyzer->hasSequence($a_table);
466 if ($seq !==
false) {
467 $seq_st =
"\n" .
'$ilDB->createSequence("' . $a_table .
'", ' . (
int) $seq .
');' .
"\n";
469 $this->printOrWrite($seq_st, $file_handle);
476 public function buildSingularSequenceStatement($file_handle =
null): void
478 $r = $this->manager->listSequences();
480 foreach ($r as $seq) {
481 if (!in_array($seq, $this->tables,
true)) {
483 if ($seq ===
"sahs_sc13_seq") {
487 $create_st =
"\n" .
'$ilDB->createSequence("' . $seq .
'");' .
"\n";
489 $this->printOrWrite($create_st, $file_handle);
497 public function buildInsertStatement(
string $a_table,
string $a_basedir): bool
500 $ilLogger =
$DIC->logger()->root();
502 $ilLogger->log(
'Starting export of:' . $a_table);
504 $set = $this->il_db->query(
"SELECT * FROM " . $this->il_db->quoteIdentifier($a_table));
509 $concurrentDirectory = $a_basedir .
'/' . $a_table .
'_inserts',
510 fileperms($a_basedir)
511 ) && !is_dir($concurrentDirectory)) {
512 throw new \RuntimeException(sprintf(
'Directory "%s" was not created', $concurrentDirectory));
516 while ($rec = $this->il_db->fetchAssoc($set)) {
518 foreach ($rec as
$f => $v) {
519 if ($this->
fields[
$f][
'type'] ===
'text' && $this->
fields[
$f][
'length'] >= 1000) {
520 $v = $this->shortenText($a_table,
$f, $v, $this->
fields[
$f][
'length']);
529 $rows[$a_table][$row++] = $values;
532 $ilLogger->log(
'Writing insert statements after 1000 lines...');
533 $fp = fopen($a_basedir .
'/' . $a_table .
'_inserts/' . $filenum++ .
'.data',
'wb');
534 fwrite($fp, serialize(
$rows));
542 $fp = fopen($a_basedir .
'/' . $a_table .
'_inserts/' . $filenum++ .
'.data',
'wb');
543 fwrite($fp, serialize(
$rows) .
"\n");
547 $ilLogger->log(
'Finished export of: ' . $a_table);
548 if (function_exists(
'memory_get_usage')) {
549 $ilLogger->log(
'Memory usage: ' . memory_get_usage(
true));
558 public function buildInsertStatements(
string $a_table, $file_handle =
null): void
560 if ($a_table ===
"lng_data") {
564 $set = $this->il_db->query(
"SELECT * FROM " . $this->il_db->quoteIdentifier($a_table));
566 while ($rec = $this->il_db->fetchAssoc($set)) {
571 foreach ($rec as
$f => $v) {
572 $v = str_replace(
'\\',
'\\\\', $v);
573 $i_str[] =
"'" .
$f .
"' => array('" . $this->
fields[
$f][
"type"] .
"', '" . str_replace(
579 $ins_st =
"\n" .
'$ilDB->insert("' . $a_table .
'", array(' .
"\n";
580 $ins_st .= implode(
", ", $i_str) .
"));\n";
582 $this->printOrWrite($ins_st, $file_handle);
590 protected function shortenText(
string $table,
string $field,
string $a_value,
int $a_size): string
593 $ilLogger =
$DIC->logger()->root();
595 if ($this->getTargetEncoding() ===
'UTF-8') {
599 $shortened = mb_convert_encoding($a_value, $this->getTargetEncoding(),
'UTF-8');
603 $shortened = mb_convert_encoding($shortened,
'UTF-8', $this->getTargetEncoding());
605 if (strlen($a_value) != strlen($shortened)) {
606 $ilLogger->log(
'Table : ' . $table);
607 $ilLogger->log(
'Field : ' . $field);
608 $ilLogger->log(
'Type : ' . $this->
fields[$field][
'type']);
609 $ilLogger->log(
'Length : ' . $this->
fields[$field][
'length']);
610 $ilLogger->log(
'Before : ' . $a_value);
611 $ilLogger->log(
'Shortened : ' . $shortened);
612 $ilLogger->log(
'Strlen Before: ' . strlen($a_value));
613 $ilLogger->log(
'Strlen After : ' . strlen($shortened));
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static shortenText(string $a_string, int $a_start_pos, int $a_num_bytes, string $a_encoding='UTF-8')
Shorten text to the given number of bytes.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc