xref: /plugin/struct/meta/SchemaBuilder.php (revision 7234bfb14e712ff548d9266ef32fdcc8eaf2d04e)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5use dokuwiki\plugin\sqlite\SQLiteDB;
6use dokuwiki\Utf8\PhpString;
7
8/**
9 * Class SchemaBuilder
10 *
11 * This class builds and updates the schema definitions for our tables. This includes CREATEing and ALTERing
12 * the actual data tables as well as updating the meta information in our meta data tables.
13 *
14 * To use, simply instantiate a new object of the Builder and run the build() method on it.
15 *
16 * Note: even though data tables use a data_ prefix in the database, this prefix is internal only and should
17 *       never be passed as $table anywhere!
18 *
19 * @package dokuwiki\plugin\struct\meta
20 */
21class SchemaBuilder
22{
23    /**
24     * @var array The posted new data for the schema
25     * @see Schema::AdminEditor()
26     */
27    protected $data = [];
28
29    protected $user;
30
31    /**
32     * @var string The table name associated with the schema
33     */
34    protected $table = '';
35
36    /**
37     * @var Schema the previously valid schema for this table
38     */
39    protected $oldschema;
40
41    /** @var int the ID of the newly created schema */
42    protected $newschemaid = 0;
43
44    /** @var \helper_plugin_struct_db */
45    protected $helper;
46
47    /** @var SQLiteDB|null */
48    protected $sqlite;
49
50    /** @var int the time for which this schema should be created - default to time() can be overriden for tests */
51    protected $time = 0;
52
53    /**
54     * SchemaBuilder constructor.
55     *
56     * @param string $table The table's name
57     * @param array $data The defining of the table (basically what get's posted in the schema editor form)
58     * @see Schema::AdminEditor()
59     */
60    public function __construct($table, $data)
61    {
62        global $INPUT;
63
64        $this->table = $table;
65        $this->data = $data;
66        $this->oldschema = new Schema($table, 0);
67
68        $this->helper = plugin_load('helper', 'struct_db');
69        $this->sqlite = $this->helper->getDB();
70        $this->user = $_SERVER['REMOTE_USER'] ?? '';
71    }
72
73    /**
74     * Create the new schema
75     *
76     * @param int $time when to create this schema 0 for now
77     * @return int the new schema id on success
78     */
79    public function build($time = 0)
80    {
81        $this->time = $time;
82        $this->fixLabelUniqueness();
83
84        $this->sqlite->query('BEGIN TRANSACTION');
85        $ok = true;
86        // create the data table if new schema
87        if (!$this->oldschema->getId()) {
88            $ok = $this->newDataTable();
89        }
90
91        // create a new schema
92        $ok = $ok && $this->newSchema();
93
94        // update column info
95        $ok = $ok && $this->updateColumns();
96        $ok = $ok && $this->addColumns();
97
98        if (!$ok) {
99            $this->sqlite->query('ROLLBACK TRANSACTION');
100            return false;
101        }
102        $this->sqlite->query('COMMIT TRANSACTION');
103
104        return (int)$this->newschemaid;
105    }
106
107    /**
108     * Makes sure all labels in the schema to save are unique
109     */
110    protected function fixLabelUniqueness()
111    {
112        $labels = [];
113
114        if (isset($this->data['cols'])) foreach ($this->data['cols'] as $idx => $column) {
115            $this->data['cols'][$idx]['label'] = $this->fixLabel($column['label'], $labels);
116        }
117
118        if (isset($this->data['new'])) foreach ($this->data['new'] as $idx => $column) {
119            $this->data['new'][$idx]['label'] = $this->fixLabel($column['label'], $labels);
120        }
121    }
122
123    /**
124     * Creates a unique label from the given one
125     *
126     * @param string $wantedlabel
127     * @param array $labels list of already assigned labels (will be filled)
128     * @return string
129     */
130    protected function fixLabel($wantedlabel, &$labels)
131    {
132        $wantedlabel = trim($wantedlabel);
133        $fixedlabel = $wantedlabel;
134        $idx = 1;
135        while (isset($labels[PhpString::strtolower($fixedlabel)])) {
136            $fixedlabel = $wantedlabel . $idx++;
137        }
138        // did we actually do a rename? apply it.
139        if ($fixedlabel !== $wantedlabel) {
140            msg(sprintf($this->helper->getLang('duplicate_label'), $wantedlabel, $fixedlabel), -1);
141            $this->data['cols']['label'] = $fixedlabel;
142        }
143        $labels[PhpString::strtolower($fixedlabel)] = 1;
144        return $fixedlabel;
145    }
146
147    /**
148     * Creates a new schema
149     */
150    protected function newSchema()
151    {
152        if (!$this->time) $this->time = time();
153
154        $config = $this->data['config'] ?? '{}';
155
156        /** @noinspection SqlResolve */
157        $sql = "INSERT INTO schemas (tbl, ts, user, config) VALUES (?, ?, ?, ?)";
158        $this->sqlite->query($sql, [$this->table, $this->time, $this->user, $config]);
159        $this->newschemaid = $this->sqlite->queryValue('SELECT last_insert_rowid()');
160
161        if (!$this->newschemaid) return false;
162        return true;
163    }
164
165    /**
166     * Updates all the existing column infos and adds them to the new schema
167     */
168    protected function updateColumns()
169    {
170        foreach ($this->oldschema->getColumns() as $column) {
171            $oldEntry = $column->getType()->getAsEntry();
172            $oldTid = $column->getTid();
173            $newEntry = $oldEntry;
174            $newTid = $oldTid;
175            $sort = $column->getSort();
176            if (isset($this->data['cols'][$column->getColref()])) {
177                // todo I'm not too happy with this hardcoded here -
178                // we should probably have a list of fields at one place
179                $newEntry['config'] = $this->data['cols'][$column->getColref()]['config'];
180                $newEntry['label'] = $this->data['cols'][$column->getColref()]['label'];
181                $newEntry['ismulti'] = $this->data['cols'][$column->getColref()]['ismulti'] ?? 0;
182                $newEntry['class'] = $this->data['cols'][$column->getColref()]['class'];
183                $sort = $this->data['cols'][$column->getColref()]['sort'];
184                $enabled = (bool)($this->data['cols'][$column->getColref()]['isenabled'] ?? 0);
185
186                // when the type definition has changed, we create a new one
187                if (array_diff_assoc($oldEntry, $newEntry)) {
188                    $ok = $this->sqlite->saveRecord('types', $newEntry);
189                    if (!$ok) return false;
190                    $newTid = $this->sqlite->queryValue('SELECT last_insert_rowid()');
191                    if (!$newTid) return false;
192                    if ($oldEntry['ismulti'] == false && $newEntry['ismulti'] == '1') {
193                        $this->migrateSingleToMulti($this->oldschema->getTable(), $column->getColref());
194                    }
195                }
196            } else {
197                $enabled = false; // no longer there for some reason
198            }
199
200            // add this type to the schema columns
201            $schemaEntry = ['sid' => $this->newschemaid, 'colref' => $column->getColref(), 'enabled' => $enabled, 'tid' => $newTid, 'sort' => $sort];
202            $ok = $this->sqlite->saveRecord('schema_cols', $schemaEntry);
203            if (!$ok) return false;
204        }
205        return true;
206    }
207
208    /**
209     * Write the latest value from an entry in a data_ table to the corresponding multi_table
210     *
211     * @param string $table
212     * @param int $colref
213     */
214    protected function migrateSingleToMulti($table, $colref)
215    {
216        /** @noinspection SqlResolve */
217        $sqlSelect = "SELECT pid, rev, published, col$colref AS value FROM data_$table WHERE latest = 1";
218        $valueSet = $this->sqlite->queryAll($sqlSelect);
219        $valueString = [];
220        $arguments = [];
221        foreach ($valueSet as $values) {
222            if (blank($values['value']) || trim($values['value']) == '') {
223                continue;
224            }
225            $valueString[] = "(?, ?, ?, ?, ?, ?)";
226            $arguments = array_merge(
227                $arguments,
228                [$colref, $values['pid'], $values['rev'], $values['published'], 1, $values['value']]
229            );
230        }
231        if ($valueString === []) {
232            return;
233        }
234        $valueString = implode(',', $valueString);
235        /** @noinspection SqlResolve */
236        $sqlInsert = "INSERT OR REPLACE INTO multi_$table (colref, pid, rev, published, row, value) VALUES $valueString"; // phpcs:ignore
237        $this->sqlite->query($sqlInsert, $arguments);
238    }
239
240    /**
241     * Adds new columns to the new schema
242     *
243     * @return bool
244     */
245    protected function addColumns()
246    {
247        if (!isset($this->data['new'])) return true;
248
249        $colref = count($this->oldschema->getColumns()) + 1;
250
251        foreach ($this->data['new'] as $column) {
252            if (!$column['isenabled']) continue; // we do not add a disabled column
253
254            // todo this duplicates the hardcoding as in  the function above
255            $newEntry = [];
256            $newEntry['config'] = $column['config'] ?? '{}';
257            $newEntry['label'] = $column['label'];
258            $newEntry['ismulti'] = $column['ismulti'] ?? 0;
259            $newEntry['class'] = $column['class'];
260            $sort = $column['sort'];
261
262
263            // only save if the column got a name
264            if (!$newEntry['label']) continue;
265
266            // add new column to the data table
267            if (!$this->addDataTableColumn($colref)) {
268                return false;
269            }
270
271            // save the type
272            $ok = $this->sqlite->saveRecord('types', $newEntry);
273            if (!$ok) return false;
274            $newTid = $this->sqlite->queryValue('SELECT last_insert_rowid()');
275
276            if (!$newTid) return false;
277
278
279            // add this type to the schema columns
280            $schemaEntry = ['sid' => $this->newschemaid, 'colref' => $colref, 'enabled' => true, 'tid' => $newTid, 'sort' => $sort];
281            $ok = $this->sqlite->saveRecord('schema_cols', $schemaEntry);
282            if (!$ok) return false;
283            $colref++;
284        }
285
286        return true;
287    }
288
289    /**
290     * Create a completely new data table with no columns yet also create the appropriate
291     * multi value table for the schema
292     *
293     * @return bool
294     * @todo how do we want to handle indexes?
295     */
296    protected function newDataTable()
297    {
298        $ok = true;
299
300        $tbl = 'data_' . $this->table;
301        $sql = "CREATE TABLE $tbl (
302                    pid TEXT DEFAULT '',
303                    rid INTEGER,
304                    rev INTEGER,
305                    latest BOOLEAN NOT NULL DEFAULT 0,
306                    published BOOLEAN DEFAULT NULL,
307                    PRIMARY KEY(pid, rid, rev)
308                )";
309        $ok = $ok && (bool)$this->sqlite->query($sql);
310
311        $tbl = 'multi_' . $this->table;
312        $sql = "CREATE TABLE $tbl (
313                    colref INTEGER NOT NULL,
314                    pid TEXT DEFAULT '',
315                    rid INTEGER,
316                    rev INTEGER,
317                    latest INTEGER NOT NULL DEFAULT 0,
318                    published BOOLEAN DEFAULT NULL,
319                    row INTEGER NOT NULL,
320                    value,
321                    PRIMARY KEY(colref, pid, rid, rev, row)
322                );";
323        $ok = $ok && (bool)$this->sqlite->query($sql);
324
325        return $ok;
326    }
327
328    /**
329     * Add an additional column to the existing data table
330     *
331     * @param int $index the new column index to add
332     * @return bool
333     */
334    protected function addDataTableColumn($index)
335    {
336        $tbl = 'data_' . $this->table;
337        $sql = " ALTER TABLE $tbl ADD COLUMN col$index DEFAULT ''";
338        if (!$this->sqlite->query($sql)) {
339            return false;
340        }
341        return true;
342    }
343
344    /**
345     * @param string $user
346     * @return SchemaBuilder
347     */
348    public function setUser($user)
349    {
350        $this->user = $user;
351        return $this;
352    }
353}
354