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
|
|
|
* Database manager for XOOPS |
14
|
|
|
* |
15
|
|
|
* PHP version 5.3 |
16
|
|
|
* |
17
|
|
|
* @category Xoops\Class\Database\Manager |
18
|
|
|
* @package Manager |
19
|
|
|
* @author Haruki Setoyama <[email protected]> |
20
|
|
|
* @copyright 2013 XOOPS Project (http://xoops.org) |
21
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
22
|
|
|
* @version Release: 2.6 |
23
|
|
|
* @link http://xoops.org |
24
|
|
|
* @since 2.6.0 |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
class XoopsDatabaseManager |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $s_tables = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $f_tables = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var XoopsDatabase |
41
|
|
|
*/ |
42
|
|
|
public $db; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
public $successStrings = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
public $failureStrings = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
*Construct declaration |
56
|
|
|
*/ |
57
|
1 |
|
public function __construct() |
58
|
|
|
{ |
59
|
1 |
|
$xoops = Xoops::getInstance(); |
60
|
1 |
|
$xoops->db(); |
61
|
1 |
|
global $xoopsDB; |
|
|
|
|
62
|
1 |
|
$this->db = $xoopsDB; |
63
|
1 |
|
$this->db->setPrefix(\XoopsBaseConfig::get('db-prefix')); |
64
|
1 |
|
$this->successStrings = array( |
65
|
|
|
'create' => XoopsLocale::SF_TABLE_CREATED, |
66
|
|
|
'insert' => XoopsLocale::SF_ENTRIES_INSERTED_TO_TABLE, |
67
|
|
|
'alter' => XoopsLocale::SF_TABLE_UPDATED, |
68
|
|
|
'drop' => XoopsLocale::SF_TABLE_DROPPED, |
69
|
|
|
); |
70
|
1 |
|
$this->failureStrings = array( |
71
|
|
|
'create' => XoopsLocale::EF_TABLE_NOT_CREATED, |
72
|
|
|
'insert' => XoopsLocale::EF_ENTRIES_NOT_INSERTED_TO_TABLE, |
73
|
|
|
'alter' => XoopsLocale::EF_TABLE_NOT_UPDATED, |
74
|
|
|
'drop' => XoopsLocale::EF_TABLE_NOT_DROPPED, |
75
|
|
|
); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Is the database connectable? |
80
|
|
|
* |
81
|
|
|
* @return bool is it connectable? |
82
|
|
|
*/ |
83
|
|
|
public function isConnectable() |
84
|
|
|
{ |
85
|
|
|
return ($this->db->connect(false) != false) ? true : false; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Checks if a database exists |
90
|
|
|
* |
91
|
|
|
* @return bool returns if exists |
92
|
|
|
*/ |
93
|
|
|
public function dbExists() |
94
|
|
|
{ |
95
|
|
|
return ($this->db->connect() != false) ? true : false; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* creates a database table |
100
|
|
|
* |
101
|
|
|
* @return bool return if successful |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function createDB() |
104
|
|
|
{ |
105
|
|
|
$this->db->connect(false); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$result = $this->db->query("CREATE DATABASE " . \XoopsBaseConfig::get('db-name')); |
|
|
|
|
108
|
|
|
|
109
|
|
|
return ($result != false) ? true : false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Loads a query from a file |
114
|
|
|
* |
115
|
|
|
* @param string $sql_file_path name of file to read |
116
|
|
|
* @param bool $force weither to force the query or not |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function queryFromFile($sql_file_path, $force = false) |
121
|
|
|
{ |
122
|
|
|
if (!XoopsLoad::fileExists($sql_file_path)) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
$queryFunc = (bool)$force ? "queryF" : "query"; |
126
|
|
|
$sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path))); |
127
|
|
|
SqlUtility::splitMySqlFile($pieces, $sql_query); |
128
|
|
|
$this->db->connect(); |
|
|
|
|
129
|
|
|
foreach ($pieces as $piece) { |
130
|
|
|
$piece = trim($piece); |
131
|
|
|
// [0] contains the prefixed query |
132
|
|
|
// [4] contains unprefixed table name |
133
|
|
|
$prefixed_query = SqlUtility::prefixQuery($piece, $this->db->prefix()); |
|
|
|
|
134
|
|
|
if ($prefixed_query != false) { |
135
|
|
|
$table = $this->db->prefix($prefixed_query[4]); |
|
|
|
|
136
|
|
|
if ($prefixed_query[1] === 'CREATE TABLE') { |
137
|
|
View Code Duplication |
if ($this->db->$queryFunc($prefixed_query[0]) != false) { |
138
|
|
|
if (!isset($this->s_tables['create'][$table])) { |
139
|
|
|
$this->s_tables['create'][$table] = 1; |
140
|
|
|
} |
141
|
|
|
} else { |
142
|
|
|
if (!isset($this->f_tables['create'][$table])) { |
143
|
|
|
$this->f_tables['create'][$table] = 1; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} else { |
147
|
|
|
if ($prefixed_query[1] === 'INSERT INTO') { |
148
|
|
View Code Duplication |
if ($this->db->$queryFunc($prefixed_query[0]) != false) { |
149
|
|
|
if (!isset($this->s_tables['insert'][$table])) { |
150
|
|
|
$this->s_tables['insert'][$table] = 1; |
151
|
|
|
} else { |
152
|
|
|
$this->s_tables['insert'][$table]++; |
153
|
|
|
} |
154
|
|
|
} else { |
155
|
|
|
if (!isset($this->f_tables['insert'][$table])) { |
156
|
|
|
$this->f_tables['insert'][$table] = 1; |
157
|
|
|
} else { |
158
|
|
|
$this->f_tables['insert'][$table]++; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} else { |
162
|
|
|
if ($prefixed_query[1] === 'ALTER TABLE') { |
163
|
|
View Code Duplication |
if ($this->db->$queryFunc($prefixed_query[0]) != false) { |
164
|
|
|
if (!isset($this->s_tables['alter'][$table])) { |
165
|
|
|
$this->s_tables['alter'][$table] = 1; |
166
|
|
|
} |
167
|
|
|
} else { |
168
|
|
|
if (!isset($this->s_tables['alter'][$table])) { |
169
|
|
|
$this->f_tables['alter'][$table] = 1; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
View Code Duplication |
} else { |
173
|
|
|
if ($prefixed_query[1] === 'DROP TABLE') { |
174
|
|
|
if ($this->db->$queryFunc('DROP TABLE ' . $table) != false) { |
175
|
|
|
if (!isset($this->s_tables['drop'][$table])) { |
176
|
|
|
$this->s_tables['drop'][$table] = 1; |
177
|
|
|
} |
178
|
|
|
} else { |
179
|
|
|
if (!isset($this->s_tables['drop'][$table])) { |
180
|
|
|
$this->f_tables['drop'][$table] = 1; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
return true; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* returns a report |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
View Code Duplication |
public function report() |
198
|
|
|
{ |
199
|
|
|
$commands = array('create', 'insert', 'alter', 'drop'); |
200
|
|
|
$content = '<ul class="log">'; |
201
|
|
|
foreach ($commands as $cmd) { |
202
|
|
|
if (!@empty($this->s_tables[$cmd])) { |
203
|
|
|
foreach ($this->s_tables[$cmd] as $key => $val) { |
204
|
|
|
$content .= '<li class="success">'; |
205
|
|
|
$content .= ($cmd !== 'insert') |
206
|
|
|
? sprintf($this->successStrings[$cmd], $key) |
207
|
|
|
: sprintf($this->successStrings[$cmd], $val, $key); |
208
|
|
|
$content .= "</li>\n"; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
foreach ($commands as $cmd) { |
213
|
|
|
if (!@empty($this->f_tables[$cmd])) { |
214
|
|
|
foreach ($this->f_tables[$cmd] as $key => $val) { |
215
|
|
|
$content .= '<li class="failure">'; |
216
|
|
|
$content .= ($cmd !== 'insert') |
217
|
|
|
? sprintf($this->failureStrings[$cmd], $key) |
218
|
|
|
: sprintf($this->failureStrings[$cmd], $val, $key); |
219
|
|
|
$content .= "</li>\n"; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
$content .= '</ul>'; |
224
|
|
|
return $content; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* runs a query |
229
|
|
|
* |
230
|
|
|
* @param string $sql sql statement to perform |
231
|
|
|
* |
232
|
|
|
* @return resource |
233
|
|
|
*/ |
234
|
|
|
public function query($sql) |
235
|
|
|
{ |
236
|
|
|
$this->db->connect(); |
|
|
|
|
237
|
|
|
return $this->db->query($sql); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Setup prefix table |
242
|
|
|
* |
243
|
|
|
* @param string $table table to prefix |
244
|
|
|
* |
245
|
|
|
* @return string prefixed table |
246
|
|
|
*/ |
247
|
|
|
public function prefix($table) |
248
|
|
|
{ |
249
|
|
|
$this->db->connect(); |
|
|
|
|
250
|
|
|
return $this->db->prefix($table); |
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* fetches an array |
255
|
|
|
* |
256
|
|
|
* @param string $ret resource that was returned from query |
257
|
|
|
* |
258
|
|
|
* @return array returns the array |
259
|
|
|
*/ |
260
|
|
|
public function fetchArray($ret) |
261
|
|
|
{ |
262
|
|
|
$this->db->connect(); |
|
|
|
|
263
|
|
|
return $this->db->fetchArray($ret); |
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Inserts into a table |
268
|
|
|
* |
269
|
|
|
* @param string $table table to insert into |
270
|
|
|
* @param string $query query to use to insert |
271
|
|
|
* |
272
|
|
|
* @return bool|void |
273
|
|
|
*/ |
274
|
|
View Code Duplication |
public function insert($table, $query) |
275
|
|
|
{ |
276
|
|
|
$this->db->connect(); |
|
|
|
|
277
|
|
|
$table = $this->db->prefix($table); |
|
|
|
|
278
|
|
|
$query = 'INSERT INTO ' . $table . ' ' . $query; |
279
|
|
|
if (!$this->db->queryF($query)) { |
|
|
|
|
280
|
|
|
if (!isset($this->f_tables['insert'][$table])) { |
281
|
|
|
$this->f_tables['insert'][$table] = 1; |
282
|
|
|
} else { |
283
|
|
|
$this->f_tables['insert'][$table]++; |
284
|
|
|
} |
285
|
|
|
return false; |
286
|
|
|
} else { |
287
|
|
|
if (!isset($this->s_tables['insert'][$table])) { |
288
|
|
|
$this->s_tables['insert'][$table] = 1; |
289
|
|
|
} else { |
290
|
|
|
$this->s_tables['insert'][$table]++; |
291
|
|
|
} |
292
|
|
|
return $this->db->getInsertId(); |
|
|
|
|
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* return the error |
298
|
|
|
* |
299
|
|
|
* @return bool |
300
|
|
|
*/ |
301
|
|
|
public function isError() |
302
|
|
|
{ |
303
|
|
|
return (isset($this->f_tables)) ? true : false; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Deletes tables |
308
|
|
|
* |
309
|
|
|
* No callers found in core. foreach loop is suspect, using key and value for |
310
|
|
|
* a list of tables? No docs or examples about what it is supposed to do. |
311
|
|
|
* |
312
|
|
|
* @param array $tables table to delete |
313
|
|
|
* |
314
|
|
|
* @return array list of dropped tables |
315
|
|
|
*/ |
316
|
|
View Code Duplication |
public function deleteTables($tables) |
317
|
|
|
{ |
318
|
|
|
$deleted = array(); |
319
|
|
|
$this->db->connect(); |
|
|
|
|
320
|
|
|
foreach ($tables as $key => $val) { |
321
|
|
|
//was: if (!$this->db->query("DROP TABLE " . $this->db->prefix($key))) { |
322
|
|
|
if (!$this->db->query("DROP TABLE " . $this->db->prefix($val))) { |
|
|
|
|
323
|
|
|
$deleted[] = $val; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
return $deleted; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Checks to see if table exists |
331
|
|
|
* |
332
|
|
|
* @param string $table name of database table looking for |
333
|
|
|
* |
334
|
|
|
* @return bool true if exists or false if doesnt |
335
|
|
|
*/ |
336
|
|
View Code Duplication |
public function tableExists($table) |
337
|
|
|
{ |
338
|
|
|
$table = trim($table); |
339
|
|
|
$ret = false; |
340
|
|
|
if ($table != '') { |
341
|
|
|
$this->db->connect(); |
|
|
|
|
342
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix($table); |
|
|
|
|
343
|
|
|
$ret = (false != $this->db->query($sql)) ? true : false; |
|
|
|
|
344
|
|
|
} |
345
|
|
|
return $ret; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* This method allows to copy fields from one table to another |
350
|
|
|
* |
351
|
|
|
* @param array $fieldsMap Map of the fields |
352
|
|
|
* ex: array('oldfieldname' => 'newfieldname'); |
353
|
|
|
* @param string $oTableName Old Table |
354
|
|
|
* @param string $nTableName New Table |
355
|
|
|
* @param bool $dropTable Drop old Table |
356
|
|
|
* |
357
|
|
|
* @return this does not return anything |
358
|
|
|
*/ |
359
|
|
|
public function copyFields( |
360
|
|
|
$fieldsMap, |
361
|
|
|
$oTableName, |
362
|
|
|
$nTableName, |
363
|
|
|
$dropTable = false |
364
|
|
|
) { |
365
|
|
|
$sql = "SHOW COLUMNS FROM " . $this->db->prefix($oTableName); |
|
|
|
|
366
|
|
|
$result = $this->db->queryF($sql); |
|
|
|
|
367
|
|
|
if (($rows = $this->db->getRowsNum($result)) == count($fieldsMap)) { |
|
|
|
|
368
|
|
|
$sql = "SELECT * FROM " . $this->db->prefix($oTableName); |
|
|
|
|
369
|
|
|
$result = $this->db->queryF($sql); |
|
|
|
|
370
|
|
|
while ($myrow = $this->db->fetchArray($result)) { |
|
|
|
|
371
|
|
|
ksort($fieldsMap); |
372
|
|
|
ksort($myrow); |
373
|
|
|
$sql = "INSERT INTO `" . $this->db->prefix($nTableName) |
|
|
|
|
374
|
|
|
. "` " . "(`" . implode("`,`", $fieldsMap) . "`)" . |
375
|
|
|
" VALUES ('" . implode("','", $myrow) . "')"; |
376
|
|
|
|
377
|
|
|
$this->db->queryF($sql); |
|
|
|
|
378
|
|
|
} |
379
|
|
|
if ($dropTable) { |
380
|
|
|
$sql = "DROP TABLE " . $this->db->prefix($oTableName); |
|
|
|
|
381
|
|
|
$this->db->queryF($sql); |
|
|
|
|
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state