@@ -1,17 +1,17 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Mysqldump File Doc Comment |
|
4 | - * |
|
5 | - * PHP version 5 |
|
6 | - * |
|
7 | - * @category Library |
|
8 | - * @package Ifsnop\Mysqldump |
|
9 | - * @author Michael J. Calkins <[email protected]> |
|
10 | - * @author Diego Torres <[email protected]> |
|
11 | - * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License |
|
12 | - * @link https://github.com/ifsnop/mysqldump-php |
|
13 | - * |
|
14 | - */ |
|
3 | + * Mysqldump File Doc Comment |
|
4 | + * |
|
5 | + * PHP version 5 |
|
6 | + * |
|
7 | + * @category Library |
|
8 | + * @package Ifsnop\Mysqldump |
|
9 | + * @author Michael J. Calkins <[email protected]> |
|
10 | + * @author Diego Torres <[email protected]> |
|
11 | + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License |
|
12 | + * @link https://github.com/ifsnop/mysqldump-php |
|
13 | + * |
|
14 | + */ |
|
15 | 15 | |
16 | 16 | namespace core\save; |
17 | 17 | |
@@ -33,696 +33,696 @@ discard block |
||
33 | 33 | class Mysqldump |
34 | 34 | { |
35 | 35 | |
36 | - // Same as mysqldump |
|
37 | - const MAXLINESIZE = 1000000; |
|
38 | - |
|
39 | - // Available compression methods as constants |
|
40 | - const GZIP = 'Gzip'; |
|
41 | - const BZIP2 = 'Bzip2'; |
|
42 | - const NONE = 'None'; |
|
43 | - |
|
44 | - // Available connection strings |
|
45 | - const UTF8 = 'utf8'; |
|
46 | - const UTF8MB4 = 'utf8mb4'; |
|
47 | - |
|
48 | - // This can be set both on constructor or manually |
|
49 | - public $host; |
|
50 | - public $port; |
|
51 | - public $user; |
|
52 | - public $pass; |
|
53 | - public $db; |
|
54 | - public $fileName; |
|
55 | - |
|
56 | - // Internal stuff |
|
57 | - private $tables = array(); |
|
58 | - private $views = array(); |
|
59 | - private $triggers = array(); |
|
60 | - private $dbHandler; |
|
61 | - private $dbType; |
|
62 | - private $compressManager; |
|
63 | - private $typeAdapter; |
|
64 | - private $dumpSettings = array(); |
|
65 | - private $pdoSettings = array(); |
|
66 | - private $version; |
|
67 | - private $tableColumnTypes = array(); |
|
68 | - |
|
69 | - /** |
|
70 | - * Constructor of Mysqldump. Note that in the case of an SQLite database |
|
71 | - * connection, the filename must be in the $db parameter. |
|
72 | - * |
|
73 | - * @param string $db Database name |
|
74 | - * @param string $user SQL account username |
|
75 | - * @param string $pass SQL account password |
|
76 | - * @param string $host SQL server to connect to |
|
77 | - * @param string $type SQL database type |
|
78 | - * @param array $dumpSettings SQL database settings |
|
79 | - * @param array $pdoSettings PDO configured attributes |
|
80 | - */ |
|
81 | - public function __construct( |
|
82 | - $db = '', |
|
83 | - $user = '', |
|
84 | - $pass = '', |
|
85 | - $host = 'localhost', |
|
86 | - $type = 'mysql', |
|
87 | - $dumpSettings = array(), |
|
88 | - $pdoSettings = array() |
|
89 | - ) { |
|
90 | - $dumpSettingsDefault = array( |
|
91 | - 'include-tables' => array(), |
|
92 | - 'exclude-tables' => array(), |
|
93 | - 'compress' => Mysqldump::NONE, |
|
94 | - 'no-data' => false, |
|
95 | - 'add-drop-table' => false, |
|
96 | - 'single-transaction' => true, |
|
97 | - 'lock-tables' => true, |
|
98 | - 'add-locks' => true, |
|
99 | - 'extended-insert' => true, |
|
100 | - 'disable-keys' => true, |
|
101 | - 'where' => '', |
|
102 | - 'no-create-info' => false, |
|
103 | - 'skip-triggers' => false, |
|
104 | - 'add-drop-trigger' => true, |
|
105 | - 'hex-blob' => true, |
|
106 | - 'databases' => false, |
|
107 | - 'add-drop-database' => false, |
|
108 | - 'skip-tz-utz' => false, |
|
109 | - 'no-autocommit' => true, |
|
110 | - 'default-character-set' => Mysqldump::UTF8, |
|
111 | - 'skip-comments' => false, |
|
112 | - 'skip-dump-date' => false, |
|
113 | - /* deprecated */ |
|
114 | - 'disable-foreign-keys-check' => true |
|
115 | - ); |
|
116 | - |
|
117 | - $pdoSettingsDefault = array( |
|
118 | - PDO::ATTR_PERSISTENT => true, |
|
119 | - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
|
120 | - PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false |
|
121 | - ); |
|
122 | - |
|
123 | - $this->db = $db; |
|
124 | - $this->user = $user; |
|
125 | - $this->pass = $pass; |
|
126 | - |
|
127 | - $colonPos = strpos($host, ':'); |
|
128 | - if (false !== $colonPos) { |
|
129 | - $this->port = substr($host, $colonPos + 1); |
|
130 | - $this->host = substr($host, 0, $colonPos); |
|
131 | - } else { |
|
132 | - $this->port = null; |
|
133 | - $this->host = $host; |
|
134 | - } |
|
135 | - |
|
136 | - $this->dbType = strtolower($type); |
|
137 | - $this->pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings); |
|
138 | - $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings); |
|
139 | - |
|
140 | - if (!isset($this->pdoSettings[PDO::MYSQL_ATTR_INIT_COMMAND])) { |
|
141 | - $this->pdoSettings[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES " . $this->dumpSettings['default-character-set']; |
|
142 | - } |
|
143 | - |
|
144 | - $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); |
|
145 | - if (count($diff)>0) { |
|
146 | - throw new Exception("Unexpected value in dumpSettings: (" . implode(",", $diff) . ")"); |
|
147 | - } |
|
148 | - |
|
149 | - // Create a new compressManager to manage compressed output |
|
150 | - $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Custom array_replace_recursive to be used if PHP < 5.3 |
|
155 | - * Replaces elements from passed arrays into the first array recursively |
|
156 | - * |
|
157 | - * @param array $array1 The array in which elements are replaced |
|
158 | - * @param array $array2 The array from which elements will be extracted |
|
159 | - * |
|
160 | - * @return array Returns an array, or NULL if an error occurs. |
|
161 | - */ |
|
162 | - public static function array_replace_recursive($array1, $array2) |
|
163 | - { |
|
164 | - if (function_exists('array_replace_recursive')) { |
|
165 | - return array_replace_recursive($array1, $array2); |
|
166 | - } |
|
167 | - |
|
168 | - foreach ($array2 as $key => $value) { |
|
169 | - if (is_array($value)) { |
|
170 | - $array1[$key] = self::array_replace_recursive($array1[$key], $value); |
|
171 | - } else { |
|
172 | - $array1[$key] = $value; |
|
173 | - } |
|
174 | - } |
|
175 | - return $array1; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Connect with PDO |
|
180 | - * |
|
181 | - * @return null |
|
182 | - */ |
|
183 | - private function connect() |
|
184 | - { |
|
185 | - // Connecting with PDO |
|
186 | - try { |
|
187 | - switch ($this->dbType) { |
|
188 | - case 'sqlite': |
|
189 | - $this->dbHandler = @new PDO("sqlite:" . $this->db, null, null, $this->pdoSettings); |
|
190 | - break; |
|
191 | - case 'mysql': |
|
192 | - case 'pgsql': |
|
193 | - case 'dblib': |
|
194 | - $dsn = $this->dbType . |
|
195 | - ":host=" . $this->host . |
|
196 | - (isset($this->port) ? ";port=" . $this->port : "") . |
|
197 | - ";dbname=" . $this->db; |
|
198 | - $this->dbHandler = @new PDO( |
|
199 | - $dsn, |
|
200 | - $this->user, |
|
201 | - $this->pass, |
|
202 | - $this->pdoSettings |
|
203 | - ); |
|
204 | - // Fix for always-unicode output |
|
205 | - $this->dbHandler->exec("SET NAMES " . $this->dumpSettings['default-character-set']); |
|
206 | - // Store server version |
|
207 | - $this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION); |
|
208 | - break; |
|
209 | - default: |
|
210 | - throw new Exception("Unsupported database type (" . $this->dbType . ")"); |
|
211 | - } |
|
212 | - } catch (PDOException $e) { |
|
213 | - throw new Exception( |
|
214 | - "Connection to " . $this->dbType . " failed with message: " . |
|
215 | - $e->getMessage() |
|
216 | - ); |
|
217 | - } |
|
218 | - |
|
219 | - $this->dbHandler->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
|
220 | - $this->typeAdapter = TypeAdapterFactory::create($this->dbType, $this->dbHandler); |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Main call |
|
225 | - * |
|
226 | - * @param string $filename Name of file to write sql dump to |
|
227 | - * @return null |
|
228 | - */ |
|
229 | - public function start($filename = '') |
|
230 | - { |
|
231 | - // Output file can be redefined here |
|
232 | - if (!empty($filename)) { |
|
233 | - $this->fileName = $filename; |
|
234 | - } |
|
235 | - // We must set a name to continue |
|
236 | - if (empty($this->fileName)) { |
|
237 | - throw new Exception("Output file name is not set"); |
|
238 | - } |
|
239 | - |
|
240 | - // Connect to database |
|
241 | - $this->connect(); |
|
242 | - |
|
243 | - // Create output file |
|
244 | - $this->compressManager->open($this->fileName); |
|
245 | - |
|
246 | - // Write some basic info to output file |
|
247 | - $this->compressManager->write($this->getDumpFileHeader()); |
|
248 | - |
|
249 | - // Store server settings and use sanner defaults to dump |
|
250 | - $this->compressManager->write( |
|
251 | - $this->typeAdapter->backup_parameters($this->dumpSettings) |
|
252 | - ); |
|
253 | - |
|
254 | - if ($this->dumpSettings['databases']) { |
|
255 | - $this->compressManager->write( |
|
256 | - $this->typeAdapter->getDatabaseHeader($this->db) |
|
257 | - ); |
|
258 | - if ($this->dumpSettings['add-drop-database']) { |
|
259 | - $this->compressManager->write( |
|
260 | - $this->typeAdapter->add_drop_database($this->db) |
|
261 | - ); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - // Get table, view and trigger structures from database |
|
266 | - $this->getDatabaseStructure(); |
|
267 | - |
|
268 | - if ($this->dumpSettings['databases']) { |
|
269 | - $this->compressManager->write( |
|
270 | - $this->typeAdapter->databases($this->db) |
|
271 | - ); |
|
272 | - } |
|
273 | - |
|
274 | - // If there still are some tables/views in include-tables array, |
|
275 | - // that means that some tables or views weren't found. |
|
276 | - // Give proper error and exit. |
|
277 | - if (0 < count($this->dumpSettings['include-tables'])) { |
|
278 | - $name = implode(",", $this->dumpSettings['include-tables']); |
|
279 | - throw new Exception("Table or View (" . $name . ") not found in database"); |
|
280 | - } |
|
281 | - |
|
282 | - $this->exportTables(); |
|
283 | - $this->exportViews(); |
|
284 | - $this->exportTriggers(); |
|
285 | - |
|
286 | - // Restore saved parameters |
|
287 | - $this->compressManager->write( |
|
288 | - $this->typeAdapter->restore_parameters($this->dumpSettings) |
|
289 | - ); |
|
290 | - // Write some stats to output file |
|
291 | - $this->compressManager->write($this->getDumpFileFooter()); |
|
292 | - // Close output file |
|
293 | - $this->compressManager->close(); |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Returns header for dump file |
|
298 | - * |
|
299 | - * @return string |
|
300 | - */ |
|
301 | - private function getDumpFileHeader() |
|
302 | - { |
|
303 | - $header = ''; |
|
304 | - if (!$this->dumpSettings['skip-comments']) { |
|
305 | - // Some info about software, source and time |
|
306 | - $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php" . PHP_EOL . |
|
307 | - "--" . PHP_EOL . |
|
308 | - "-- Host: {$this->host}\tDatabase: {$this->db}" . PHP_EOL . |
|
309 | - "-- ------------------------------------------------------" . PHP_EOL; |
|
310 | - |
|
311 | - if (!empty($this->version)) { |
|
312 | - $header .= "-- Server version \t" . $this->version . PHP_EOL; |
|
313 | - } |
|
314 | - |
|
315 | - $header .= "-- Date: " . date('r') . PHP_EOL . PHP_EOL; |
|
316 | - } |
|
317 | - return $header; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Returns footer for dump file |
|
322 | - * |
|
323 | - * @return string |
|
324 | - */ |
|
325 | - private function getDumpFileFooter() |
|
326 | - { |
|
327 | - $footer = ''; |
|
328 | - if (!$this->dumpSettings['skip-comments']) { |
|
329 | - $footer .= '-- Dump completed'; |
|
330 | - if (!$this->dumpSettings['skip-dump-date']) { |
|
331 | - $footer .= ' on: ' . date('r'); |
|
332 | - } |
|
333 | - $footer .= PHP_EOL; |
|
334 | - } |
|
335 | - |
|
336 | - return $footer; |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Reads table and views names from database. |
|
341 | - * Fills $this->tables array so they will be dumped later. |
|
342 | - * |
|
343 | - * @return null |
|
344 | - */ |
|
345 | - private function getDatabaseStructure() |
|
346 | - { |
|
347 | - // Listing all tables from database |
|
348 | - if (empty($this->dumpSettings['include-tables'])) { |
|
349 | - // include all tables for now, blacklisting happens later |
|
350 | - foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->db)) as $row) { |
|
351 | - array_push($this->tables, current($row)); |
|
352 | - } |
|
353 | - } else { |
|
354 | - // include only the tables mentioned in include-tables |
|
355 | - foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->db)) as $row) { |
|
356 | - if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
|
357 | - array_push($this->tables, current($row)); |
|
358 | - $elem = array_search( |
|
359 | - current($row), |
|
360 | - $this->dumpSettings['include-tables'] |
|
361 | - ); |
|
362 | - unset($this->dumpSettings['include-tables'][$elem]); |
|
363 | - } |
|
364 | - } |
|
365 | - } |
|
366 | - |
|
367 | - // Listing all views from database |
|
368 | - if (empty($this->dumpSettings['include-tables'])) { |
|
369 | - // include all views for now, blacklisting happens later |
|
370 | - foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->db)) as $row) { |
|
371 | - array_push($this->views, current($row)); |
|
372 | - } |
|
373 | - } else { |
|
374 | - // include only the tables mentioned in include-tables |
|
375 | - foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->db)) as $row) { |
|
376 | - if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
|
377 | - array_push($this->views, current($row)); |
|
378 | - $elem = array_search( |
|
379 | - current($row), |
|
380 | - $this->dumpSettings['include-tables'] |
|
381 | - ); |
|
382 | - unset($this->dumpSettings['include-tables'][$elem]); |
|
383 | - } |
|
384 | - } |
|
385 | - } |
|
386 | - |
|
387 | - // Listing all triggers from database |
|
388 | - if (false === $this->dumpSettings['skip-triggers']) { |
|
389 | - foreach ($this->dbHandler->query($this->typeAdapter->show_triggers($this->db)) as $row) { |
|
390 | - array_push($this->triggers, $row['Trigger']); |
|
391 | - } |
|
392 | - } |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Exports all the tables selected from database |
|
397 | - * |
|
398 | - * @return null |
|
399 | - */ |
|
400 | - private function exportTables() |
|
401 | - { |
|
402 | - // Exporting tables one by one |
|
403 | - foreach ($this->tables as $table) { |
|
404 | - if (in_array($table, $this->dumpSettings['exclude-tables'], true)) { |
|
405 | - continue; |
|
406 | - } |
|
407 | - $this->getTableStructure($table); |
|
408 | - if (false === $this->dumpSettings['no-data']) { |
|
409 | - $this->listValues($table); |
|
410 | - } |
|
411 | - } |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Exports all the views found in database |
|
416 | - * |
|
417 | - * @return null |
|
418 | - */ |
|
419 | - private function exportViews() |
|
420 | - { |
|
421 | - if (false === $this->dumpSettings['no-create-info']) { |
|
422 | - // Exporting views one by one |
|
423 | - foreach ($this->views as $view) { |
|
424 | - if (in_array($view, $this->dumpSettings['exclude-tables'], true)) { |
|
425 | - continue; |
|
426 | - } |
|
427 | - $this->getViewStructure($view); |
|
428 | - } |
|
429 | - } |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * Exports all the triggers found in database |
|
434 | - * |
|
435 | - * @return null |
|
436 | - */ |
|
437 | - private function exportTriggers() |
|
438 | - { |
|
439 | - // Exporting triggers one by one |
|
440 | - foreach ($this->triggers as $trigger) { |
|
441 | - $this->getTriggerStructure($trigger); |
|
442 | - } |
|
443 | - } |
|
444 | - |
|
445 | - |
|
446 | - private function getTableStructure($tableName) |
|
447 | - { |
|
448 | - if (!$this->dumpSettings['no-create-info']) { |
|
449 | - $ret = ''; |
|
450 | - if (!$this->dumpSettings['skip-comments']) { |
|
451 | - $ret = "--" . PHP_EOL . |
|
452 | - "-- Table structure for table `$tableName`" . PHP_EOL . |
|
453 | - "--" . PHP_EOL . PHP_EOL; |
|
454 | - } |
|
455 | - $stmt = $this->typeAdapter->show_create_table($tableName); |
|
456 | - foreach ($this->dbHandler->query($stmt) as $r) { |
|
457 | - $this->compressManager->write($ret); |
|
458 | - if ($this->dumpSettings['add-drop-table']) { |
|
459 | - $this->compressManager->write( |
|
460 | - $this->typeAdapter->drop_table($tableName) |
|
461 | - ); |
|
462 | - } |
|
463 | - $this->compressManager->write( |
|
464 | - $this->typeAdapter->create_table($r, $this->dumpSettings) |
|
465 | - ); |
|
466 | - break; |
|
467 | - } |
|
468 | - } |
|
469 | - |
|
470 | - $columnTypes = array(); |
|
471 | - $columns = $this->dbHandler->query( |
|
472 | - $this->typeAdapter->show_columns($tableName) |
|
473 | - ); |
|
474 | - $columns->setFetchMode(PDO::FETCH_ASSOC); |
|
475 | - |
|
476 | - foreach($columns as $key => $col) { |
|
477 | - $types = $this->typeAdapter->parseColumnType($col); |
|
478 | - $columnTypes[$col['Field']] = array( |
|
479 | - 'is_numeric'=> $types['is_numeric'], |
|
480 | - 'is_blob' => $types['is_blob'], |
|
481 | - 'type' => $types['type'] |
|
482 | - ); |
|
483 | - } |
|
484 | - $this->tableColumnTypes[$tableName] = $columnTypes; |
|
485 | - return; |
|
486 | - } |
|
487 | - |
|
488 | - |
|
489 | - private function getViewStructure($viewName) |
|
490 | - { |
|
491 | - $ret = ''; |
|
492 | - if (!$this->dumpSettings['skip-comments']) { |
|
493 | - $ret = "--" . PHP_EOL . |
|
494 | - "-- Table structure for view `${viewName}`" . PHP_EOL . |
|
495 | - "--" . PHP_EOL . PHP_EOL; |
|
496 | - } |
|
497 | - $this->compressManager->write($ret); |
|
498 | - $stmt = $this->typeAdapter->show_create_view($viewName); |
|
499 | - foreach ($this->dbHandler->query($stmt) as $r) { |
|
500 | - if ($this->dumpSettings['add-drop-table']) { |
|
501 | - $this->compressManager->write( |
|
502 | - $this->typeAdapter->drop_view($viewName) |
|
503 | - ); |
|
504 | - } |
|
505 | - $this->compressManager->write( |
|
506 | - $this->typeAdapter->create_view($r) |
|
507 | - ); |
|
508 | - break; |
|
509 | - } |
|
510 | - } |
|
511 | - |
|
512 | - /** |
|
513 | - * Trigger structure extractor |
|
514 | - * |
|
515 | - * @param string $triggerName Name of trigger to export |
|
516 | - * @return null |
|
517 | - */ |
|
518 | - private function getTriggerStructure($triggerName) |
|
519 | - { |
|
520 | - $stmt = $this->typeAdapter->show_create_trigger($triggerName); |
|
521 | - foreach ($this->dbHandler->query($stmt) as $r) { |
|
522 | - if ($this->dumpSettings['add-drop-trigger']) { |
|
523 | - $this->compressManager->write( |
|
524 | - $this->typeAdapter->add_drop_trigger($triggerName) |
|
525 | - ); |
|
526 | - } |
|
527 | - $this->compressManager->write( |
|
528 | - $this->typeAdapter->create_trigger($r) |
|
529 | - ); |
|
530 | - return; |
|
531 | - } |
|
532 | - |
|
533 | - } |
|
534 | - |
|
535 | - |
|
536 | - /** |
|
537 | - * Escape values with quotes when needed |
|
538 | - * |
|
539 | - * @param string $tableName Name of table which contains rows |
|
540 | - * @param array $row Associative array of column names and values to be quoted |
|
541 | - * |
|
542 | - * @return string |
|
543 | - */ |
|
544 | - private function escape($tableName, $row) |
|
545 | - { |
|
546 | - $ret = array(); |
|
547 | - $columnTypes = $this->tableColumnTypes[$tableName]; |
|
548 | - foreach ($row as $colName => $colValue) { |
|
549 | - if (is_null($colValue)) { |
|
550 | - $ret[] = "NULL"; |
|
551 | - } elseif ($this->dumpSettings['hex-blob'] && $columnTypes[$colName]['is_blob']) { |
|
552 | - if ($columnTypes[$colName]['type'] == 'bit' || !empty($colValue)) { |
|
553 | - $ret[] = "0x${colValue}"; |
|
554 | - } else { |
|
555 | - $ret[] = "''"; |
|
556 | - } |
|
557 | - } elseif ($columnTypes[$colName]['is_numeric']) { |
|
558 | - $ret[] = $colValue; |
|
559 | - } else { |
|
560 | - $ret[] = $this->dbHandler->quote($colValue); |
|
561 | - } |
|
562 | - } |
|
563 | - return $ret; |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * Table rows extractor |
|
568 | - * |
|
569 | - * @param string $tableName Name of table to export |
|
570 | - * |
|
571 | - * @return null |
|
572 | - */ |
|
573 | - private function listValues($tableName) |
|
574 | - { |
|
575 | - $this->prepareListValues($tableName); |
|
576 | - |
|
577 | - $onlyOnce = true; |
|
578 | - $lineSize = 0; |
|
579 | - |
|
580 | - $colStmt = $this->getColumnStmt($tableName); |
|
581 | - $stmt = "SELECT $colStmt FROM `$tableName`"; |
|
582 | - |
|
583 | - if ($this->dumpSettings['where']) { |
|
584 | - $stmt .= " WHERE {$this->dumpSettings['where']}"; |
|
585 | - } |
|
586 | - $resultSet = $this->dbHandler->query($stmt); |
|
587 | - $resultSet->setFetchMode(PDO::FETCH_ASSOC); |
|
588 | - |
|
589 | - foreach ($resultSet as $row) { |
|
590 | - $vals = $this->escape($tableName, $row); |
|
591 | - if ($onlyOnce || !$this->dumpSettings['extended-insert']) { |
|
592 | - $lineSize += $this->compressManager->write( |
|
593 | - "INSERT INTO `$tableName` VALUES (" . implode(",", $vals) . ")" |
|
594 | - ); |
|
595 | - $onlyOnce = false; |
|
596 | - } else { |
|
597 | - $lineSize += $this->compressManager->write(",(" . implode(",", $vals) . ")"); |
|
598 | - } |
|
599 | - if (($lineSize > self::MAXLINESIZE) || |
|
600 | - !$this->dumpSettings['extended-insert']) { |
|
601 | - $onlyOnce = true; |
|
602 | - $lineSize = $this->compressManager->write(";" . PHP_EOL); |
|
603 | - } |
|
604 | - } |
|
605 | - $resultSet->closeCursor(); |
|
606 | - |
|
607 | - if (!$onlyOnce) { |
|
608 | - $this->compressManager->write(";" . PHP_EOL); |
|
609 | - } |
|
610 | - |
|
611 | - $this->endListValues($tableName); |
|
612 | - } |
|
613 | - |
|
614 | - /** |
|
615 | - * Table rows extractor, append information prior to dump |
|
616 | - * |
|
617 | - * @param string $tableName Name of table to export |
|
618 | - * |
|
619 | - * @return null |
|
620 | - */ |
|
621 | - function prepareListValues($tableName) |
|
622 | - { |
|
623 | - if (!$this->dumpSettings['skip-comments']) { |
|
624 | - $this->compressManager->write( |
|
625 | - "--" . PHP_EOL . |
|
626 | - "-- Dumping data for table `$tableName`" . PHP_EOL . |
|
627 | - "--" . PHP_EOL . PHP_EOL |
|
628 | - ); |
|
629 | - } |
|
630 | - |
|
631 | - if ($this->dumpSettings['single-transaction']) { |
|
632 | - $this->dbHandler->exec($this->typeAdapter->setup_transaction()); |
|
633 | - $this->dbHandler->exec($this->typeAdapter->start_transaction()); |
|
634 | - } |
|
635 | - |
|
636 | - if ($this->dumpSettings['lock-tables']) { |
|
637 | - $this->typeAdapter->lock_table($tableName); |
|
638 | - } |
|
639 | - |
|
640 | - if ($this->dumpSettings['add-locks']) { |
|
641 | - $this->compressManager->write( |
|
642 | - $this->typeAdapter->start_add_lock_table($tableName) |
|
643 | - ); |
|
644 | - } |
|
645 | - |
|
646 | - if ($this->dumpSettings['disable-keys']) { |
|
647 | - $this->compressManager->write( |
|
648 | - $this->typeAdapter->start_add_disable_keys($tableName) |
|
649 | - ); |
|
650 | - } |
|
651 | - |
|
652 | - // Disable autocommit for faster reload |
|
653 | - if ($this->dumpSettings['no-autocommit']) { |
|
654 | - $this->compressManager->write( |
|
655 | - $this->typeAdapter->start_disable_autocommit() |
|
656 | - ); |
|
657 | - } |
|
658 | - |
|
659 | - return; |
|
660 | - } |
|
661 | - |
|
662 | - /** |
|
663 | - * Table rows extractor, close locks and commits after dump |
|
664 | - * |
|
665 | - * @param string $tableName Name of table to export |
|
666 | - * |
|
667 | - * @return null |
|
668 | - */ |
|
669 | - function endListValues($tableName) |
|
670 | - { |
|
671 | - if ($this->dumpSettings['disable-keys']) { |
|
672 | - $this->compressManager->write( |
|
673 | - $this->typeAdapter->end_add_disable_keys($tableName) |
|
674 | - ); |
|
675 | - } |
|
676 | - |
|
677 | - if ($this->dumpSettings['add-locks']) { |
|
678 | - $this->compressManager->write( |
|
679 | - $this->typeAdapter->end_add_lock_table($tableName) |
|
680 | - ); |
|
681 | - } |
|
682 | - |
|
683 | - if ($this->dumpSettings['single-transaction']) { |
|
684 | - $this->dbHandler->exec($this->typeAdapter->commit_transaction()); |
|
685 | - } |
|
686 | - |
|
687 | - if ($this->dumpSettings['lock-tables']) { |
|
688 | - $this->typeAdapter->unlock_table($tableName); |
|
689 | - } |
|
690 | - |
|
691 | - // Commit to enable autocommit |
|
692 | - if ($this->dumpSettings['no-autocommit']) { |
|
693 | - $this->compressManager->write( |
|
694 | - $this->typeAdapter->end_disable_autocommit() |
|
695 | - ); |
|
696 | - } |
|
697 | - |
|
698 | - $this->compressManager->write(PHP_EOL); |
|
699 | - |
|
700 | - return; |
|
701 | - } |
|
702 | - |
|
703 | - /** |
|
704 | - * Build SQL List of all columns on current table |
|
705 | - * |
|
706 | - * @param string $tableName Name of table to get columns |
|
707 | - * |
|
708 | - * @return string SQL sentence with columns |
|
709 | - */ |
|
710 | - function getColumnStmt($tableName) |
|
711 | - { |
|
712 | - $colStmt = array(); |
|
713 | - foreach($this->tableColumnTypes[$tableName] as $colName => $colType) { |
|
714 | - if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { |
|
715 | - $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; |
|
716 | - } else if ($colType['is_blob'] && $this->dumpSettings['hex-blob']) { |
|
717 | - $colStmt[] = "HEX(`${colName}`) AS `${colName}`"; |
|
718 | - } else { |
|
719 | - $colStmt[] = "`${colName}`"; |
|
720 | - } |
|
721 | - } |
|
722 | - $colStmt = implode($colStmt, ","); |
|
723 | - |
|
724 | - return $colStmt; |
|
725 | - } |
|
36 | + // Same as mysqldump |
|
37 | + const MAXLINESIZE = 1000000; |
|
38 | + |
|
39 | + // Available compression methods as constants |
|
40 | + const GZIP = 'Gzip'; |
|
41 | + const BZIP2 = 'Bzip2'; |
|
42 | + const NONE = 'None'; |
|
43 | + |
|
44 | + // Available connection strings |
|
45 | + const UTF8 = 'utf8'; |
|
46 | + const UTF8MB4 = 'utf8mb4'; |
|
47 | + |
|
48 | + // This can be set both on constructor or manually |
|
49 | + public $host; |
|
50 | + public $port; |
|
51 | + public $user; |
|
52 | + public $pass; |
|
53 | + public $db; |
|
54 | + public $fileName; |
|
55 | + |
|
56 | + // Internal stuff |
|
57 | + private $tables = array(); |
|
58 | + private $views = array(); |
|
59 | + private $triggers = array(); |
|
60 | + private $dbHandler; |
|
61 | + private $dbType; |
|
62 | + private $compressManager; |
|
63 | + private $typeAdapter; |
|
64 | + private $dumpSettings = array(); |
|
65 | + private $pdoSettings = array(); |
|
66 | + private $version; |
|
67 | + private $tableColumnTypes = array(); |
|
68 | + |
|
69 | + /** |
|
70 | + * Constructor of Mysqldump. Note that in the case of an SQLite database |
|
71 | + * connection, the filename must be in the $db parameter. |
|
72 | + * |
|
73 | + * @param string $db Database name |
|
74 | + * @param string $user SQL account username |
|
75 | + * @param string $pass SQL account password |
|
76 | + * @param string $host SQL server to connect to |
|
77 | + * @param string $type SQL database type |
|
78 | + * @param array $dumpSettings SQL database settings |
|
79 | + * @param array $pdoSettings PDO configured attributes |
|
80 | + */ |
|
81 | + public function __construct( |
|
82 | + $db = '', |
|
83 | + $user = '', |
|
84 | + $pass = '', |
|
85 | + $host = 'localhost', |
|
86 | + $type = 'mysql', |
|
87 | + $dumpSettings = array(), |
|
88 | + $pdoSettings = array() |
|
89 | + ) { |
|
90 | + $dumpSettingsDefault = array( |
|
91 | + 'include-tables' => array(), |
|
92 | + 'exclude-tables' => array(), |
|
93 | + 'compress' => Mysqldump::NONE, |
|
94 | + 'no-data' => false, |
|
95 | + 'add-drop-table' => false, |
|
96 | + 'single-transaction' => true, |
|
97 | + 'lock-tables' => true, |
|
98 | + 'add-locks' => true, |
|
99 | + 'extended-insert' => true, |
|
100 | + 'disable-keys' => true, |
|
101 | + 'where' => '', |
|
102 | + 'no-create-info' => false, |
|
103 | + 'skip-triggers' => false, |
|
104 | + 'add-drop-trigger' => true, |
|
105 | + 'hex-blob' => true, |
|
106 | + 'databases' => false, |
|
107 | + 'add-drop-database' => false, |
|
108 | + 'skip-tz-utz' => false, |
|
109 | + 'no-autocommit' => true, |
|
110 | + 'default-character-set' => Mysqldump::UTF8, |
|
111 | + 'skip-comments' => false, |
|
112 | + 'skip-dump-date' => false, |
|
113 | + /* deprecated */ |
|
114 | + 'disable-foreign-keys-check' => true |
|
115 | + ); |
|
116 | + |
|
117 | + $pdoSettingsDefault = array( |
|
118 | + PDO::ATTR_PERSISTENT => true, |
|
119 | + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
|
120 | + PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false |
|
121 | + ); |
|
122 | + |
|
123 | + $this->db = $db; |
|
124 | + $this->user = $user; |
|
125 | + $this->pass = $pass; |
|
126 | + |
|
127 | + $colonPos = strpos($host, ':'); |
|
128 | + if (false !== $colonPos) { |
|
129 | + $this->port = substr($host, $colonPos + 1); |
|
130 | + $this->host = substr($host, 0, $colonPos); |
|
131 | + } else { |
|
132 | + $this->port = null; |
|
133 | + $this->host = $host; |
|
134 | + } |
|
135 | + |
|
136 | + $this->dbType = strtolower($type); |
|
137 | + $this->pdoSettings = self::array_replace_recursive($pdoSettingsDefault, $pdoSettings); |
|
138 | + $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings); |
|
139 | + |
|
140 | + if (!isset($this->pdoSettings[PDO::MYSQL_ATTR_INIT_COMMAND])) { |
|
141 | + $this->pdoSettings[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES " . $this->dumpSettings['default-character-set']; |
|
142 | + } |
|
143 | + |
|
144 | + $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); |
|
145 | + if (count($diff)>0) { |
|
146 | + throw new Exception("Unexpected value in dumpSettings: (" . implode(",", $diff) . ")"); |
|
147 | + } |
|
148 | + |
|
149 | + // Create a new compressManager to manage compressed output |
|
150 | + $this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Custom array_replace_recursive to be used if PHP < 5.3 |
|
155 | + * Replaces elements from passed arrays into the first array recursively |
|
156 | + * |
|
157 | + * @param array $array1 The array in which elements are replaced |
|
158 | + * @param array $array2 The array from which elements will be extracted |
|
159 | + * |
|
160 | + * @return array Returns an array, or NULL if an error occurs. |
|
161 | + */ |
|
162 | + public static function array_replace_recursive($array1, $array2) |
|
163 | + { |
|
164 | + if (function_exists('array_replace_recursive')) { |
|
165 | + return array_replace_recursive($array1, $array2); |
|
166 | + } |
|
167 | + |
|
168 | + foreach ($array2 as $key => $value) { |
|
169 | + if (is_array($value)) { |
|
170 | + $array1[$key] = self::array_replace_recursive($array1[$key], $value); |
|
171 | + } else { |
|
172 | + $array1[$key] = $value; |
|
173 | + } |
|
174 | + } |
|
175 | + return $array1; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Connect with PDO |
|
180 | + * |
|
181 | + * @return null |
|
182 | + */ |
|
183 | + private function connect() |
|
184 | + { |
|
185 | + // Connecting with PDO |
|
186 | + try { |
|
187 | + switch ($this->dbType) { |
|
188 | + case 'sqlite': |
|
189 | + $this->dbHandler = @new PDO("sqlite:" . $this->db, null, null, $this->pdoSettings); |
|
190 | + break; |
|
191 | + case 'mysql': |
|
192 | + case 'pgsql': |
|
193 | + case 'dblib': |
|
194 | + $dsn = $this->dbType . |
|
195 | + ":host=" . $this->host . |
|
196 | + (isset($this->port) ? ";port=" . $this->port : "") . |
|
197 | + ";dbname=" . $this->db; |
|
198 | + $this->dbHandler = @new PDO( |
|
199 | + $dsn, |
|
200 | + $this->user, |
|
201 | + $this->pass, |
|
202 | + $this->pdoSettings |
|
203 | + ); |
|
204 | + // Fix for always-unicode output |
|
205 | + $this->dbHandler->exec("SET NAMES " . $this->dumpSettings['default-character-set']); |
|
206 | + // Store server version |
|
207 | + $this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION); |
|
208 | + break; |
|
209 | + default: |
|
210 | + throw new Exception("Unsupported database type (" . $this->dbType . ")"); |
|
211 | + } |
|
212 | + } catch (PDOException $e) { |
|
213 | + throw new Exception( |
|
214 | + "Connection to " . $this->dbType . " failed with message: " . |
|
215 | + $e->getMessage() |
|
216 | + ); |
|
217 | + } |
|
218 | + |
|
219 | + $this->dbHandler->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
|
220 | + $this->typeAdapter = TypeAdapterFactory::create($this->dbType, $this->dbHandler); |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Main call |
|
225 | + * |
|
226 | + * @param string $filename Name of file to write sql dump to |
|
227 | + * @return null |
|
228 | + */ |
|
229 | + public function start($filename = '') |
|
230 | + { |
|
231 | + // Output file can be redefined here |
|
232 | + if (!empty($filename)) { |
|
233 | + $this->fileName = $filename; |
|
234 | + } |
|
235 | + // We must set a name to continue |
|
236 | + if (empty($this->fileName)) { |
|
237 | + throw new Exception("Output file name is not set"); |
|
238 | + } |
|
239 | + |
|
240 | + // Connect to database |
|
241 | + $this->connect(); |
|
242 | + |
|
243 | + // Create output file |
|
244 | + $this->compressManager->open($this->fileName); |
|
245 | + |
|
246 | + // Write some basic info to output file |
|
247 | + $this->compressManager->write($this->getDumpFileHeader()); |
|
248 | + |
|
249 | + // Store server settings and use sanner defaults to dump |
|
250 | + $this->compressManager->write( |
|
251 | + $this->typeAdapter->backup_parameters($this->dumpSettings) |
|
252 | + ); |
|
253 | + |
|
254 | + if ($this->dumpSettings['databases']) { |
|
255 | + $this->compressManager->write( |
|
256 | + $this->typeAdapter->getDatabaseHeader($this->db) |
|
257 | + ); |
|
258 | + if ($this->dumpSettings['add-drop-database']) { |
|
259 | + $this->compressManager->write( |
|
260 | + $this->typeAdapter->add_drop_database($this->db) |
|
261 | + ); |
|
262 | + } |
|
263 | + } |
|
264 | + |
|
265 | + // Get table, view and trigger structures from database |
|
266 | + $this->getDatabaseStructure(); |
|
267 | + |
|
268 | + if ($this->dumpSettings['databases']) { |
|
269 | + $this->compressManager->write( |
|
270 | + $this->typeAdapter->databases($this->db) |
|
271 | + ); |
|
272 | + } |
|
273 | + |
|
274 | + // If there still are some tables/views in include-tables array, |
|
275 | + // that means that some tables or views weren't found. |
|
276 | + // Give proper error and exit. |
|
277 | + if (0 < count($this->dumpSettings['include-tables'])) { |
|
278 | + $name = implode(",", $this->dumpSettings['include-tables']); |
|
279 | + throw new Exception("Table or View (" . $name . ") not found in database"); |
|
280 | + } |
|
281 | + |
|
282 | + $this->exportTables(); |
|
283 | + $this->exportViews(); |
|
284 | + $this->exportTriggers(); |
|
285 | + |
|
286 | + // Restore saved parameters |
|
287 | + $this->compressManager->write( |
|
288 | + $this->typeAdapter->restore_parameters($this->dumpSettings) |
|
289 | + ); |
|
290 | + // Write some stats to output file |
|
291 | + $this->compressManager->write($this->getDumpFileFooter()); |
|
292 | + // Close output file |
|
293 | + $this->compressManager->close(); |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Returns header for dump file |
|
298 | + * |
|
299 | + * @return string |
|
300 | + */ |
|
301 | + private function getDumpFileHeader() |
|
302 | + { |
|
303 | + $header = ''; |
|
304 | + if (!$this->dumpSettings['skip-comments']) { |
|
305 | + // Some info about software, source and time |
|
306 | + $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php" . PHP_EOL . |
|
307 | + "--" . PHP_EOL . |
|
308 | + "-- Host: {$this->host}\tDatabase: {$this->db}" . PHP_EOL . |
|
309 | + "-- ------------------------------------------------------" . PHP_EOL; |
|
310 | + |
|
311 | + if (!empty($this->version)) { |
|
312 | + $header .= "-- Server version \t" . $this->version . PHP_EOL; |
|
313 | + } |
|
314 | + |
|
315 | + $header .= "-- Date: " . date('r') . PHP_EOL . PHP_EOL; |
|
316 | + } |
|
317 | + return $header; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Returns footer for dump file |
|
322 | + * |
|
323 | + * @return string |
|
324 | + */ |
|
325 | + private function getDumpFileFooter() |
|
326 | + { |
|
327 | + $footer = ''; |
|
328 | + if (!$this->dumpSettings['skip-comments']) { |
|
329 | + $footer .= '-- Dump completed'; |
|
330 | + if (!$this->dumpSettings['skip-dump-date']) { |
|
331 | + $footer .= ' on: ' . date('r'); |
|
332 | + } |
|
333 | + $footer .= PHP_EOL; |
|
334 | + } |
|
335 | + |
|
336 | + return $footer; |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Reads table and views names from database. |
|
341 | + * Fills $this->tables array so they will be dumped later. |
|
342 | + * |
|
343 | + * @return null |
|
344 | + */ |
|
345 | + private function getDatabaseStructure() |
|
346 | + { |
|
347 | + // Listing all tables from database |
|
348 | + if (empty($this->dumpSettings['include-tables'])) { |
|
349 | + // include all tables for now, blacklisting happens later |
|
350 | + foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->db)) as $row) { |
|
351 | + array_push($this->tables, current($row)); |
|
352 | + } |
|
353 | + } else { |
|
354 | + // include only the tables mentioned in include-tables |
|
355 | + foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->db)) as $row) { |
|
356 | + if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
|
357 | + array_push($this->tables, current($row)); |
|
358 | + $elem = array_search( |
|
359 | + current($row), |
|
360 | + $this->dumpSettings['include-tables'] |
|
361 | + ); |
|
362 | + unset($this->dumpSettings['include-tables'][$elem]); |
|
363 | + } |
|
364 | + } |
|
365 | + } |
|
366 | + |
|
367 | + // Listing all views from database |
|
368 | + if (empty($this->dumpSettings['include-tables'])) { |
|
369 | + // include all views for now, blacklisting happens later |
|
370 | + foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->db)) as $row) { |
|
371 | + array_push($this->views, current($row)); |
|
372 | + } |
|
373 | + } else { |
|
374 | + // include only the tables mentioned in include-tables |
|
375 | + foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->db)) as $row) { |
|
376 | + if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
|
377 | + array_push($this->views, current($row)); |
|
378 | + $elem = array_search( |
|
379 | + current($row), |
|
380 | + $this->dumpSettings['include-tables'] |
|
381 | + ); |
|
382 | + unset($this->dumpSettings['include-tables'][$elem]); |
|
383 | + } |
|
384 | + } |
|
385 | + } |
|
386 | + |
|
387 | + // Listing all triggers from database |
|
388 | + if (false === $this->dumpSettings['skip-triggers']) { |
|
389 | + foreach ($this->dbHandler->query($this->typeAdapter->show_triggers($this->db)) as $row) { |
|
390 | + array_push($this->triggers, $row['Trigger']); |
|
391 | + } |
|
392 | + } |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Exports all the tables selected from database |
|
397 | + * |
|
398 | + * @return null |
|
399 | + */ |
|
400 | + private function exportTables() |
|
401 | + { |
|
402 | + // Exporting tables one by one |
|
403 | + foreach ($this->tables as $table) { |
|
404 | + if (in_array($table, $this->dumpSettings['exclude-tables'], true)) { |
|
405 | + continue; |
|
406 | + } |
|
407 | + $this->getTableStructure($table); |
|
408 | + if (false === $this->dumpSettings['no-data']) { |
|
409 | + $this->listValues($table); |
|
410 | + } |
|
411 | + } |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Exports all the views found in database |
|
416 | + * |
|
417 | + * @return null |
|
418 | + */ |
|
419 | + private function exportViews() |
|
420 | + { |
|
421 | + if (false === $this->dumpSettings['no-create-info']) { |
|
422 | + // Exporting views one by one |
|
423 | + foreach ($this->views as $view) { |
|
424 | + if (in_array($view, $this->dumpSettings['exclude-tables'], true)) { |
|
425 | + continue; |
|
426 | + } |
|
427 | + $this->getViewStructure($view); |
|
428 | + } |
|
429 | + } |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * Exports all the triggers found in database |
|
434 | + * |
|
435 | + * @return null |
|
436 | + */ |
|
437 | + private function exportTriggers() |
|
438 | + { |
|
439 | + // Exporting triggers one by one |
|
440 | + foreach ($this->triggers as $trigger) { |
|
441 | + $this->getTriggerStructure($trigger); |
|
442 | + } |
|
443 | + } |
|
444 | + |
|
445 | + |
|
446 | + private function getTableStructure($tableName) |
|
447 | + { |
|
448 | + if (!$this->dumpSettings['no-create-info']) { |
|
449 | + $ret = ''; |
|
450 | + if (!$this->dumpSettings['skip-comments']) { |
|
451 | + $ret = "--" . PHP_EOL . |
|
452 | + "-- Table structure for table `$tableName`" . PHP_EOL . |
|
453 | + "--" . PHP_EOL . PHP_EOL; |
|
454 | + } |
|
455 | + $stmt = $this->typeAdapter->show_create_table($tableName); |
|
456 | + foreach ($this->dbHandler->query($stmt) as $r) { |
|
457 | + $this->compressManager->write($ret); |
|
458 | + if ($this->dumpSettings['add-drop-table']) { |
|
459 | + $this->compressManager->write( |
|
460 | + $this->typeAdapter->drop_table($tableName) |
|
461 | + ); |
|
462 | + } |
|
463 | + $this->compressManager->write( |
|
464 | + $this->typeAdapter->create_table($r, $this->dumpSettings) |
|
465 | + ); |
|
466 | + break; |
|
467 | + } |
|
468 | + } |
|
469 | + |
|
470 | + $columnTypes = array(); |
|
471 | + $columns = $this->dbHandler->query( |
|
472 | + $this->typeAdapter->show_columns($tableName) |
|
473 | + ); |
|
474 | + $columns->setFetchMode(PDO::FETCH_ASSOC); |
|
475 | + |
|
476 | + foreach($columns as $key => $col) { |
|
477 | + $types = $this->typeAdapter->parseColumnType($col); |
|
478 | + $columnTypes[$col['Field']] = array( |
|
479 | + 'is_numeric'=> $types['is_numeric'], |
|
480 | + 'is_blob' => $types['is_blob'], |
|
481 | + 'type' => $types['type'] |
|
482 | + ); |
|
483 | + } |
|
484 | + $this->tableColumnTypes[$tableName] = $columnTypes; |
|
485 | + return; |
|
486 | + } |
|
487 | + |
|
488 | + |
|
489 | + private function getViewStructure($viewName) |
|
490 | + { |
|
491 | + $ret = ''; |
|
492 | + if (!$this->dumpSettings['skip-comments']) { |
|
493 | + $ret = "--" . PHP_EOL . |
|
494 | + "-- Table structure for view `${viewName}`" . PHP_EOL . |
|
495 | + "--" . PHP_EOL . PHP_EOL; |
|
496 | + } |
|
497 | + $this->compressManager->write($ret); |
|
498 | + $stmt = $this->typeAdapter->show_create_view($viewName); |
|
499 | + foreach ($this->dbHandler->query($stmt) as $r) { |
|
500 | + if ($this->dumpSettings['add-drop-table']) { |
|
501 | + $this->compressManager->write( |
|
502 | + $this->typeAdapter->drop_view($viewName) |
|
503 | + ); |
|
504 | + } |
|
505 | + $this->compressManager->write( |
|
506 | + $this->typeAdapter->create_view($r) |
|
507 | + ); |
|
508 | + break; |
|
509 | + } |
|
510 | + } |
|
511 | + |
|
512 | + /** |
|
513 | + * Trigger structure extractor |
|
514 | + * |
|
515 | + * @param string $triggerName Name of trigger to export |
|
516 | + * @return null |
|
517 | + */ |
|
518 | + private function getTriggerStructure($triggerName) |
|
519 | + { |
|
520 | + $stmt = $this->typeAdapter->show_create_trigger($triggerName); |
|
521 | + foreach ($this->dbHandler->query($stmt) as $r) { |
|
522 | + if ($this->dumpSettings['add-drop-trigger']) { |
|
523 | + $this->compressManager->write( |
|
524 | + $this->typeAdapter->add_drop_trigger($triggerName) |
|
525 | + ); |
|
526 | + } |
|
527 | + $this->compressManager->write( |
|
528 | + $this->typeAdapter->create_trigger($r) |
|
529 | + ); |
|
530 | + return; |
|
531 | + } |
|
532 | + |
|
533 | + } |
|
534 | + |
|
535 | + |
|
536 | + /** |
|
537 | + * Escape values with quotes when needed |
|
538 | + * |
|
539 | + * @param string $tableName Name of table which contains rows |
|
540 | + * @param array $row Associative array of column names and values to be quoted |
|
541 | + * |
|
542 | + * @return string |
|
543 | + */ |
|
544 | + private function escape($tableName, $row) |
|
545 | + { |
|
546 | + $ret = array(); |
|
547 | + $columnTypes = $this->tableColumnTypes[$tableName]; |
|
548 | + foreach ($row as $colName => $colValue) { |
|
549 | + if (is_null($colValue)) { |
|
550 | + $ret[] = "NULL"; |
|
551 | + } elseif ($this->dumpSettings['hex-blob'] && $columnTypes[$colName]['is_blob']) { |
|
552 | + if ($columnTypes[$colName]['type'] == 'bit' || !empty($colValue)) { |
|
553 | + $ret[] = "0x${colValue}"; |
|
554 | + } else { |
|
555 | + $ret[] = "''"; |
|
556 | + } |
|
557 | + } elseif ($columnTypes[$colName]['is_numeric']) { |
|
558 | + $ret[] = $colValue; |
|
559 | + } else { |
|
560 | + $ret[] = $this->dbHandler->quote($colValue); |
|
561 | + } |
|
562 | + } |
|
563 | + return $ret; |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * Table rows extractor |
|
568 | + * |
|
569 | + * @param string $tableName Name of table to export |
|
570 | + * |
|
571 | + * @return null |
|
572 | + */ |
|
573 | + private function listValues($tableName) |
|
574 | + { |
|
575 | + $this->prepareListValues($tableName); |
|
576 | + |
|
577 | + $onlyOnce = true; |
|
578 | + $lineSize = 0; |
|
579 | + |
|
580 | + $colStmt = $this->getColumnStmt($tableName); |
|
581 | + $stmt = "SELECT $colStmt FROM `$tableName`"; |
|
582 | + |
|
583 | + if ($this->dumpSettings['where']) { |
|
584 | + $stmt .= " WHERE {$this->dumpSettings['where']}"; |
|
585 | + } |
|
586 | + $resultSet = $this->dbHandler->query($stmt); |
|
587 | + $resultSet->setFetchMode(PDO::FETCH_ASSOC); |
|
588 | + |
|
589 | + foreach ($resultSet as $row) { |
|
590 | + $vals = $this->escape($tableName, $row); |
|
591 | + if ($onlyOnce || !$this->dumpSettings['extended-insert']) { |
|
592 | + $lineSize += $this->compressManager->write( |
|
593 | + "INSERT INTO `$tableName` VALUES (" . implode(",", $vals) . ")" |
|
594 | + ); |
|
595 | + $onlyOnce = false; |
|
596 | + } else { |
|
597 | + $lineSize += $this->compressManager->write(",(" . implode(",", $vals) . ")"); |
|
598 | + } |
|
599 | + if (($lineSize > self::MAXLINESIZE) || |
|
600 | + !$this->dumpSettings['extended-insert']) { |
|
601 | + $onlyOnce = true; |
|
602 | + $lineSize = $this->compressManager->write(";" . PHP_EOL); |
|
603 | + } |
|
604 | + } |
|
605 | + $resultSet->closeCursor(); |
|
606 | + |
|
607 | + if (!$onlyOnce) { |
|
608 | + $this->compressManager->write(";" . PHP_EOL); |
|
609 | + } |
|
610 | + |
|
611 | + $this->endListValues($tableName); |
|
612 | + } |
|
613 | + |
|
614 | + /** |
|
615 | + * Table rows extractor, append information prior to dump |
|
616 | + * |
|
617 | + * @param string $tableName Name of table to export |
|
618 | + * |
|
619 | + * @return null |
|
620 | + */ |
|
621 | + function prepareListValues($tableName) |
|
622 | + { |
|
623 | + if (!$this->dumpSettings['skip-comments']) { |
|
624 | + $this->compressManager->write( |
|
625 | + "--" . PHP_EOL . |
|
626 | + "-- Dumping data for table `$tableName`" . PHP_EOL . |
|
627 | + "--" . PHP_EOL . PHP_EOL |
|
628 | + ); |
|
629 | + } |
|
630 | + |
|
631 | + if ($this->dumpSettings['single-transaction']) { |
|
632 | + $this->dbHandler->exec($this->typeAdapter->setup_transaction()); |
|
633 | + $this->dbHandler->exec($this->typeAdapter->start_transaction()); |
|
634 | + } |
|
635 | + |
|
636 | + if ($this->dumpSettings['lock-tables']) { |
|
637 | + $this->typeAdapter->lock_table($tableName); |
|
638 | + } |
|
639 | + |
|
640 | + if ($this->dumpSettings['add-locks']) { |
|
641 | + $this->compressManager->write( |
|
642 | + $this->typeAdapter->start_add_lock_table($tableName) |
|
643 | + ); |
|
644 | + } |
|
645 | + |
|
646 | + if ($this->dumpSettings['disable-keys']) { |
|
647 | + $this->compressManager->write( |
|
648 | + $this->typeAdapter->start_add_disable_keys($tableName) |
|
649 | + ); |
|
650 | + } |
|
651 | + |
|
652 | + // Disable autocommit for faster reload |
|
653 | + if ($this->dumpSettings['no-autocommit']) { |
|
654 | + $this->compressManager->write( |
|
655 | + $this->typeAdapter->start_disable_autocommit() |
|
656 | + ); |
|
657 | + } |
|
658 | + |
|
659 | + return; |
|
660 | + } |
|
661 | + |
|
662 | + /** |
|
663 | + * Table rows extractor, close locks and commits after dump |
|
664 | + * |
|
665 | + * @param string $tableName Name of table to export |
|
666 | + * |
|
667 | + * @return null |
|
668 | + */ |
|
669 | + function endListValues($tableName) |
|
670 | + { |
|
671 | + if ($this->dumpSettings['disable-keys']) { |
|
672 | + $this->compressManager->write( |
|
673 | + $this->typeAdapter->end_add_disable_keys($tableName) |
|
674 | + ); |
|
675 | + } |
|
676 | + |
|
677 | + if ($this->dumpSettings['add-locks']) { |
|
678 | + $this->compressManager->write( |
|
679 | + $this->typeAdapter->end_add_lock_table($tableName) |
|
680 | + ); |
|
681 | + } |
|
682 | + |
|
683 | + if ($this->dumpSettings['single-transaction']) { |
|
684 | + $this->dbHandler->exec($this->typeAdapter->commit_transaction()); |
|
685 | + } |
|
686 | + |
|
687 | + if ($this->dumpSettings['lock-tables']) { |
|
688 | + $this->typeAdapter->unlock_table($tableName); |
|
689 | + } |
|
690 | + |
|
691 | + // Commit to enable autocommit |
|
692 | + if ($this->dumpSettings['no-autocommit']) { |
|
693 | + $this->compressManager->write( |
|
694 | + $this->typeAdapter->end_disable_autocommit() |
|
695 | + ); |
|
696 | + } |
|
697 | + |
|
698 | + $this->compressManager->write(PHP_EOL); |
|
699 | + |
|
700 | + return; |
|
701 | + } |
|
702 | + |
|
703 | + /** |
|
704 | + * Build SQL List of all columns on current table |
|
705 | + * |
|
706 | + * @param string $tableName Name of table to get columns |
|
707 | + * |
|
708 | + * @return string SQL sentence with columns |
|
709 | + */ |
|
710 | + function getColumnStmt($tableName) |
|
711 | + { |
|
712 | + $colStmt = array(); |
|
713 | + foreach($this->tableColumnTypes[$tableName] as $colName => $colType) { |
|
714 | + if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { |
|
715 | + $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; |
|
716 | + } else if ($colType['is_blob'] && $this->dumpSettings['hex-blob']) { |
|
717 | + $colStmt[] = "HEX(`${colName}`) AS `${colName}`"; |
|
718 | + } else { |
|
719 | + $colStmt[] = "`${colName}`"; |
|
720 | + } |
|
721 | + } |
|
722 | + $colStmt = implode($colStmt, ","); |
|
723 | + |
|
724 | + return $colStmt; |
|
725 | + } |
|
726 | 726 | } |
727 | 727 | |
728 | 728 | /** |
@@ -731,137 +731,137 @@ discard block |
||
731 | 731 | */ |
732 | 732 | abstract class CompressMethod |
733 | 733 | { |
734 | - public static $enums = array( |
|
735 | - "None", |
|
736 | - "Gzip", |
|
737 | - "Bzip2" |
|
738 | - ); |
|
739 | - |
|
740 | - /** |
|
741 | - * @param string $c |
|
742 | - * @return boolean |
|
743 | - */ |
|
744 | - public static function isValid($c) |
|
745 | - { |
|
746 | - return in_array($c, self::$enums); |
|
747 | - } |
|
734 | + public static $enums = array( |
|
735 | + "None", |
|
736 | + "Gzip", |
|
737 | + "Bzip2" |
|
738 | + ); |
|
739 | + |
|
740 | + /** |
|
741 | + * @param string $c |
|
742 | + * @return boolean |
|
743 | + */ |
|
744 | + public static function isValid($c) |
|
745 | + { |
|
746 | + return in_array($c, self::$enums); |
|
747 | + } |
|
748 | 748 | } |
749 | 749 | |
750 | 750 | abstract class CompressManagerFactory |
751 | 751 | { |
752 | - /** |
|
753 | - * @param string $c |
|
754 | - * @return CompressBzip2|CompressGzip|CompressNone |
|
755 | - */ |
|
756 | - public static function create($c) |
|
757 | - { |
|
758 | - $c = ucfirst(strtolower($c)); |
|
759 | - if (! CompressMethod::isValid($c)) { |
|
760 | - throw new Exception("Compression method ($c) is not defined yet"); |
|
761 | - } |
|
762 | - |
|
763 | - $method = __NAMESPACE__ . "\\" . "Compress" . $c; |
|
764 | - |
|
765 | - return new $method; |
|
766 | - } |
|
752 | + /** |
|
753 | + * @param string $c |
|
754 | + * @return CompressBzip2|CompressGzip|CompressNone |
|
755 | + */ |
|
756 | + public static function create($c) |
|
757 | + { |
|
758 | + $c = ucfirst(strtolower($c)); |
|
759 | + if (! CompressMethod::isValid($c)) { |
|
760 | + throw new Exception("Compression method ($c) is not defined yet"); |
|
761 | + } |
|
762 | + |
|
763 | + $method = __NAMESPACE__ . "\\" . "Compress" . $c; |
|
764 | + |
|
765 | + return new $method; |
|
766 | + } |
|
767 | 767 | } |
768 | 768 | |
769 | 769 | class CompressBzip2 extends CompressManagerFactory |
770 | 770 | { |
771 | - private $fileHandler = null; |
|
772 | - |
|
773 | - public function __construct() |
|
774 | - { |
|
775 | - if (! function_exists("bzopen")) { |
|
776 | - throw new Exception("Compression is enabled, but bzip2 lib is not installed or configured properly"); |
|
777 | - } |
|
778 | - } |
|
779 | - |
|
780 | - public function open($filename) |
|
781 | - { |
|
782 | - $this->fileHandler = bzopen($filename, "w"); |
|
783 | - if (false === $this->fileHandler) { |
|
784 | - throw new Exception("Output file is not writable"); |
|
785 | - } |
|
786 | - |
|
787 | - return true; |
|
788 | - } |
|
789 | - |
|
790 | - public function write($str) |
|
791 | - { |
|
792 | - if (false === ($bytesWritten = bzwrite($this->fileHandler, $str))) { |
|
793 | - throw new Exception("Writting to file failed! Probably, there is no more free space left?"); |
|
794 | - } |
|
795 | - return $bytesWritten; |
|
796 | - } |
|
797 | - |
|
798 | - public function close() |
|
799 | - { |
|
800 | - return bzclose($this->fileHandler); |
|
801 | - } |
|
771 | + private $fileHandler = null; |
|
772 | + |
|
773 | + public function __construct() |
|
774 | + { |
|
775 | + if (! function_exists("bzopen")) { |
|
776 | + throw new Exception("Compression is enabled, but bzip2 lib is not installed or configured properly"); |
|
777 | + } |
|
778 | + } |
|
779 | + |
|
780 | + public function open($filename) |
|
781 | + { |
|
782 | + $this->fileHandler = bzopen($filename, "w"); |
|
783 | + if (false === $this->fileHandler) { |
|
784 | + throw new Exception("Output file is not writable"); |
|
785 | + } |
|
786 | + |
|
787 | + return true; |
|
788 | + } |
|
789 | + |
|
790 | + public function write($str) |
|
791 | + { |
|
792 | + if (false === ($bytesWritten = bzwrite($this->fileHandler, $str))) { |
|
793 | + throw new Exception("Writting to file failed! Probably, there is no more free space left?"); |
|
794 | + } |
|
795 | + return $bytesWritten; |
|
796 | + } |
|
797 | + |
|
798 | + public function close() |
|
799 | + { |
|
800 | + return bzclose($this->fileHandler); |
|
801 | + } |
|
802 | 802 | } |
803 | 803 | |
804 | 804 | class CompressGzip extends CompressManagerFactory |
805 | 805 | { |
806 | - private $fileHandler = null; |
|
807 | - |
|
808 | - public function __construct() |
|
809 | - { |
|
810 | - if (! function_exists("gzopen")) { |
|
811 | - throw new Exception("Compression is enabled, but gzip lib is not installed or configured properly"); |
|
812 | - } |
|
813 | - } |
|
814 | - |
|
815 | - public function open($filename) |
|
816 | - { |
|
817 | - $this->fileHandler = gzopen($filename, "wb"); |
|
818 | - if (false === $this->fileHandler) { |
|
819 | - throw new Exception("Output file is not writable"); |
|
820 | - } |
|
821 | - |
|
822 | - return true; |
|
823 | - } |
|
824 | - |
|
825 | - public function write($str) |
|
826 | - { |
|
827 | - if (false === ($bytesWritten = gzwrite($this->fileHandler, $str))) { |
|
828 | - throw new Exception("Writting to file failed! Probably, there is no more free space left?"); |
|
829 | - } |
|
830 | - return $bytesWritten; |
|
831 | - } |
|
832 | - |
|
833 | - public function close() |
|
834 | - { |
|
835 | - return gzclose($this->fileHandler); |
|
836 | - } |
|
806 | + private $fileHandler = null; |
|
807 | + |
|
808 | + public function __construct() |
|
809 | + { |
|
810 | + if (! function_exists("gzopen")) { |
|
811 | + throw new Exception("Compression is enabled, but gzip lib is not installed or configured properly"); |
|
812 | + } |
|
813 | + } |
|
814 | + |
|
815 | + public function open($filename) |
|
816 | + { |
|
817 | + $this->fileHandler = gzopen($filename, "wb"); |
|
818 | + if (false === $this->fileHandler) { |
|
819 | + throw new Exception("Output file is not writable"); |
|
820 | + } |
|
821 | + |
|
822 | + return true; |
|
823 | + } |
|
824 | + |
|
825 | + public function write($str) |
|
826 | + { |
|
827 | + if (false === ($bytesWritten = gzwrite($this->fileHandler, $str))) { |
|
828 | + throw new Exception("Writting to file failed! Probably, there is no more free space left?"); |
|
829 | + } |
|
830 | + return $bytesWritten; |
|
831 | + } |
|
832 | + |
|
833 | + public function close() |
|
834 | + { |
|
835 | + return gzclose($this->fileHandler); |
|
836 | + } |
|
837 | 837 | } |
838 | 838 | |
839 | 839 | class CompressNone extends CompressManagerFactory |
840 | 840 | { |
841 | - private $fileHandler = null; |
|
842 | - |
|
843 | - public function open($filename) |
|
844 | - { |
|
845 | - $this->fileHandler = fopen($filename, "wb"); |
|
846 | - if (false === $this->fileHandler) { |
|
847 | - throw new Exception("Output file is not writable"); |
|
848 | - } |
|
849 | - |
|
850 | - return true; |
|
851 | - } |
|
852 | - |
|
853 | - public function write($str) |
|
854 | - { |
|
855 | - if (false === ($bytesWritten = fwrite($this->fileHandler, $str))) { |
|
856 | - throw new Exception("Writting to file failed! Probably, there is no more free space left?"); |
|
857 | - } |
|
858 | - return $bytesWritten; |
|
859 | - } |
|
860 | - |
|
861 | - public function close() |
|
862 | - { |
|
863 | - return fclose($this->fileHandler); |
|
864 | - } |
|
841 | + private $fileHandler = null; |
|
842 | + |
|
843 | + public function open($filename) |
|
844 | + { |
|
845 | + $this->fileHandler = fopen($filename, "wb"); |
|
846 | + if (false === $this->fileHandler) { |
|
847 | + throw new Exception("Output file is not writable"); |
|
848 | + } |
|
849 | + |
|
850 | + return true; |
|
851 | + } |
|
852 | + |
|
853 | + public function write($str) |
|
854 | + { |
|
855 | + if (false === ($bytesWritten = fwrite($this->fileHandler, $str))) { |
|
856 | + throw new Exception("Writting to file failed! Probably, there is no more free space left?"); |
|
857 | + } |
|
858 | + return $bytesWritten; |
|
859 | + } |
|
860 | + |
|
861 | + public function close() |
|
862 | + { |
|
863 | + return fclose($this->fileHandler); |
|
864 | + } |
|
865 | 865 | } |
866 | 866 | |
867 | 867 | /** |
@@ -870,19 +870,19 @@ discard block |
||
870 | 870 | */ |
871 | 871 | abstract class TypeAdapter |
872 | 872 | { |
873 | - public static $enums = array( |
|
874 | - "Sqlite", |
|
875 | - "Mysql" |
|
876 | - ); |
|
877 | - |
|
878 | - /** |
|
879 | - * @param string $c |
|
880 | - * @return boolean |
|
881 | - */ |
|
882 | - public static function isValid($c) |
|
883 | - { |
|
884 | - return in_array($c, self::$enums); |
|
885 | - } |
|
873 | + public static $enums = array( |
|
874 | + "Sqlite", |
|
875 | + "Mysql" |
|
876 | + ); |
|
877 | + |
|
878 | + /** |
|
879 | + * @param string $c |
|
880 | + * @return boolean |
|
881 | + */ |
|
882 | + public static function isValid($c) |
|
883 | + { |
|
884 | + return in_array($c, self::$enums); |
|
885 | + } |
|
886 | 886 | } |
887 | 887 | |
888 | 888 | /** |
@@ -891,186 +891,186 @@ discard block |
||
891 | 891 | */ |
892 | 892 | abstract class TypeAdapterFactory |
893 | 893 | { |
894 | - /** |
|
895 | - * @param string $c Type of database factory to create (Mysql, Sqlite,...) |
|
896 | - * @param PDO $dbHandler |
|
897 | - */ |
|
898 | - public static function create($c, $dbHandler = null) |
|
899 | - { |
|
900 | - $c = ucfirst(strtolower($c)); |
|
901 | - if (! TypeAdapter::isValid($c)) { |
|
902 | - throw new Exception("Database type support for ($c) not yet available"); |
|
903 | - } |
|
904 | - $method = __NAMESPACE__ . "\\" . "TypeAdapter" . $c; |
|
905 | - return new $method($dbHandler); |
|
906 | - } |
|
907 | - |
|
908 | - |
|
909 | - public function databases() |
|
910 | - { |
|
911 | - return ""; |
|
912 | - } |
|
913 | - |
|
914 | - public function show_create_table($tableName) |
|
915 | - { |
|
916 | - return "SELECT tbl_name as 'Table', sql as 'Create Table' " . |
|
917 | - "FROM sqlite_master " . |
|
918 | - "WHERE type='table' AND tbl_name='$tableName'"; |
|
919 | - } |
|
920 | - |
|
921 | - |
|
922 | - public function create_table($row, $dumpSettings) |
|
923 | - { |
|
924 | - return ""; |
|
925 | - } |
|
926 | - |
|
927 | - public function show_create_view($viewName) |
|
928 | - { |
|
929 | - return "SELECT tbl_name as 'View', sql as 'Create View' " . |
|
930 | - "FROM sqlite_master " . |
|
931 | - "WHERE type='view' AND tbl_name='$viewName'"; |
|
932 | - } |
|
933 | - |
|
934 | - |
|
935 | - public function create_view($row) |
|
936 | - { |
|
937 | - return ""; |
|
938 | - } |
|
939 | - |
|
940 | - |
|
941 | - public function show_create_trigger($triggerName) |
|
942 | - { |
|
943 | - return ""; |
|
944 | - } |
|
945 | - |
|
946 | - |
|
947 | - public function create_trigger($triggerName) |
|
948 | - { |
|
949 | - return ""; |
|
950 | - } |
|
951 | - |
|
952 | - public function show_tables() |
|
953 | - { |
|
954 | - return "SELECT tbl_name FROM sqlite_master WHERE type='table'"; |
|
955 | - } |
|
956 | - |
|
957 | - public function show_views() |
|
958 | - { |
|
959 | - return "SELECT tbl_name FROM sqlite_master WHERE type='view'"; |
|
960 | - } |
|
961 | - |
|
962 | - public function show_triggers() |
|
963 | - { |
|
964 | - return "SELECT name FROM sqlite_master WHERE type='trigger'"; |
|
965 | - } |
|
966 | - |
|
967 | - public function show_columns() |
|
968 | - { |
|
969 | - if (func_num_args() != 1) { |
|
970 | - return ""; |
|
971 | - } |
|
972 | - |
|
973 | - $args = func_get_args(); |
|
974 | - |
|
975 | - return "pragma table_info(${args[0]})"; |
|
976 | - } |
|
977 | - |
|
978 | - public function setup_transaction() |
|
979 | - { |
|
980 | - return ""; |
|
981 | - } |
|
982 | - |
|
983 | - public function start_transaction() |
|
984 | - { |
|
985 | - return "BEGIN EXCLUSIVE"; |
|
986 | - } |
|
987 | - |
|
988 | - public function commit_transaction() |
|
989 | - { |
|
990 | - return "COMMIT"; |
|
991 | - } |
|
992 | - |
|
993 | - public function lock_table() |
|
994 | - { |
|
995 | - return ""; |
|
996 | - } |
|
997 | - |
|
998 | - public function unlock_table() |
|
999 | - { |
|
1000 | - return ""; |
|
1001 | - } |
|
1002 | - |
|
1003 | - public function start_add_lock_table() |
|
1004 | - { |
|
1005 | - return PHP_EOL; |
|
1006 | - } |
|
1007 | - |
|
1008 | - public function end_add_lock_table() |
|
1009 | - { |
|
1010 | - return PHP_EOL; |
|
1011 | - } |
|
1012 | - |
|
1013 | - public function start_add_disable_keys() |
|
1014 | - { |
|
1015 | - return PHP_EOL; |
|
1016 | - } |
|
1017 | - |
|
1018 | - public function end_add_disable_keys() |
|
1019 | - { |
|
1020 | - return PHP_EOL; |
|
1021 | - } |
|
1022 | - |
|
1023 | - public function start_disable_foreign_keys_check() |
|
1024 | - { |
|
1025 | - return PHP_EOL; |
|
1026 | - } |
|
1027 | - |
|
1028 | - public function end_disable_foreign_keys_check() |
|
1029 | - { |
|
1030 | - return PHP_EOL; |
|
1031 | - } |
|
1032 | - |
|
1033 | - public function add_drop_database() |
|
1034 | - { |
|
1035 | - return PHP_EOL; |
|
1036 | - } |
|
1037 | - |
|
1038 | - public function add_drop_trigger() |
|
1039 | - { |
|
1040 | - return PHP_EOL; |
|
1041 | - } |
|
1042 | - |
|
1043 | - public function drop_table() |
|
1044 | - { |
|
1045 | - return PHP_EOL; |
|
1046 | - } |
|
1047 | - |
|
1048 | - public function drop_view() |
|
1049 | - { |
|
1050 | - return PHP_EOL; |
|
1051 | - } |
|
1052 | - |
|
1053 | - /** |
|
1054 | - * Decode column metadata and fill info structure. |
|
1055 | - * type, is_numeric and is_blob will always be available. |
|
1056 | - * |
|
1057 | - * @param array $colType Array returned from "SHOW COLUMNS FROM tableName" |
|
1058 | - * @return array |
|
1059 | - */ |
|
1060 | - public function parseColumnType($colType) |
|
1061 | - { |
|
1062 | - return array(); |
|
1063 | - } |
|
1064 | - |
|
1065 | - public function backup_parameters() |
|
1066 | - { |
|
1067 | - return PHP_EOL; |
|
1068 | - } |
|
1069 | - |
|
1070 | - public function restore_parameters() |
|
1071 | - { |
|
1072 | - return PHP_EOL; |
|
1073 | - } |
|
894 | + /** |
|
895 | + * @param string $c Type of database factory to create (Mysql, Sqlite,...) |
|
896 | + * @param PDO $dbHandler |
|
897 | + */ |
|
898 | + public static function create($c, $dbHandler = null) |
|
899 | + { |
|
900 | + $c = ucfirst(strtolower($c)); |
|
901 | + if (! TypeAdapter::isValid($c)) { |
|
902 | + throw new Exception("Database type support for ($c) not yet available"); |
|
903 | + } |
|
904 | + $method = __NAMESPACE__ . "\\" . "TypeAdapter" . $c; |
|
905 | + return new $method($dbHandler); |
|
906 | + } |
|
907 | + |
|
908 | + |
|
909 | + public function databases() |
|
910 | + { |
|
911 | + return ""; |
|
912 | + } |
|
913 | + |
|
914 | + public function show_create_table($tableName) |
|
915 | + { |
|
916 | + return "SELECT tbl_name as 'Table', sql as 'Create Table' " . |
|
917 | + "FROM sqlite_master " . |
|
918 | + "WHERE type='table' AND tbl_name='$tableName'"; |
|
919 | + } |
|
920 | + |
|
921 | + |
|
922 | + public function create_table($row, $dumpSettings) |
|
923 | + { |
|
924 | + return ""; |
|
925 | + } |
|
926 | + |
|
927 | + public function show_create_view($viewName) |
|
928 | + { |
|
929 | + return "SELECT tbl_name as 'View', sql as 'Create View' " . |
|
930 | + "FROM sqlite_master " . |
|
931 | + "WHERE type='view' AND tbl_name='$viewName'"; |
|
932 | + } |
|
933 | + |
|
934 | + |
|
935 | + public function create_view($row) |
|
936 | + { |
|
937 | + return ""; |
|
938 | + } |
|
939 | + |
|
940 | + |
|
941 | + public function show_create_trigger($triggerName) |
|
942 | + { |
|
943 | + return ""; |
|
944 | + } |
|
945 | + |
|
946 | + |
|
947 | + public function create_trigger($triggerName) |
|
948 | + { |
|
949 | + return ""; |
|
950 | + } |
|
951 | + |
|
952 | + public function show_tables() |
|
953 | + { |
|
954 | + return "SELECT tbl_name FROM sqlite_master WHERE type='table'"; |
|
955 | + } |
|
956 | + |
|
957 | + public function show_views() |
|
958 | + { |
|
959 | + return "SELECT tbl_name FROM sqlite_master WHERE type='view'"; |
|
960 | + } |
|
961 | + |
|
962 | + public function show_triggers() |
|
963 | + { |
|
964 | + return "SELECT name FROM sqlite_master WHERE type='trigger'"; |
|
965 | + } |
|
966 | + |
|
967 | + public function show_columns() |
|
968 | + { |
|
969 | + if (func_num_args() != 1) { |
|
970 | + return ""; |
|
971 | + } |
|
972 | + |
|
973 | + $args = func_get_args(); |
|
974 | + |
|
975 | + return "pragma table_info(${args[0]})"; |
|
976 | + } |
|
977 | + |
|
978 | + public function setup_transaction() |
|
979 | + { |
|
980 | + return ""; |
|
981 | + } |
|
982 | + |
|
983 | + public function start_transaction() |
|
984 | + { |
|
985 | + return "BEGIN EXCLUSIVE"; |
|
986 | + } |
|
987 | + |
|
988 | + public function commit_transaction() |
|
989 | + { |
|
990 | + return "COMMIT"; |
|
991 | + } |
|
992 | + |
|
993 | + public function lock_table() |
|
994 | + { |
|
995 | + return ""; |
|
996 | + } |
|
997 | + |
|
998 | + public function unlock_table() |
|
999 | + { |
|
1000 | + return ""; |
|
1001 | + } |
|
1002 | + |
|
1003 | + public function start_add_lock_table() |
|
1004 | + { |
|
1005 | + return PHP_EOL; |
|
1006 | + } |
|
1007 | + |
|
1008 | + public function end_add_lock_table() |
|
1009 | + { |
|
1010 | + return PHP_EOL; |
|
1011 | + } |
|
1012 | + |
|
1013 | + public function start_add_disable_keys() |
|
1014 | + { |
|
1015 | + return PHP_EOL; |
|
1016 | + } |
|
1017 | + |
|
1018 | + public function end_add_disable_keys() |
|
1019 | + { |
|
1020 | + return PHP_EOL; |
|
1021 | + } |
|
1022 | + |
|
1023 | + public function start_disable_foreign_keys_check() |
|
1024 | + { |
|
1025 | + return PHP_EOL; |
|
1026 | + } |
|
1027 | + |
|
1028 | + public function end_disable_foreign_keys_check() |
|
1029 | + { |
|
1030 | + return PHP_EOL; |
|
1031 | + } |
|
1032 | + |
|
1033 | + public function add_drop_database() |
|
1034 | + { |
|
1035 | + return PHP_EOL; |
|
1036 | + } |
|
1037 | + |
|
1038 | + public function add_drop_trigger() |
|
1039 | + { |
|
1040 | + return PHP_EOL; |
|
1041 | + } |
|
1042 | + |
|
1043 | + public function drop_table() |
|
1044 | + { |
|
1045 | + return PHP_EOL; |
|
1046 | + } |
|
1047 | + |
|
1048 | + public function drop_view() |
|
1049 | + { |
|
1050 | + return PHP_EOL; |
|
1051 | + } |
|
1052 | + |
|
1053 | + /** |
|
1054 | + * Decode column metadata and fill info structure. |
|
1055 | + * type, is_numeric and is_blob will always be available. |
|
1056 | + * |
|
1057 | + * @param array $colType Array returned from "SHOW COLUMNS FROM tableName" |
|
1058 | + * @return array |
|
1059 | + */ |
|
1060 | + public function parseColumnType($colType) |
|
1061 | + { |
|
1062 | + return array(); |
|
1063 | + } |
|
1064 | + |
|
1065 | + public function backup_parameters() |
|
1066 | + { |
|
1067 | + return PHP_EOL; |
|
1068 | + } |
|
1069 | + |
|
1070 | + public function restore_parameters() |
|
1071 | + { |
|
1072 | + return PHP_EOL; |
|
1073 | + } |
|
1074 | 1074 | } |
1075 | 1075 | |
1076 | 1076 | class TypeAdapterPgsql extends TypeAdapterFactory |
@@ -1088,421 +1088,421 @@ discard block |
||
1088 | 1088 | class TypeAdapterMysql extends TypeAdapterFactory |
1089 | 1089 | { |
1090 | 1090 | |
1091 | - private $dbHandler = null; |
|
1092 | - |
|
1093 | - // Numerical Mysql types |
|
1094 | - public $mysqlTypes = array( |
|
1095 | - 'numerical' => array( |
|
1096 | - 'bit', |
|
1097 | - 'tinyint', |
|
1098 | - 'smallint', |
|
1099 | - 'mediumint', |
|
1100 | - 'int', |
|
1101 | - 'integer', |
|
1102 | - 'bigint', |
|
1103 | - 'real', |
|
1104 | - 'double', |
|
1105 | - 'float', |
|
1106 | - 'decimal', |
|
1107 | - 'numeric' |
|
1108 | - ), |
|
1109 | - 'blob' => array( |
|
1110 | - 'tinyblob', |
|
1111 | - 'blob', |
|
1112 | - 'mediumblob', |
|
1113 | - 'longblob', |
|
1114 | - 'binary', |
|
1115 | - 'varbinary', |
|
1116 | - 'bit' |
|
1117 | - ) |
|
1118 | - ); |
|
1119 | - |
|
1120 | - public function __construct ($dbHandler) |
|
1121 | - { |
|
1122 | - $this->dbHandler = $dbHandler; |
|
1123 | - } |
|
1124 | - |
|
1125 | - public function databases() |
|
1126 | - { |
|
1127 | - if (func_num_args() != 1) { |
|
1128 | - throw new Exception("Unexpected parameter passed to " . __METHOD__); |
|
1129 | - } |
|
1130 | - |
|
1131 | - $args = func_get_args(); |
|
1132 | - $databaseName = $args[0]; |
|
1133 | - |
|
1134 | - $resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'character_set_database';"); |
|
1135 | - $characterSet = $resultSet->fetchColumn(1); |
|
1136 | - $resultSet->closeCursor(); |
|
1137 | - |
|
1138 | - $resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'collation_database';"); |
|
1139 | - $collationDb = $resultSet->fetchColumn(1); |
|
1140 | - $resultSet->closeCursor(); |
|
1141 | - $ret = ""; |
|
1142 | - |
|
1143 | - $ret .= "CREATE DATABASE /*!32312 IF NOT EXISTS*/ `${databaseName}`". |
|
1144 | - " /*!40100 DEFAULT CHARACTER SET ${characterSet} " . |
|
1145 | - " COLLATE ${collationDb} */;" . PHP_EOL . PHP_EOL . |
|
1146 | - "USE `${databaseName}`;" . PHP_EOL . PHP_EOL; |
|
1147 | - |
|
1148 | - return $ret; |
|
1149 | - } |
|
1150 | - |
|
1151 | - public function show_create_table($tableName) |
|
1152 | - { |
|
1153 | - return "SHOW CREATE TABLE `$tableName`"; |
|
1154 | - } |
|
1155 | - |
|
1156 | - public function show_create_view($viewName) |
|
1157 | - { |
|
1158 | - return "SHOW CREATE VIEW `$viewName`"; |
|
1159 | - } |
|
1160 | - |
|
1161 | - public function show_create_trigger($triggerName) |
|
1162 | - { |
|
1163 | - return "SHOW CREATE TRIGGER `$triggerName`"; |
|
1164 | - } |
|
1165 | - |
|
1166 | - public function create_table($row, $dumpSettings) |
|
1167 | - { |
|
1168 | - if (!isset($row['Create Table'])) { |
|
1169 | - throw new Exception("Error getting table code, unknown output"); |
|
1170 | - } |
|
1171 | - |
|
1172 | - $ret = "/*!40101 SET @saved_cs_client = @@character_set_client */;" . PHP_EOL . |
|
1173 | - "/*!40101 SET character_set_client = " . $dumpSettings['default-character-set'] . " */;" . PHP_EOL . |
|
1174 | - $row['Create Table'] . ";" . PHP_EOL . |
|
1175 | - "/*!40101 SET character_set_client = @saved_cs_client */;" . PHP_EOL . |
|
1176 | - PHP_EOL; |
|
1177 | - return $ret; |
|
1178 | - } |
|
1179 | - |
|
1180 | - public function create_view($row) |
|
1181 | - { |
|
1182 | - $ret = ""; |
|
1183 | - if (!isset($row['Create View'])) { |
|
1184 | - throw new Exception("Error getting view structure, unknown output"); |
|
1185 | - } |
|
1186 | - |
|
1187 | - $triggerStmt = $row['Create View']; |
|
1188 | - $triggerStmtReplaced1 = str_replace( |
|
1189 | - "CREATE ALGORITHM", |
|
1190 | - "/*!50001 CREATE ALGORITHM", |
|
1191 | - $triggerStmt |
|
1192 | - ); |
|
1193 | - $triggerStmtReplaced2 = str_replace( |
|
1194 | - " DEFINER=", |
|
1195 | - " */" . PHP_EOL . "/*!50013 DEFINER=", |
|
1196 | - $triggerStmtReplaced1 |
|
1197 | - ); |
|
1198 | - $triggerStmtReplaced3 = str_replace( |
|
1199 | - " VIEW ", |
|
1200 | - " */" . PHP_EOL . "/*!50001 VIEW ", |
|
1201 | - $triggerStmtReplaced2 |
|
1202 | - ); |
|
1203 | - if (false === $triggerStmtReplaced1 || |
|
1204 | - false === $triggerStmtReplaced2 || |
|
1205 | - false === $triggerStmtReplaced3) { |
|
1206 | - $triggerStmtReplaced = $triggerStmt; |
|
1207 | - } else { |
|
1208 | - $triggerStmtReplaced = $triggerStmtReplaced3 . " */;"; |
|
1209 | - } |
|
1210 | - |
|
1211 | - $ret .= $triggerStmtReplaced . PHP_EOL . PHP_EOL; |
|
1212 | - return $ret; |
|
1213 | - } |
|
1214 | - |
|
1215 | - public function create_trigger($row) |
|
1216 | - { |
|
1217 | - $ret = ""; |
|
1218 | - if (!isset($row['SQL Original Statement'])) { |
|
1219 | - throw new Exception("Error getting trigger code, unknown output"); |
|
1220 | - } |
|
1221 | - |
|
1222 | - $triggerStmt = $row['SQL Original Statement']; |
|
1223 | - $triggerStmtReplaced = str_replace( |
|
1224 | - "CREATE DEFINER", |
|
1225 | - "/*!50003 CREATE*/ /*!50017 DEFINER", |
|
1226 | - $triggerStmt |
|
1227 | - ); |
|
1228 | - $triggerStmtReplaced = str_replace( |
|
1229 | - " TRIGGER", |
|
1230 | - "*/ /*!50003 TRIGGER", |
|
1231 | - $triggerStmtReplaced |
|
1232 | - ); |
|
1233 | - if ( false === $triggerStmtReplaced ) { |
|
1234 | - $triggerStmtReplaced = $triggerStmt; |
|
1235 | - } |
|
1236 | - |
|
1237 | - $ret .= "DELIMITER ;;" . PHP_EOL . |
|
1238 | - $triggerStmtReplaced . "*/;;" . PHP_EOL . |
|
1239 | - "DELIMITER ;" . PHP_EOL . PHP_EOL; |
|
1240 | - return $ret; |
|
1241 | - } |
|
1242 | - |
|
1243 | - public function show_tables() |
|
1244 | - { |
|
1245 | - if (func_num_args() != 1) { |
|
1246 | - return ""; |
|
1247 | - } |
|
1248 | - |
|
1249 | - $args = func_get_args(); |
|
1250 | - |
|
1251 | - return "SELECT TABLE_NAME AS tbl_name " . |
|
1252 | - "FROM INFORMATION_SCHEMA.TABLES " . |
|
1253 | - "WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='${args[0]}'"; |
|
1254 | - } |
|
1255 | - |
|
1256 | - public function show_views() |
|
1257 | - { |
|
1258 | - if (func_num_args() != 1) { |
|
1259 | - return ""; |
|
1260 | - } |
|
1261 | - |
|
1262 | - $args = func_get_args(); |
|
1263 | - |
|
1264 | - return "SELECT TABLE_NAME AS tbl_name " . |
|
1265 | - "FROM INFORMATION_SCHEMA.TABLES " . |
|
1266 | - "WHERE TABLE_TYPE='VIEW' AND TABLE_SCHEMA='${args[0]}'"; |
|
1267 | - } |
|
1268 | - |
|
1269 | - public function show_triggers() |
|
1270 | - { |
|
1271 | - if (func_num_args() != 1) { |
|
1272 | - return ""; |
|
1273 | - } |
|
1274 | - |
|
1275 | - $args = func_get_args(); |
|
1276 | - |
|
1277 | - return "SHOW TRIGGERS FROM `${args[0]}`;"; |
|
1278 | - } |
|
1279 | - |
|
1280 | - |
|
1281 | - public function show_columns() |
|
1282 | - { |
|
1283 | - if (func_num_args() != 1) { |
|
1284 | - return ""; |
|
1285 | - } |
|
1286 | - |
|
1287 | - $args = func_get_args(); |
|
1288 | - |
|
1289 | - return "SHOW COLUMNS FROM `${args[0]}`;"; |
|
1290 | - } |
|
1291 | - |
|
1292 | - public function setup_transaction() |
|
1293 | - { |
|
1294 | - return "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ"; |
|
1295 | - } |
|
1296 | - |
|
1297 | - public function start_transaction() |
|
1298 | - { |
|
1299 | - return "START TRANSACTION"; |
|
1300 | - } |
|
1301 | - |
|
1302 | - public function commit_transaction() |
|
1303 | - { |
|
1304 | - return "COMMIT"; |
|
1305 | - } |
|
1306 | - |
|
1307 | - public function lock_table() |
|
1308 | - { |
|
1309 | - if (func_num_args() != 1) { |
|
1310 | - return ""; |
|
1311 | - } |
|
1312 | - |
|
1313 | - $args = func_get_args(); |
|
1314 | - //$tableName = $args[0]; |
|
1315 | - //return "LOCK TABLES `$tableName` READ LOCAL"; |
|
1316 | - return $this->dbHandler->exec("LOCK TABLES `${args[0]}` READ LOCAL"); |
|
1317 | - |
|
1318 | - } |
|
1319 | - |
|
1320 | - public function unlock_table() |
|
1321 | - { |
|
1322 | - return $this->dbHandler->exec("UNLOCK TABLES"); |
|
1323 | - } |
|
1324 | - |
|
1325 | - public function start_add_lock_table() |
|
1326 | - { |
|
1327 | - if (func_num_args() != 1) { |
|
1328 | - return ""; |
|
1329 | - } |
|
1330 | - |
|
1331 | - $args = func_get_args(); |
|
1332 | - |
|
1333 | - return "LOCK TABLES `${args[0]}` WRITE;" . PHP_EOL; |
|
1334 | - } |
|
1335 | - |
|
1336 | - public function end_add_lock_table() |
|
1337 | - { |
|
1338 | - return "UNLOCK TABLES;" . PHP_EOL; |
|
1339 | - } |
|
1340 | - |
|
1341 | - public function start_add_disable_keys() |
|
1342 | - { |
|
1343 | - if (func_num_args() != 1) { |
|
1344 | - return ""; |
|
1345 | - } |
|
1346 | - $args = func_get_args(); |
|
1347 | - return "/*!40000 ALTER TABLE `${args[0]}` DISABLE KEYS */;" . |
|
1348 | - PHP_EOL; |
|
1349 | - } |
|
1350 | - |
|
1351 | - public function end_add_disable_keys() |
|
1352 | - { |
|
1353 | - if (func_num_args() != 1) { |
|
1354 | - return ""; |
|
1355 | - } |
|
1356 | - $args = func_get_args(); |
|
1357 | - return "/*!40000 ALTER TABLE `${args[0]}` ENABLE KEYS */;" . |
|
1358 | - PHP_EOL; |
|
1359 | - } |
|
1360 | - |
|
1361 | - public function start_disable_autocommit() |
|
1362 | - { |
|
1363 | - return "SET autocommit=0;" . PHP_EOL; |
|
1364 | - } |
|
1365 | - |
|
1366 | - public function end_disable_autocommit() |
|
1367 | - { |
|
1368 | - return "COMMIT;" . PHP_EOL; |
|
1369 | - } |
|
1370 | - |
|
1371 | - public function add_drop_database() |
|
1372 | - { |
|
1373 | - if (func_num_args() != 1) { |
|
1374 | - return ""; |
|
1375 | - } |
|
1376 | - |
|
1377 | - $args = func_get_args(); |
|
1378 | - |
|
1379 | - return "/*!40000 DROP DATABASE IF EXISTS `${args[0]}`*/;" . |
|
1380 | - PHP_EOL . PHP_EOL; |
|
1381 | - } |
|
1382 | - |
|
1383 | - public function add_drop_trigger() |
|
1384 | - { |
|
1385 | - if (func_num_args() != 1) { |
|
1386 | - return ""; |
|
1387 | - } |
|
1388 | - |
|
1389 | - $args = func_get_args(); |
|
1390 | - |
|
1391 | - return "DROP TRIGGER IF EXISTS `${args[0]}`;" . PHP_EOL; |
|
1392 | - } |
|
1393 | - |
|
1394 | - public function drop_table() |
|
1395 | - { |
|
1396 | - if (func_num_args() != 1) { |
|
1397 | - return ""; |
|
1398 | - } |
|
1399 | - |
|
1400 | - $args = func_get_args(); |
|
1401 | - |
|
1402 | - return "DROP TABLE IF EXISTS `${args[0]}`;" . PHP_EOL; |
|
1403 | - } |
|
1404 | - |
|
1405 | - public function drop_view() |
|
1406 | - { |
|
1407 | - if (func_num_args() != 1) { |
|
1408 | - return ""; |
|
1409 | - } |
|
1410 | - |
|
1411 | - $args = func_get_args(); |
|
1412 | - |
|
1413 | - return "DROP TABLE IF EXISTS `${args[0]}`;" . PHP_EOL . |
|
1414 | - "/*!50001 DROP VIEW IF EXISTS `${args[0]}`*/;" . PHP_EOL; |
|
1415 | - } |
|
1416 | - |
|
1417 | - public function getDatabaseHeader() |
|
1418 | - { |
|
1419 | - if (func_num_args() != 1) { |
|
1420 | - return ""; |
|
1421 | - } |
|
1422 | - |
|
1423 | - $args = func_get_args(); |
|
1424 | - |
|
1425 | - return "--" . PHP_EOL . |
|
1426 | - "-- Current Database: `${args[0]}`" . PHP_EOL . |
|
1427 | - "--" . PHP_EOL . PHP_EOL; |
|
1428 | - } |
|
1429 | - |
|
1430 | - /** |
|
1431 | - * Decode column metadata and fill info structure. |
|
1432 | - * type, is_numeric and is_blob will always be available. |
|
1433 | - * |
|
1434 | - * @param array $colType Array returned from "SHOW COLUMNS FROM tableName" |
|
1435 | - * @return array |
|
1436 | - */ |
|
1437 | - public function parseColumnType($colType) |
|
1438 | - { |
|
1439 | - $colInfo = array(); |
|
1440 | - $colParts = explode(" ", $colType['Type']); |
|
1441 | - |
|
1442 | - if($fparen = strpos($colParts[0], "(")) |
|
1443 | - { |
|
1444 | - $colInfo['type'] = substr($colParts[0], 0, $fparen); |
|
1445 | - $colInfo['length'] = str_replace(")", "", substr($colParts[0], $fparen+1)); |
|
1446 | - $colInfo['attributes'] = isset($colParts[1]) ? $colParts[1] : NULL; |
|
1447 | - } |
|
1448 | - else |
|
1449 | - { |
|
1450 | - $colInfo['type'] = $colParts[0]; |
|
1451 | - } |
|
1452 | - $colInfo['is_numeric'] = in_array($colInfo['type'], $this->mysqlTypes['numerical']); |
|
1453 | - $colInfo['is_blob'] = in_array($colInfo['type'], $this->mysqlTypes['blob']); |
|
1454 | - |
|
1455 | - return $colInfo; |
|
1456 | - } |
|
1457 | - |
|
1458 | - public function backup_parameters() |
|
1459 | - { |
|
1460 | - if (func_num_args() != 1) { |
|
1461 | - throw new Exception("Unexpected parameter passed to " . __METHOD__); |
|
1462 | - } |
|
1463 | - |
|
1464 | - $args = func_get_args(); |
|
1465 | - $dumpSettings = $args[0]; |
|
1466 | - $ret = "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;" . PHP_EOL . |
|
1467 | - "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;" . PHP_EOL . |
|
1468 | - "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;" . PHP_EOL . |
|
1469 | - "/*!40101 SET NAMES " . $dumpSettings['default-character-set'] . " */;" . PHP_EOL; |
|
1470 | - |
|
1471 | - if (false === $dumpSettings['skip-tz-utz']) { |
|
1472 | - $ret .= "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;" . PHP_EOL . |
|
1473 | - "/*!40103 SET TIME_ZONE='+00:00' */;" . PHP_EOL; |
|
1474 | - } |
|
1475 | - |
|
1476 | - $ret .= "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;" . PHP_EOL . |
|
1477 | - "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;" . PHP_EOL . |
|
1478 | - "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;" . PHP_EOL . |
|
1479 | - "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;" . PHP_EOL .PHP_EOL; |
|
1480 | - |
|
1481 | - return $ret; |
|
1482 | - } |
|
1483 | - |
|
1484 | - public function restore_parameters() |
|
1485 | - { |
|
1486 | - if (func_num_args() != 1) { |
|
1487 | - throw new Exception("Unexpected parameter passed to " . __METHOD__); |
|
1488 | - } |
|
1489 | - |
|
1490 | - $args = func_get_args(); |
|
1491 | - $dumpSettings = $args[0]; |
|
1492 | - $ret = ""; |
|
1493 | - |
|
1494 | - if (false === $dumpSettings['skip-tz-utz']) { |
|
1495 | - $ret .= "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;" . PHP_EOL; |
|
1496 | - } |
|
1497 | - |
|
1498 | - $ret .= "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;" . PHP_EOL . |
|
1499 | - "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;" . PHP_EOL . |
|
1500 | - "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;" . PHP_EOL . |
|
1501 | - "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;" . PHP_EOL . |
|
1502 | - "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;" . PHP_EOL . |
|
1503 | - "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;" . PHP_EOL . |
|
1504 | - "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;" . PHP_EOL . PHP_EOL; |
|
1505 | - |
|
1506 | - return $ret; |
|
1507 | - } |
|
1091 | + private $dbHandler = null; |
|
1092 | + |
|
1093 | + // Numerical Mysql types |
|
1094 | + public $mysqlTypes = array( |
|
1095 | + 'numerical' => array( |
|
1096 | + 'bit', |
|
1097 | + 'tinyint', |
|
1098 | + 'smallint', |
|
1099 | + 'mediumint', |
|
1100 | + 'int', |
|
1101 | + 'integer', |
|
1102 | + 'bigint', |
|
1103 | + 'real', |
|
1104 | + 'double', |
|
1105 | + 'float', |
|
1106 | + 'decimal', |
|
1107 | + 'numeric' |
|
1108 | + ), |
|
1109 | + 'blob' => array( |
|
1110 | + 'tinyblob', |
|
1111 | + 'blob', |
|
1112 | + 'mediumblob', |
|
1113 | + 'longblob', |
|
1114 | + 'binary', |
|
1115 | + 'varbinary', |
|
1116 | + 'bit' |
|
1117 | + ) |
|
1118 | + ); |
|
1119 | + |
|
1120 | + public function __construct ($dbHandler) |
|
1121 | + { |
|
1122 | + $this->dbHandler = $dbHandler; |
|
1123 | + } |
|
1124 | + |
|
1125 | + public function databases() |
|
1126 | + { |
|
1127 | + if (func_num_args() != 1) { |
|
1128 | + throw new Exception("Unexpected parameter passed to " . __METHOD__); |
|
1129 | + } |
|
1130 | + |
|
1131 | + $args = func_get_args(); |
|
1132 | + $databaseName = $args[0]; |
|
1133 | + |
|
1134 | + $resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'character_set_database';"); |
|
1135 | + $characterSet = $resultSet->fetchColumn(1); |
|
1136 | + $resultSet->closeCursor(); |
|
1137 | + |
|
1138 | + $resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'collation_database';"); |
|
1139 | + $collationDb = $resultSet->fetchColumn(1); |
|
1140 | + $resultSet->closeCursor(); |
|
1141 | + $ret = ""; |
|
1142 | + |
|
1143 | + $ret .= "CREATE DATABASE /*!32312 IF NOT EXISTS*/ `${databaseName}`". |
|
1144 | + " /*!40100 DEFAULT CHARACTER SET ${characterSet} " . |
|
1145 | + " COLLATE ${collationDb} */;" . PHP_EOL . PHP_EOL . |
|
1146 | + "USE `${databaseName}`;" . PHP_EOL . PHP_EOL; |
|
1147 | + |
|
1148 | + return $ret; |
|
1149 | + } |
|
1150 | + |
|
1151 | + public function show_create_table($tableName) |
|
1152 | + { |
|
1153 | + return "SHOW CREATE TABLE `$tableName`"; |
|
1154 | + } |
|
1155 | + |
|
1156 | + public function show_create_view($viewName) |
|
1157 | + { |
|
1158 | + return "SHOW CREATE VIEW `$viewName`"; |
|
1159 | + } |
|
1160 | + |
|
1161 | + public function show_create_trigger($triggerName) |
|
1162 | + { |
|
1163 | + return "SHOW CREATE TRIGGER `$triggerName`"; |
|
1164 | + } |
|
1165 | + |
|
1166 | + public function create_table($row, $dumpSettings) |
|
1167 | + { |
|
1168 | + if (!isset($row['Create Table'])) { |
|
1169 | + throw new Exception("Error getting table code, unknown output"); |
|
1170 | + } |
|
1171 | + |
|
1172 | + $ret = "/*!40101 SET @saved_cs_client = @@character_set_client */;" . PHP_EOL . |
|
1173 | + "/*!40101 SET character_set_client = " . $dumpSettings['default-character-set'] . " */;" . PHP_EOL . |
|
1174 | + $row['Create Table'] . ";" . PHP_EOL . |
|
1175 | + "/*!40101 SET character_set_client = @saved_cs_client */;" . PHP_EOL . |
|
1176 | + PHP_EOL; |
|
1177 | + return $ret; |
|
1178 | + } |
|
1179 | + |
|
1180 | + public function create_view($row) |
|
1181 | + { |
|
1182 | + $ret = ""; |
|
1183 | + if (!isset($row['Create View'])) { |
|
1184 | + throw new Exception("Error getting view structure, unknown output"); |
|
1185 | + } |
|
1186 | + |
|
1187 | + $triggerStmt = $row['Create View']; |
|
1188 | + $triggerStmtReplaced1 = str_replace( |
|
1189 | + "CREATE ALGORITHM", |
|
1190 | + "/*!50001 CREATE ALGORITHM", |
|
1191 | + $triggerStmt |
|
1192 | + ); |
|
1193 | + $triggerStmtReplaced2 = str_replace( |
|
1194 | + " DEFINER=", |
|
1195 | + " */" . PHP_EOL . "/*!50013 DEFINER=", |
|
1196 | + $triggerStmtReplaced1 |
|
1197 | + ); |
|
1198 | + $triggerStmtReplaced3 = str_replace( |
|
1199 | + " VIEW ", |
|
1200 | + " */" . PHP_EOL . "/*!50001 VIEW ", |
|
1201 | + $triggerStmtReplaced2 |
|
1202 | + ); |
|
1203 | + if (false === $triggerStmtReplaced1 || |
|
1204 | + false === $triggerStmtReplaced2 || |
|
1205 | + false === $triggerStmtReplaced3) { |
|
1206 | + $triggerStmtReplaced = $triggerStmt; |
|
1207 | + } else { |
|
1208 | + $triggerStmtReplaced = $triggerStmtReplaced3 . " */;"; |
|
1209 | + } |
|
1210 | + |
|
1211 | + $ret .= $triggerStmtReplaced . PHP_EOL . PHP_EOL; |
|
1212 | + return $ret; |
|
1213 | + } |
|
1214 | + |
|
1215 | + public function create_trigger($row) |
|
1216 | + { |
|
1217 | + $ret = ""; |
|
1218 | + if (!isset($row['SQL Original Statement'])) { |
|
1219 | + throw new Exception("Error getting trigger code, unknown output"); |
|
1220 | + } |
|
1221 | + |
|
1222 | + $triggerStmt = $row['SQL Original Statement']; |
|
1223 | + $triggerStmtReplaced = str_replace( |
|
1224 | + "CREATE DEFINER", |
|
1225 | + "/*!50003 CREATE*/ /*!50017 DEFINER", |
|
1226 | + $triggerStmt |
|
1227 | + ); |
|
1228 | + $triggerStmtReplaced = str_replace( |
|
1229 | + " TRIGGER", |
|
1230 | + "*/ /*!50003 TRIGGER", |
|
1231 | + $triggerStmtReplaced |
|
1232 | + ); |
|
1233 | + if ( false === $triggerStmtReplaced ) { |
|
1234 | + $triggerStmtReplaced = $triggerStmt; |
|
1235 | + } |
|
1236 | + |
|
1237 | + $ret .= "DELIMITER ;;" . PHP_EOL . |
|
1238 | + $triggerStmtReplaced . "*/;;" . PHP_EOL . |
|
1239 | + "DELIMITER ;" . PHP_EOL . PHP_EOL; |
|
1240 | + return $ret; |
|
1241 | + } |
|
1242 | + |
|
1243 | + public function show_tables() |
|
1244 | + { |
|
1245 | + if (func_num_args() != 1) { |
|
1246 | + return ""; |
|
1247 | + } |
|
1248 | + |
|
1249 | + $args = func_get_args(); |
|
1250 | + |
|
1251 | + return "SELECT TABLE_NAME AS tbl_name " . |
|
1252 | + "FROM INFORMATION_SCHEMA.TABLES " . |
|
1253 | + "WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='${args[0]}'"; |
|
1254 | + } |
|
1255 | + |
|
1256 | + public function show_views() |
|
1257 | + { |
|
1258 | + if (func_num_args() != 1) { |
|
1259 | + return ""; |
|
1260 | + } |
|
1261 | + |
|
1262 | + $args = func_get_args(); |
|
1263 | + |
|
1264 | + return "SELECT TABLE_NAME AS tbl_name " . |
|
1265 | + "FROM INFORMATION_SCHEMA.TABLES " . |
|
1266 | + "WHERE TABLE_TYPE='VIEW' AND TABLE_SCHEMA='${args[0]}'"; |
|
1267 | + } |
|
1268 | + |
|
1269 | + public function show_triggers() |
|
1270 | + { |
|
1271 | + if (func_num_args() != 1) { |
|
1272 | + return ""; |
|
1273 | + } |
|
1274 | + |
|
1275 | + $args = func_get_args(); |
|
1276 | + |
|
1277 | + return "SHOW TRIGGERS FROM `${args[0]}`;"; |
|
1278 | + } |
|
1279 | + |
|
1280 | + |
|
1281 | + public function show_columns() |
|
1282 | + { |
|
1283 | + if (func_num_args() != 1) { |
|
1284 | + return ""; |
|
1285 | + } |
|
1286 | + |
|
1287 | + $args = func_get_args(); |
|
1288 | + |
|
1289 | + return "SHOW COLUMNS FROM `${args[0]}`;"; |
|
1290 | + } |
|
1291 | + |
|
1292 | + public function setup_transaction() |
|
1293 | + { |
|
1294 | + return "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ"; |
|
1295 | + } |
|
1296 | + |
|
1297 | + public function start_transaction() |
|
1298 | + { |
|
1299 | + return "START TRANSACTION"; |
|
1300 | + } |
|
1301 | + |
|
1302 | + public function commit_transaction() |
|
1303 | + { |
|
1304 | + return "COMMIT"; |
|
1305 | + } |
|
1306 | + |
|
1307 | + public function lock_table() |
|
1308 | + { |
|
1309 | + if (func_num_args() != 1) { |
|
1310 | + return ""; |
|
1311 | + } |
|
1312 | + |
|
1313 | + $args = func_get_args(); |
|
1314 | + //$tableName = $args[0]; |
|
1315 | + //return "LOCK TABLES `$tableName` READ LOCAL"; |
|
1316 | + return $this->dbHandler->exec("LOCK TABLES `${args[0]}` READ LOCAL"); |
|
1317 | + |
|
1318 | + } |
|
1319 | + |
|
1320 | + public function unlock_table() |
|
1321 | + { |
|
1322 | + return $this->dbHandler->exec("UNLOCK TABLES"); |
|
1323 | + } |
|
1324 | + |
|
1325 | + public function start_add_lock_table() |
|
1326 | + { |
|
1327 | + if (func_num_args() != 1) { |
|
1328 | + return ""; |
|
1329 | + } |
|
1330 | + |
|
1331 | + $args = func_get_args(); |
|
1332 | + |
|
1333 | + return "LOCK TABLES `${args[0]}` WRITE;" . PHP_EOL; |
|
1334 | + } |
|
1335 | + |
|
1336 | + public function end_add_lock_table() |
|
1337 | + { |
|
1338 | + return "UNLOCK TABLES;" . PHP_EOL; |
|
1339 | + } |
|
1340 | + |
|
1341 | + public function start_add_disable_keys() |
|
1342 | + { |
|
1343 | + if (func_num_args() != 1) { |
|
1344 | + return ""; |
|
1345 | + } |
|
1346 | + $args = func_get_args(); |
|
1347 | + return "/*!40000 ALTER TABLE `${args[0]}` DISABLE KEYS */;" . |
|
1348 | + PHP_EOL; |
|
1349 | + } |
|
1350 | + |
|
1351 | + public function end_add_disable_keys() |
|
1352 | + { |
|
1353 | + if (func_num_args() != 1) { |
|
1354 | + return ""; |
|
1355 | + } |
|
1356 | + $args = func_get_args(); |
|
1357 | + return "/*!40000 ALTER TABLE `${args[0]}` ENABLE KEYS */;" . |
|
1358 | + PHP_EOL; |
|
1359 | + } |
|
1360 | + |
|
1361 | + public function start_disable_autocommit() |
|
1362 | + { |
|
1363 | + return "SET autocommit=0;" . PHP_EOL; |
|
1364 | + } |
|
1365 | + |
|
1366 | + public function end_disable_autocommit() |
|
1367 | + { |
|
1368 | + return "COMMIT;" . PHP_EOL; |
|
1369 | + } |
|
1370 | + |
|
1371 | + public function add_drop_database() |
|
1372 | + { |
|
1373 | + if (func_num_args() != 1) { |
|
1374 | + return ""; |
|
1375 | + } |
|
1376 | + |
|
1377 | + $args = func_get_args(); |
|
1378 | + |
|
1379 | + return "/*!40000 DROP DATABASE IF EXISTS `${args[0]}`*/;" . |
|
1380 | + PHP_EOL . PHP_EOL; |
|
1381 | + } |
|
1382 | + |
|
1383 | + public function add_drop_trigger() |
|
1384 | + { |
|
1385 | + if (func_num_args() != 1) { |
|
1386 | + return ""; |
|
1387 | + } |
|
1388 | + |
|
1389 | + $args = func_get_args(); |
|
1390 | + |
|
1391 | + return "DROP TRIGGER IF EXISTS `${args[0]}`;" . PHP_EOL; |
|
1392 | + } |
|
1393 | + |
|
1394 | + public function drop_table() |
|
1395 | + { |
|
1396 | + if (func_num_args() != 1) { |
|
1397 | + return ""; |
|
1398 | + } |
|
1399 | + |
|
1400 | + $args = func_get_args(); |
|
1401 | + |
|
1402 | + return "DROP TABLE IF EXISTS `${args[0]}`;" . PHP_EOL; |
|
1403 | + } |
|
1404 | + |
|
1405 | + public function drop_view() |
|
1406 | + { |
|
1407 | + if (func_num_args() != 1) { |
|
1408 | + return ""; |
|
1409 | + } |
|
1410 | + |
|
1411 | + $args = func_get_args(); |
|
1412 | + |
|
1413 | + return "DROP TABLE IF EXISTS `${args[0]}`;" . PHP_EOL . |
|
1414 | + "/*!50001 DROP VIEW IF EXISTS `${args[0]}`*/;" . PHP_EOL; |
|
1415 | + } |
|
1416 | + |
|
1417 | + public function getDatabaseHeader() |
|
1418 | + { |
|
1419 | + if (func_num_args() != 1) { |
|
1420 | + return ""; |
|
1421 | + } |
|
1422 | + |
|
1423 | + $args = func_get_args(); |
|
1424 | + |
|
1425 | + return "--" . PHP_EOL . |
|
1426 | + "-- Current Database: `${args[0]}`" . PHP_EOL . |
|
1427 | + "--" . PHP_EOL . PHP_EOL; |
|
1428 | + } |
|
1429 | + |
|
1430 | + /** |
|
1431 | + * Decode column metadata and fill info structure. |
|
1432 | + * type, is_numeric and is_blob will always be available. |
|
1433 | + * |
|
1434 | + * @param array $colType Array returned from "SHOW COLUMNS FROM tableName" |
|
1435 | + * @return array |
|
1436 | + */ |
|
1437 | + public function parseColumnType($colType) |
|
1438 | + { |
|
1439 | + $colInfo = array(); |
|
1440 | + $colParts = explode(" ", $colType['Type']); |
|
1441 | + |
|
1442 | + if($fparen = strpos($colParts[0], "(")) |
|
1443 | + { |
|
1444 | + $colInfo['type'] = substr($colParts[0], 0, $fparen); |
|
1445 | + $colInfo['length'] = str_replace(")", "", substr($colParts[0], $fparen+1)); |
|
1446 | + $colInfo['attributes'] = isset($colParts[1]) ? $colParts[1] : NULL; |
|
1447 | + } |
|
1448 | + else |
|
1449 | + { |
|
1450 | + $colInfo['type'] = $colParts[0]; |
|
1451 | + } |
|
1452 | + $colInfo['is_numeric'] = in_array($colInfo['type'], $this->mysqlTypes['numerical']); |
|
1453 | + $colInfo['is_blob'] = in_array($colInfo['type'], $this->mysqlTypes['blob']); |
|
1454 | + |
|
1455 | + return $colInfo; |
|
1456 | + } |
|
1457 | + |
|
1458 | + public function backup_parameters() |
|
1459 | + { |
|
1460 | + if (func_num_args() != 1) { |
|
1461 | + throw new Exception("Unexpected parameter passed to " . __METHOD__); |
|
1462 | + } |
|
1463 | + |
|
1464 | + $args = func_get_args(); |
|
1465 | + $dumpSettings = $args[0]; |
|
1466 | + $ret = "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;" . PHP_EOL . |
|
1467 | + "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;" . PHP_EOL . |
|
1468 | + "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;" . PHP_EOL . |
|
1469 | + "/*!40101 SET NAMES " . $dumpSettings['default-character-set'] . " */;" . PHP_EOL; |
|
1470 | + |
|
1471 | + if (false === $dumpSettings['skip-tz-utz']) { |
|
1472 | + $ret .= "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;" . PHP_EOL . |
|
1473 | + "/*!40103 SET TIME_ZONE='+00:00' */;" . PHP_EOL; |
|
1474 | + } |
|
1475 | + |
|
1476 | + $ret .= "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;" . PHP_EOL . |
|
1477 | + "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;" . PHP_EOL . |
|
1478 | + "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;" . PHP_EOL . |
|
1479 | + "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;" . PHP_EOL .PHP_EOL; |
|
1480 | + |
|
1481 | + return $ret; |
|
1482 | + } |
|
1483 | + |
|
1484 | + public function restore_parameters() |
|
1485 | + { |
|
1486 | + if (func_num_args() != 1) { |
|
1487 | + throw new Exception("Unexpected parameter passed to " . __METHOD__); |
|
1488 | + } |
|
1489 | + |
|
1490 | + $args = func_get_args(); |
|
1491 | + $dumpSettings = $args[0]; |
|
1492 | + $ret = ""; |
|
1493 | + |
|
1494 | + if (false === $dumpSettings['skip-tz-utz']) { |
|
1495 | + $ret .= "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;" . PHP_EOL; |
|
1496 | + } |
|
1497 | + |
|
1498 | + $ret .= "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;" . PHP_EOL . |
|
1499 | + "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;" . PHP_EOL . |
|
1500 | + "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;" . PHP_EOL . |
|
1501 | + "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;" . PHP_EOL . |
|
1502 | + "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;" . PHP_EOL . |
|
1503 | + "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;" . PHP_EOL . |
|
1504 | + "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;" . PHP_EOL . PHP_EOL; |
|
1505 | + |
|
1506 | + return $ret; |
|
1507 | + } |
|
1508 | 1508 | } |
@@ -138,12 +138,12 @@ discard block |
||
138 | 138 | $this->dumpSettings = self::array_replace_recursive($dumpSettingsDefault, $dumpSettings); |
139 | 139 | |
140 | 140 | if (!isset($this->pdoSettings[PDO::MYSQL_ATTR_INIT_COMMAND])) { |
141 | - $this->pdoSettings[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES " . $this->dumpSettings['default-character-set']; |
|
141 | + $this->pdoSettings[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES ".$this->dumpSettings['default-character-set']; |
|
142 | 142 | } |
143 | 143 | |
144 | 144 | $diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); |
145 | - if (count($diff)>0) { |
|
146 | - throw new Exception("Unexpected value in dumpSettings: (" . implode(",", $diff) . ")"); |
|
145 | + if (count($diff) > 0) { |
|
146 | + throw new Exception("Unexpected value in dumpSettings: (".implode(",", $diff).")"); |
|
147 | 147 | } |
148 | 148 | |
149 | 149 | // Create a new compressManager to manage compressed output |
@@ -186,15 +186,15 @@ discard block |
||
186 | 186 | try { |
187 | 187 | switch ($this->dbType) { |
188 | 188 | case 'sqlite': |
189 | - $this->dbHandler = @new PDO("sqlite:" . $this->db, null, null, $this->pdoSettings); |
|
189 | + $this->dbHandler = @new PDO("sqlite:".$this->db, null, null, $this->pdoSettings); |
|
190 | 190 | break; |
191 | 191 | case 'mysql': |
192 | 192 | case 'pgsql': |
193 | 193 | case 'dblib': |
194 | - $dsn = $this->dbType . |
|
195 | - ":host=" . $this->host . |
|
196 | - (isset($this->port) ? ";port=" . $this->port : "") . |
|
197 | - ";dbname=" . $this->db; |
|
194 | + $dsn = $this->dbType. |
|
195 | + ":host=".$this->host. |
|
196 | + (isset($this->port) ? ";port=".$this->port : ""). |
|
197 | + ";dbname=".$this->db; |
|
198 | 198 | $this->dbHandler = @new PDO( |
199 | 199 | $dsn, |
200 | 200 | $this->user, |
@@ -202,16 +202,16 @@ discard block |
||
202 | 202 | $this->pdoSettings |
203 | 203 | ); |
204 | 204 | // Fix for always-unicode output |
205 | - $this->dbHandler->exec("SET NAMES " . $this->dumpSettings['default-character-set']); |
|
205 | + $this->dbHandler->exec("SET NAMES ".$this->dumpSettings['default-character-set']); |
|
206 | 206 | // Store server version |
207 | 207 | $this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION); |
208 | 208 | break; |
209 | 209 | default: |
210 | - throw new Exception("Unsupported database type (" . $this->dbType . ")"); |
|
210 | + throw new Exception("Unsupported database type (".$this->dbType.")"); |
|
211 | 211 | } |
212 | 212 | } catch (PDOException $e) { |
213 | 213 | throw new Exception( |
214 | - "Connection to " . $this->dbType . " failed with message: " . |
|
214 | + "Connection to ".$this->dbType." failed with message: ". |
|
215 | 215 | $e->getMessage() |
216 | 216 | ); |
217 | 217 | } |
@@ -276,7 +276,7 @@ discard block |
||
276 | 276 | // Give proper error and exit. |
277 | 277 | if (0 < count($this->dumpSettings['include-tables'])) { |
278 | 278 | $name = implode(",", $this->dumpSettings['include-tables']); |
279 | - throw new Exception("Table or View (" . $name . ") not found in database"); |
|
279 | + throw new Exception("Table or View (".$name.") not found in database"); |
|
280 | 280 | } |
281 | 281 | |
282 | 282 | $this->exportTables(); |
@@ -303,16 +303,16 @@ discard block |
||
303 | 303 | $header = ''; |
304 | 304 | if (!$this->dumpSettings['skip-comments']) { |
305 | 305 | // Some info about software, source and time |
306 | - $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php" . PHP_EOL . |
|
307 | - "--" . PHP_EOL . |
|
308 | - "-- Host: {$this->host}\tDatabase: {$this->db}" . PHP_EOL . |
|
309 | - "-- ------------------------------------------------------" . PHP_EOL; |
|
306 | + $header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php".PHP_EOL. |
|
307 | + "--".PHP_EOL. |
|
308 | + "-- Host: {$this->host}\tDatabase: {$this->db}".PHP_EOL. |
|
309 | + "-- ------------------------------------------------------".PHP_EOL; |
|
310 | 310 | |
311 | 311 | if (!empty($this->version)) { |
312 | - $header .= "-- Server version \t" . $this->version . PHP_EOL; |
|
312 | + $header .= "-- Server version \t".$this->version.PHP_EOL; |
|
313 | 313 | } |
314 | 314 | |
315 | - $header .= "-- Date: " . date('r') . PHP_EOL . PHP_EOL; |
|
315 | + $header .= "-- Date: ".date('r').PHP_EOL.PHP_EOL; |
|
316 | 316 | } |
317 | 317 | return $header; |
318 | 318 | } |
@@ -328,7 +328,7 @@ discard block |
||
328 | 328 | if (!$this->dumpSettings['skip-comments']) { |
329 | 329 | $footer .= '-- Dump completed'; |
330 | 330 | if (!$this->dumpSettings['skip-dump-date']) { |
331 | - $footer .= ' on: ' . date('r'); |
|
331 | + $footer .= ' on: '.date('r'); |
|
332 | 332 | } |
333 | 333 | $footer .= PHP_EOL; |
334 | 334 | } |
@@ -448,9 +448,9 @@ discard block |
||
448 | 448 | if (!$this->dumpSettings['no-create-info']) { |
449 | 449 | $ret = ''; |
450 | 450 | if (!$this->dumpSettings['skip-comments']) { |
451 | - $ret = "--" . PHP_EOL . |
|
452 | - "-- Table structure for table `$tableName`" . PHP_EOL . |
|
453 | - "--" . PHP_EOL . PHP_EOL; |
|
451 | + $ret = "--".PHP_EOL. |
|
452 | + "-- Table structure for table `$tableName`".PHP_EOL. |
|
453 | + "--".PHP_EOL.PHP_EOL; |
|
454 | 454 | } |
455 | 455 | $stmt = $this->typeAdapter->show_create_table($tableName); |
456 | 456 | foreach ($this->dbHandler->query($stmt) as $r) { |
@@ -473,7 +473,7 @@ discard block |
||
473 | 473 | ); |
474 | 474 | $columns->setFetchMode(PDO::FETCH_ASSOC); |
475 | 475 | |
476 | - foreach($columns as $key => $col) { |
|
476 | + foreach ($columns as $key => $col) { |
|
477 | 477 | $types = $this->typeAdapter->parseColumnType($col); |
478 | 478 | $columnTypes[$col['Field']] = array( |
479 | 479 | 'is_numeric'=> $types['is_numeric'], |
@@ -490,9 +490,9 @@ discard block |
||
490 | 490 | { |
491 | 491 | $ret = ''; |
492 | 492 | if (!$this->dumpSettings['skip-comments']) { |
493 | - $ret = "--" . PHP_EOL . |
|
494 | - "-- Table structure for view `${viewName}`" . PHP_EOL . |
|
495 | - "--" . PHP_EOL . PHP_EOL; |
|
493 | + $ret = "--".PHP_EOL. |
|
494 | + "-- Table structure for view `${viewName}`".PHP_EOL. |
|
495 | + "--".PHP_EOL.PHP_EOL; |
|
496 | 496 | } |
497 | 497 | $this->compressManager->write($ret); |
498 | 498 | $stmt = $this->typeAdapter->show_create_view($viewName); |
@@ -590,22 +590,22 @@ discard block |
||
590 | 590 | $vals = $this->escape($tableName, $row); |
591 | 591 | if ($onlyOnce || !$this->dumpSettings['extended-insert']) { |
592 | 592 | $lineSize += $this->compressManager->write( |
593 | - "INSERT INTO `$tableName` VALUES (" . implode(",", $vals) . ")" |
|
593 | + "INSERT INTO `$tableName` VALUES (".implode(",", $vals).")" |
|
594 | 594 | ); |
595 | 595 | $onlyOnce = false; |
596 | 596 | } else { |
597 | - $lineSize += $this->compressManager->write(",(" . implode(",", $vals) . ")"); |
|
597 | + $lineSize += $this->compressManager->write(",(".implode(",", $vals).")"); |
|
598 | 598 | } |
599 | 599 | if (($lineSize > self::MAXLINESIZE) || |
600 | 600 | !$this->dumpSettings['extended-insert']) { |
601 | 601 | $onlyOnce = true; |
602 | - $lineSize = $this->compressManager->write(";" . PHP_EOL); |
|
602 | + $lineSize = $this->compressManager->write(";".PHP_EOL); |
|
603 | 603 | } |
604 | 604 | } |
605 | 605 | $resultSet->closeCursor(); |
606 | 606 | |
607 | 607 | if (!$onlyOnce) { |
608 | - $this->compressManager->write(";" . PHP_EOL); |
|
608 | + $this->compressManager->write(";".PHP_EOL); |
|
609 | 609 | } |
610 | 610 | |
611 | 611 | $this->endListValues($tableName); |
@@ -622,9 +622,9 @@ discard block |
||
622 | 622 | { |
623 | 623 | if (!$this->dumpSettings['skip-comments']) { |
624 | 624 | $this->compressManager->write( |
625 | - "--" . PHP_EOL . |
|
626 | - "-- Dumping data for table `$tableName`" . PHP_EOL . |
|
627 | - "--" . PHP_EOL . PHP_EOL |
|
625 | + "--".PHP_EOL. |
|
626 | + "-- Dumping data for table `$tableName`".PHP_EOL. |
|
627 | + "--".PHP_EOL.PHP_EOL |
|
628 | 628 | ); |
629 | 629 | } |
630 | 630 | |
@@ -710,7 +710,7 @@ discard block |
||
710 | 710 | function getColumnStmt($tableName) |
711 | 711 | { |
712 | 712 | $colStmt = array(); |
713 | - foreach($this->tableColumnTypes[$tableName] as $colName => $colType) { |
|
713 | + foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { |
|
714 | 714 | if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { |
715 | 715 | $colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; |
716 | 716 | } else if ($colType['is_blob'] && $this->dumpSettings['hex-blob']) { |
@@ -756,11 +756,11 @@ discard block |
||
756 | 756 | public static function create($c) |
757 | 757 | { |
758 | 758 | $c = ucfirst(strtolower($c)); |
759 | - if (! CompressMethod::isValid($c)) { |
|
759 | + if (!CompressMethod::isValid($c)) { |
|
760 | 760 | throw new Exception("Compression method ($c) is not defined yet"); |
761 | 761 | } |
762 | 762 | |
763 | - $method = __NAMESPACE__ . "\\" . "Compress" . $c; |
|
763 | + $method = __NAMESPACE__."\\"."Compress".$c; |
|
764 | 764 | |
765 | 765 | return new $method; |
766 | 766 | } |
@@ -772,7 +772,7 @@ discard block |
||
772 | 772 | |
773 | 773 | public function __construct() |
774 | 774 | { |
775 | - if (! function_exists("bzopen")) { |
|
775 | + if (!function_exists("bzopen")) { |
|
776 | 776 | throw new Exception("Compression is enabled, but bzip2 lib is not installed or configured properly"); |
777 | 777 | } |
778 | 778 | } |
@@ -807,7 +807,7 @@ discard block |
||
807 | 807 | |
808 | 808 | public function __construct() |
809 | 809 | { |
810 | - if (! function_exists("gzopen")) { |
|
810 | + if (!function_exists("gzopen")) { |
|
811 | 811 | throw new Exception("Compression is enabled, but gzip lib is not installed or configured properly"); |
812 | 812 | } |
813 | 813 | } |
@@ -898,10 +898,10 @@ discard block |
||
898 | 898 | public static function create($c, $dbHandler = null) |
899 | 899 | { |
900 | 900 | $c = ucfirst(strtolower($c)); |
901 | - if (! TypeAdapter::isValid($c)) { |
|
901 | + if (!TypeAdapter::isValid($c)) { |
|
902 | 902 | throw new Exception("Database type support for ($c) not yet available"); |
903 | 903 | } |
904 | - $method = __NAMESPACE__ . "\\" . "TypeAdapter" . $c; |
|
904 | + $method = __NAMESPACE__."\\"."TypeAdapter".$c; |
|
905 | 905 | return new $method($dbHandler); |
906 | 906 | } |
907 | 907 | |
@@ -913,8 +913,8 @@ discard block |
||
913 | 913 | |
914 | 914 | public function show_create_table($tableName) |
915 | 915 | { |
916 | - return "SELECT tbl_name as 'Table', sql as 'Create Table' " . |
|
917 | - "FROM sqlite_master " . |
|
916 | + return "SELECT tbl_name as 'Table', sql as 'Create Table' ". |
|
917 | + "FROM sqlite_master ". |
|
918 | 918 | "WHERE type='table' AND tbl_name='$tableName'"; |
919 | 919 | } |
920 | 920 | |
@@ -926,8 +926,8 @@ discard block |
||
926 | 926 | |
927 | 927 | public function show_create_view($viewName) |
928 | 928 | { |
929 | - return "SELECT tbl_name as 'View', sql as 'Create View' " . |
|
930 | - "FROM sqlite_master " . |
|
929 | + return "SELECT tbl_name as 'View', sql as 'Create View' ". |
|
930 | + "FROM sqlite_master ". |
|
931 | 931 | "WHERE type='view' AND tbl_name='$viewName'"; |
932 | 932 | } |
933 | 933 | |
@@ -1117,7 +1117,7 @@ discard block |
||
1117 | 1117 | ) |
1118 | 1118 | ); |
1119 | 1119 | |
1120 | - public function __construct ($dbHandler) |
|
1120 | + public function __construct($dbHandler) |
|
1121 | 1121 | { |
1122 | 1122 | $this->dbHandler = $dbHandler; |
1123 | 1123 | } |
@@ -1125,7 +1125,7 @@ discard block |
||
1125 | 1125 | public function databases() |
1126 | 1126 | { |
1127 | 1127 | if (func_num_args() != 1) { |
1128 | - throw new Exception("Unexpected parameter passed to " . __METHOD__); |
|
1128 | + throw new Exception("Unexpected parameter passed to ".__METHOD__); |
|
1129 | 1129 | } |
1130 | 1130 | |
1131 | 1131 | $args = func_get_args(); |
@@ -1141,9 +1141,9 @@ discard block |
||
1141 | 1141 | $ret = ""; |
1142 | 1142 | |
1143 | 1143 | $ret .= "CREATE DATABASE /*!32312 IF NOT EXISTS*/ `${databaseName}`". |
1144 | - " /*!40100 DEFAULT CHARACTER SET ${characterSet} " . |
|
1145 | - " COLLATE ${collationDb} */;" . PHP_EOL . PHP_EOL . |
|
1146 | - "USE `${databaseName}`;" . PHP_EOL . PHP_EOL; |
|
1144 | + " /*!40100 DEFAULT CHARACTER SET ${characterSet} ". |
|
1145 | + " COLLATE ${collationDb} */;".PHP_EOL.PHP_EOL. |
|
1146 | + "USE `${databaseName}`;".PHP_EOL.PHP_EOL; |
|
1147 | 1147 | |
1148 | 1148 | return $ret; |
1149 | 1149 | } |
@@ -1169,10 +1169,10 @@ discard block |
||
1169 | 1169 | throw new Exception("Error getting table code, unknown output"); |
1170 | 1170 | } |
1171 | 1171 | |
1172 | - $ret = "/*!40101 SET @saved_cs_client = @@character_set_client */;" . PHP_EOL . |
|
1173 | - "/*!40101 SET character_set_client = " . $dumpSettings['default-character-set'] . " */;" . PHP_EOL . |
|
1174 | - $row['Create Table'] . ";" . PHP_EOL . |
|
1175 | - "/*!40101 SET character_set_client = @saved_cs_client */;" . PHP_EOL . |
|
1172 | + $ret = "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL. |
|
1173 | + "/*!40101 SET character_set_client = ".$dumpSettings['default-character-set']." */;".PHP_EOL. |
|
1174 | + $row['Create Table'].";".PHP_EOL. |
|
1175 | + "/*!40101 SET character_set_client = @saved_cs_client */;".PHP_EOL. |
|
1176 | 1176 | PHP_EOL; |
1177 | 1177 | return $ret; |
1178 | 1178 | } |
@@ -1192,12 +1192,12 @@ discard block |
||
1192 | 1192 | ); |
1193 | 1193 | $triggerStmtReplaced2 = str_replace( |
1194 | 1194 | " DEFINER=", |
1195 | - " */" . PHP_EOL . "/*!50013 DEFINER=", |
|
1195 | + " */".PHP_EOL."/*!50013 DEFINER=", |
|
1196 | 1196 | $triggerStmtReplaced1 |
1197 | 1197 | ); |
1198 | 1198 | $triggerStmtReplaced3 = str_replace( |
1199 | 1199 | " VIEW ", |
1200 | - " */" . PHP_EOL . "/*!50001 VIEW ", |
|
1200 | + " */".PHP_EOL."/*!50001 VIEW ", |
|
1201 | 1201 | $triggerStmtReplaced2 |
1202 | 1202 | ); |
1203 | 1203 | if (false === $triggerStmtReplaced1 || |
@@ -1205,10 +1205,10 @@ discard block |
||
1205 | 1205 | false === $triggerStmtReplaced3) { |
1206 | 1206 | $triggerStmtReplaced = $triggerStmt; |
1207 | 1207 | } else { |
1208 | - $triggerStmtReplaced = $triggerStmtReplaced3 . " */;"; |
|
1208 | + $triggerStmtReplaced = $triggerStmtReplaced3." */;"; |
|
1209 | 1209 | } |
1210 | 1210 | |
1211 | - $ret .= $triggerStmtReplaced . PHP_EOL . PHP_EOL; |
|
1211 | + $ret .= $triggerStmtReplaced.PHP_EOL.PHP_EOL; |
|
1212 | 1212 | return $ret; |
1213 | 1213 | } |
1214 | 1214 | |
@@ -1230,13 +1230,13 @@ discard block |
||
1230 | 1230 | "*/ /*!50003 TRIGGER", |
1231 | 1231 | $triggerStmtReplaced |
1232 | 1232 | ); |
1233 | - if ( false === $triggerStmtReplaced ) { |
|
1233 | + if (false === $triggerStmtReplaced) { |
|
1234 | 1234 | $triggerStmtReplaced = $triggerStmt; |
1235 | 1235 | } |
1236 | 1236 | |
1237 | - $ret .= "DELIMITER ;;" . PHP_EOL . |
|
1238 | - $triggerStmtReplaced . "*/;;" . PHP_EOL . |
|
1239 | - "DELIMITER ;" . PHP_EOL . PHP_EOL; |
|
1237 | + $ret .= "DELIMITER ;;".PHP_EOL. |
|
1238 | + $triggerStmtReplaced."*/;;".PHP_EOL. |
|
1239 | + "DELIMITER ;".PHP_EOL.PHP_EOL; |
|
1240 | 1240 | return $ret; |
1241 | 1241 | } |
1242 | 1242 | |
@@ -1248,8 +1248,8 @@ discard block |
||
1248 | 1248 | |
1249 | 1249 | $args = func_get_args(); |
1250 | 1250 | |
1251 | - return "SELECT TABLE_NAME AS tbl_name " . |
|
1252 | - "FROM INFORMATION_SCHEMA.TABLES " . |
|
1251 | + return "SELECT TABLE_NAME AS tbl_name ". |
|
1252 | + "FROM INFORMATION_SCHEMA.TABLES ". |
|
1253 | 1253 | "WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='${args[0]}'"; |
1254 | 1254 | } |
1255 | 1255 | |
@@ -1261,8 +1261,8 @@ discard block |
||
1261 | 1261 | |
1262 | 1262 | $args = func_get_args(); |
1263 | 1263 | |
1264 | - return "SELECT TABLE_NAME AS tbl_name " . |
|
1265 | - "FROM INFORMATION_SCHEMA.TABLES " . |
|
1264 | + return "SELECT TABLE_NAME AS tbl_name ". |
|
1265 | + "FROM INFORMATION_SCHEMA.TABLES ". |
|
1266 | 1266 | "WHERE TABLE_TYPE='VIEW' AND TABLE_SCHEMA='${args[0]}'"; |
1267 | 1267 | } |
1268 | 1268 | |
@@ -1330,12 +1330,12 @@ discard block |
||
1330 | 1330 | |
1331 | 1331 | $args = func_get_args(); |
1332 | 1332 | |
1333 | - return "LOCK TABLES `${args[0]}` WRITE;" . PHP_EOL; |
|
1333 | + return "LOCK TABLES `${args[0]}` WRITE;".PHP_EOL; |
|
1334 | 1334 | } |
1335 | 1335 | |
1336 | 1336 | public function end_add_lock_table() |
1337 | 1337 | { |
1338 | - return "UNLOCK TABLES;" . PHP_EOL; |
|
1338 | + return "UNLOCK TABLES;".PHP_EOL; |
|
1339 | 1339 | } |
1340 | 1340 | |
1341 | 1341 | public function start_add_disable_keys() |
@@ -1344,7 +1344,7 @@ discard block |
||
1344 | 1344 | return ""; |
1345 | 1345 | } |
1346 | 1346 | $args = func_get_args(); |
1347 | - return "/*!40000 ALTER TABLE `${args[0]}` DISABLE KEYS */;" . |
|
1347 | + return "/*!40000 ALTER TABLE `${args[0]}` DISABLE KEYS */;". |
|
1348 | 1348 | PHP_EOL; |
1349 | 1349 | } |
1350 | 1350 | |
@@ -1354,18 +1354,18 @@ discard block |
||
1354 | 1354 | return ""; |
1355 | 1355 | } |
1356 | 1356 | $args = func_get_args(); |
1357 | - return "/*!40000 ALTER TABLE `${args[0]}` ENABLE KEYS */;" . |
|
1357 | + return "/*!40000 ALTER TABLE `${args[0]}` ENABLE KEYS */;". |
|
1358 | 1358 | PHP_EOL; |
1359 | 1359 | } |
1360 | 1360 | |
1361 | 1361 | public function start_disable_autocommit() |
1362 | 1362 | { |
1363 | - return "SET autocommit=0;" . PHP_EOL; |
|
1363 | + return "SET autocommit=0;".PHP_EOL; |
|
1364 | 1364 | } |
1365 | 1365 | |
1366 | 1366 | public function end_disable_autocommit() |
1367 | 1367 | { |
1368 | - return "COMMIT;" . PHP_EOL; |
|
1368 | + return "COMMIT;".PHP_EOL; |
|
1369 | 1369 | } |
1370 | 1370 | |
1371 | 1371 | public function add_drop_database() |
@@ -1376,8 +1376,8 @@ discard block |
||
1376 | 1376 | |
1377 | 1377 | $args = func_get_args(); |
1378 | 1378 | |
1379 | - return "/*!40000 DROP DATABASE IF EXISTS `${args[0]}`*/;" . |
|
1380 | - PHP_EOL . PHP_EOL; |
|
1379 | + return "/*!40000 DROP DATABASE IF EXISTS `${args[0]}`*/;". |
|
1380 | + PHP_EOL.PHP_EOL; |
|
1381 | 1381 | } |
1382 | 1382 | |
1383 | 1383 | public function add_drop_trigger() |
@@ -1388,7 +1388,7 @@ discard block |
||
1388 | 1388 | |
1389 | 1389 | $args = func_get_args(); |
1390 | 1390 | |
1391 | - return "DROP TRIGGER IF EXISTS `${args[0]}`;" . PHP_EOL; |
|
1391 | + return "DROP TRIGGER IF EXISTS `${args[0]}`;".PHP_EOL; |
|
1392 | 1392 | } |
1393 | 1393 | |
1394 | 1394 | public function drop_table() |
@@ -1399,7 +1399,7 @@ discard block |
||
1399 | 1399 | |
1400 | 1400 | $args = func_get_args(); |
1401 | 1401 | |
1402 | - return "DROP TABLE IF EXISTS `${args[0]}`;" . PHP_EOL; |
|
1402 | + return "DROP TABLE IF EXISTS `${args[0]}`;".PHP_EOL; |
|
1403 | 1403 | } |
1404 | 1404 | |
1405 | 1405 | public function drop_view() |
@@ -1410,8 +1410,8 @@ discard block |
||
1410 | 1410 | |
1411 | 1411 | $args = func_get_args(); |
1412 | 1412 | |
1413 | - return "DROP TABLE IF EXISTS `${args[0]}`;" . PHP_EOL . |
|
1414 | - "/*!50001 DROP VIEW IF EXISTS `${args[0]}`*/;" . PHP_EOL; |
|
1413 | + return "DROP TABLE IF EXISTS `${args[0]}`;".PHP_EOL. |
|
1414 | + "/*!50001 DROP VIEW IF EXISTS `${args[0]}`*/;".PHP_EOL; |
|
1415 | 1415 | } |
1416 | 1416 | |
1417 | 1417 | public function getDatabaseHeader() |
@@ -1422,9 +1422,9 @@ discard block |
||
1422 | 1422 | |
1423 | 1423 | $args = func_get_args(); |
1424 | 1424 | |
1425 | - return "--" . PHP_EOL . |
|
1426 | - "-- Current Database: `${args[0]}`" . PHP_EOL . |
|
1427 | - "--" . PHP_EOL . PHP_EOL; |
|
1425 | + return "--".PHP_EOL. |
|
1426 | + "-- Current Database: `${args[0]}`".PHP_EOL. |
|
1427 | + "--".PHP_EOL.PHP_EOL; |
|
1428 | 1428 | } |
1429 | 1429 | |
1430 | 1430 | /** |
@@ -1439,10 +1439,10 @@ discard block |
||
1439 | 1439 | $colInfo = array(); |
1440 | 1440 | $colParts = explode(" ", $colType['Type']); |
1441 | 1441 | |
1442 | - if($fparen = strpos($colParts[0], "(")) |
|
1442 | + if ($fparen = strpos($colParts[0], "(")) |
|
1443 | 1443 | { |
1444 | 1444 | $colInfo['type'] = substr($colParts[0], 0, $fparen); |
1445 | - $colInfo['length'] = str_replace(")", "", substr($colParts[0], $fparen+1)); |
|
1445 | + $colInfo['length'] = str_replace(")", "", substr($colParts[0], $fparen + 1)); |
|
1446 | 1446 | $colInfo['attributes'] = isset($colParts[1]) ? $colParts[1] : NULL; |
1447 | 1447 | } |
1448 | 1448 | else |
@@ -1458,25 +1458,25 @@ discard block |
||
1458 | 1458 | public function backup_parameters() |
1459 | 1459 | { |
1460 | 1460 | if (func_num_args() != 1) { |
1461 | - throw new Exception("Unexpected parameter passed to " . __METHOD__); |
|
1461 | + throw new Exception("Unexpected parameter passed to ".__METHOD__); |
|
1462 | 1462 | } |
1463 | 1463 | |
1464 | 1464 | $args = func_get_args(); |
1465 | 1465 | $dumpSettings = $args[0]; |
1466 | - $ret = "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;" . PHP_EOL . |
|
1467 | - "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;" . PHP_EOL . |
|
1468 | - "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;" . PHP_EOL . |
|
1469 | - "/*!40101 SET NAMES " . $dumpSettings['default-character-set'] . " */;" . PHP_EOL; |
|
1466 | + $ret = "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;".PHP_EOL. |
|
1467 | + "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;".PHP_EOL. |
|
1468 | + "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;".PHP_EOL. |
|
1469 | + "/*!40101 SET NAMES ".$dumpSettings['default-character-set']." */;".PHP_EOL; |
|
1470 | 1470 | |
1471 | 1471 | if (false === $dumpSettings['skip-tz-utz']) { |
1472 | - $ret .= "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;" . PHP_EOL . |
|
1473 | - "/*!40103 SET TIME_ZONE='+00:00' */;" . PHP_EOL; |
|
1472 | + $ret .= "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;".PHP_EOL. |
|
1473 | + "/*!40103 SET TIME_ZONE='+00:00' */;".PHP_EOL; |
|
1474 | 1474 | } |
1475 | 1475 | |
1476 | - $ret .= "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;" . PHP_EOL . |
|
1477 | - "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;" . PHP_EOL . |
|
1478 | - "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;" . PHP_EOL . |
|
1479 | - "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;" . PHP_EOL .PHP_EOL; |
|
1476 | + $ret .= "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;".PHP_EOL. |
|
1477 | + "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;".PHP_EOL. |
|
1478 | + "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;".PHP_EOL. |
|
1479 | + "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;".PHP_EOL.PHP_EOL; |
|
1480 | 1480 | |
1481 | 1481 | return $ret; |
1482 | 1482 | } |
@@ -1484,7 +1484,7 @@ discard block |
||
1484 | 1484 | public function restore_parameters() |
1485 | 1485 | { |
1486 | 1486 | if (func_num_args() != 1) { |
1487 | - throw new Exception("Unexpected parameter passed to " . __METHOD__); |
|
1487 | + throw new Exception("Unexpected parameter passed to ".__METHOD__); |
|
1488 | 1488 | } |
1489 | 1489 | |
1490 | 1490 | $args = func_get_args(); |
@@ -1492,16 +1492,16 @@ discard block |
||
1492 | 1492 | $ret = ""; |
1493 | 1493 | |
1494 | 1494 | if (false === $dumpSettings['skip-tz-utz']) { |
1495 | - $ret .= "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;" . PHP_EOL; |
|
1495 | + $ret .= "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;".PHP_EOL; |
|
1496 | 1496 | } |
1497 | 1497 | |
1498 | - $ret .= "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;" . PHP_EOL . |
|
1499 | - "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;" . PHP_EOL . |
|
1500 | - "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;" . PHP_EOL . |
|
1501 | - "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;" . PHP_EOL . |
|
1502 | - "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;" . PHP_EOL . |
|
1503 | - "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;" . PHP_EOL . |
|
1504 | - "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;" . PHP_EOL . PHP_EOL; |
|
1498 | + $ret .= "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;".PHP_EOL. |
|
1499 | + "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;".PHP_EOL. |
|
1500 | + "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;".PHP_EOL. |
|
1501 | + "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;".PHP_EOL. |
|
1502 | + "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;".PHP_EOL. |
|
1503 | + "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;".PHP_EOL. |
|
1504 | + "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;".PHP_EOL.PHP_EOL; |
|
1505 | 1505 | |
1506 | 1506 | return $ret; |
1507 | 1507 | } |
@@ -1444,8 +1444,7 @@ |
||
1444 | 1444 | $colInfo['type'] = substr($colParts[0], 0, $fparen); |
1445 | 1445 | $colInfo['length'] = str_replace(")", "", substr($colParts[0], $fparen+1)); |
1446 | 1446 | $colInfo['attributes'] = isset($colParts[1]) ? $colParts[1] : NULL; |
1447 | - } |
|
1448 | - else |
|
1447 | + } else |
|
1449 | 1448 | { |
1450 | 1449 | $colInfo['type'] = $colParts[0]; |
1451 | 1450 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | } |
58 | 58 | |
59 | 59 | //si le compte est archiver (bloqué) l'utilisateur ne peut pas se connecter au site |
60 | - if ($archiver == 1) { |
|
60 | + if ($archiver == 1) { |
|
61 | 61 | FlashMessage::setFlash("Votre compte a été bloqué par un administrateur, vous ne pouvez donc pas vous connecter à ce site, veuillez réesseyer ultérieurement"); |
62 | 62 | header("location:$page_retour_err"); |
63 | 63 | } |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | |
70 | 70 | |
71 | 71 | //si les mdp sont egaux on redirige ver esace membre sinon ver login avec un mess d'erreur |
72 | - if (($valide == 1) && ($archiver != 1)) { |
|
72 | + if (($valide == 1) && ($archiver != 1)) { |
|
73 | 73 | if ($mdp == $mdpbdd) { |
74 | 74 | $_SESSION['login'] = $pseudo; |
75 | 75 | $_SESSION["idlogin".CLEF_SITE] = $id; |
@@ -79,19 +79,19 @@ discard block |
||
79 | 79 | $last_change_mdp = mktime(0, 0, 0, $date_array[1], $date_array[2], $date_array[0]); |
80 | 80 | $today = mktime(0, 0, 0, date("m"), date("d"), date("Y")); |
81 | 81 | |
82 | - if (($today-$last_change_mdp) > 259200) { |
|
82 | + if (($today - $last_change_mdp) > 259200) { |
|
83 | 83 | self::setUpdatelastConnexion($id); |
84 | 84 | |
85 | 85 | $membre = new Membre($id); |
86 | 86 | $membre->setMdp($mdpbdd, $mdp_noncrypt, $mdp_noncrypt); |
87 | 87 | |
88 | 88 | if (isset($_POST['remember'])) { |
89 | - setcookie("auth".CLEF_SITE, $id."-----".sha1($pseudo . $membre->getMdp()), time()+3600*24*3, "/", "", false, true); |
|
89 | + setcookie("auth".CLEF_SITE, $id."-----".sha1($pseudo.$membre->getMdp()), time() + 3600 * 24 * 3, "/", "", false, true); |
|
90 | 90 | } |
91 | 91 | } |
92 | 92 | else { |
93 | 93 | if (isset($_POST['remember'])) { |
94 | - setcookie("auth".CLEF_SITE, $id."-----".sha1($pseudo . $mdpbdd), time()+3600*24*3, "/", "", false, true); |
|
94 | + setcookie("auth".CLEF_SITE, $id."-----".sha1($pseudo.$mdpbdd), time() + 3600 * 24 * 3, "/", "", false, true); |
|
95 | 95 | } |
96 | 96 | } |
97 | 97 | |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | $last_change_mdp = mktime(0, 0, 0, $date_array[1], $date_array[2], $date_array[0]); |
144 | 144 | $today = mktime(0, 0, 0, date("m"), date("d"), date("Y")); |
145 | 145 | |
146 | - if (($today-$last_change_mdp) > 259200) { |
|
146 | + if (($today - $last_change_mdp) > 259200) { |
|
147 | 147 | self::setUpdatelastConnexion($obj->ID_identite); |
148 | 148 | |
149 | 149 | //on refait un nouveau mdp encrypté avec le même mdp |
@@ -154,10 +154,10 @@ discard block |
||
154 | 154 | //on detruit le cookie et on le refait avec le mdp regénéré |
155 | 155 | setcookie("auth".CLEF_SITE, NULL, -1); |
156 | 156 | $key = sha1($obj->pseudo.$membre->getMdp()); |
157 | - setcookie("auth".CLEF_SITE, $obj->ID_identite."-----".$key, time()+3600*24*3, "/", "", false, true); |
|
157 | + setcookie("auth".CLEF_SITE, $obj->ID_identite."-----".$key, time() + 3600 * 24 * 3, "/", "", false, true); |
|
158 | 158 | } |
159 | 159 | else { |
160 | - setcookie("auth".CLEF_SITE, $obj->ID_identite."-----".$key, time()+3600*24*3, "/", "", false, true); |
|
160 | + setcookie("auth".CLEF_SITE, $obj->ID_identite."-----".$key, time() + 3600 * 24 * 3, "/", "", false, true); |
|
161 | 161 | } |
162 | 162 | } |
163 | 163 | else { |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | public static function setUpdatelastConnexion($id_identite) { |
205 | 205 | $dbc = App::getDb(); |
206 | 206 | |
207 | - $dbc->prepare("UPDATE identite SET last_change_mdp=:date WHERE ID_identite=:id_identite", array("date"=>date("Y-m-d"),"id_identite"=>$id_identite)); |
|
207 | + $dbc->prepare("UPDATE identite SET last_change_mdp=:date WHERE ID_identite=:id_identite", array("date"=>date("Y-m-d"), "id_identite"=>$id_identite)); |
|
208 | 208 | } |
209 | 209 | |
210 | 210 | /** |
@@ -44,8 +44,7 @@ discard block |
||
44 | 44 | if (!isset($id)) { |
45 | 45 | FlashMessage::setFlash("Vos identifiants de connexions sont incorrects"); |
46 | 46 | header("location:$page_retour_err"); |
47 | - } |
|
48 | - else { |
|
47 | + } else { |
|
49 | 48 | $config = new Configuration(); |
50 | 49 | |
51 | 50 | //cela veut dire que l'utilisateur doit obligatoirement etre valider pour avoir acces au site |
@@ -88,8 +87,7 @@ discard block |
||
88 | 87 | if (isset($_POST['remember'])) { |
89 | 88 | setcookie("auth".CLEF_SITE, $id."-----".sha1($pseudo . $membre->getMdp()), time()+3600*24*3, "/", "", false, true); |
90 | 89 | } |
91 | - } |
|
92 | - else { |
|
90 | + } else { |
|
93 | 91 | if (isset($_POST['remember'])) { |
94 | 92 | setcookie("auth".CLEF_SITE, $id."-----".sha1($pseudo . $mdpbdd), time()+3600*24*3, "/", "", false, true); |
95 | 93 | } |
@@ -98,8 +96,7 @@ discard block |
||
98 | 96 | |
99 | 97 | FlashMessage::setFlash("Vous êtes maintenant connecté", "info"); |
100 | 98 | header("location:$page_retour"); |
101 | - } |
|
102 | - else { |
|
99 | + } else { |
|
103 | 100 | FlashMessage::setFlash("Vos identifiants de connexions sont incorrects"); |
104 | 101 | header("location:$page_retour_err"); |
105 | 102 | } |
@@ -130,8 +127,7 @@ discard block |
||
130 | 127 | if ($obj->archiver == 1) { |
131 | 128 | setcookie("auth".CLEF_SITE, NULL, -1); |
132 | 129 | self::setDeconnexion($page_retour); |
133 | - } |
|
134 | - else { |
|
130 | + } else { |
|
135 | 131 | $key = sha1($obj->pseudo.$obj->mdp); |
136 | 132 | |
137 | 133 | if ($key == $auth[1]) { |
@@ -155,12 +151,10 @@ discard block |
||
155 | 151 | setcookie("auth".CLEF_SITE, NULL, -1); |
156 | 152 | $key = sha1($obj->pseudo.$membre->getMdp()); |
157 | 153 | setcookie("auth".CLEF_SITE, $obj->ID_identite."-----".$key, time()+3600*24*3, "/", "", false, true); |
158 | - } |
|
159 | - else { |
|
154 | + } else { |
|
160 | 155 | setcookie("auth".CLEF_SITE, $obj->ID_identite."-----".$key, time()+3600*24*3, "/", "", false, true); |
161 | 156 | } |
162 | - } |
|
163 | - else { |
|
157 | + } else { |
|
164 | 158 | if ($obj_connecte == 1) { |
165 | 159 | self::setDeconnexion($page_retour); |
166 | 160 | } |
@@ -168,8 +162,7 @@ discard block |
||
168 | 162 | } |
169 | 163 | |
170 | 164 | } |
171 | - } |
|
172 | - else if (!isset($_SESSION["idlogin".CLEF_SITE])) { |
|
165 | + } else if (!isset($_SESSION["idlogin".CLEF_SITE])) { |
|
173 | 166 | if ($obj_connecte == 1) { |
174 | 167 | FlashMessage::setFlash("Vous devez être connecté pour accéder à cette page"); |
175 | 168 | header("location:".$page_retour); |
@@ -216,6 +209,8 @@ discard block |
||
216 | 209 | $dbc = App::getDb(); |
217 | 210 | |
218 | 211 | $query = $dbc->query("SELECT last_change_mdp FROM identite WHERE ID_identite=".$id_identite); |
219 | - foreach ($query as $obj) return $obj->last_change_mdp; |
|
212 | + foreach ($query as $obj) { |
|
213 | + return $obj->last_change_mdp; |
|
214 | + } |
|
220 | 215 | } |
221 | 216 | } |
222 | 217 | \ No newline at end of file |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | |
14 | 14 | |
15 | 15 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
16 | - public static function getParams($id_identite=null) { |
|
16 | + public static function getParams($id_identite = null) { |
|
17 | 17 | if ($id_identite != null) { |
18 | 18 | $dbc = App::getDb(); |
19 | 19 | |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | * @param $id_identite |
39 | 39 | * @return string |
40 | 40 | */ |
41 | - public static function setEncryptMdp($mdp, $id_identite=null) { |
|
41 | + public static function setEncryptMdp($mdp, $id_identite = null) { |
|
42 | 42 | $encrypt_str = ChaineCaractere::random(154); |
43 | 43 | |
44 | 44 | //on cache le mdp |
@@ -48,9 +48,9 @@ discard block |
||
48 | 48 | $longeur_mdp = strlen($mdp); |
49 | 49 | |
50 | 50 | //on va couper le mot de passe en 2 suivant un nombre aleatoire |
51 | - $nb_aleatoire_mdp = rand(3, $longeur_mdp-2); |
|
52 | - $bout1_mdp = mb_substr($mdp, 0, $longeur_mdp/2, "UTF-8"); |
|
53 | - $bout2_mdp = mb_substr($mdp, $longeur_mdp/2, $longeur_mdp, "UTF-8"); |
|
51 | + $nb_aleatoire_mdp = rand(3, $longeur_mdp - 2); |
|
52 | + $bout1_mdp = mb_substr($mdp, 0, $longeur_mdp / 2, "UTF-8"); |
|
53 | + $bout2_mdp = mb_substr($mdp, $longeur_mdp / 2, $longeur_mdp, "UTF-8"); |
|
54 | 54 | |
55 | 55 | //on stock la taille des bouts pour pouvoir les décrypter |
56 | 56 | $taille_bout1 = strlen($bout1_mdp); |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | //on insere le premier bout aleatoirement dans le hashmdp |
60 | 60 | //on calcul sa longeur total (celle duhash + la logneur du mdp que l'on va rajouter dedans) |
61 | 61 | $longueur_hash = strlen($encrypt_str); |
62 | - $debut_bout1 = rand(0, $longueur_hash/2); |
|
62 | + $debut_bout1 = rand(0, $longueur_hash / 2); |
|
63 | 63 | |
64 | 64 | //on rajouter le premier bout dans le mot de passe + recalcule de la longeur du hash avec le mdp add |
65 | 65 | $encrypt_str = mb_substr($encrypt_str, 0, $debut_bout1).$bout1_mdp.mb_substr($encrypt_str, $debut_bout1, $longueur_hash); |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | //on insere le second bout aleatoirement dans le hashmdp |
68 | 68 | //on calcul sa longeur total (celle duhash + la logneur premier bout du mdp que l'on va rajouter dedans) |
69 | 69 | $longueur_hash = strlen($encrypt_str); |
70 | - $debut_bout2 = rand($longueur_hash/2, $longueur_hash); |
|
70 | + $debut_bout2 = rand($longueur_hash / 2, $longueur_hash); |
|
71 | 71 | |
72 | 72 | //on rajoute le deuxieme |
73 | 73 | $mdp_encrypt = mb_substr($encrypt_str, 0, $debut_bout2).$bout2_mdp.mb_substr($encrypt_str, $debut_bout2, $longueur_hash); |
@@ -21,8 +21,7 @@ discard block |
||
21 | 21 | foreach ($query as $obj) { |
22 | 22 | $params = $obj->mdp_params; |
23 | 23 | } |
24 | - } |
|
25 | - else { |
|
24 | + } else { |
|
26 | 25 | $params = self::$params; |
27 | 26 | } |
28 | 27 | |
@@ -74,8 +73,7 @@ discard block |
||
74 | 73 | |
75 | 74 | if ($id_identite != null) { |
76 | 75 | self::setSaveParams("$taille_bout1, $debut_bout2, $nb_aleatoire_mdp, $taille_bout2, $debut_bout1, ".ChaineCaractere::random(20), $id_identite); |
77 | - } |
|
78 | - else { |
|
76 | + } else { |
|
79 | 77 | self::$params = "$taille_bout1, $debut_bout2, $nb_aleatoire_mdp, $taille_bout2, $debut_bout1, ".ChaineCaractere::random(20); |
80 | 78 | } |
81 | 79 |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | //-------------------------- GETTER ----------------------------------------------------------------------------// |
16 | 16 | /** |
17 | 17 | * @param null $id_identite |
18 | - * @return mixed |
|
18 | + * @return string |
|
19 | 19 | */ |
20 | 20 | public static function getParams($id_identite=null) { |
21 | 21 | if ($id_identite != null) { |
@@ -38,7 +38,7 @@ discard block |
||
38 | 38 | //-------------------------- SETTER ----------------------------------------------------------------------------// |
39 | 39 | /** |
40 | 40 | * fonction qui permet d'encrypter un mot de passe, une fois encrype, on save les params pour le retrouver en bdd |
41 | - * @param $mdp |
|
41 | + * @param string|null $mdp |
|
42 | 42 | * @param $id_identite |
43 | 43 | * @return string |
44 | 44 | */ |
@@ -7,8 +7,7 @@ |
||
7 | 7 | |
8 | 8 | if (isset($_POST['admin'])) { |
9 | 9 | Connexion::setLogin($pseudo, $mdp, WEBROOT."administrator/login", WEBROOT."administrator"); |
10 | - } |
|
11 | - else { |
|
10 | + } else { |
|
12 | 11 | Connexion::setLogin($pseudo, $mdp, WEBROOT."login", WEBROOT."index.php"); |
13 | 12 | } |
14 | 13 | ?> |
15 | 14 | \ No newline at end of file |
@@ -14,7 +14,7 @@ discard block |
||
14 | 14 | |
15 | 15 | //-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------// |
16 | 16 | public function __construct($datas) { |
17 | - for ($i=0 ; $i<count($datas) ; $i++) { |
|
17 | + for ($i = 0; $i < count($datas); $i++) { |
|
18 | 18 | $function = "setVerif".ucfirst($datas[$i][0]); |
19 | 19 | |
20 | 20 | if (isset($datas[$i][2])) { |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | return $this->mail; |
44 | 44 | } |
45 | 45 | public function getErreur() { |
46 | - return $this->erreur;; |
|
46 | + return $this->erreur; ; |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | protected function getTestRequired($value) { |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | * @param null $required |
84 | 84 | * @return bool |
85 | 85 | */ |
86 | - protected function setVerifNom($value, $required=null) { |
|
86 | + protected function setVerifNom($value, $required = null) { |
|
87 | 87 | //test avec le required, si le champe est vide et que le required est != null on return fa lse sinon on va tester |
88 | 88 | if (($required != null) && ($this->getTestRequired($value) == false)) { |
89 | 89 | $this->erreur .= "<li>Le champs nom ne peut pas être vide</li>"; |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | * @param null $required |
111 | 111 | * @return bool |
112 | 112 | */ |
113 | - protected function setVerifPrenom($value, $required=null) { |
|
113 | + protected function setVerifPrenom($value, $required = null) { |
|
114 | 114 | //test avec le required, si le champe est vide et que le required est != null on return fa lse sinon on va tester |
115 | 115 | if (($required != null) && ($this->getTestRequired($value) == false)) { |
116 | 116 | $this->erreur .= "<li>Le champs prénom ne peut pas être vide</li>"; |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | * @param null $required |
138 | 138 | * @return bool |
139 | 139 | */ |
140 | - protected function setVerifMdp($value, $required=null) { |
|
140 | + protected function setVerifMdp($value, $required = null) { |
|
141 | 141 | //test avec le required, si le champe est vide et que le required est != null on return fa lse sinon on va tester |
142 | 142 | if (($required != null) && ($this->getTestRequired($value) == false)) { |
143 | 143 | $this->erreur .= "<li>Le champs mot de passe ne peut pas être vide</li>"; |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | * @param null $required |
165 | 165 | * @return bool |
166 | 166 | */ |
167 | - protected function setVerifRetapeMdp($value, $required=null) { |
|
167 | + protected function setVerifRetapeMdp($value, $required = null) { |
|
168 | 168 | if ($this->mdp == $value) { |
169 | 169 | return true; |
170 | 170 | } |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | * @param null $required |
182 | 182 | * @return bool |
183 | 183 | */ |
184 | - protected function setVerifPseudo($value, $required=null) { |
|
184 | + protected function setVerifPseudo($value, $required = null) { |
|
185 | 185 | //test avec le required, si le champe est vide et que le required est != null on return fa lse sinon on va tester |
186 | 186 | if (($required != null) && ($this->getTestRequired($value) == false)) { |
187 | 187 | $this->erreur .= "<li>Le champs pseudo ne peut pas être vide</li>"; |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | * @param null $required |
209 | 209 | * @return bool |
210 | 210 | */ |
211 | - protected function setVerifMail($value, $required=null) { |
|
211 | + protected function setVerifMail($value, $required = null) { |
|
212 | 212 | //test avec le required, si le champe est vide et que le required est != null on return fa lse sinon on va tester |
213 | 213 | if (($required != null) && ($this->getTestRequired($value) == false)) { |
214 | 214 | $this->erreur .= "<li>Le champs E-mail ne peut pas être vide</li>"; |
@@ -19,8 +19,7 @@ discard block |
||
19 | 19 | |
20 | 20 | if (isset($datas[$i][2])) { |
21 | 21 | $this->$function($datas[$i][1], $datas[$i][2]); |
22 | - } |
|
23 | - else { |
|
22 | + } else { |
|
24 | 23 | $this->$function($datas[$i][1]); |
25 | 24 | } |
26 | 25 | } |
@@ -49,8 +48,7 @@ discard block |
||
49 | 48 | protected function getTestRequired($value) { |
50 | 49 | if (($value == "") || ($value == null) || (strlen($value) == 0)) { |
51 | 50 | return false; |
52 | - } |
|
53 | - else { |
|
51 | + } else { |
|
54 | 52 | return true; |
55 | 53 | } |
56 | 54 | } |
@@ -58,8 +56,7 @@ discard block |
||
58 | 56 | protected function getTestLongueur($value, $longeur) { |
59 | 57 | if (strlen($value) > $longeur) { |
60 | 58 | return true; |
61 | - } |
|
62 | - else { |
|
59 | + } else { |
|
63 | 60 | return false; |
64 | 61 | } |
65 | 62 | } |
@@ -67,8 +64,7 @@ discard block |
||
67 | 64 | protected function getTestInt($value) { |
68 | 65 | if (is_numeric($value)) { |
69 | 66 | return true; |
70 | - } |
|
71 | - else { |
|
67 | + } else { |
|
72 | 68 | return false; |
73 | 69 | } |
74 | 70 | } |
@@ -88,17 +84,14 @@ discard block |
||
88 | 84 | if (($required != null) && ($this->getTestRequired($value) == false)) { |
89 | 85 | $this->erreur .= "<li>Le champs nom ne peut pas être vide</li>"; |
90 | 86 | return false; |
91 | - } |
|
92 | - else { |
|
87 | + } else { |
|
93 | 88 | if (($value != "") && ($this->getTestLongueur($value, 2) == true)) { |
94 | 89 | $this->nom = $value; |
95 | 90 | return true; |
96 | - } |
|
97 | - else if (($value != "") && ($this->getTestLongueur($value, 2) == false)) { |
|
91 | + } else if (($value != "") && ($this->getTestLongueur($value, 2) == false)) { |
|
98 | 92 | $this->erreur .= "<li>Le champs nom soit être supérieur à deux caractères</li>"; |
99 | 93 | return false; |
100 | - } |
|
101 | - else { |
|
94 | + } else { |
|
102 | 95 | return true; |
103 | 96 | } |
104 | 97 | } |
@@ -115,17 +108,14 @@ discard block |
||
115 | 108 | if (($required != null) && ($this->getTestRequired($value) == false)) { |
116 | 109 | $this->erreur .= "<li>Le champs prénom ne peut pas être vide</li>"; |
117 | 110 | return false; |
118 | - } |
|
119 | - else { |
|
111 | + } else { |
|
120 | 112 | if (($value != "") && ($this->getTestLongueur($value, 2) == true)) { |
121 | 113 | $this->prenom = $value; |
122 | 114 | return true; |
123 | - } |
|
124 | - else if (($value != "") && ($this->getTestLongueur($value, 2) == false)) { |
|
115 | + } else if (($value != "") && ($this->getTestLongueur($value, 2) == false)) { |
|
125 | 116 | $this->erreur .= "<li>Le champs prénom soit être supérieur à deux caractères</li>"; |
126 | 117 | return false; |
127 | - } |
|
128 | - else { |
|
118 | + } else { |
|
129 | 119 | return true; |
130 | 120 | } |
131 | 121 | } |
@@ -142,17 +132,14 @@ discard block |
||
142 | 132 | if (($required != null) && ($this->getTestRequired($value) == false)) { |
143 | 133 | $this->erreur .= "<li>Le champs mot de passe ne peut pas être vide</li>"; |
144 | 134 | return false; |
145 | - } |
|
146 | - else { |
|
135 | + } else { |
|
147 | 136 | if (($value != "") && ($this->getTestLongueur($value, 2) == true)) { |
148 | 137 | $this->mdp = $value; |
149 | 138 | return true; |
150 | - } |
|
151 | - else if (($value != "") && ($this->getTestLongueur($value, 2) == false)) { |
|
139 | + } else if (($value != "") && ($this->getTestLongueur($value, 2) == false)) { |
|
152 | 140 | $this->erreur .= "<li>Le champs mot de passe soit être supérieur à deux caractères</li>"; |
153 | 141 | return false; |
154 | - } |
|
155 | - else { |
|
142 | + } else { |
|
156 | 143 | return true; |
157 | 144 | } |
158 | 145 | } |
@@ -167,8 +154,7 @@ discard block |
||
167 | 154 | protected function setVerifRetapeMdp($value, $required=null) { |
168 | 155 | if ($this->mdp == $value) { |
169 | 156 | return true; |
170 | - } |
|
171 | - else { |
|
157 | + } else { |
|
172 | 158 | $this->erreur .= "<li>Les mots de passent ne correspondent pas</li>"; |
173 | 159 | $this->mdp = null; |
174 | 160 | return false; |
@@ -186,17 +172,14 @@ discard block |
||
186 | 172 | if (($required != null) && ($this->getTestRequired($value) == false)) { |
187 | 173 | $this->erreur .= "<li>Le champs pseudo ne peut pas être vide</li>"; |
188 | 174 | return false; |
189 | - } |
|
190 | - else { |
|
175 | + } else { |
|
191 | 176 | if (($value != "") && ($this->getTestLongueur($value, 2) == true)) { |
192 | 177 | $this->pseudo = $value; |
193 | 178 | return true; |
194 | - } |
|
195 | - else if (($value != "") && ($this->getTestLongueur($value, 2) == false)) { |
|
179 | + } else if (($value != "") && ($this->getTestLongueur($value, 2) == false)) { |
|
196 | 180 | $this->erreur .= "<li>Le champs pseudo soit être supérieur à deux caractères</li>"; |
197 | 181 | return false; |
198 | - } |
|
199 | - else { |
|
182 | + } else { |
|
200 | 183 | return true; |
201 | 184 | } |
202 | 185 | } |
@@ -213,17 +196,14 @@ discard block |
||
213 | 196 | if (($required != null) && ($this->getTestRequired($value) == false)) { |
214 | 197 | $this->erreur .= "<li>Le champs E-mail ne peut pas être vide</li>"; |
215 | 198 | return false; |
216 | - } |
|
217 | - else { |
|
199 | + } else { |
|
218 | 200 | if (($value != "") && (filter_var($value, FILTER_VALIDATE_EMAIL) != false)) { |
219 | 201 | $this->mail = $value; |
220 | 202 | return true; |
221 | - } |
|
222 | - else if (($value != "") && (filter_var($value, FILTER_VALIDATE_EMAIL) == false)) { |
|
203 | + } else if (($value != "") && (filter_var($value, FILTER_VALIDATE_EMAIL) == false)) { |
|
223 | 204 | $this->erreur .= "<li>Le champs E-mail doit être une adresse E-mail valide</li>"; |
224 | 205 | return false; |
225 | - } |
|
226 | - else { |
|
206 | + } else { |
|
227 | 207 | return true; |
228 | 208 | } |
229 | 209 | } |
@@ -18,20 +18,17 @@ |
||
18 | 18 | if ($membre->getErreur()) { |
19 | 19 | FlashMessage::setFlash($membre->getErreur()); |
20 | 20 | header("location:".ADMWEBROOT."gestion-comptes/mon-compte"); |
21 | - } |
|
22 | - else { |
|
21 | + } else { |
|
23 | 22 | Connexion::setDeconnexion(WEBROOT."administrator/login"); |
24 | 23 | FlashMessage::setFlash("Votre mot de passe a été changé avec succès, vous pouvez vous reconnecter avec votre nouveau mot de passe", "info"); |
25 | 24 | } |
26 | - } |
|
27 | - else { |
|
25 | + } else { |
|
28 | 26 | $membre->setMdp($mdp, $new_mdp, $verif_new_mdp); |
29 | 27 | |
30 | 28 | if ($membre->getErreur()) { |
31 | 29 | FlashMessage::setFlash($membre->getErreur()); |
32 | 30 | header("location:index.php"); |
33 | - } |
|
34 | - else { |
|
31 | + } else { |
|
35 | 32 | Connexion::setDeconnexion("index.php?page=login"); |
36 | 33 | FlashMessage::setFlash("Votre mot de passe a été changé avec succès, vous pouvez vous reconnecter avec votre nouveau mot de passe", "info"); |
37 | 34 | } |
@@ -1,7 +1,9 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | //error_reporting(E_ALL ^ E_NOTICE); |
3 | 3 | |
4 | - if (!isset($page_root)) $page_root = "index.php"; |
|
4 | + if (!isset($page_root)) { |
|
5 | + $page_root = "index.php"; |
|
6 | + } |
|
5 | 7 | |
6 | 8 | //-------------------------- CONSTANTE POUR LES ROUTES ----------------------------------------------------------------------------// |
7 | 9 | //definit le chemin vers la racine du projet (depuis racine serveur web |
@@ -48,15 +50,13 @@ discard block |
||
48 | 50 | if ($ini["installation"] == 1) { |
49 | 51 | header("location:".WEBROOT."installation"); |
50 | 52 | } |
51 | - } |
|
52 | - else { |
|
53 | + } else { |
|
53 | 54 | header("location:".WEBROOT."installation"); |
54 | 55 | } |
55 | 56 | |
56 | 57 | if ($ini["developpment"] == 1) { |
57 | 58 | $tab = "dev"; |
58 | - } |
|
59 | - else { |
|
59 | + } else { |
|
60 | 60 | $tab = "prod"; |
61 | 61 | } |
62 | 62 |
@@ -2,8 +2,6 @@ |
||
2 | 2 | namespace core\auth; |
3 | 3 | |
4 | 4 | |
5 | - use core\images\Resize; |
|
6 | - |
|
7 | 5 | class Membre { |
8 | 6 | protected $id_identite; |
9 | 7 | protected $nom; |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | |
20 | 20 | //------------------------------ constructeur----------------------------------- |
21 | 21 | //Récupérer en base de données les infos du membre |
22 | - public function __construct($id_identite=null){ |
|
22 | + public function __construct($id_identite = null) { |
|
23 | 23 | $dbc = \core\App::getDb(); |
24 | 24 | |
25 | 25 | $this->debut_lien = IMGROOT."profil/"; |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | /** |
86 | 86 | * @param null $id_identite |
87 | 87 | */ |
88 | - public function setSupprimUser($id_identite=null) { |
|
88 | + public function setSupprimUser($id_identite = null) { |
|
89 | 89 | $dbc = \core\App::getDb(); |
90 | 90 | |
91 | 91 | if ($id_identite == null) { |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | $oldimg_profil = $obj->img_profil; |
100 | 100 | if ($oldimg_profil != "") { |
101 | 101 | $oldimg_profil = explode("/", $oldimg_profil); |
102 | - if(end($oldimg_profil) == "defaut.png") { |
|
102 | + if (end($oldimg_profil) == "defaut.png") { |
|
103 | 103 | |
104 | 104 | } |
105 | 105 | else { |
@@ -194,22 +194,22 @@ discard block |
||
194 | 194 | * @param string $mdp |
195 | 195 | * @return integer |
196 | 196 | */ |
197 | - function testpassword($mdp) { |
|
197 | + function testpassword($mdp) { |
|
198 | 198 | $longueur = strlen($mdp); |
199 | 199 | $point = 0; |
200 | 200 | |
201 | - for ($i=0 ; $i<$longueur ; $i++) { |
|
201 | + for ($i = 0; $i < $longueur; $i++) { |
|
202 | 202 | $lettre = $mdp[$i]; |
203 | 203 | |
204 | 204 | if ($lettre >= 'a' && $lettre <= 'z') { |
205 | 205 | $point = $point + 1; |
206 | 206 | $point_min = 1; |
207 | 207 | } |
208 | - else if ($lettre >= 'A' && $lettre <= 'Z'){ |
|
208 | + else if ($lettre >= 'A' && $lettre <= 'Z') { |
|
209 | 209 | $point = $point + 2; |
210 | 210 | $point_maj = 2; |
211 | 211 | } |
212 | - else if ($lettre >= '0' && $lettre <= '9'){ |
|
212 | + else if ($lettre >= '0' && $lettre <= '9') { |
|
213 | 213 | $point = $point + 3; |
214 | 214 | $point_chiffre = 3; |
215 | 215 | } |
@@ -38,8 +38,7 @@ discard block |
||
38 | 38 | |
39 | 39 | if ($obj->img_profil == "") { |
40 | 40 | $this->img = $this->debut_lien."defaut.png"; |
41 | - } |
|
42 | - else { |
|
41 | + } else { |
|
43 | 42 | $this->img = $obj->img_profil; |
44 | 43 | } |
45 | 44 | } |
@@ -101,8 +100,7 @@ discard block |
||
101 | 100 | $oldimg_profil = explode("/", $oldimg_profil); |
102 | 101 | if(end($oldimg_profil) == "defaut.png") { |
103 | 102 | |
104 | - } |
|
105 | - else { |
|
103 | + } else { |
|
106 | 104 | unlink("../../images/profil/".end($oldimg_profil)); |
107 | 105 | |
108 | 106 | } |
@@ -127,16 +125,13 @@ discard block |
||
127 | 125 | if (strlen($new_pseudo) < 5) { |
128 | 126 | $err = "Votre pseudo est trop court"; |
129 | 127 | $this->erreur = $err; |
130 | - } |
|
131 | - else if (strlen($new_pseudo) > 15) { |
|
128 | + } else if (strlen($new_pseudo) > 15) { |
|
132 | 129 | $err = "Votre pseudo est trop long"; |
133 | 130 | $this->erreur = $err; |
134 | - } |
|
135 | - else if ($new_pseudo == $pseudo_bdd) { |
|
131 | + } else if ($new_pseudo == $pseudo_bdd) { |
|
136 | 132 | $err = "Ce pseudo est déjà utilisé, veuillez en choisir un autre"; |
137 | 133 | $this->erreur = $err; |
138 | - } |
|
139 | - else { |
|
134 | + } else { |
|
140 | 135 | $dbc->query("UPDATE identite set pseudo=$new_pseudo WHERE ID_identite=".$_SESSION["idlogin".CLEF_SITE]); |
141 | 136 | $this->pseudo = $new_pseudo; |
142 | 137 | } |
@@ -157,24 +152,20 @@ discard block |
||
157 | 152 | if (md5($old_mdp) != $mdp) { |
158 | 153 | $err = "Votre mot de passe est incorrect"; |
159 | 154 | $this->erreur = $err; |
160 | - } |
|
161 | - else { |
|
155 | + } else { |
|
162 | 156 | if ($new_mdp != $verif_new_mdp) { |
163 | 157 | $err = "Vos mots de passe sont différents"; |
164 | 158 | $this->erreur = $err; |
165 | - } |
|
166 | - else { |
|
159 | + } else { |
|
167 | 160 | $testmdp = $this->testpassword($new_mdp); |
168 | 161 | |
169 | 162 | if (strlen($new_mdp) < 5) { |
170 | 163 | $err = "Votre mot de passe est trop court"; |
171 | 164 | $this->erreur = $err; |
172 | - } |
|
173 | - else if ($testmdp < 40) { |
|
165 | + } else if ($testmdp < 40) { |
|
174 | 166 | $err = "Votre mot de passe est trop simple"; |
175 | 167 | $this->erreur = $err; |
176 | - } |
|
177 | - else { |
|
168 | + } else { |
|
178 | 169 | $mdpok = Encrypt::setEncryptMdp($new_mdp, $this->id_identite); |
179 | 170 | //le nouveau mdp est bon on update |
180 | 171 | $dbc->query("UPDATE identite SET mdp='$mdpok' WHERE ID_identite=".$this->id_identite); |
@@ -204,16 +195,13 @@ discard block |
||
204 | 195 | if ($lettre >= 'a' && $lettre <= 'z') { |
205 | 196 | $point = $point + 1; |
206 | 197 | $point_min = 1; |
207 | - } |
|
208 | - else if ($lettre >= 'A' && $lettre <= 'Z'){ |
|
198 | + } else if ($lettre >= 'A' && $lettre <= 'Z'){ |
|
209 | 199 | $point = $point + 2; |
210 | 200 | $point_maj = 2; |
211 | - } |
|
212 | - else if ($lettre >= '0' && $lettre <= '9'){ |
|
201 | + } else if ($lettre >= '0' && $lettre <= '9'){ |
|
213 | 202 | $point = $point + 3; |
214 | 203 | $point_chiffre = 3; |
215 | - } |
|
216 | - else { |
|
204 | + } else { |
|
217 | 205 | $point = $point + 5; |
218 | 206 | $point_caracteres = 5; |
219 | 207 | } |
@@ -1,4 +1,4 @@ |
||
1 | -<?=\core\HTML\flashmessage\FlashMessage::getFlash();?> |
|
1 | +<?=\core\HTML\flashmessage\FlashMessage::getFlash(); ?> |
|
2 | 2 | <header> |
3 | 3 | <div class="inner"> |
4 | 4 | <h1>Mon compte</h1> |