19 protected $target_encoding =
'UTF-8';
23 protected $whitelist = array();
27 protected $blacklist = array();
31 protected $tables = array();
35 protected $filter = array();
51 $this->allowed_attributes =
$ilDB->getAllowedAttributes();
58 public static function lookupAbstractedTables()
63 $query =
"SELECT DISTINCT(table_name) FROM abstraction_progress ";
67 $names[] = $row->table_name;
73 $abs_tables = array_merge($names, array(
75 'acc_user_access_key',
78 'qpl_question_orderinghorizontal',
79 'qpl_question_fileupload',
82 'style_template_class',
86 'page_editor_settings',
101 'qpl_qst_javaapplet',
107 'qpl_qst_textsubset',
146 public function setTargetEncoding($a_encoding)
148 $this->target_encoding = $a_encoding;
157 public function getTargetEncoding()
159 return $this->target_encoding;
169 public function setBlackList($a_blacklist)
171 $this->blacklist = $a_blacklist;
180 public function getBlackList()
182 return $this->blacklist;
193 public function setWhiteList($a_whitelist)
195 $this->whitelist = $a_whitelist;
204 public function getWhiteList()
206 return $this->whitelist;
214 public function setFilter($a_filter, $a_value)
216 $this->filter[$a_filter] = $a_value;
223 public function getTables()
225 $r = $this->manager->listTables();
228 return $this->tables;
235 public function checkProcessing($a_table)
238 if (in_array($a_table, $this->blacklist)) {
243 if (count($this->whitelist) > 0 && !in_array($a_table, $this->whitelist)) {
251 protected function openFile($a_path)
254 $file = fopen($a_path,
"w");
255 $start .=
"\t" .
'global $ilDB;' .
"\n\n";
256 fwrite($file, $start);
261 $file = fopen($a_path,
"w");
262 $start =
'<?php' .
"\n" .
'function setupILIASDatabase()' .
"\n{\n";
263 $start .=
"\t" .
'global $ilDB;' .
"\n\n";
264 fwrite($file, $start);
270 protected function closeFile($fp)
290 public function buildDBGenerationScript($a_filename =
"")
292 if (@is_dir($a_filename)) {
296 $isDirectory =
false;
301 if ($a_filename !=
"" and !$isDirectory) {
302 $file = fopen($a_filename,
"w");
304 $start =
'<?php' .
"\n" .
'function setupILIASDatabase()' .
"\n{\n";
305 $start .=
"\t" .
'global $ilDB;' .
"\n\n";
306 fwrite($file, $start);
307 } elseif ($isDirectory) {
315 foreach ($this->tables as $table) {
316 if ($this->checkProcessing($table)) {
317 if ($a_filename !=
"") {
322 $file = $this->openFile($path .
'/' . $table);
326 $this->buildCreateTableStatement($table, $file);
329 $this->buildAddPrimaryKeyStatement($table, $file);
332 $this->buildAddIndexStatements($table, $file);
335 $this->buildAddUniqueConstraintStatements($table, $file);
338 $this->buildCreateSequenceStatement($table, $file);
340 if (in_array($table, array(
'usr_session_stats',
'usr_session_raw'))) {
346 $this->buildInsertStatement($table, $path);
347 #$this->buildInsertStatementsXML($table,$path);
349 $this->buildInsertStatements($table, $file);
353 $this->closeFile($file);
356 if ($a_filename !=
"") {
357 echo
"<br><b>missing: " . $table .
"</b>";
364 $this->buildSingularSequenceStatement($file);
366 if ($a_filename ==
"") {
368 } elseif (!$isDirectory) {
370 $ok = fwrite($file, $end);
383 public function buildCreateTableStatement($a_table, $a_file =
"")
385 $fields = $this->analyzer->getFieldInformation($a_table,
true);
387 $create_st =
"\n\n//\n// " . $a_table .
"\n//\n";
388 $create_st .=
'$fields = array (' .
"\n";
390 foreach ($fields as
$f => $def) {
391 $create_st .=
"\t" . $f_sep .
'"' .
$f .
'" => array (' .
"\n";
394 foreach ($def as $k => $v) {
395 if ($k !=
"nativetype" && $k !=
"alt_types" && $k !=
"autoincrement" && !is_null($v)) {
400 $v = $v ?
"true" :
"false";
411 $create_st .=
"\t\t" . $a_sep .
'"' . $k .
'" => ' . $v .
"\n";
415 $create_st .=
"\t" .
')' .
"\n";
417 $create_st .=
');' .
"\n";
418 $create_st .=
'$ilDB->createTable("' . $a_table .
'", $fields);' .
"\n";
423 fwrite($a_file, $create_st);
434 public function buildAddPrimaryKeyStatement($a_table, $a_file =
"")
436 $pk = $this->analyzer->getPrimaryKeyInformation($a_table);
438 if (is_array($pk[
"fields"]) && count($pk[
"fields"]) > 0) {
439 $pk_st =
"\n" .
'$pk_fields = array(';
441 foreach ($pk[
"fields"] as
$f => $pos) {
442 $pk_st .= $sep .
'"' .
$f .
'"';
446 $pk_st .=
'$ilDB->addPrimaryKey("' . $a_table .
'", $pk_fields);' .
"\n";
451 fwrite($a_file, $pk_st);
463 public function buildAddIndexStatements($a_table, $a_file =
"")
465 $ind = $this->analyzer->getIndicesInformation($a_table,
true);
467 if (is_array($ind)) {
468 foreach ($ind as
$i) {
469 if (
$i[
"fulltext"]) {
474 $in_st =
"\n" .
'$in_fields = array(';
476 foreach (
$i[
"fields"] as
$f => $pos) {
477 $in_st .= $sep .
'"' .
$f .
'"';
481 $in_st .=
'$ilDB->addIndex("' . $a_table .
'", $in_fields, "' .
$i[
"name"] .
'"' . $ft .
');' .
"\n";
486 fwrite($a_file, $in_st);
499 public function buildAddUniqueConstraintStatements($a_table, $a_file =
"")
501 $con = $this->analyzer->getConstraintsInformation($a_table,
true);
503 if (is_array($con)) {
504 foreach ($con as
$i) {
505 $in_st =
"\n" .
'$in_fields = array(';
507 foreach (
$i[
"fields"] as
$f => $pos) {
508 $in_st .= $sep .
'"' .
$f .
'"';
512 $in_st .=
'$ilDB->addUniqueConstraint("' . $a_table .
'", $in_fields, "' .
$i[
"name"] .
'");' .
"\n";
517 fwrite($a_file, $in_st);
530 public function buildCreateSequenceStatement($a_table, $a_file =
"")
532 $seq = $this->analyzer->hasSequence($a_table);
533 if ($seq !==
false) {
534 $seq_st =
"\n" .
'$ilDB->createSequence("' . $a_table .
'", ' . (int) $seq .
');' .
"\n";
539 fwrite($a_file, $seq_st);
550 public function buildSingularSequenceStatement($a_file =
"")
552 $r = $this->manager->listSequences();
554 foreach ($r as $seq) {
555 if (!in_array($seq, $this->tables)) {
557 if ($seq ==
"sahs_sc13_seq") {
561 $create_st =
"\n" .
'$ilDB->createSequence("' . $seq .
'");' .
"\n";
566 fwrite($a_file, $create_st);
580 public function buildInsertStatement($a_table, $a_basedir)
583 $ilLogger =
$DIC->logger()->root();
585 $ilLogger->log(
'Starting export of:' . $a_table);
587 $set = $this->il_db->query(
"SELECT * FROM " . $this->il_db->quoteIdentifier($a_table));
591 mkdir($a_basedir .
'/' . $a_table .
'_inserts', fileperms($a_basedir));
594 while ($rec = $this->il_db->fetchAssoc($set)) {
596 foreach ($rec as
$f => $v) {
597 if ($this->
fields[
$f][
'type'] ==
'text' and $this->
fields[
$f][
'length'] >= 1000) {
598 $v = $this->shortenText($a_table,
$f, $v, $this->
fields[
$f][
'length']);
607 $rows[$a_table][$row++] = $values;
610 $ilLogger->log(
'Writing insert statements after 1000 lines...');
611 $fp = fopen($a_basedir .
'/' . $a_table .
'_inserts/' . $filenum++ .
'.data',
'w');
612 fwrite($fp, serialize((array)
$rows));
620 $fp = fopen($a_basedir .
'/' . $a_table .
'_inserts/' . $filenum++ .
'.data',
'w');
621 fwrite($fp, serialize((array)
$rows) .
"\n");
625 $ilLogger->log(
'Finished export of: ' . $a_table);
626 if (function_exists(
'memory_get_usage')) {
627 $ilLogger->log(
'Memory usage: ' . memory_get_usage(
true));
640 public function buildInsertStatementsXML($a_table, $a_basedir)
643 $w->xmlStartTag(
'Table', array(
'name' => $a_table ));
645 $set = $this->il_db->query(
"SELECT * FROM " . $this->il_db->quoteIdentifier($a_table));
648 while ($rec = $this->il_db->fetchAssoc($set)) {
649 #$ilLog->write('Num: '.$num++);
650 $w->xmlStartTag(
'Row');
655 foreach ($rec as
$f => $v) {
656 if ($this->
fields[
$f][
'type'] ==
'text' and $this->
fields[
$f][
'length'] >= 1000) {
657 $v = $this->shortenText($a_table,
$f, $v, $this->
fields[
$f][
'length']);
660 $w->xmlElement(
'Value', array(
662 'type' => $this->
fields[
$f][
'type'],
666 $w->xmlEndTag(
'Row');
668 $w->xmlEndTag(
'Table');
670 $w->xmlDumpFile($a_basedir .
'/' . $a_table .
'.xml',
false);
680 public function buildInsertStatements($a_table, $a_file =
"")
682 if ($a_table ==
"lng_data") {
686 $set = $this->il_db->query(
"SELECT * FROM " . $this->il_db->quoteIdentifier($a_table));
689 while ($rec = $this->il_db->fetchAssoc($set)) {
694 foreach ($rec as
$f => $v) {
696 $types[] =
'"' . $this->
fields[
$f][
"type"] .
'"';
697 $v = str_replace(
'\\',
'\\\\', $v);
698 $values[] =
"'" . str_replace(
"'",
"\'", $v) .
"'";
699 $i_str[] =
"'" .
$f .
"' => array('" . $this->
fields[
$f][
"type"] .
"', '" . str_replace(
"'",
"\'", $v) .
"')";
701 $fields_str =
"(" . implode(
",", $fields) .
")";
702 $types_str =
"array(" . implode(
",", $types) .
")";
703 $values_str =
"array(" . implode(
",", $values) .
")";
704 $ins_st =
"\n" .
'$ilDB->insert("' . $a_table .
'", array(' .
"\n";
705 $ins_st .= implode(
", ", $i_str) .
"));\n";
713 fwrite($a_file, $ins_st);
725 public function getHTMLOverview($a_filename =
"")
731 foreach ($this->tables as $table) {
732 if ($this->checkProcessing($table)) {
734 if ($this->addTableToOverview($table,
$tpl, $cnt)) {
740 $tpl->setVariable(
"TXT_TITLE",
"ILIAS Abstract DB Tables (" .
ILIAS_VERSION .
")");
742 if ($a_filename ==
"") {
751 public function addTableToOverview($a_table, $a_tpl, $a_cnt)
753 $fields = $this->analyzer->getFieldInformation($a_table);
754 $indices = $this->analyzer->getIndicesInformation($a_table);
755 $constraints = $this->analyzer->getConstraintsInformation($a_table);
756 $pk = $this->analyzer->getPrimaryKeyInformation($a_table);
757 $auto = $this->analyzer->getAutoIncrementField($a_table);
758 $has_sequence = $this->analyzer->hasSequence($a_table);
761 if (isset($this->filter[
"has_sequence"])) {
762 if ((!$has_sequence && $auto ==
"" && $this->filter[
"has_sequence"])
763 || (($has_sequence || $auto !=
"") && !$this->filter[
"has_sequence"])
770 $indices_output =
false;
771 if (is_array($indices) && count($indices) > 0 && !$this->filter[
"skip_indices"]) {
772 foreach ($indices as
$index => $def) {
774 foreach ($def[
"fields"] as
$f => $pos) {
777 $a_tpl->setCurrentBlock(
"index");
778 $a_tpl->setVariable(
"VAL_INDEX", $def[
"name"]);
779 $a_tpl->setVariable(
"VAL_FIELDS", implode(
", ", $f2));
780 $a_tpl->parseCurrentBlock();
781 $indices_output =
true;
783 $a_tpl->setCurrentBlock(
"index_table");
784 $a_tpl->parseCurrentBlock();
788 $constraints_output =
false;
789 if (is_array($constraints) && count($constraints) > 0 && !$this->filter[
"skip_constraints"]) {
790 foreach ($constraints as
$index => $def) {
792 foreach ($def[
"fields"] as
$f => $pos) {
795 $a_tpl->setCurrentBlock(
"constraint");
796 $a_tpl->setVariable(
"VAL_CONSTRAINT", $def[
"name"]);
797 $a_tpl->setVariable(
"VAL_CTYPE", $def[
"type"]);
798 $a_tpl->setVariable(
"VAL_CFIELDS", implode(
", ", $f2));
799 $a_tpl->parseCurrentBlock();
800 $constraints_output =
true;
802 $a_tpl->setCurrentBlock(
"constraint_table");
803 $a_tpl->parseCurrentBlock();
807 $fields_output =
false;
808 foreach ($fields as $field => $def) {
810 if (isset($this->filter[
"alt_types"])) {
811 if (($def[
"alt_types"] ==
"" && $this->filter[
"alt_types"])
812 || ($def[
"alt_types"] !=
"" && !$this->filter[
"alt_types"])
817 if (isset($this->filter[
"type"])) {
818 if ($def[
"type"] != $this->filter[
"type"]) {
822 if (isset($this->filter[
"nativetype"])) {
823 if ($def[
"nativetype"] != $this->filter[
"nativetype"]) {
827 if (isset($this->filter[
"unsigned"])) {
828 if ($def[
"unsigned"] != $this->filter[
"unsigned"]) {
833 $a_tpl->setCurrentBlock(
"field");
834 if (empty($pk[
"fields"][$field])) {
835 $a_tpl->setVariable(
"VAL_FIELD", strtolower($field));
837 $a_tpl->setVariable(
"VAL_FIELD",
"<u>" . strtolower($field) .
"</u>");
839 $a_tpl->setVariable(
"VAL_TYPE", $def[
"type"]);
840 $a_tpl->setVariable(
"VAL_LENGTH", (!is_null($def[
"length"])) ? $def[
"length"] :
" ");
842 if (strtolower($def[
"default"]) ==
"current_timestamp") {
844 unset($def[
"default"]);
847 $a_tpl->setVariable(
"VAL_DEFAULT", (!is_null($def[
"default"])) ? $def[
"default"] :
" ");
848 $a_tpl->setVariable(
"VAL_NOT_NULL", (!is_null($def[
"notnull"])) ? (($def[
"notnull"]) ?
"true" :
"false") :
" ");
849 $a_tpl->setVariable(
"VAL_FIXED", (!is_null($def[
"fixed"])) ? (($def[
"fixed"]) ?
"true" :
"false") :
" ");
850 $a_tpl->setVariable(
"VAL_UNSIGNED", (!is_null($def[
"unsigned"])) ? (($def[
"unsigned"]) ?
"true" :
"false") :
" ");
851 $a_tpl->setVariable(
"VAL_ALTERNATIVE_TYPES", ($def[
"alt_types"] !=
"") ? $def[
"alt_types"] :
" ");
852 $a_tpl->setVariable(
"VAL_NATIVETYPE", ($def[
"nativetype"] !=
"") ? $def[
"nativetype"] :
" ");
853 $a_tpl->parseCurrentBlock();
854 $fields_output =
true;
857 if ($fields_output) {
858 $a_tpl->setCurrentBlock(
"field_table");
859 $a_tpl->parseCurrentBlock();
863 if ($indices_output || $fields_output || $constraints_output) {
864 $a_tpl->setCurrentBlock(
"table");
865 $a_tpl->setVariable(
"TXT_TABLE_NAME", strtolower($a_table));
866 if ($has_sequence || $auto !=
"") {
867 $a_tpl->setVariable(
"TXT_SEQUENCE",
"Has Sequence");
869 $a_tpl->setVariable(
"TXT_SEQUENCE",
"No Sequence");
871 $a_tpl->setVariable(
"VAL_CNT", (
int) $a_cnt);
872 $a_tpl->parseCurrentBlock();
890 protected function shortenText($table, $field, $a_value, $a_size)
893 $ilLogger =
$DIC->logger()->root();
895 if ($this->getTargetEncoding() ==
'UTF-8') {
899 $shortened = mb_convert_encoding($a_value, $this->getTargetEncoding(),
'UTF-8');
903 $shortened = mb_convert_encoding($shortened,
'UTF-8', $this->getTargetEncoding());
905 if (strlen($a_value) != strlen($shortened)) {
906 $ilLogger->log(
'Table : ' . $table);
907 $ilLogger->log(
'Field : ' . $field);
908 $ilLogger->log(
'Type : ' . $this->
fields[$field][
'type']);
909 $ilLogger->log(
'Length : ' . $this->
fields[$field][
'length']);
910 $ilLogger->log(
'Before : ' . $a_value);
911 $ilLogger->log(
'Shortened : ' . $shortened);
912 $ilLogger->log(
'Strlen Before: ' . strlen($a_value));
913 $ilLogger->log(
'Strlen After : ' . strlen($shortened));
An exception for terminatinating execution or to throw for unit testing.
This class gives all kind of DB information using the database manager and reverse module.
special template class to simplify handling of ITX/PEAR
static shortenText($a_string, $a_start_pos, $a_num_bytes, $a_encoding='UTF-8')
Shorten text to the given number of bytes.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
foreach($_POST as $key=> $value) $res
if(isset($_FILES['img_file']['size']) && $_FILES['img_file']['size'] > 0) $tpl