xref: /plugin/struct/meta/CSVPageImporter.php (revision 1fc2361fe236472e7585162e283a99db231aaf7e)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4use dokuwiki\plugin\struct\types\Page;
5
6class CSVPageImporter extends CSVImporter {
7    /**
8     * Chceck if schema is page schema
9     *
10     * @throws StructException
11     * @param string $table
12     * @param string $file
13     */
14    public function __construct($table, $file) {
15        parent::__construct($table, $file);
16
17        if($this->schema->isLookup()) throw new StructException($table.' is not a page schema');
18    }
19
20    /**
21     * Import page schema only when the pid header is present.
22     */
23    protected function readHeaders() {
24
25        //add pid to struct
26        $pageType = new Page(null, 'pid');
27        $this->columns[] = new Column(0, $pageType);
28
29        parent::readHeaders();
30
31        if(!in_array('pid', $this->header)) throw new StructException('There is no "pid" header in the CSV. Schema not imported.');
32    }
33
34    /**
35     * Creates the insert string for the single value table
36     *
37     * @return string
38     */
39    protected function getSQLforAllValues() {
40        $colnames = array();
41        foreach($this->columns as $i => $col) {
42            $colnames[] = 'col' . $col->getColref();
43        }
44        //replace first column with pid
45        $colnames[0] = 'pid';
46        //insert rev at the end
47        $colnames[] = 'rev';
48
49        $placeholds = join(', ', array_fill(0, count($colnames), '?'));
50        $colnames = join(', ', $colnames);
51        $table = $this->schema->getTable();
52
53        //replace previous data
54        return "REPLACE INTO data_$table ($colnames, latest) VALUES ($placeholds, 1)";
55    }
56
57    /**
58     * Add the revision.
59     *
60     * @param string[] $values
61     * @param          $line
62     * @param string   $single
63     * @param string   $multi
64     */
65    protected function saveLine($values, $line, $single, $multi) {
66        //read the lastest revision of inserted page
67        $values[] = @filemtime(wikiFN($values[0]));
68        parent::saveLine($values, $line, $single, $multi);
69    }
70
71    /**
72     * In the paga schemas primary key is a touple of (pid, rev)
73     *
74     * @param string[] $values
75     * @param string   $single
76     * @return array(pid, rev)
77     */
78    protected function insertIntoSingle($values, $single) {
79        parent::insertIntoSingle($values, $single);
80        $pid = $values[0];
81        $rev = $values[count($values) - 1];
82        //primary key is touple of (pid, rev)
83        return array($pid, $rev);
84    }
85
86    /**
87     * Add pid and rev to insert query parameters
88     *
89     * @param string $multi
90     * @param string $pk
91     * @param string $column
92     * @param string $row
93     * @param string $value
94     */
95    protected function insertIntoMulti($multi, $pk, $column, $row, $value) {
96        list($pid, $rev) = $pk;
97        $this->sqlite->query($multi, array($pid, $rev, $column->getColref(), $row + 1, $value));
98    }
99
100    /**
101     * In page schemas we use REPLACE instead of INSERT to prevent ambiguity
102     *
103     * @return string
104     */
105    protected function getSQLforMultiValue() {
106        $table = $this->schema->getTable();
107        /** @noinspection SqlResolve */
108        return "REPLACE INTO multi_$table (pid, rev, colref, row, value, latest) VALUES (?,?,?,?,?,1)";
109    }
110
111    /**
112     * Check if page id realy exists
113     *
114     * @param Column $col
115     * @param mixed  $rawvalue
116     * @return bool
117     */
118    protected function validateValue(Column $col, &$rawvalue) {
119        //check if page id exists
120        if($col->getLabel() == 'pid') {
121            $rawvalue = cleanID($rawvalue);
122            if(page_exists($rawvalue)) {
123                return true;
124            }
125            $this->errors[] = 'Page "'.$rawvalue.'" does not exists. Skipping the row.';
126            return false;
127        }
128
129        return parent::validateValue($col, $rawvalue);
130    }
131}
132