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
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* maintenance extensions |
14
|
|
|
* |
15
|
|
|
* @package Maintenance |
16
|
|
|
* @author Mage Grégory (AKA Mage), Cointin Maxime (AKA Kraven30) |
17
|
|
|
* @copyright 2000-2016 XOOPS Project (http://xoops.org) |
18
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
19
|
|
|
* @link http://xoops.org |
20
|
|
|
*/ |
21
|
|
|
class Maintenance |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var XoopsMySQLDatabase |
25
|
|
|
*/ |
26
|
|
|
public $db; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
public $prefix; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$db = \XoopsDatabaseFactory::getDatabaseConnection(); |
39
|
|
|
$this->db = $db; |
40
|
|
|
$this->prefix = $this->db->prefix . '_'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Display Tables |
45
|
|
|
* |
46
|
|
|
* @param bool $array true to return as array, false to return as comma delimited string |
47
|
|
|
* |
48
|
|
|
* @return array|string list of tables |
49
|
|
|
*/ |
50
|
|
|
public function displayTables($array = true) |
51
|
|
|
{ |
52
|
|
|
$tables = array(); |
53
|
|
|
$result = $this->db->queryF('SHOW TABLES'); |
|
|
|
|
54
|
|
|
while ($myrow = $this->db->fetchArray($result)) { |
|
|
|
|
55
|
|
|
$value = array_values($myrow); |
56
|
|
|
$value = substr($value[0], strlen($this->prefix)); |
57
|
|
|
$tables[$value] = $value; |
58
|
|
|
} |
59
|
|
|
if ($array) { |
60
|
|
|
return $tables; |
61
|
|
|
} else { |
62
|
|
|
return implode(',', $tables); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Dump table structure |
68
|
|
|
* |
69
|
|
|
* @param string $table table name |
70
|
|
|
* @param int $drop 1 to include drop table if exists |
71
|
|
|
* |
72
|
|
|
* @return array 'ret[sql_text] = dump, ret[structure] = display structure |
73
|
|
|
*/ |
74
|
|
|
public function dumpTableStructure($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
|
|
|
* Dump table data |
97
|
|
|
* |
98
|
|
|
* @param string $table table name |
99
|
|
|
* |
100
|
|
|
* @return array 'ret[sql_text] = dump, ret[records] = display records |
101
|
|
|
*/ |
102
|
|
|
public function dumpTableData($table) |
103
|
|
|
{ |
104
|
|
|
$sql_text = ''; |
105
|
|
|
$count = 0; |
106
|
|
|
$result = $this->db->queryF('SELECT * FROM ' . $table . ';'); |
|
|
|
|
107
|
|
|
if ($result) { |
108
|
|
|
$num_rows = $this->db->getRowsNum($result); |
|
|
|
|
109
|
|
|
$num_fields = $this->db->getFieldsNum($result); |
|
|
|
|
110
|
|
|
|
111
|
|
|
if ($num_rows > 0) { |
112
|
|
|
$field_type = array(); |
113
|
|
|
$i = 0; |
114
|
|
|
while ($i < $num_fields) { |
115
|
|
|
$field_type[] = $this->db->getFieldType($result, $i); |
|
|
|
|
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 .= "'" . $this->db->escape($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
|
|
|
* Dump Write |
160
|
|
|
* |
161
|
|
|
* @param string $sql_text SQL to write |
162
|
|
|
* |
163
|
|
|
* @return array 'ret[file_name] = file name, ret[write] = write |
164
|
|
|
*/ |
165
|
|
|
public function dump_write($sql_text) |
166
|
|
|
{ |
167
|
|
|
$file_name = "dump_" . date("Y.m.d") . "_" . date("H.i.s") . ".sql"; |
168
|
|
|
$path_file = "../dump/" . $file_name; |
169
|
|
|
if (file_put_contents($path_file, $sql_text)) { |
170
|
|
|
$write = true; |
171
|
|
|
} else { |
172
|
|
|
$write = false; |
173
|
|
|
} |
174
|
|
|
$ret['file_name'] = $file_name; |
|
|
|
|
175
|
|
|
$ret['write'] = $write; |
176
|
|
|
return $ret; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.