1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* database manager for XOOPS installer |
4
|
|
|
* |
5
|
|
|
* You may not change or alter any portion of this comment or credits |
6
|
|
|
* of supporting developers from this source code or any supporting source code |
7
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
8
|
|
|
* This program is distributed in the hope that it will be useful, |
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11
|
|
|
* |
12
|
|
|
* @copyright The XOOPS Project http://sourceforge.net/projects/xoops/ |
13
|
|
|
* @license GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html) |
14
|
|
|
* @package upgrader |
15
|
|
|
* @author Haruki Setoyama <[email protected]> |
16
|
|
|
* @version $Id$ |
17
|
|
|
* @access public |
18
|
|
|
**/ |
19
|
|
|
|
20
|
|
|
include_once XOOPS_ROOT_PATH . '/class/database/databasefactory.php'; |
21
|
|
|
include_once XOOPS_ROOT_PATH . '/class/database/' . XOOPS_DB_TYPE . 'database.php'; |
22
|
|
|
include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Database manager |
26
|
|
|
* |
27
|
|
|
* @copyright copyright (c) 2000 XOOPS.org |
28
|
|
|
* @package upgrader |
29
|
|
|
*/ |
30
|
|
|
class db_manager |
31
|
|
|
{ |
32
|
|
|
public $s_tables = array(); |
33
|
|
|
public $f_tables = array(); |
34
|
|
|
public $db; |
35
|
|
|
|
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->db = XoopsDatabaseFactory::getDatabase(); |
|
|
|
|
39
|
|
|
$this->db->setPrefix(XOOPS_DB_PREFIX); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function isConnectable() |
43
|
|
|
{ |
44
|
|
|
return ($this->db->connect(false) != false) ? true : false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function dbExists() |
48
|
|
|
{ |
49
|
|
|
return ($this->db->connect() != false) ? true : false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function createDB() |
53
|
|
|
{ |
54
|
|
|
$this->db->connect(false); |
55
|
|
|
|
56
|
|
|
$result = $this->db->query("CREATE DATABASE " . XOOPS_DB_NAME); |
57
|
|
|
|
58
|
|
|
return ($result != false) ? true : false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function queryFromFile($sql_file_path) |
62
|
|
|
{ |
63
|
|
|
$tables = array(); |
|
|
|
|
64
|
|
|
|
65
|
|
|
if (!file_exists($sql_file_path)) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
$sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path))); |
|
|
|
|
69
|
|
|
SqlUtility::splitMySqlFile($pieces, $sql_query); |
70
|
|
|
$this->db->connect(); |
71
|
|
|
foreach ($pieces as $piece) { |
72
|
|
|
$piece = trim($piece); |
73
|
|
|
// [0] contains the prefixed query |
74
|
|
|
// [4] contains unprefixed table name |
75
|
|
|
$prefixed_query = SqlUtility::prefixQuery($piece, $this->db->prefix()); |
76
|
|
|
if ($prefixed_query != false) { |
77
|
|
|
$table = $this->db->prefix($prefixed_query[4]); |
78
|
|
|
if ($prefixed_query[1] == 'CREATE TABLE') { |
79
|
|
|
if ($this->db->query($prefixed_query[0]) != false) { |
80
|
|
|
if (!isset($this->s_tables['create'][$table])) { |
81
|
|
|
$this->s_tables['create'][$table] = 1; |
82
|
|
|
} |
83
|
|
|
} else { |
84
|
|
|
if (!isset($this->f_tables['create'][$table])) { |
85
|
|
|
$this->f_tables['create'][$table] = 1; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} elseif ($prefixed_query[1] == 'INSERT INTO') { |
89
|
|
|
if ($this->db->query($prefixed_query[0]) != false) { |
90
|
|
|
if (!isset($this->s_tables['insert'][$table])) { |
91
|
|
|
$this->s_tables['insert'][$table] = 1; |
92
|
|
|
} else { |
93
|
|
|
$this->s_tables['insert'][$table]++; |
94
|
|
|
} |
95
|
|
|
} else { |
96
|
|
|
if (!isset($this->f_tables['insert'][$table])) { |
97
|
|
|
$this->f_tables['insert'][$table] = 1; |
98
|
|
|
} else { |
99
|
|
|
$this->f_tables['insert'][$table]++; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} elseif ($prefixed_query[1] == 'ALTER TABLE') { |
103
|
|
|
if ($this->db->query($prefixed_query[0]) != false) { |
104
|
|
|
if (!isset($this->s_tables['alter'][$table])) { |
105
|
|
|
$this->s_tables['alter'][$table] = 1; |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
if (!isset($this->s_tables['alter'][$table])) { |
109
|
|
|
$this->f_tables['alter'][$table] = 1; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} elseif ($prefixed_query[1] == 'DROP TABLE') { |
113
|
|
|
if ($this->db->query('DROP TABLE '.$table) != false) { |
114
|
|
|
if (!isset($this->s_tables['drop'][$table])) { |
115
|
|
|
$this->s_tables['drop'][$table] = 1; |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
if (!isset($this->s_tables['drop'][$table])) { |
119
|
|
|
$this->f_tables['drop'][$table] = 1; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public $successStrings = array( |
129
|
|
|
'create' => 'create', |
130
|
|
|
'insert' => 'insert', |
131
|
|
|
'alter' => 'alter', |
132
|
|
|
'drop' => 'drop', |
133
|
|
|
); |
134
|
|
|
public $failureStrings = array( |
135
|
|
|
'create' => 'fail', |
136
|
|
|
'insert' => 'fail', |
137
|
|
|
'alter' => 'error', |
138
|
|
|
'drop' => 'error', |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
public function report() |
143
|
|
|
{ |
144
|
|
|
$commands = array( 'create', 'insert', 'alter', 'drop' ); |
145
|
|
|
$content = '<ul class="log">'; |
146
|
|
|
foreach ($commands as $cmd) { |
147
|
|
|
if (!@empty($this->s_tables[$cmd])) { |
148
|
|
|
foreach ($this->s_tables[$cmd] as $key => $val) { |
149
|
|
|
$content .= '<li class="success">'; |
150
|
|
|
$content .= ($cmd != 'insert') ? sprintf($this->successStrings[$cmd], $key) : sprintf($this->successStrings[$cmd], $val, $key); |
151
|
|
|
$content .= "</li>\n"; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
foreach ($commands as $cmd) { |
156
|
|
|
if (!@empty($this->f_tables[$cmd])) { |
157
|
|
|
foreach ($this->f_tables[$cmd] as $key => $val) { |
158
|
|
|
$content .= '<li class="failure">'; |
159
|
|
|
$content .= ($cmd != 'insert') ? sprintf($this->failureStrings[$cmd], $key) : sprintf($this->failureStrings[$cmd], $val, $key); |
160
|
|
|
$content .= "</li>\n"; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
$content .= '</ul>'; |
165
|
|
|
return $content; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function query($sql) |
169
|
|
|
{ |
170
|
|
|
$this->db->connect(); |
171
|
|
|
return $this->db->query($sql); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function prefix($table) |
175
|
|
|
{ |
176
|
|
|
$this->db->connect(); |
177
|
|
|
return $this->db->prefix($table); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function fetchArray($ret) |
181
|
|
|
{ |
182
|
|
|
$this->db->connect(); |
183
|
|
|
return $this->db->fetchArray($ret); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function insert($table, $query) |
187
|
|
|
{ |
188
|
|
|
$this->db->connect(); |
189
|
|
|
$table = $this->db->prefix($table); |
190
|
|
|
$query = 'INSERT INTO ' . $table . ' '. $query; |
191
|
|
|
if (!$this->db->queryF($query)) { |
192
|
|
|
if (!isset($this->f_tables['insert'][$table])) { |
193
|
|
|
$this->f_tables['insert'][$table] = 1; |
194
|
|
|
} else { |
195
|
|
|
$this->f_tables['insert'][$table]++; |
196
|
|
|
} |
197
|
|
|
return false; |
198
|
|
|
} else { |
199
|
|
|
if (!isset($this->s_tables['insert'][$table])) { |
200
|
|
|
$this->s_tables['insert'][$table] = 1; |
201
|
|
|
} else { |
202
|
|
|
$this->s_tables['insert'][$table]++; |
203
|
|
|
} |
204
|
|
|
return $this->db->getInsertId(); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function isError() |
209
|
|
|
{ |
210
|
|
|
return (isset($this->f_tables)) ? true : false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function deleteTables($tables) |
214
|
|
|
{ |
215
|
|
|
$deleted = array(); |
216
|
|
|
$this->db->connect(); |
217
|
|
|
foreach ($tables as $key => $val) { |
218
|
|
|
if (!$this->db->query("DROP TABLE " . $this->db->prefix($key))) { |
219
|
|
|
$deleted[] = $va; |
|
|
|
|
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
return $deleted; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function tableExists($table) |
226
|
|
|
{ |
227
|
|
|
$table = trim($table); |
228
|
|
|
$ret = false; |
229
|
|
|
if ($table != '') { |
230
|
|
|
$this->db->connect(); |
231
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix($table); |
232
|
|
|
$ret = (false != $this->db->query($sql)) ? true : false; |
233
|
|
|
} |
234
|
|
|
return $ret; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.