| 1 | <?php |
|---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
|---|
| 3 | # |
|---|
| 4 | # This file is part of importExport, a plugin for DotClear2. |
|---|
| 5 | # |
|---|
| 6 | # Copyright (c) 2003-2012 Olivier Meunier & Association Dotclear |
|---|
| 7 | # Licensed under the GPL version 2.0 license. |
|---|
| 8 | # See LICENSE file or |
|---|
| 9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
|---|
| 10 | # |
|---|
| 11 | # -- END LICENSE BLOCK ----------------------------------------- |
|---|
| 12 | if (!defined('DC_RC_PATH')) { return; } |
|---|
| 13 | |
|---|
| 14 | class flatExport |
|---|
| 15 | { |
|---|
| 16 | private $con; |
|---|
| 17 | private $prefix; |
|---|
| 18 | |
|---|
| 19 | private $line_reg = array('/\\\\/u', '/\n/u','/\r/u','/"/u'); |
|---|
| 20 | private $line_rep = array('\\\\\\\\', '\n' ,'\r' ,'\"'); |
|---|
| 21 | |
|---|
| 22 | public $fp; |
|---|
| 23 | |
|---|
| 24 | 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 | function __destruct() |
|---|
| 36 | { |
|---|
| 37 | if (is_resource($this->fp)) { |
|---|
| 38 | fclose($this->fp); |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | function export($name,$sql) |
|---|
| 43 | { |
|---|
| 44 | $rs = $this->con->select($sql); |
|---|
| 45 | |
|---|
| 46 | if (!$rs->isEmpty()) |
|---|
| 47 | { |
|---|
| 48 | fwrite($this->fp,"\n[".$name.' '.implode(',',$rs->columns())."]\n"); |
|---|
| 49 | while ($rs->fetch()) { |
|---|
| 50 | fwrite($this->fp,$this->getLine($rs)); |
|---|
| 51 | } |
|---|
| 52 | fflush($this->fp); |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | function exportAll() |
|---|
| 57 | { |
|---|
| 58 | $tables = $this->getTables(); |
|---|
| 59 | |
|---|
| 60 | foreach ($tables as $table) |
|---|
| 61 | { |
|---|
| 62 | $this->exportTable($table); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | function exportTable($table) |
|---|
| 67 | { |
|---|
| 68 | $req = 'SELECT * FROM '.$this->con->escapeSystem($this->prefix.$table); |
|---|
| 69 | |
|---|
| 70 | $this->export($table,$req); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | function getTables() |
|---|
| 74 | { |
|---|
| 75 | $schema = dbSchema::init($this->con); |
|---|
| 76 | $db_tables = $schema->getTables(); |
|---|
| 77 | |
|---|
| 78 | $tables = array(); |
|---|
| 79 | foreach ($db_tables as $t) |
|---|
| 80 | { |
|---|
| 81 | if ($this->prefix) { |
|---|
| 82 | if (strpos($t,$this->prefix) === 0) { |
|---|
| 83 | $tables[] = $t; |
|---|
| 84 | } |
|---|
| 85 | } else { |
|---|
| 86 | $tables[] = $t; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | return $tables; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | function getLine($rs) |
|---|
| 94 | { |
|---|
| 95 | $l = array(); |
|---|
| 96 | $cols = $rs->columns(); |
|---|
| 97 | foreach ($cols as $i => &$c) { |
|---|
| 98 | $s = $rs->f($c); |
|---|
| 99 | $s = preg_replace($this->line_reg,$this->line_rep,$s); |
|---|
| 100 | $s = '"'.$s.'"'; |
|---|
| 101 | $l[$i] = $s; |
|---|
| 102 | } |
|---|
| 103 | return implode(',',$l)."\n"; |
|---|
| 104 | } |
|---|
| 105 | } |
|---|