1 | <?php |
---|
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 | |
---|
12 | if (!defined('DC_RC_PATH')) {return;} |
---|
13 | |
---|
14 | class flatExport |
---|
15 | { |
---|
16 | private $con; |
---|
17 | private $prefix; |
---|
18 | |
---|
19 | private $line_reg = ['/\\\\/u', '/\n/u', '/\r/u', '/"/u']; |
---|
20 | private $line_rep = ['\\\\\\\\', '\n', '\r', '\"']; |
---|
21 | |
---|
22 | public $fp; |
---|
23 | |
---|
24 | public function __construct($con, $out = 'php://output', $prefix = null) |
---|
25 | { |
---|
26 | $this->con = &$con; |
---|
27 | $this->prefix = $prefix; |
---|
28 | |
---|
29 | if (($this->fp = fopen($out, 'w')) === false) { |
---|
30 | return false; |
---|
31 | } |
---|
32 | @set_time_limit(300); |
---|
33 | } |
---|
34 | |
---|
35 | public function __destruct() |
---|
36 | { |
---|
37 | if (is_resource($this->fp)) { |
---|
38 | fclose($this->fp); |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | public function export($name, $sql) |
---|
43 | { |
---|
44 | $rs = $this->con->select($sql); |
---|
45 | |
---|
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 | } |
---|
54 | |
---|
55 | public function exportAll() |
---|
56 | { |
---|
57 | $tables = $this->getTables(); |
---|
58 | |
---|
59 | foreach ($tables as $table) { |
---|
60 | $this->exportTable($table); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | public function exportTable($table) |
---|
65 | { |
---|
66 | $req = 'SELECT * FROM ' . $this->con->escapeSystem($this->prefix . $table); |
---|
67 | |
---|
68 | $this->export($table, $req); |
---|
69 | } |
---|
70 | |
---|
71 | public function getTables() |
---|
72 | { |
---|
73 | $schema = dbSchema::init($this->con); |
---|
74 | $db_tables = $schema->getTables(); |
---|
75 | |
---|
76 | $tables = []; |
---|
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 | } |
---|
86 | |
---|
87 | return $tables; |
---|
88 | } |
---|
89 | |
---|
90 | public function getLine($rs) |
---|
91 | { |
---|
92 | $l = []; |
---|
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 | } |
---|
102 | } |
---|