1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: 24: 25: 26: 27: 28:
29: class Maintenance
30: {
31: var $db;
32: var $prefix;
33:
34: 35: 36:
37: function Maintenance()
38: {
39: global $xoopsDB;
40: $db = $xoopsDB;
41: $this->db = $db;
42: $this->prefix = $this->db->prefix . '_';
43: }
44:
45: 46: 47: 48: 49: 50:
51: function displayTables($array = true)
52: {
53: $tables = array();
54: $result = $this->db->queryF('SHOW TABLES');
55: while ($myrow = $this->db->fetchArray($result)) {
56: $value = array_values($myrow);
57: $value = substr($value[0], 5);
58: $tables[$value] = $value;
59: }
60: if ($array = true) {
61: return $tables;
62: } else {
63: return join(',', $tables);
64: }
65: }
66:
67: 68: 69: 70: 71: 72: 73:
74: function dump_table_structure($table, $drop)
75: {
76: $sql_text = '';
77: $verif = false;
78: $result = $this->db->queryF('SHOW create table `' . $table . '`;');
79: if ($result) {
80: if ($row = $this->db->fetchArray($result)) {
81: $sql_text .= "# Table structure for table `" . $table . "` \n\n";
82: if ($drop == 1) {
83: $sql_text .= "DROP TABLE IF EXISTS `" . $table . "`;\n\n";
84: }
85: $verif = true;
86: $sql_text .= $row['Create Table'] . ";\n\n";
87: }
88: }
89: $this->db->freeRecordSet($result);
90: $ret['sql_text'] = $sql_text;
91: $ret['structure'] = $verif;
92: return $ret;
93: }
94:
95: 96: 97: 98: 99: 100:
101: function dump_table_datas($table)
102: {
103: $sql_text = '';
104: $count = 0;
105: $result = $this->db->queryF('SELECT * FROM ' . $table . ';');
106: if ($result) {
107: $num_rows = $this->db->getRowsNum($result);
108: $num_fields = $this->db->getFieldsNum($result);
109:
110: if ($num_rows > 0) {
111: $field_type = array();
112: $i = 0;
113: while ($i < $num_fields) {
114: $meta = mysql_fetch_field($result, $i);
115: array_push($field_type, $meta->type);
116: ++$i;
117: }
118:
119: $sql_text .= "INSERT INTO `" . $table . "` values\n";
120: $index = 0;
121: while ($row = $this->db->fetchRow($result)) {
122: ++$count;
123: $sql_text .= "(";
124: for ($i = 0; $i < $num_fields; ++$i) {
125: if (is_null($row[$i])) {
126: $sql_text .= "null";
127: } else {
128: switch ($field_type[$i]) {
129: case 'int':
130: $sql_text .= $row[$i];
131: break;
132: default:
133: $sql_text .= "'" . mysql_real_escape_string($row[$i]) . "'";
134: }
135: }
136: if ($i < $num_fields - 1) {
137: $sql_text .= ",";
138: }
139: }
140: $sql_text .= ")";
141:
142: if ($index < $num_rows - 1) {
143: $sql_text .= ",";
144: } else {
145: $sql_text .= ";";
146: }
147: $sql_text .= "\n";
148: ++$index;
149: }
150: }
151: }
152: $this->db->freeRecordSet($result);
153: $ret['sql_text'] = $sql_text . "\n\n";
154: $ret['records'] = $count;
155: return $ret;
156: }
157:
158: 159: 160: 161: 162: 163:
164: function dump_write($sql_text)
165: {
166: $file_name = "dump_" . date("Y.m.d") . "_" . date("H.i.s") . ".sql";
167: $path_file = "../dump/" . $file_name;
168: if (file_put_contents($path_file, $sql_text)) {
169: $write = true;
170: } else {
171: $write = false;
172: }
173: $ret['file_name'] = $file_name;
174: $ret['write'] = $write;
175: return $ret;
176: }
177: }
178: