28 $this->manager = $ilDB->db->loadModule(
'Manager');
29 $this->reverse = $ilDB->db->loadModule(
'Reverse');
31 include_once(
"./Services/Database/classes/class.ilDBAnalyzer.php");
34 $this->allowed_attributes = $ilDB->getAllowedAttributes();
41 $query =
"SELECT DISTINCT(table_name) FROM abstraction_progress ";
46 $names[] =
$row->table_name;
52 $abs_tables = array_merge($names, array(
54 'acc_user_access_key',
57 'qpl_question_orderinghorizontal',
58 'qpl_question_fileupload',
62 'style_template_class',
66 'page_editor_settings',
127 $this->target_encoding = $a_encoding;
147 $this->blacklist = $a_blacklist;
169 $this->whitelist = $a_whitelist;
187 $this->filter[$a_filter] = $a_value;
195 $r = $this->manager->listTables();
209 if (in_array($a_table, $this->blacklist))
215 if (count($this->whitelist) > 0 && !in_array($a_table, $this->whitelist))
227 $file = fopen($a_path,
"w");
228 $start.=
"\t".
'global $ilDB;'.
"\n\n";
229 fwrite(
$file, $start);
233 $file = fopen($a_path,
"w");
234 $start =
'<?php'.
"\n".
'function setupILIASDatabase()'.
"\n{\n";
235 $start.=
"\t".
'global $ilDB;'.
"\n\n";
236 fwrite(
$file, $start);
264 $isDirectory =
false;
265 if(@is_dir($a_filename))
272 $isDirectory =
false;
277 if ($a_filename !=
"" and !$isDirectory)
279 $file = fopen($a_filename,
"w");
281 $start =
'<?php'.
"\n".
'function setupILIASDatabase()'.
"\n{\n";
282 $start.=
"\t".
'global $ilDB;'.
"\n\n";
283 fwrite(
$file, $start);
296 foreach ($this->tables as $table)
300 if ($a_filename !=
"")
302 echo
"<br>$table"; flush();
326 #$this->buildInsertStatementsXML($table,$path);
341 if ($a_filename !=
"")
343 echo
"<br><b>missing: ".$table.
"</b>"; flush();
348 if ($a_filename ==
"")
352 elseif(!$isDirectory)
355 fwrite (
$file, $end);
368 $fields = $this->analyzer->getFieldInformation($a_table,
true);
370 $create_st =
"\n\n//\n// ".$a_table.
"\n//\n";
371 $create_st.=
'$fields = array ('.
"\n";
373 foreach ($fields as $f => $def)
376 $create_st.=
"\t".$f_sep.
'"'.$f.
'" => array ('.
"\n";
379 foreach ($def as $k => $v)
381 if ($k !=
"nativetype" && $k !=
"alt_types" && $k !=
"autoincrement" && !is_null($v))
388 $v = $v ?
"true" :
"false";
399 $create_st.=
"\t\t".$a_sep.
'"'.$k.
'" => '.$v.
"\n";
403 $create_st.=
"\t".
')'.
"\n";
405 $create_st.=
');'.
"\n";
406 $create_st.=
'$ilDB->createTable("'.$a_table.
'", $fields);'.
"\n";
414 fwrite($a_file, $create_st);
426 $pk = $this->analyzer->getPrimaryKeyInformation($a_table);
428 if (is_array($pk[
"fields"]) && count($pk[
"fields"]) > 0)
430 $pk_st =
"\n".
'$pk_fields = array(';
432 foreach ($pk[
"fields"] as $f => $pos)
434 $pk_st.= $sep.
'"'.$f.
'"';
438 $pk_st.=
'$ilDB->addPrimaryKey("'.$a_table.
'", $pk_fields);'.
"\n";
446 fwrite($a_file, $pk_st);
459 $ind = $this->analyzer->getIndicesInformation($a_table,
true);
473 $in_st =
"\n".
'$in_fields = array(';
475 foreach ($i[
"fields"] as $f => $pos)
477 $in_st.= $sep.
'"'.$f.
'"';
481 $in_st.=
'$ilDB->addIndex("'.$a_table.
'", $in_fields, "'.$i[
"name"].
'"'.$ft.
');'.
"\n";
489 fwrite($a_file, $in_st);
503 $seq = $this->analyzer->hasSequence($a_table);
506 $seq_st =
"\n".
'$ilDB->createSequence("'.$a_table.
'", '.(int) $seq.
');'.
"\n";
514 fwrite($a_file, $seq_st);
529 $ilLog->write(
'Starting export of:'.$a_table);
531 $set = $this->il_db->query(
"SELECT * FROM `".$a_table.
"`");
535 mkdir($a_basedir.
'/'.$a_table.
'_inserts',fileperms($a_basedir));
538 while ($rec = $this->il_db->fetchAssoc($set))
541 foreach($rec as $f => $v)
543 if($this->
fields[$f][
'type'] ==
'text' and $this->
fields[$f][
'length'] >= 1000)
549 $this->
fields[$f][
'type'],
554 $rows[$a_table][
$row++] = $values;
558 $ilLog->write(
'Writing insert statements after 1000 lines...');
559 $fp = fopen($a_basedir.
'/'.$a_table.
'_inserts/'.$filenum++.
'.data',
'w');
560 fwrite($fp,serialize((array) $rows));
570 $fp = fopen($a_basedir.
'/'.$a_table.
'_inserts/'.$filenum++.
'.data',
'w');
571 fwrite($fp,serialize((array) $rows).
"\n");
575 $ilLog->write(
'Finished export of: '.$a_table);
576 if(function_exists(
'memory_get_usage'))
578 $ilLog->write(
'Memory usage: '.memory_get_usage(
true));
593 include_once
'./Services/Xml/classes/class.ilXmlWriter.php';
595 $w->xmlStartTag(
'Table',array(
'name' => $a_table));
597 $set = $this->il_db->query(
"SELECT * FROM `".$a_table.
"`");
600 while ($rec = $this->il_db->fetchAssoc($set))
602 #$ilLog->write('Num: '.$num++);
603 $w->xmlStartTag(
'Row');
608 foreach($rec as $f => $v)
610 if($this->
fields[$f][
'type'] ==
'text' and $this->
fields[$f][
'length'] >= 1000)
619 'type' => $this->
fields[$f][
'type']
625 $w->xmlEndTag(
'Row');
627 $w->xmlEndTag(
'Table');
629 $w->xmlDumpFile($a_basedir.
'/'.$a_table.
'.xml',FALSE);
640 if ($a_table ==
"lng_data")
645 $set = $this->il_db->query(
"SELECT * FROM `".$a_table.
"`");
648 while ($rec = $this->il_db->fetchAssoc($set))
654 foreach ($rec as $f => $v)
657 $types[] =
'"'.$this->fields[$f][
"type"].
'"';
658 $v = str_replace(
'\\',
'\\\\', $v);
659 $values[] =
"'".str_replace(
"'",
"\'", $v).
"'";
660 $i_str[] =
"'".$f.
"' => array('".$this->
fields[$f][
"type"].
661 "', '".str_replace(
"'",
"\'", $v).
"')";
663 $fields_str =
"(".implode($fields,
",").
")";
664 $types_str =
"array(".implode($types,
",").
")";
665 $values_str =
"array(".implode($values,
",").
")";
666 $ins_st =
"\n".
'$ilDB->insert("'.$a_table.
'", array('.
"\n";
667 $ins_st.= implode($i_str,
", ").
"));\n";
678 fwrite($a_file, $ins_st);
691 $tpl =
new ilTemplate(
"tpl.db_overview.html",
true,
true,
"Services/Database");
695 foreach ($this->tables as $table)
709 if ($a_filename ==
"")
720 $fields = $this->analyzer->getFieldInformation($a_table);
721 $indices = $this->analyzer->getIndicesInformation($a_table);
722 $constraints = $this->analyzer->getConstraintsInformation($a_table);
723 $pk = $this->analyzer->getPrimaryKeyInformation($a_table);
724 $auto = $this->analyzer->getAutoIncrementField($a_table);
725 $has_sequence = $this->analyzer->hasSequence($a_table);
728 if (isset($this->filter[
"has_sequence"]))
730 if ((!$has_sequence && $auto ==
"" && $this->filter[
"has_sequence"]) ||
731 (($has_sequence || $auto !=
"") && !$this->filter[
"has_sequence"]))
738 $indices_output =
false;
739 if (is_array($indices) && count($indices) > 0 && !$this->filter[
"skip_indices"])
741 foreach ($indices as $index => $def)
744 foreach ($def[
"fields"] as $f => $pos)
748 $a_tpl->setCurrentBlock(
"index");
749 $a_tpl->setVariable(
"VAL_INDEX", $def[
"name"]);
750 $a_tpl->setVariable(
"VAL_FIELDS", implode($f2,
", "));
751 $a_tpl->parseCurrentBlock();
752 $indices_output =
true;
754 $a_tpl->setCurrentBlock(
"index_table");
755 $a_tpl->parseCurrentBlock();
759 $constraints_output =
false;
760 if (is_array($constraints) && count($constraints) > 0 && !$this->filter[
"skip_constraints"])
762 foreach ($constraints as $index => $def)
765 foreach ($def[
"fields"] as $f => $pos)
769 $a_tpl->setCurrentBlock(
"constraint");
770 $a_tpl->setVariable(
"VAL_CONSTRAINT", $def[
"name"]);
771 $a_tpl->setVariable(
"VAL_CTYPE", $def[
"type"]);
772 $a_tpl->setVariable(
"VAL_CFIELDS", implode($f2,
", "));
773 $a_tpl->parseCurrentBlock();
774 $constraints_output =
true;
776 $a_tpl->setCurrentBlock(
"constraint_table");
777 $a_tpl->parseCurrentBlock();
781 $fields_output =
false;
782 foreach ($fields as $field => $def)
785 if (isset($this->filter[
"alt_types"]))
787 if (($def[
"alt_types"] ==
"" && $this->filter[
"alt_types"]) ||
788 ($def[
"alt_types"] !=
"" && !$this->filter[
"alt_types"]))
793 if (isset($this->filter[
"type"]))
795 if ($def[
"type"] != $this->filter[
"type"])
800 if (isset($this->filter[
"nativetype"]))
802 if ($def[
"nativetype"] != $this->filter[
"nativetype"])
807 if (isset($this->filter[
"unsigned"]))
809 if ($def[
"unsigned"] != $this->filter[
"unsigned"])
815 $a_tpl->setCurrentBlock(
"field");
816 if (empty($pk[
"fields"][$field]))
818 $a_tpl->setVariable(
"VAL_FIELD", strtolower($field));
822 $a_tpl->setVariable(
"VAL_FIELD",
"<u>".strtolower($field).
"</u>");
824 $a_tpl->setVariable(
"VAL_TYPE", $def[
"type"]);
825 $a_tpl->setVariable(
"VAL_LENGTH", (!is_null($def[
"length"])) ? $def[
"length"] :
" ");
827 if (strtolower($def[
"default"]) ==
"current_timestamp")
830 unset($def[
"default"]);
833 $a_tpl->setVariable(
"VAL_DEFAULT", (!is_null($def[
"default"])) ? $def[
"default"] :
" ");
834 $a_tpl->setVariable(
"VAL_NOT_NULL", (!is_null($def[
"notnull"]))
835 ? (($def[
"notnull"]) ?
"true" :
"false")
837 $a_tpl->setVariable(
"VAL_FIXED", (!is_null($def[
"fixed"]))
838 ? (($def[
"fixed"]) ?
"true" :
"false")
840 $a_tpl->setVariable(
"VAL_UNSIGNED", (!is_null($def[
"unsigned"]))
841 ? (($def[
"unsigned"]) ?
"true" :
"false")
843 $a_tpl->setVariable(
"VAL_ALTERNATIVE_TYPES", ($def[
"alt_types"] !=
"") ? $def[
"alt_types"] :
" ");
844 $a_tpl->setVariable(
"VAL_NATIVETYPE", ($def[
"nativetype"] !=
"") ? $def[
"nativetype"] :
" ");
845 $a_tpl->parseCurrentBlock();
846 $fields_output =
true;
851 $a_tpl->setCurrentBlock(
"field_table");
852 $a_tpl->parseCurrentBlock();
856 if ($indices_output || $fields_output || $constraints_output)
858 $a_tpl->setCurrentBlock(
"table");
859 $a_tpl->setVariable(
"TXT_TABLE_NAME", strtolower($a_table));
860 if ($has_sequence || $auto !=
"")
862 $a_tpl->setVariable(
"TXT_SEQUENCE",
"Has Sequence");
866 $a_tpl->setVariable(
"TXT_SEQUENCE",
"No Sequence");
868 $a_tpl->setVariable(
"VAL_CNT", (
int) $a_cnt);
869 $a_tpl->parseCurrentBlock();
896 include_once
'./Services/Utilities/classes/class.ilStr.php';
899 $shortened = mb_convert_encoding($shortened,
'UTF-8',$this->
getTargetEncoding());
901 if(strlen($a_value) != strlen($shortened))
903 $ilLog->write(
'Table : '.$table);
904 $ilLog->write(
'Field : '.$field );
905 $ilLog->write(
'Type : '.$this->
fields[$field][
'type']);
906 $ilLog->write(
'Length : '.$this->
fields[$field][
'length']);
907 $ilLog->write(
'Before : '.$a_value);
908 $ilLog->write(
'Shortened : '.$shortened);
909 $ilLog->write(
'Strlen Before: '.strlen($a_value));
910 $ilLog->write(
'Strlen After : '.strlen($shortened));