xref: /plugin/struct/meta/CSVExporter.php (revision 69d92b7aa050d48e9f54bcdf74981f89787ea326)
1f36cc634SAndreas Gohr<?php
2f36cc634SAndreas Gohr
3f36cc634SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
4f36cc634SAndreas Gohr
5f36cc634SAndreas Gohr/**
6f36cc634SAndreas Gohr * Class CSVExporter
7f36cc634SAndreas Gohr *
8f36cc634SAndreas Gohr * exports raw schema data to CSV. For lookup schemas this data can be reimported again through
9f36cc634SAndreas Gohr * CSVImporter
10f36cc634SAndreas Gohr *
11f36cc634SAndreas Gohr * Note this is different from syntax/csv.php
12f36cc634SAndreas Gohr *
13f36cc634SAndreas Gohr * @package dokuwiki\plugin\struct\meta
14f36cc634SAndreas Gohr */
15d6d97f60SAnna Dabrowskaclass CSVExporter
16d6d97f60SAnna Dabrowska{
17*69d92b7aSAnna Dabrowska    protected $type = '';
18*69d92b7aSAnna Dabrowska
19f36cc634SAndreas Gohr    /**
20*69d92b7aSAnna Dabrowska     * CSVExporter constructor.
21f36cc634SAndreas Gohr     *
22f36cc634SAndreas Gohr     * @param string $table
23*69d92b7aSAnna Dabrowska     * @param string $type
24f36cc634SAndreas Gohr     */
25*69d92b7aSAnna Dabrowska    public function __construct($table, $type)
26d6d97f60SAnna Dabrowska    {
27*69d92b7aSAnna Dabrowska        // TODO make it nicer
28*69d92b7aSAnna Dabrowska        $this->type = $type;
29f36cc634SAndreas Gohr
30f36cc634SAndreas Gohr        $search = new Search();
31f36cc634SAndreas Gohr        $search->addSchema($table);
32f36cc634SAndreas Gohr        $search->addColumn('*');
33f36cc634SAndreas Gohr        $result = $search->execute();
34f36cc634SAndreas Gohr
35*69d92b7aSAnna Dabrowska        if ($this->type !== 'lookup') {
36f36cc634SAndreas Gohr            $pids = $search->getPids();
37*69d92b7aSAnna Dabrowska        }
38f36cc634SAndreas Gohr
39f36cc634SAndreas Gohr        echo $this->header($search->getColumns());
40f36cc634SAndreas Gohr        foreach ($result as $i => $row) {
41*69d92b7aSAnna Dabrowska            if ($this->type !== 'lookup') {
42f36cc634SAndreas Gohr                $pid = $pids[$i];
43*69d92b7aSAnna Dabrowska            } else {
44*69d92b7aSAnna Dabrowska                $pid = '';
45*69d92b7aSAnna Dabrowska            }
46f36cc634SAndreas Gohr            echo $this->row($row, $pid);
47f36cc634SAndreas Gohr        }
48f36cc634SAndreas Gohr    }
49f36cc634SAndreas Gohr
50f36cc634SAndreas Gohr    /**
51f36cc634SAndreas Gohr     * Create the header
52f36cc634SAndreas Gohr     *
53f36cc634SAndreas Gohr     * @param Column[] $columns
54f36cc634SAndreas Gohr     * @return string
55f36cc634SAndreas Gohr     */
56d6d97f60SAnna Dabrowska    protected function header($columns)
57d6d97f60SAnna Dabrowska    {
58f36cc634SAndreas Gohr        $row = '';
59*69d92b7aSAnna Dabrowska
60*69d92b7aSAnna Dabrowska        if($this->type !== 'lookup') {
61f36cc634SAndreas Gohr            $row .= $this->escape('pid');
62f36cc634SAndreas Gohr            $row .= ',';
63*69d92b7aSAnna Dabrowska        }
64f36cc634SAndreas Gohr
65f36cc634SAndreas Gohr        foreach ($columns as $i => $col) {
66f36cc634SAndreas Gohr            $row .= $this->escape($col->getLabel());
67f36cc634SAndreas Gohr            $row .= ',';
68f36cc634SAndreas Gohr        }
69f36cc634SAndreas Gohr        return rtrim($row, ',') . "\r\n";
70f36cc634SAndreas Gohr    }
71f36cc634SAndreas Gohr
72f36cc634SAndreas Gohr    /**
73f36cc634SAndreas Gohr     * Create one row of data
74f36cc634SAndreas Gohr     *
75f36cc634SAndreas Gohr     * @param Value[] $values
76f36cc634SAndreas Gohr     * @param string $pid pid of this row
77f36cc634SAndreas Gohr     * @return string
78f36cc634SAndreas Gohr     */
79d6d97f60SAnna Dabrowska    protected function row($values, $pid)
80d6d97f60SAnna Dabrowska    {
81f36cc634SAndreas Gohr        $row = '';
82*69d92b7aSAnna Dabrowska        if($this->type !== 'lookup') {
83f36cc634SAndreas Gohr            $row .= $this->escape($pid);
84f36cc634SAndreas Gohr            $row .= ',';
85*69d92b7aSAnna Dabrowska        }
86f36cc634SAndreas Gohr
87f36cc634SAndreas Gohr        foreach ($values as $value) {
88f36cc634SAndreas Gohr            /** @var Value $value */
89f36cc634SAndreas Gohr            $val = $value->getRawValue();
90f36cc634SAndreas Gohr            if (is_array($val)) $val = join(',', $val);
91f36cc634SAndreas Gohr
92*69d92b7aSAnna Dabrowska            // FIXME check escaping of composite ids (JSON with """")
93f36cc634SAndreas Gohr            $row .= $this->escape($val);
94f36cc634SAndreas Gohr            $row .= ',';
95f36cc634SAndreas Gohr        }
96f36cc634SAndreas Gohr
97f36cc634SAndreas Gohr        return rtrim($row, ',') . "\r\n";
98f36cc634SAndreas Gohr    }
99f36cc634SAndreas Gohr
100f36cc634SAndreas Gohr    /**
101f36cc634SAndreas Gohr     * Escapes and wraps the given string
102f36cc634SAndreas Gohr     *
103f36cc634SAndreas Gohr     * Uses doubled quotes for escaping which seems to be the standard escaping method for CSV
104f36cc634SAndreas Gohr     *
105f36cc634SAndreas Gohr     * @param string $str
106f36cc634SAndreas Gohr     * @return string
107f36cc634SAndreas Gohr     */
108d6d97f60SAnna Dabrowska    protected function escape($str)
109d6d97f60SAnna Dabrowska    {
110f36cc634SAndreas Gohr        return '"' . str_replace('"', '""', $str) . '"';
111f36cc634SAndreas Gohr    }
112f36cc634SAndreas Gohr}
113