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: class XoopsDatabaseManager
28: {
29: 30: 31:
32: private $s_tables = array();
33:
34: 35: 36:
37: private $f_tables = array();
38:
39: 40: 41:
42: public $db;
43:
44: 45: 46:
47: public $successStrings = array();
48:
49: 50: 51:
52: public $failureStrings = array();
53:
54: 55: 56:
57: public function __construct()
58: {
59: $xoops = Xoops::getInstance();
60: $xoops->db();
61: global $xoopsDB;
62: $this->db = $xoopsDB;
63: $this->db->setPrefix(\XoopsBaseConfig::get('db-prefix'));
64: $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: $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: }
77:
78: 79: 80: 81: 82:
83: public function isConnectable()
84: {
85: return ($this->db->connect(false) != false) ? true : false;
86: }
87:
88: 89: 90: 91: 92:
93: public function dbExists()
94: {
95: return ($this->db->connect() != false) ? true : false;
96: }
97:
98: 99: 100: 101: 102:
103: 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: 114: 115: 116: 117: 118: 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:
132:
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: 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: 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: 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: } 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: 194: 195: 196:
197: 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: 229: 230: 231: 232: 233:
234: public function query($sql)
235: {
236: $this->db->connect();
237: return $this->db->query($sql);
238: }
239:
240: 241: 242: 243: 244: 245: 246:
247: public function prefix($table)
248: {
249: $this->db->connect();
250: return $this->db->prefix($table);
251: }
252:
253: 254: 255: 256: 257: 258: 259:
260: public function fetchArray($ret)
261: {
262: $this->db->connect();
263: return $this->db->fetchArray($ret);
264: }
265:
266: 267: 268: 269: 270: 271: 272: 273:
274: 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: 298: 299: 300:
301: public function isError()
302: {
303: return (isset($this->f_tables)) ? true : false;
304: }
305:
306: 307: 308: 309: 310: 311: 312: 313: 314: 315:
316: public function deleteTables($tables)
317: {
318: $deleted = array();
319: $this->db->connect();
320: foreach ($tables as $key => $val) {
321:
322: if (!$this->db->query("DROP TABLE " . $this->db->prefix($val))) {
323: $deleted[] = $val;
324: }
325: }
326: return $deleted;
327: }
328:
329: 330: 331: 332: 333: 334: 335:
336: 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: 350: 351: 352: 353: 354: 355: 356: 357: 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: