xref: /plugin/struct/meta/CSVExporter.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
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 */
15*d6d97f60SAnna Dabrowskaclass CSVExporter
16*d6d97f60SAnna Dabrowska{
17f36cc634SAndreas Gohr
18f36cc634SAndreas Gohr    protected $islookup = false;
19f36cc634SAndreas Gohr
20f36cc634SAndreas Gohr    /**
21f36cc634SAndreas Gohr     * CSVImporter constructor.
22f36cc634SAndreas Gohr     *
23f36cc634SAndreas Gohr     * @throws StructException
24f36cc634SAndreas Gohr     * @param string $table
25f36cc634SAndreas Gohr     */
26*d6d97f60SAnna Dabrowska    public function __construct($table)
27*d6d97f60SAnna Dabrowska    {
28f36cc634SAndreas Gohr
29f36cc634SAndreas Gohr        $search = new Search();
30f36cc634SAndreas Gohr        $search->addSchema($table);
31f36cc634SAndreas Gohr        $search->addColumn('*');
32f36cc634SAndreas Gohr        $result = $search->execute();
33f36cc634SAndreas Gohr
34f36cc634SAndreas Gohr        $this->islookup = $search->getSchemas()[0]->isLookup();
35f36cc634SAndreas Gohr        if (!$this->islookup) {
36f36cc634SAndreas Gohr            $pids = $search->getPids();
37f36cc634SAndreas Gohr        } else {
38f36cc634SAndreas Gohr            $pids = array();
39f36cc634SAndreas Gohr        }
40f36cc634SAndreas Gohr
41f36cc634SAndreas Gohr        echo $this->header($search->getColumns());
42f36cc634SAndreas Gohr        foreach ($result as $i => $row) {
43f36cc634SAndreas Gohr            if (!$this->islookup) {
44f36cc634SAndreas Gohr                $pid = $pids[$i];
45f36cc634SAndreas Gohr            } else {
46f36cc634SAndreas Gohr                $pid = 0;
47f36cc634SAndreas Gohr            }
48f36cc634SAndreas Gohr
49f36cc634SAndreas Gohr            echo $this->row($row, $pid);
50f36cc634SAndreas Gohr        }
51f36cc634SAndreas Gohr    }
52f36cc634SAndreas Gohr
53f36cc634SAndreas Gohr    /**
54f36cc634SAndreas Gohr     * Create the header
55f36cc634SAndreas Gohr     *
56f36cc634SAndreas Gohr     * @param Column[] $columns
57f36cc634SAndreas Gohr     * @return string
58f36cc634SAndreas Gohr     */
59*d6d97f60SAnna Dabrowska    protected function header($columns)
60*d6d97f60SAnna Dabrowska    {
61f36cc634SAndreas Gohr        $row = '';
62f36cc634SAndreas Gohr
63f36cc634SAndreas Gohr        if (!$this->islookup) {
64f36cc634SAndreas Gohr            $row .= $this->escape('pid');
65f36cc634SAndreas Gohr            $row .= ',';
66f36cc634SAndreas Gohr        }
67f36cc634SAndreas Gohr
68f36cc634SAndreas Gohr        foreach ($columns as $i => $col) {
69f36cc634SAndreas Gohr            $row .= $this->escape($col->getLabel());
70f36cc634SAndreas Gohr            $row .= ',';
71f36cc634SAndreas Gohr        }
72f36cc634SAndreas Gohr        return rtrim($row, ',') . "\r\n";
73f36cc634SAndreas Gohr    }
74f36cc634SAndreas Gohr
75f36cc634SAndreas Gohr    /**
76f36cc634SAndreas Gohr     * Create one row of data
77f36cc634SAndreas Gohr     *
78f36cc634SAndreas Gohr     * @param Value[] $values
79f36cc634SAndreas Gohr     * @param string $pid pid of this row
80f36cc634SAndreas Gohr     * @return string
81f36cc634SAndreas Gohr     */
82*d6d97f60SAnna Dabrowska    protected function row($values, $pid)
83*d6d97f60SAnna Dabrowska    {
84f36cc634SAndreas Gohr        $row = '';
85f36cc634SAndreas Gohr
86f36cc634SAndreas Gohr        if (!$this->islookup) {
87f36cc634SAndreas Gohr            $row .= $this->escape($pid);
88f36cc634SAndreas Gohr            $row .= ',';
89f36cc634SAndreas Gohr        }
90f36cc634SAndreas Gohr
91f36cc634SAndreas Gohr        foreach ($values as $value) {
92f36cc634SAndreas Gohr            /** @var Value $value */
93f36cc634SAndreas Gohr            $val = $value->getRawValue();
94f36cc634SAndreas Gohr            if (is_array($val)) $val = join(',', $val);
95f36cc634SAndreas Gohr
96f36cc634SAndreas Gohr            $row .= $this->escape($val);
97f36cc634SAndreas Gohr            $row .= ',';
98f36cc634SAndreas Gohr        }
99f36cc634SAndreas Gohr
100f36cc634SAndreas Gohr        return rtrim($row, ',') . "\r\n";
101f36cc634SAndreas Gohr    }
102f36cc634SAndreas Gohr
103f36cc634SAndreas Gohr    /**
104f36cc634SAndreas Gohr     * Escapes and wraps the given string
105f36cc634SAndreas Gohr     *
106f36cc634SAndreas Gohr     * Uses doubled quotes for escaping which seems to be the standard escaping method for CSV
107f36cc634SAndreas Gohr     *
108f36cc634SAndreas Gohr     * @param string $str
109f36cc634SAndreas Gohr     * @return string
110f36cc634SAndreas Gohr     */
111*d6d97f60SAnna Dabrowska    protected function escape($str)
112*d6d97f60SAnna Dabrowska    {
113f36cc634SAndreas Gohr        return '"' . str_replace('"', '""', $str) . '"';
114f36cc634SAndreas Gohr    }
115f36cc634SAndreas Gohr}
116