[0] | 1 | <?php |
---|
[3731] | 2 | /** |
---|
| 3 | * @brief importExport, a plugin for Dotclear 2 |
---|
| 4 | * |
---|
| 5 | * @package Dotclear |
---|
| 6 | * @subpackage Plugins |
---|
| 7 | * |
---|
| 8 | * @copyright Olivier Meunier & Association Dotclear |
---|
| 9 | * @copyright GPL-2.0-only |
---|
| 10 | */ |
---|
| 11 | |
---|
[3730] | 12 | if (!defined('DC_RC_PATH')) {return;} |
---|
[0] | 13 | |
---|
[840] | 14 | class flatExport |
---|
[0] | 15 | { |
---|
[3730] | 16 | private $con; |
---|
| 17 | private $prefix; |
---|
[2566] | 18 | |
---|
[3874] | 19 | private $line_reg = ['/\\\\/u', '/\n/u', '/\r/u', '/"/u']; |
---|
| 20 | private $line_rep = ['\\\\\\\\', '\n', '\r', '\"']; |
---|
[2566] | 21 | |
---|
[3730] | 22 | public $fp; |
---|
[2566] | 23 | |
---|
[3730] | 24 | public function __construct($con, $out = 'php://output', $prefix = null) |
---|
| 25 | { |
---|
| 26 | $this->con = &$con; |
---|
| 27 | $this->prefix = $prefix; |
---|
[2566] | 28 | |
---|
[3730] | 29 | if (($this->fp = fopen($out, 'w')) === false) { |
---|
| 30 | return false; |
---|
| 31 | } |
---|
| 32 | @set_time_limit(300); |
---|
| 33 | } |
---|
[2566] | 34 | |
---|
[3730] | 35 | public function __destruct() |
---|
| 36 | { |
---|
| 37 | if (is_resource($this->fp)) { |
---|
| 38 | fclose($this->fp); |
---|
| 39 | } |
---|
| 40 | } |
---|
[2566] | 41 | |
---|
[3730] | 42 | public function export($name, $sql) |
---|
| 43 | { |
---|
| 44 | $rs = $this->con->select($sql); |
---|
[2566] | 45 | |
---|
[3730] | 46 | if (!$rs->isEmpty()) { |
---|
| 47 | fwrite($this->fp, "\n[" . $name . ' ' . implode(',', $rs->columns()) . "]\n"); |
---|
| 48 | while ($rs->fetch()) { |
---|
| 49 | fwrite($this->fp, $this->getLine($rs)); |
---|
| 50 | } |
---|
| 51 | fflush($this->fp); |
---|
| 52 | } |
---|
| 53 | } |
---|
[2566] | 54 | |
---|
[3730] | 55 | public function exportAll() |
---|
| 56 | { |
---|
| 57 | $tables = $this->getTables(); |
---|
[2566] | 58 | |
---|
[3730] | 59 | foreach ($tables as $table) { |
---|
| 60 | $this->exportTable($table); |
---|
| 61 | } |
---|
| 62 | } |
---|
[2566] | 63 | |
---|
[3730] | 64 | public function exportTable($table) |
---|
| 65 | { |
---|
| 66 | $req = 'SELECT * FROM ' . $this->con->escapeSystem($this->prefix . $table); |
---|
[2566] | 67 | |
---|
[3730] | 68 | $this->export($table, $req); |
---|
| 69 | } |
---|
[2566] | 70 | |
---|
[3730] | 71 | public function getTables() |
---|
| 72 | { |
---|
| 73 | $schema = dbSchema::init($this->con); |
---|
| 74 | $db_tables = $schema->getTables(); |
---|
[2566] | 75 | |
---|
[3874] | 76 | $tables = []; |
---|
[3730] | 77 | foreach ($db_tables as $t) { |
---|
| 78 | if ($this->prefix) { |
---|
| 79 | if (strpos($t, $this->prefix) === 0) { |
---|
| 80 | $tables[] = $t; |
---|
| 81 | } |
---|
| 82 | } else { |
---|
| 83 | $tables[] = $t; |
---|
| 84 | } |
---|
| 85 | } |
---|
[2566] | 86 | |
---|
[3730] | 87 | return $tables; |
---|
| 88 | } |
---|
[2566] | 89 | |
---|
[3730] | 90 | public function getLine($rs) |
---|
| 91 | { |
---|
[3874] | 92 | $l = []; |
---|
[3730] | 93 | $cols = $rs->columns(); |
---|
| 94 | foreach ($cols as $i => &$c) { |
---|
| 95 | $s = $rs->f($c); |
---|
| 96 | $s = preg_replace($this->line_reg, $this->line_rep, $s); |
---|
| 97 | $s = '"' . $s . '"'; |
---|
| 98 | $l[$i] = $s; |
---|
| 99 | } |
---|
| 100 | return implode(',', $l) . "\n"; |
---|
| 101 | } |
---|
[0] | 102 | } |
---|