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',
61 'style_template_class',
65 'page_editor_settings',
126 $this->target_encoding = $a_encoding;
146 $this->blacklist = $a_blacklist;
168 $this->whitelist = $a_whitelist;
186 $this->filter[$a_filter] = $a_value;
194 $r = $this->manager->listTables();
208 if (in_array($a_table, $this->blacklist))
214 if (count($this->whitelist) > 0 && !in_array($a_table, $this->whitelist))
226 $file = fopen($a_path,
"w");
227 $start.=
"\t".
'global $ilDB;'.
"\n\n";
228 fwrite(
$file, $start);
232 $file = fopen($a_path,
"w");
233 $start =
'<?php'.
"\n".
'function setupILIASDatabase()'.
"\n{\n";
234 $start.=
"\t".
'global $ilDB;'.
"\n\n";
235 fwrite(
$file, $start);
263 $isDirectory =
false;
264 if(@is_dir($a_filename))
271 $isDirectory =
false;
276 if ($a_filename !=
"" and !$isDirectory)
278 $file = fopen($a_filename,
"w");
280 $start =
'<?php'.
"\n".
'function setupILIASDatabase()'.
"\n{\n";
281 $start.=
"\t".
'global $ilDB;'.
"\n\n";
282 fwrite(
$file, $start);
295 foreach ($this->tables as $table)
299 if ($a_filename !=
"")
301 echo
"<br>$table"; flush();
325 #$this->buildInsertStatementsXML($table,$path);
340 if ($a_filename !=
"")
342 echo
"<br><b>missing: ".$table.
"</b>"; flush();
347 if ($a_filename ==
"")
351 elseif(!$isDirectory)
354 fwrite (
$file, $end);
367 $fields = $this->analyzer->getFieldInformation($a_table,
true);
368 $this->fields = $fields;
369 $create_st =
"\n\n//\n// ".$a_table.
"\n//\n";
370 $create_st.=
'$fields = array ('.
"\n";
372 foreach ($fields as
$f => $def)
375 $create_st.=
"\t".$f_sep.
'"'.
$f.
'" => array ('.
"\n";
378 foreach ($def as $k => $v)
380 if ($k !=
"nativetype" && $k !=
"alt_types" && $k !=
"autoincrement" && !is_null($v))
387 $v = $v ?
"true" :
"false";
398 $create_st.=
"\t\t".$a_sep.
'"'.$k.
'" => '.$v.
"\n";
402 $create_st.=
"\t".
')'.
"\n";
404 $create_st.=
');'.
"\n";
405 $create_st.=
'$ilDB->createTable("'.$a_table.
'", $fields);'.
"\n";
413 fwrite($a_file, $create_st);
425 $pk = $this->analyzer->getPrimaryKeyInformation($a_table);
427 if (is_array($pk[
"fields"]) && count($pk[
"fields"]) > 0)
429 $pk_st =
"\n".
'$pk_fields = array(';
431 foreach ($pk[
"fields"] as
$f =>
$pos)
433 $pk_st.= $sep.
'"'.
$f.
'"';
437 $pk_st.=
'$ilDB->addPrimaryKey("'.$a_table.
'", $pk_fields);'.
"\n";
445 fwrite($a_file, $pk_st);
458 $ind = $this->analyzer->getIndicesInformation($a_table,
true);
472 $in_st =
"\n".
'$in_fields = array(';
474 foreach ($i[
"fields"] as
$f =>
$pos)
476 $in_st.= $sep.
'"'.
$f.
'"';
480 $in_st.=
'$ilDB->addIndex("'.$a_table.
'", $in_fields, "'.$i[
"name"].
'"'.$ft.
');'.
"\n";
488 fwrite($a_file, $in_st);
502 $seq = $this->analyzer->hasSequence($a_table);
505 $seq_st =
"\n".
'$ilDB->createSequence("'.$a_table.
'", '.(int) $seq.
');'.
"\n";
513 fwrite($a_file, $seq_st);
528 $ilLog->write(
'Starting export of:'.$a_table);
530 $set = $this->il_db->query(
"SELECT * FROM `".$a_table.
"`");
534 mkdir($a_basedir.
'/'.$a_table.
'_inserts',fileperms($a_basedir));
537 while ($rec = $this->il_db->fetchAssoc($set))
540 foreach($rec as
$f => $v)
542 if($this->fields[
$f][
'type'] ==
'text' and $this->fields[
$f][
'length'] >= 1000)
544 $v = $this->
shortenText($a_table,
$f, $v, $this->fields[
$f][
'length']);
548 $this->fields[
$f][
'type'],
553 $rows[$a_table][
$row++] = $values;
557 $ilLog->write(
'Writing insert statements after 1000 lines...');
558 $fp = fopen($a_basedir.
'/'.$a_table.
'_inserts/'.$filenum++.
'.data',
'w');
559 fwrite($fp,serialize((array) $rows));
569 $fp = fopen($a_basedir.
'/'.$a_table.
'_inserts/'.$filenum++.
'.data',
'w');
570 fwrite($fp,serialize((array) $rows).
"\n");
574 $ilLog->write(
'Finished export of: '.$a_table);
575 if(function_exists(
'memory_get_usage'))
577 $ilLog->write(
'Memory usage: '.memory_get_usage(
true));
592 include_once
'./Services/Xml/classes/class.ilXmlWriter.php';
594 $w->xmlStartTag(
'Table',array(
'name' => $a_table));
596 $set = $this->il_db->query(
"SELECT * FROM `".$a_table.
"`");
599 while ($rec = $this->il_db->fetchAssoc($set))
601 #$ilLog->write('Num: '.$num++);
602 $w->xmlStartTag(
'Row');
607 foreach($rec as
$f => $v)
609 if($this->fields[
$f][
'type'] ==
'text' and $this->fields[
$f][
'length'] >= 1000)
611 $v = $this->
shortenText($a_table,
$f, $v, $this->fields[
$f][
'length']);
618 'type' => $this->fields[
$f][
'type']
624 $w->xmlEndTag(
'Row');
626 $w->xmlEndTag(
'Table');
628 $w->xmlDumpFile($a_basedir.
'/'.$a_table.
'.xml',FALSE);
639 if ($a_table ==
"lng_data")
644 $set = $this->il_db->query(
"SELECT * FROM `".$a_table.
"`");
647 while ($rec = $this->il_db->fetchAssoc($set))
653 foreach ($rec as
$f => $v)
656 $types[] =
'"'.$this->fields[
$f][
"type"].
'"';
657 $v = str_replace(
'\\',
'\\\\', $v);
658 $values[] =
"'".str_replace(
"'",
"\'", $v).
"'";
659 $i_str[] =
"'".$f.
"' => array('".$this->fields[
$f][
"type"].
660 "', '".str_replace(
"'",
"\'", $v).
"')";
662 $fields_str =
"(".implode($fields,
",").
")";
663 $types_str =
"array(".implode($types,
",").
")";
664 $values_str =
"array(".implode($values,
",").
")";
665 $ins_st =
"\n".
'$ilDB->insert("'.$a_table.
'", array('.
"\n";
666 $ins_st.= implode($i_str,
", ").
"));\n";
677 fwrite($a_file, $ins_st);
690 $tpl =
new ilTemplate(
"tpl.db_overview.html",
true,
true,
"Services/Database");
694 foreach ($this->tables as $table)
708 if ($a_filename ==
"")
719 $fields = $this->analyzer->getFieldInformation($a_table);
720 $indices = $this->analyzer->getIndicesInformation($a_table);
721 $constraints = $this->analyzer->getConstraintsInformation($a_table);
722 $pk = $this->analyzer->getPrimaryKeyInformation($a_table);
723 $auto = $this->analyzer->getAutoIncrementField($a_table);
724 $has_sequence = $this->analyzer->hasSequence($a_table);
727 if (isset($this->filter[
"has_sequence"]))
729 if ((!$has_sequence && $auto ==
"" && $this->filter[
"has_sequence"]) ||
730 (($has_sequence || $auto !=
"") && !$this->filter[
"has_sequence"]))
737 $indices_output =
false;
738 if (is_array($indices) && count($indices) > 0 && !$this->filter[
"skip_indices"])
740 foreach ($indices as $index => $def)
743 foreach ($def[
"fields"] as
$f =>
$pos)
747 $a_tpl->setCurrentBlock(
"index");
748 $a_tpl->setVariable(
"VAL_INDEX", $def[
"name"]);
749 $a_tpl->setVariable(
"VAL_FIELDS", implode($f2,
", "));
750 $a_tpl->parseCurrentBlock();
751 $indices_output =
true;
753 $a_tpl->setCurrentBlock(
"index_table");
754 $a_tpl->parseCurrentBlock();
758 $constraints_output =
false;
759 if (is_array($constraints) && count($constraints) > 0 && !$this->filter[
"skip_constraints"])
761 foreach ($constraints as $index => $def)
764 foreach ($def[
"fields"] as
$f =>
$pos)
768 $a_tpl->setCurrentBlock(
"constraint");
769 $a_tpl->setVariable(
"VAL_CONSTRAINT", $def[
"name"]);
770 $a_tpl->setVariable(
"VAL_CTYPE", $def[
"type"]);
771 $a_tpl->setVariable(
"VAL_CFIELDS", implode($f2,
", "));
772 $a_tpl->parseCurrentBlock();
773 $constraints_output =
true;
775 $a_tpl->setCurrentBlock(
"constraint_table");
776 $a_tpl->parseCurrentBlock();
780 $fields_output =
false;
781 foreach ($fields as $field => $def)
784 if (isset($this->filter[
"alt_types"]))
786 if (($def[
"alt_types"] ==
"" && $this->filter[
"alt_types"]) ||
787 ($def[
"alt_types"] !=
"" && !$this->filter[
"alt_types"]))
792 if (isset($this->filter[
"type"]))
794 if ($def[
"type"] != $this->filter[
"type"])
799 if (isset($this->filter[
"nativetype"]))
801 if ($def[
"nativetype"] != $this->filter[
"nativetype"])
806 if (isset($this->filter[
"unsigned"]))
808 if ($def[
"unsigned"] != $this->filter[
"unsigned"])
814 $a_tpl->setCurrentBlock(
"field");
815 if (empty($pk[
"fields"][$field]))
817 $a_tpl->setVariable(
"VAL_FIELD", strtolower($field));
821 $a_tpl->setVariable(
"VAL_FIELD",
"<u>".strtolower($field).
"</u>");
823 $a_tpl->setVariable(
"VAL_TYPE", $def[
"type"]);
824 $a_tpl->setVariable(
"VAL_LENGTH", (!is_null($def[
"length"])) ? $def[
"length"] :
" ");
826 if (strtolower($def[
"default"]) ==
"current_timestamp")
829 unset($def[
"default"]);
832 $a_tpl->setVariable(
"VAL_DEFAULT", (!is_null($def[
"default"])) ? $def[
"default"] :
" ");
833 $a_tpl->setVariable(
"VAL_NOT_NULL", (!is_null($def[
"notnull"]))
834 ? (($def[
"notnull"]) ?
"true" :
"false")
836 $a_tpl->setVariable(
"VAL_FIXED", (!is_null($def[
"fixed"]))
837 ? (($def[
"fixed"]) ?
"true" :
"false")
839 $a_tpl->setVariable(
"VAL_UNSIGNED", (!is_null($def[
"unsigned"]))
840 ? (($def[
"unsigned"]) ?
"true" :
"false")
842 $a_tpl->setVariable(
"VAL_ALTERNATIVE_TYPES", ($def[
"alt_types"] !=
"") ? $def[
"alt_types"] :
" ");
843 $a_tpl->setVariable(
"VAL_NATIVETYPE", ($def[
"nativetype"] !=
"") ? $def[
"nativetype"] :
" ");
844 $a_tpl->parseCurrentBlock();
845 $fields_output =
true;
850 $a_tpl->setCurrentBlock(
"field_table");
851 $a_tpl->parseCurrentBlock();
855 if ($indices_output || $fields_output || $constraints_output)
857 $a_tpl->setCurrentBlock(
"table");
858 $a_tpl->setVariable(
"TXT_TABLE_NAME", strtolower($a_table));
859 if ($has_sequence || $auto !=
"")
861 $a_tpl->setVariable(
"TXT_SEQUENCE",
"Has Sequence");
865 $a_tpl->setVariable(
"TXT_SEQUENCE",
"No Sequence");
867 $a_tpl->setVariable(
"VAL_CNT", (
int) $a_cnt);
868 $a_tpl->parseCurrentBlock();
895 include_once
'./Services/Utilities/classes/class.ilStr.php';
898 $shortened = mb_convert_encoding($shortened,
'UTF-8',$this->
getTargetEncoding());
900 if(strlen($a_value) != strlen($shortened))
902 $ilLog->write(
'Table : '.$table);
903 $ilLog->write(
'Field : '.$field );
904 $ilLog->write(
'Type : '.$this->fields[$field][
'type']);
905 $ilLog->write(
'Length : '.$this->fields[$field][
'length']);
906 $ilLog->write(
'Before : '.$a_value);
907 $ilLog->write(
'Shortened : '.$shortened);
908 $ilLog->write(
'Strlen Before: '.strlen($a_value));
909 $ilLog->write(
'Strlen After : '.strlen($shortened));