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