XOOPS  2.6.0
maintenance.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
30 {
31  var $db;
32  var $prefix;
33 
37  function Maintenance()
38  {
39  global $xoopsDB;
40  $db = $xoopsDB;
41  $this->db = $db;
42  $this->prefix = $this->db->prefix . '_';
43  }
44 
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 
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 
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 
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 }
$i
Definition: dialog.php:68
$result
Definition: pda.php:33
$index
Definition: menu.php:41
dump_table_structure($table, $drop)
Definition: maintenance.php:74
dump_table_datas($table)
dump_write($sql_text)
global $xoopsDB
Definition: common.php:36
displayTables($array=true)
Definition: maintenance.php:51