XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
dbmanager.php
1<?php
12
13include_once XOOPS_ROOT_PATH . '/class/logger.php';
14include_once XOOPS_ROOT_PATH . '/class/database/databasefactory.php';
15include_once XOOPS_ROOT_PATH . '/class/database/' . XOOPS_DB_TYPE . 'database.php';
16include_once XOOPS_ROOT_PATH . '/class/database/sqlutility.php';
17
18
20
21 public $s_tables = [];
22 public $f_tables = [];
23 public $db;
24
25 public function __construct() {
27 $this->db->setPrefix( XOOPS_DB_PREFIX );
28 $this->db->setLogger( XoopsLogger::instance() );
29 }
30
31 public function connectDB( $selectdb = true ) {
32 $ret = $this->db->connect( $selectdb );
33 if ( false !== $ret ) {
34 $fname = dirname( __DIR__ ) . '/language/' . $GLOBALS['language'] . '/charset_mysql.php';
35 if ( file_exists( $fname ) ) {
36 require( $fname );
37 }
38 }
39
40 return $ret;
41 }
42
43 public function isConnectable() {
44 return $this->connectDB(false) != false;
45 }
46
47 public function dbExists() {
48 return $this->connectDB() != false;
49 }
50
51 public function createDB() {
52 $this->connectDB(false);
53
54 $result = $this->db->query("CREATE DATABASE ".XOOPS_DB_NAME);
55
56 return $result != false;
57 }
58
59 public function queryFromFile( $sql_file_path ) {
60 $pieces = null;
61 $tables = [];
62
63 if ( ! file_exists( $sql_file_path ) ) {
64 return false;
65 }
66 $sql_query = trim( fread( fopen( $sql_file_path, 'r' ), filesize( $sql_file_path ) ) );
67 SqlUtility::splitMySqlFile( $pieces, $sql_query );
68 $this->connectDB();
69 foreach ( $pieces as $piece ) {
70 $piece = trim( $piece );
71 // [0] contains the prefixed query
72 // [4] contains unprefixed table name
73 $prefixed_query = SqlUtility::prefixQuery( $piece, $this->db->prefix() );
74 if ( false !== $prefixed_query ) {
75 $table = $this->db->prefix( $prefixed_query[4] );
76 if ( 'CREATE TABLE' === $prefixed_query[1] ) {
77 if ( false !== $this->db->query( $prefixed_query[0] ) ) {
78 if ( ! isset( $this->s_tables['create'][ $table ] ) ) {
79 $this->s_tables['create'][ $table ] = 1;
80 }
81 } else {
82 if ( ! isset( $this->f_tables['create'][ $table ] ) ) {
83 $this->f_tables['create'][ $table ] = 1;
84 }
85 }
86 } elseif ( 'INSERT INTO' === $prefixed_query[1] ) {
87 if ( false !== $this->db->query( $prefixed_query[0] ) ) {
88 if ( ! isset( $this->s_tables['insert'][ $table ] ) ) {
89 $this->s_tables['insert'][ $table ] = 1;
90 } else {
91 $this->s_tables['insert'][ $table ] ++;
92 }
93 } else if ( ! isset( $this->f_tables['insert'][ $table ] ) ) {
94 $this->f_tables['insert'][ $table ] = 1;
95 } else {
96 $this->f_tables['insert'][ $table ] ++;
97 }
98 } elseif ( 'ALTER TABLE' === $prefixed_query[1] ) {
99 if ( false !== $this->db->query( $prefixed_query[0] ) ) {
100 if ( ! isset( $this->s_tables['alter'][ $table ] ) ) {
101 $this->s_tables['alter'][ $table ] = 1;
102 }
103 } else if ( ! isset( $this->s_tables['alter'][ $table ] ) ) {
104 $this->f_tables['alter'][ $table ] = 1;
105 }
106 } elseif ( 'DROP TABLE' === $prefixed_query[1] ) {
107 if ( false !== $this->db->query( 'DROP TABLE ' . $table ) ) {
108 if ( ! isset( $this->s_tables['drop'][ $table ] ) ) {
109 $this->s_tables['drop'][ $table ] = 1;
110 }
111 } else if ( ! isset( $this->s_tables['drop'][ $table ] ) ) {
112 $this->f_tables['drop'][ $table ] = 1;
113 }
114 }
115 }
116 }
117
118 return true;
119 }
120
121 public function report() {
122 $reports = [];
123 if ( isset( $this->s_tables['create'] ) ) {
124 foreach ( $this->s_tables['create'] as $key => $val ) {
125 $reports[] = _OKIMG . sprintf( _INSTALL_L45, "<b>$key</b>" );
126 }
127 }
128 if ( isset( $this->s_tables['insert'] ) ) {
129 foreach ( $this->s_tables['insert'] as $key => $val ) {
130 $reports[] = _OKIMG . sprintf( _INSTALL_L119, $val, "<b>$key</b>" );
131 }
132 }
133 if ( isset( $this->s_tables['alter'] ) ) {
134 foreach ( $this->s_tables['alter'] as $key => $val ) {
135 $reports[] = _OKIMG . sprintf( _INSTALL_L133, "<b>$key</b>" );
136 }
137 }
138 if ( isset( $this->s_tables['drop'] ) ) {
139 foreach ( $this->s_tables['drop'] as $key => $val ) {
140 $reports[] = _OKIMG . sprintf( _INSTALL_L163, "<b>$key</b>" );
141 }
142 }
143// $content .= "<br>\n"; //< What's!?
144 if ( isset( $this->f_tables['create'] ) ) {
145 foreach ( $this->f_tables['create'] as $key => $val ) {
146 $reports[] = _NGIMG . sprintf( _INSTALL_L118, "<b>$key</b>" );
147 }
148 }
149 if ( isset( $this->f_tables['insert'] ) ) {
150 foreach ( $this->f_tables['insert'] as $key => $val ) {
151 $reports[] = _NGIMG . sprintf( _INSTALL_L120, $val, "<b>$key</b>" );
152 }
153 }
154 if ( isset( $this->f_tables['alter'] ) ) {
155 foreach ( $this->f_tables['alter'] as $key => $val ) {
156 $reports[] = _NGIMG . sprintf( _INSTALL_L134, "<b>$key</b>" );
157 }
158 }
159 if ( isset( $this->f_tables['drop'] ) ) {
160 foreach ( $this->f_tables['drop'] as $key => $val ) {
161 $reports[] = _NGIMG . sprintf( _INSTALL_L164, "<b>$key</b>" );
162 }
163 }
164
165 return $reports;
166 }
167
168 public function query( $sql ) {
169 $this->connectDB();
170
171 return $this->db->query( $sql );
172 }
173
174 public function prefix( $table ) {
175 $this->connectDB();
176
177 return $this->db->prefix( $table );
178 }
179
180 public function fetchArray( $ret ) {
181 $this->connectDB();
182
183 return $this->db->fetchArray( $ret );
184 }
185
186 public function insert( $table, $query ) {
187 $this->connectDB();
188 $table = $this->db->prefix( $table );
189 $query = 'INSERT INTO ' . $table . ' ' . $query;
190 if ( ! $this->db->queryF( $query ) ) {
191 if ( ! isset( $this->f_tables['insert'][ $table ] ) ) {
192 $this->f_tables['insert'][ $table ] = 1;
193 } else {
194 $this->f_tables['insert'][ $table ] ++;
195 }
196
197 return false;
198 }
199
200 if ( ! isset( $this->s_tables['insert'][ $table ] ) ) {
201 $this->s_tables['insert'][ $table ] = 1;
202 } else {
203 $this->s_tables['insert'][ $table ] ++;
204 }
205
206 return $this->db->getInsertId();
207 }
208
209 public function isError() {
210 //return ( isset( $this->f_tables ) ) ? true : false;
211 return isset( $this->f_tables );
212 }
213
214 public function deleteTables( $tables ) {
215 $deleted = [];
216 $this->connectDB();
217 foreach ( $tables as $key => $val ) {
218 if ( ! $this->db->query( 'DROP TABLE ' . $this->db->prefix( $key ) ) ) {
219 $deleted[] = $ct;
220 }
221 }
222
223 return $deleted;
224 }
225
226 public function tableExists( $table ) {
227 $table = trim( $table );
228 $ret = false;
229 if ( $table !== '' ) {
230 $this->connectDB();
231 $sql = 'SELECT * FROM ' . $this->db->prefix( $table );
232 $ret = false !== $this->db->query( $sql );
233 }
234
235 return $ret;
236 }
237}
static & instance()
Definition logger.php:45