1 | <?php |
---|
2 | |
---|
3 | /* ----------------------------------------------------------------------------- |
---|
4 | class name: manage_tables |
---|
5 | class version: 1.4 |
---|
6 | date: 2008-12-28 |
---|
7 | ------------------------------------------------------------------------------ |
---|
8 | author: grum at grum.dnsalias.com |
---|
9 | << May the Little SpaceFrog be with you >> |
---|
10 | ------------------------------------------------------------------------------ |
---|
11 | |
---|
12 | this class provides base functions to manage tables while plugin installation |
---|
13 | - constructor manage_tables($tables) |
---|
14 | - (public) function create_tables($tables_def) |
---|
15 | - (public) function update_tables_fields($tables_alteration) |
---|
16 | - (public) function drop_tables() |
---|
17 | - (public) function rename_tables($list) -v1.1 |
---|
18 | - (public) function tables_exists() -v1.1 |
---|
19 | - (public) function export($filename, $options, $tables, $infos) -v1.3 |
---|
20 | - (public) function multi_queries($queries) -v1.3 |
---|
21 | - (public) function import($filename) -v1.3 |
---|
22 | |
---|
23 | ------------------------------------------------------------------------------ |
---|
24 | v1.1 + add rename_tables($list) function |
---|
25 | + add tables_exists() function |
---|
26 | v1.2 + add export($filename, $options, $tables) function |
---|
27 | v1.3 + modify export($filename, $options, $tables, $infos, $resultboolean) function |
---|
28 | + new parameters '$infos' allows to add some information on the |
---|
29 | exported file |
---|
30 | + add 'delete' and 'colnames' options |
---|
31 | + $resultbooelan option for return |
---|
32 | + add multi_queries($queries) function |
---|
33 | + add import($filename) function |
---|
34 | v1.4 - 2009-03-01 + PHP5 migration (use of public/protected/private) |
---|
35 | + export => DROP TABLE IF EXISTS |
---|
36 | + import => if DROP TABLE without IF EXISTS, add IF EXISTS |
---|
37 | |
---|
38 | -------------------------------------------------------------------------- */ |
---|
39 | class manage_tables |
---|
40 | { |
---|
41 | public $tables; //array of tables names |
---|
42 | public $version = "1.4"; |
---|
43 | |
---|
44 | public function manage_tables($tables) |
---|
45 | { |
---|
46 | $this->tables = $tables; |
---|
47 | } |
---|
48 | |
---|
49 | /* |
---|
50 | create tables |
---|
51 | $tables_def is an array of SQL CREATE queries |
---|
52 | |
---|
53 | return true if everything is ok, otherwise tablename |
---|
54 | */ |
---|
55 | public function create_tables($tables_def) |
---|
56 | { |
---|
57 | //deleting tables if exists |
---|
58 | $this->drop_tables(); |
---|
59 | |
---|
60 | for($i=0;$i<count($tables_def);$i++) |
---|
61 | { |
---|
62 | $result=pwg_query($tables_def[$i]); |
---|
63 | if(!$result) |
---|
64 | { |
---|
65 | //if an error occurs, deleting created tables |
---|
66 | $this->drop_tables(); |
---|
67 | return(false); |
---|
68 | } |
---|
69 | } |
---|
70 | return(true); |
---|
71 | } |
---|
72 | |
---|
73 | /* update tables definition |
---|
74 | $tables_alteration : array of arrays |
---|
75 | example : |
---|
76 | $tables_alteration['table1']=array( |
---|
77 | "attribute1" => " ADD COLUMN `attribute1` text null default ''", |
---|
78 | "attribute2" => " ADD COLUMN `attribute2` text null default ''")); |
---|
79 | $tables_alteration['table2']=array( |
---|
80 | "attribute1" => " ADD COLUMN `attribute1` text null default ''", |
---|
81 | "attribute2" => " ADD COLUMN `attribute2` text null default ''")); |
---|
82 | |
---|
83 | return true if no error, otherwise return table.fields of error |
---|
84 | */ |
---|
85 | public function update_tables_fields($tables_alteration) |
---|
86 | { |
---|
87 | if(!is_array($tables_alteration)) |
---|
88 | { |
---|
89 | return(''); |
---|
90 | } |
---|
91 | |
---|
92 | reset($tables_alteration); |
---|
93 | while (list($key, $val) = each($tables_alteration)) |
---|
94 | { |
---|
95 | $sql="SHOW COLUMNS FROM $key"; |
---|
96 | $result=pwg_query($sql); |
---|
97 | if($result) |
---|
98 | { |
---|
99 | $columns=array(); |
---|
100 | while($row=mysql_fetch_assoc($result)) |
---|
101 | { |
---|
102 | array_push($columns, $row['Field']); |
---|
103 | } |
---|
104 | |
---|
105 | reset($val); |
---|
106 | while (list($attname, $sql) = each($val)) |
---|
107 | { |
---|
108 | if(!in_array($attname, $columns)) |
---|
109 | { |
---|
110 | $result=pwg_query("ALTER TABLE `$key` ".$sql); |
---|
111 | if(!$result) |
---|
112 | { |
---|
113 | return($key.".".$attname); |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | return(true); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | /* |
---|
124 | delete tables listed in $this->tables_list |
---|
125 | */ |
---|
126 | public function drop_tables() |
---|
127 | { |
---|
128 | foreach($this->tables as $key => $table_name) |
---|
129 | { |
---|
130 | $sql="DROP TABLE IF EXISTS ".$table_name; |
---|
131 | $result=pwg_query($sql); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | /* |
---|
136 | rename tables name of list |
---|
137 | $list is an array('old_name' => 'new_name') |
---|
138 | return true if ok, else old table name |
---|
139 | */ |
---|
140 | public function rename_tables($list) |
---|
141 | { |
---|
142 | $tmplist=array_flip($this->tables); |
---|
143 | foreach($list as $key => $val) |
---|
144 | { |
---|
145 | if(isset($tmplist[$key])) |
---|
146 | { |
---|
147 | $this->tables[$tmplist[$key]] = $val; |
---|
148 | $sql="ALTER TABLE `$key` RENAME TO `$val`"; |
---|
149 | if(!pwg_query($sql)) |
---|
150 | { |
---|
151 | return($key); |
---|
152 | } |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | return($key); |
---|
157 | } |
---|
158 | } |
---|
159 | return(true); |
---|
160 | } |
---|
161 | |
---|
162 | /* |
---|
163 | return true if all listed tables exists |
---|
164 | */ |
---|
165 | public function tables_exists() |
---|
166 | { |
---|
167 | $list=array_flip($this->tables); |
---|
168 | $sql="SHOW TABLES"; |
---|
169 | $result=pwg_query($sql); |
---|
170 | if($result) |
---|
171 | { |
---|
172 | while($row=mysql_fetch_row($result)) |
---|
173 | { |
---|
174 | if(isset($list[$row[0]])) |
---|
175 | { |
---|
176 | array_splice($list, $row[0],1); |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | if(count($list)>0) |
---|
181 | { |
---|
182 | return(false); |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | return(true); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | /* |
---|
191 | export all tables as SQL in a text file |
---|
192 | |
---|
193 | each query end with a " -- EOQ" ; it's just a method to increase parsing for |
---|
194 | import function |
---|
195 | |
---|
196 | $filename : name of the file |
---|
197 | $options : array of options like |
---|
198 | array( |
---|
199 | 'drop' => true/false, //add DROP TABLE statements |
---|
200 | 'create' => true/false, //add CREATE TABLE statements |
---|
201 | 'insert' => true/false, //add INSERT statements |
---|
202 | 'delete' => true/false, //add delete statements |
---|
203 | 'colnames' => true/false, //add columns names for inserts statements |
---|
204 | ) |
---|
205 | $tables : array of tables names to export |
---|
206 | array('tablename1', 'tablenamen', 'tablename3', ...) |
---|
207 | if empty, assume that all tables have to be exported |
---|
208 | $infos : additional info written in exported file (as comment) |
---|
209 | $resultboolean : if true, result is true/false ; |
---|
210 | if false, if result, return a string with nfo about export |
---|
211 | */ |
---|
212 | public function export($filename, $options=array(), $tables=array(), $infos="", $resultboolean=true) |
---|
213 | { |
---|
214 | $defaultopt=array( |
---|
215 | 'drop' => true, |
---|
216 | 'create' => true, |
---|
217 | 'insert' => true, |
---|
218 | 'delete' => false, |
---|
219 | 'colnames' => false |
---|
220 | ); |
---|
221 | |
---|
222 | if(!isset($options['drop'])) |
---|
223 | { |
---|
224 | $options['drop']=$defaultopt['drop']; |
---|
225 | } |
---|
226 | if(!isset($options['create'])) |
---|
227 | { |
---|
228 | $options['create']=$defaultopt['create']; |
---|
229 | } |
---|
230 | if(!isset($options['insert'])) |
---|
231 | { |
---|
232 | $options['insert']=$defaultopt['insert']; |
---|
233 | } |
---|
234 | if(!isset($options['delete'])) |
---|
235 | { |
---|
236 | $options['delete']=$defaultopt['delete']; |
---|
237 | } |
---|
238 | if(!isset($options['colnames'])) |
---|
239 | { |
---|
240 | $options['colnames']=$defaultopt['colnames']; |
---|
241 | } |
---|
242 | if(count($tables)==0) |
---|
243 | { |
---|
244 | $tables=$this->tables; |
---|
245 | } |
---|
246 | |
---|
247 | $resultnfo=''; |
---|
248 | |
---|
249 | $returned=true; |
---|
250 | $text=' |
---|
251 | -- *************************************************************** -- EOQ |
---|
252 | -- * SQL export made with Grum Plugins Classes (Export tool r'.$this->version.') -- EOQ |
---|
253 | -- * Export date :'.date('Y-m-d H:i:s').' -- EOQ |
---|
254 | -- * Export options :'; |
---|
255 | if($options['drop']){$text.=' [drop]';} |
---|
256 | if($options['delete']){$text.=' [delete]';} |
---|
257 | if($options['create']){$text.=' [create]';} |
---|
258 | if($options['insert']){$text.=' [insert]';} |
---|
259 | if($options['colnames']){$text.=' [colnames]';} |
---|
260 | $text.=" -- EOQ"; |
---|
261 | if($infos!="") |
---|
262 | { |
---|
263 | $text.=' |
---|
264 | -- * '.$infos." -- EOQ"; |
---|
265 | } |
---|
266 | $text.=' |
---|
267 | -- *************************************************************** -- EOQ |
---|
268 | |
---|
269 | '; |
---|
270 | foreach($tables as $key => $val) |
---|
271 | { |
---|
272 | $countelems=0; |
---|
273 | |
---|
274 | $text.=" |
---|
275 | |
---|
276 | -- *************************************************************** -- EOQ |
---|
277 | -- * Statements for ".$this->tables[$key]." table -- EOQ |
---|
278 | -- *************************************************************** -- EOQ |
---|
279 | "; |
---|
280 | |
---|
281 | if($options['drop']) |
---|
282 | { |
---|
283 | $text.=sprintf("DROP TABLE IF EXISTS `%s`; -- EOQ\n", $this->tables[$key]); |
---|
284 | } |
---|
285 | |
---|
286 | if($options['delete']) |
---|
287 | { |
---|
288 | $text.=sprintf("DELETE FROM `%s`; -- EOQ\n", $this->tables[$key]); |
---|
289 | } |
---|
290 | |
---|
291 | if($options['create']) |
---|
292 | { |
---|
293 | $sql='SHOW CREATE TABLE '.$this->tables[$key]; |
---|
294 | $result=pwg_query($sql); |
---|
295 | if($result) |
---|
296 | { |
---|
297 | while($row=mysql_fetch_row($result)) |
---|
298 | { |
---|
299 | $text.=sprintf("%s; -- EOQ\n", $row[1]); |
---|
300 | } |
---|
301 | } |
---|
302 | else |
---|
303 | { |
---|
304 | $returned=false; |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | if($options['insert']) |
---|
309 | { |
---|
310 | $colnames=""; |
---|
311 | if($options['colnames']) |
---|
312 | { |
---|
313 | $sql='SHOW COLUMNS FROM `'.$this->tables[$key].'`'; |
---|
314 | $result=pwg_query($sql); |
---|
315 | if($result) |
---|
316 | { |
---|
317 | $tmp=array(); |
---|
318 | while($row=mysql_fetch_row($result)) |
---|
319 | { |
---|
320 | $tmp[]=$row[0]; |
---|
321 | } |
---|
322 | } |
---|
323 | $colnames='('.implode(',', $tmp).')'; |
---|
324 | } |
---|
325 | |
---|
326 | $sql='SELECT * FROM '.$this->tables[$key]; |
---|
327 | $result=pwg_query($sql); |
---|
328 | if($result) |
---|
329 | { |
---|
330 | while($row=mysql_fetch_row($result)) |
---|
331 | { |
---|
332 | foreach($row as $key2 => $val2) |
---|
333 | { |
---|
334 | $row[$key2]="'".addslashes($val2)."'"; |
---|
335 | } |
---|
336 | $text.=sprintf("INSERT INTO `%s` %s VALUES(%s); -- EOQ\n", $this->tables[$key], $colnames, implode(', ', $row)); |
---|
337 | $countelems++; |
---|
338 | } |
---|
339 | } |
---|
340 | else |
---|
341 | { |
---|
342 | $returned=false; |
---|
343 | } |
---|
344 | $resultnfo.=$key.':'.$countelems.'@'; |
---|
345 | } |
---|
346 | } |
---|
347 | $fhandle=fopen($filename, 'wb'); |
---|
348 | if($fhandle) |
---|
349 | { |
---|
350 | fwrite($fhandle, $text); |
---|
351 | fclose($fhandle); |
---|
352 | } |
---|
353 | else |
---|
354 | { |
---|
355 | $returned=false; |
---|
356 | } |
---|
357 | if(($resultboolean==false)&&($returned)) |
---|
358 | { |
---|
359 | $returned=$resultnfo; |
---|
360 | } |
---|
361 | return($returned); |
---|
362 | } |
---|
363 | |
---|
364 | /* |
---|
365 | import an .sql file |
---|
366 | $filename : name of the file |
---|
367 | 'errors' : -1 file don't exists |
---|
368 | -2 can't open file |
---|
369 | */ |
---|
370 | public function import($filename) |
---|
371 | { |
---|
372 | $return = array( |
---|
373 | 'numinsert'=>0, |
---|
374 | 'numdelete'=>0, |
---|
375 | 'numdrop'=>0, |
---|
376 | 'numcreate'=>0, |
---|
377 | 'errors'=>array(), |
---|
378 | 'total_ok'=>0 |
---|
379 | ); |
---|
380 | |
---|
381 | if(file_exists($filename)) |
---|
382 | { |
---|
383 | $fhandle=fopen($filename, 'r'); |
---|
384 | if($fhandle) |
---|
385 | { |
---|
386 | $queries=fread($fhandle, filesize($filename)); |
---|
387 | fclose($fhandle); |
---|
388 | $return=$this->multi_queries($queries); |
---|
389 | } |
---|
390 | else |
---|
391 | { |
---|
392 | $return['errors']=-2; |
---|
393 | } |
---|
394 | } |
---|
395 | else |
---|
396 | { |
---|
397 | $return['errors']=-1; |
---|
398 | } |
---|
399 | return($return); |
---|
400 | } |
---|
401 | |
---|
402 | /* |
---|
403 | execute multiple query |
---|
404 | each query have to be separated by a "-- EOQ\n" |
---|
405 | |
---|
406 | $queries : sql queries |
---|
407 | */ |
---|
408 | public function multi_queries($queries) |
---|
409 | { |
---|
410 | $queries_list=preg_split( |
---|
411 | '/\s*;?\s*--\s+EOQ[\r\n]{1}/i', $queries, -1, PREG_SPLIT_NO_EMPTY); |
---|
412 | |
---|
413 | $return = array( |
---|
414 | 'numinsert'=>0, |
---|
415 | 'numdelete'=>0, |
---|
416 | 'numdrop'=>0, |
---|
417 | 'numcreate'=>0, |
---|
418 | 'errors'=>array(), |
---|
419 | 'total_ok'=>0 |
---|
420 | ); |
---|
421 | |
---|
422 | $i=0; |
---|
423 | foreach($queries_list as $key => $sql) |
---|
424 | { |
---|
425 | $i++; |
---|
426 | |
---|
427 | // replace " DROP TABLE `xxxx`; " by " DROP TABLE IF EXISTS `xxxx`; " |
---|
428 | $sql=preg_replace("/(.*DROP TABLE)\s*(`.*)/i", "$1 IF EXISTS $2", $sql); |
---|
429 | |
---|
430 | @$result=pwg_query($sql); |
---|
431 | if($result) |
---|
432 | { |
---|
433 | $return['total_ok']++; |
---|
434 | if(preg_match('/\b[\s]*insert[\s]+/i', $sql)>0) |
---|
435 | {$return['numinsert']++;} |
---|
436 | elseif(preg_match('/\b[\s]*drop[\s]+/i', $sql)>0) |
---|
437 | {$return['numdrop']++;} |
---|
438 | elseif(preg_match('/\b[\s]*delete[\s]+/i', $sql)>0) |
---|
439 | {$return['numdelete']++;} |
---|
440 | elseif(preg_match('/\b[\s]*create[\s]+/i',$sql)>0) |
---|
441 | {$return['numcreate']++;} |
---|
442 | } |
---|
443 | else |
---|
444 | { |
---|
445 | array_push($return['errors'], '['.$i.'] '.$sql); |
---|
446 | } |
---|
447 | } |
---|
448 | return($return); |
---|
449 | } |
---|
450 | |
---|
451 | } //class |
---|
452 | |
---|
453 | |
---|
454 | ?> |
---|