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