xref: /plugin/struct/meta/CSVPageImporter.php (revision efff5841a67c7104be410aa5325f04223270a128)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4use dokuwiki\plugin\struct\types\Page;
5
6class CSVPageImporter extends CSVImporter {
7
8    protected $importedPids = array();
9
10    /** @var bool[]  */
11    protected $createPage = [];
12
13    /**
14     * Chceck if schema is page schema
15     *
16     * @throws StructException
17     * @param string $table
18     * @param string $file
19     */
20    public function __construct($table, $file) {
21        parent::__construct($table, $file);
22
23        if($this->schema->isLookup()) throw new StructException($table.' is not a page schema');
24    }
25
26    /**
27     * Import page schema only when the pid header is present.
28     */
29    protected function readHeaders() {
30
31        //add pid to struct
32        $pageType = new Page(null, 'pid');
33        $this->columns[] = new Column(0, $pageType, 0, true, $this->schema->getTable());
34
35        parent::readHeaders();
36
37        if(!in_array('pid', $this->header)) throw new StructException('There is no "pid" header in the CSV. Schema not imported.');
38    }
39
40    /**
41     * Creates the insert string for the single value table
42     *
43     * @return string
44     */
45    protected function getSQLforAllValues() {
46        $colnames = array();
47        foreach($this->columns as $i => $col) {
48            $colnames[] = 'col' . $col->getColref();
49        }
50        //replace first column with pid
51        $colnames[0] = 'pid';
52        //insert rev at the end
53        $colnames[] = 'rev';
54
55        $placeholds = join(', ', array_fill(0, count($colnames), '?'));
56        $colnames = join(', ', $colnames);
57        $table = $this->schema->getTable();
58
59        return "INSERT INTO data_$table ($colnames, latest) VALUES ($placeholds, 1)";
60    }
61
62    /**
63     * Add the revision.
64     *
65     * @param string[] $values
66     * @param          $line
67     * @param string   $single
68     * @param string   $multi
69     */
70    protected function saveLine($values, $line, $single, $multi) {
71        //create new page revision
72        $pid = cleanID($values[0]);
73        if ($this->createPage[$pid]) {
74            $this->createPage($pid, $line);
75        }
76        $helper = plugin_load('helper', 'struct');
77        $revision = $helper->createPageRevision($pid, 'CSV data imported');
78        p_get_metadata($pid); // reparse the metadata of the page top update the titles/rev/lasteditor table
79
80        // make sure this schema is assigned
81        /** @noinspection PhpUndefinedVariableInspection */
82        Assignments::getInstance()->assignPageSchema(
83            $pid,
84            $this->schema->getTable()
85        );
86
87        //add page revision to values
88        $values[] = $revision;
89
90        parent::saveLine($values, $line, $single, $multi);
91    }
92
93    /**
94     * Create a page from a namespace template and replace column-label-placeholders
95     *
96     * This is intended to use the same placeholders as bureaucracy in their most basic version
97     * (i.e. without default values, formatting, etc. )
98     *
99     * @param string $pid
100     * @param array  $line
101     */
102    protected function createPage($pid, $line)
103    {
104        $text = pageTemplate($pid);
105        if (trim($text) === '') {
106            $pageParts = explode(':', $pid);
107            $pagename = end($pageParts);
108            $text = "====== $pagename ======\n";
109        }
110        $keys = array_reduce($this->columns,
111            function ($keys, Column $col) {
112                if (!in_array($col->getLabel(), $keys, true)) {
113                    return $keys;
114                }
115                $index = array_search($col->getLabel(), $keys, true);
116                $keys[$index] = $col->getFullQualifiedLabel();
117                return $keys;
118            },
119            $this->header
120        );
121
122        $keysAt = array_map(function ($key) { return "@@$key@@";}, $keys);
123        $keysHash = array_map(function ($key) { return "##$key##";}, $keys);
124        $flatValues = array_map(
125            function($value) {
126                if (is_array($value)) {
127                    return implode(', ', $value);
128                }
129                return $value;
130            }, $line);
131        $text = str_replace($keysAt, $flatValues, $text);
132        /** @noinspection CascadeStringReplacementInspection */
133        $text = str_replace($keysHash, $flatValues, $text);
134        saveWikiText($pid, $text, 'Created by struct csv import');
135    }
136
137    /**
138     * In the paga schemas primary key is a touple of (pid, rev)
139     *
140     * @param string[] $values
141     * @param string   $single
142     * @return array(pid, rev)
143     */
144    protected function insertIntoSingle($values, $single) {
145        $pid = $values[0];
146        $rev = $values[count($values) - 1];
147
148        //update latest
149        $table = $this->schema->getTable();
150        $this->sqlite->query("UPDATE data_$table SET latest = 0 WHERE latest = 1 AND pid = ?", array($pid));
151
152        //insert into table
153        parent::insertIntoSingle($values, $single);
154
155        //primary key is touple of (pid, rev)
156        return array($pid, $rev);
157    }
158
159    /**
160     * Add pid and rev to insert query parameters
161     *
162     * @param string $multi
163     * @param string $pk
164     * @param string $column
165     * @param string $row
166     * @param string $value
167     */
168    protected function insertIntoMulti($multi, $pk, $column, $row, $value) {
169        list($pid, $rev) = $pk;
170
171        //update latest
172        $table = $this->schema->getTable();
173        $this->sqlite->query("UPDATE multi_$table SET latest = 0 WHERE latest = 1 AND pid = ?", array($pid));
174
175        $this->sqlite->query($multi, array($pid, $rev, $column->getColref(), $row + 1, $value));
176    }
177
178    /**
179     * In page schemas we use REPLACE instead of INSERT to prevent ambiguity
180     *
181     * @return string
182     */
183    protected function getSQLforMultiValue() {
184        $table = $this->schema->getTable();
185        /** @noinspection SqlResolve */
186        return "INSERT INTO multi_$table (pid, rev, colref, row, value, latest) VALUES (?,?,?,?,?,1)";
187    }
188
189    /**
190     * Check if page id realy exists
191     *
192     * @param Column $col
193     * @param mixed  $rawvalue
194     * @return bool
195     */
196    protected function validateValue(Column $col, &$rawvalue) {
197        //check if page id exists and schema is bounded to the page
198        if($col->getLabel() == 'pid') {
199            $pid = cleanID($rawvalue);
200            if (isset($this->importedPids[$pid])) {
201                $this->errors[] = 'Page "'.$pid.'" already imported. Skipping the row.';
202                return false;
203            }
204            if(page_exists($pid)) {
205                $this->importedPids[$pid] = true;
206                return true;
207            }
208            global $INPUT;
209            if ($INPUT->bool('createPage')) {
210                $this->createPage[$pid] = true;
211                return true;
212            }
213            $this->errors[] = 'Page "'.$pid.'" does not exists. Skipping the row.';
214            return false;
215        }
216
217        return parent::validateValue($col, $rawvalue);
218    }
219}
220