xref: /plugin/struct/action/migration.php (revision 61e808e6602cb7540f481a83befe41f3a08f17cb)
1<?php
2
3/**
4 * DokuWiki Plugin struct (Action Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8 */
9
10/**
11 * Class action_plugin_struct_migration
12 *
13 * Handle migrations that need more than just SQL
14 */
15class action_plugin_struct_migration extends DokuWiki_Action_Plugin
16{
17    /**
18     * @inheritDoc
19     */
20    public function register(Doku_Event_Handler $controller)
21    {
22        $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'BEFORE', $this, 'handleMigrations');
23    }
24
25    /**
26     * Call our custom migrations when defined
27     *
28     * @param Doku_Event $event
29     * @param $param
30     */
31    public function handleMigrations(Doku_Event $event, $param)
32    {
33        if ($event->data['sqlite']->getAdapter()->getDbname() !== 'struct') {
34            return;
35        }
36        $to = $event->data['to'];
37
38        if (is_callable(array($this, "migration$to"))) {
39            $event->preventDefault();
40            $event->result = call_user_func(array($this, "migration$to"), $event->data['sqlite']);
41        }
42    }
43
44    /**
45     * Executes Migration 12
46     *
47     * Add a latest column to all existing multi tables
48     *
49     * @param helper_plugin_sqlite $sqlite
50     * @return bool
51     */
52    protected function migration12(helper_plugin_sqlite $sqlite)
53    {
54        /** @noinspection SqlResolve */
55        $sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'multi_%'";
56        $res = $sqlite->query($sql);
57        $tables = $sqlite->res2arr($res);
58        $sqlite->res_close($res);
59
60        foreach ($tables as $row) {
61            $sql = 'ALTER TABLE ? ADD COLUMN latest INT DEFAULT 1';
62            $sqlite->query($sql, $row['name']);
63        }
64
65        return true;
66    }
67
68    /**
69     * Executes Migration 16
70     *
71     * Unifies previous page and lookup schema types
72     *
73     * @param helper_plugin_sqlite $sqlite
74     * @return bool
75     */
76    protected function migration16(helper_plugin_sqlite $sqlite)
77    {
78        // get tables and their SQL definitions
79        $sql = "SELECT sql, name FROM sqlite_master
80                WHERE type = 'table'
81                AND (name LIKE 'data_%' OR name LIKE 'multi_%')";
82        $res = $sqlite->query($sql);
83        $tables = $sqlite->res2arr($res);
84        $sqlite->res_close($res);
85
86        // get latest versions of schemas with islookup property
87        $sql = "SELECT MAX(id) AS id, tbl, islookup FROM schemas
88                    GROUP BY tbl
89            ";
90        $res = $sqlite->query($sql);
91        $schemas = $sqlite->res2arr($res);
92
93        $sqlite->query('BEGIN TRANSACTION');
94        $ok = true;
95
96        // Step 1: move original data to temporary tables and create new ones with modified schemas
97        foreach ($tables as $table) {
98            $name = $table['name'];
99            $sql = $table['sql'];
100
101            // move original data to temp_*
102            $ok = $ok && $sqlite->query("ALTER TABLE $name RENAME TO temp_$name");
103
104            // update pid definitions
105            $sql = preg_replace('/pid (\w* ?NOT NULL|\w* ?PRIMARY KEY)/', 'pid TEXT DEFAULT ""', $sql);
106
107            // add rid and new primary key to regular tables
108            $cnt = 0;
109            $sql = preg_replace('/(PRIMARY KEY ?\([^\)]+?)(\))/', ' rid INTEGER, $1, rid $2', $sql, -1, $cnt);
110            // add rid and new primary key to lookup tables
111            if (!$cnt) {
112                $sql = str_replace(')', ', rid INTEGER, PRIMARY KEY(pid,rid) )', $sql);
113            }
114
115            // create the new table
116            $ok = $ok && $sqlite->query($sql);
117            if (!$ok) return false;
118        }
119
120        // Step 2: transfer data back from original tables (temp_*)
121        foreach ($schemas as $schema) {
122            $name = $schema['tbl'];
123            $sid = $schema['id'];
124            $isLookup = $schema['islookup'];
125
126            if (!$isLookup) {
127                $s = sprintf('INSERT INTO data_%s SELECT *, 0 FROM temp_data_%s', $name, $name);
128                $ok = $ok && $sqlite->query($s);
129                if (!$ok) return false;
130
131                $s = sprintf('INSERT INTO multi_%s SELECT *, 0 FROM temp_multi_%s', $name, $name);
132                $ok = $ok && $sqlite->query($s);
133                if (!$ok) return false;
134            } else {
135                // transfer pid to rid
136                $s = sprintf('INSERT INTO data_%s SELECT *, pid FROM temp_data_%s', $name, $name);
137                $ok = $ok && $sqlite->query($s);
138                if (!$ok) return false;
139
140                $s = sprintf('INSERT INTO multi_%s SELECT *, pid FROM temp_multi_%s', $name, $name);
141                $ok = $ok && $sqlite->query($s);
142                if (!$ok) return false;
143
144                // all lookup data has empty pids at this point
145                $s = "UPDATE data_$name SET pid = ''";
146                $ok = $ok && $sqlite->query($s);
147                if (!$ok) return false;
148
149                $s = "UPDATE multi_$name SET pid = ''";
150                $ok = $ok && $sqlite->query($s);
151                if (!$ok) return false;
152            }
153
154            // introduce composite ids in lookup columns
155            $s = $this->getLookupColsSql($sid);
156            $res = $sqlite->query($s);
157            $cols = $sqlite->res2arr($res);
158
159            if ($cols) {
160                foreach ($cols as $col) {
161                    $colno = $col['COL'];
162                    $colname = "col$colno";
163                    // lookup fields pointing to pages have to be migrated first!
164                    // they rely on a simplistic not-a-number check, and already migrated lookups pass the test!
165                    $f = 'UPDATE data_%s SET %s = \'["\'||%s||\'",0]\' WHERE %s != \'\' AND CAST(%s AS DECIMAL) != %s';
166                    $s = sprintf($f, $name, $colname, $colname, $colname, $colname, $colname);
167                    $ok = $ok && $sqlite->query($s);
168                    if (!$ok) return false;
169                    // multi_
170                    $f = 'UPDATE multi_%s SET value = \'["value",0]\' WHERE colref = %s AND CAST(value AS DECIMAL) != value';
171                    $s = sprintf($f, $name, $colno);
172                    $ok = $ok && $sqlite->query($s);
173                    if (!$ok) return false;
174
175                    // simple lookup fields
176                    $s = "UPDATE data_$name SET col$colno = '[" . '""' . ",'||col$colno||']' WHERE col$colno != '' AND CAST(col$colno AS DECIMAL) = col$colno";
177                    $ok = $ok && $sqlite->query($s);
178                    if (!$ok) return false;
179                    // multi_
180                    $s = "UPDATE multi_$name SET value = '[" . '""' . ",'||value||']' WHERE colref=$colno AND CAST(value AS DECIMAL) = value";
181                    $ok = $ok && $sqlite->query($s);
182                    if (!$ok) return false;
183                }
184            }
185        }
186
187        // Step 3: delete temp_* tables
188        foreach ($tables as $table) {
189            $name = $table['name'];
190            $s = "DROP TABLE temp_$name";
191            $ok = $ok && $sqlite->query($s);
192            if (!$ok) return false;
193        }
194
195        // Step 4: remove islookup in schemas table
196        $sql = "SELECT sql FROM sqlite_master
197                WHERE type = 'table'
198                AND name = 'schemas'";
199        $res = $sqlite->query($sql);
200        $t = $sqlite->res2arr($res);
201        $sql = $t[0]['sql'];
202        $sql = str_replace('islookup INTEGER,', '', $sql);
203
204        $s = 'ALTER TABLE schemas RENAME TO temp_schemas';
205        $ok = $ok && $sqlite->query($s);
206        if (!$ok) return false;
207
208        // create a new table without islookup
209        $ok = $ok && $sqlite->query($sql);
210        if (!$ok) return false;
211
212        $s = 'INSERT INTO schemas SELECT id, tbl, ts, user, comment, config FROM temp_schemas';
213        $ok = $ok && $sqlite->query($s);
214
215        if (!$ok) {
216            $sqlite->query('ROLLBACK TRANSACTION');
217            return false;
218        }
219        $sqlite->query('COMMIT TRANSACTION');
220        return true;
221    }
222
223    /**
224     * Executes Migration 17
225     *
226     * Fixes lookup data not correctly migrated by #16
227     * All lookups were presumed to reference lookup data, not pages, so the migrated value
228     * was always ["", <previous-pid-aka-new-rid>]. For page references it is ["<previous-pid>", 0]
229     *
230     * @param helper_plugin_sqlite $sqlite
231     * @return bool
232     */
233    protected function migration17(helper_plugin_sqlite $sqlite)
234    {
235        $sql = "SELECT MAX(id) AS id, tbl FROM schemas
236                    GROUP BY tbl
237            ";
238        $res = $sqlite->query($sql);
239        $schemas = $sqlite->res2arr($res);
240
241        $sqlite->query('BEGIN TRANSACTION');
242        $ok = true;
243
244        foreach ($schemas as $schema) {
245            // find lookup columns
246            $name = $schema['tbl'];
247            $sid = $schema['id'];
248            $s = $this->getLookupColsSql($sid);
249            $res = $sqlite->query($s);
250            $cols = $sqlite->res2arr($res);
251
252            if ($cols) {
253                $colnames = array_map(function ($c) {
254                    return 'col' . $c['COL'];
255                }, $cols);
256
257                // data_ tables
258                $s = 'SELECT pid, rid, rev, ' . implode(', ', $colnames) . " FROM data_$name";
259                $res = $sqlite->query($s);
260                $allValues = $sqlite->res2arr($res);
261
262                if (!empty($allValues)) {
263                    foreach ($allValues as $row) {
264                        list($pid, $rid, $rev, $colref, $rowno, $fixes) = $this->getFixedValues($row);
265                        // now fix the values
266                        if (!empty($fixes)) {
267                            $sql = "UPDATE data_$name SET " . implode(', ', $fixes) . " WHERE pid = ? and rid = ? AND rev = ?";
268                            $params = [$pid, $rid, $rev];
269                            $ok = $ok && $sqlite->query($sql, $params);
270                        }
271                    }
272                }
273
274                // multi_ tables
275                $s = "SELECT colref, pid, rid, rev, row, value FROM multi_$name";
276                $res = $sqlite->query($s);
277                $allValues = $sqlite->res2arr($res);
278
279                if (!empty($allValues)) {
280                    foreach ($allValues as $row) {
281                        list($pid, $rid, $rev, $colref, $rowno, $fixes) = $this->getFixedValues($row);
282                        // now fix the values
283                        if (!empty($fixes)) {
284                            $sql = "UPDATE multi_$name SET " . implode(', ', $fixes) . " WHERE pid = ? AND rid = ? AND rev = ? AND colref = ? AND row = ?";
285                            $params = [$pid, $rid, $rev, $colref, $rowno];
286                            $ok = $ok && $sqlite->query($sql, $params);
287                        }
288                    }
289                }
290            }
291        }
292
293        if (!$ok) {
294            $sqlite->query('ROLLBACK TRANSACTION');
295            return false;
296        }
297        $sqlite->query('COMMIT TRANSACTION');
298        return true;
299    }
300
301    /**
302     * Returns a select statement to fetch Lookup columns in the current schema
303     *
304     * @param int $sid Id of the schema
305     * @return string SQL statement
306     */
307    protected function getLookupColsSql($sid)
308    {
309        return "SELECT C.colref AS COL, T.class AS TYPE
310                FROM schema_cols AS C
311                LEFT OUTER JOIN types AS T
312                    ON C.tid = T.id
313                WHERE C.sid = $sid
314                AND TYPE LIKE '%Lookup'
315            ";
316    }
317
318    /**
319     * Checks for improperly migrated values and returns an array with
320     * "<column> = <fixed-value>" fragments to be used in the UPDATE statement.
321     *
322     * @param array $row
323     * @return array
324     */
325    protected function getFixedValues($row)
326    {
327        $pid = $row['pid'];
328        $rid = $row['rid'];
329        $rev = $row['rev'];
330        $colref = $row['colref'];
331        $rowno = $row['row'];
332        $fixes = [];
333        $matches = [];
334
335        // check if anything needs to be fixed in data columns
336        foreach ($row as $col => $value) {
337            if (in_array($col, ['pid', 'rid', 'rev', 'colref', 'row'])) {
338                continue;
339            }
340            preg_match('/^\["",(?<pid>.*?\D+.*?)\]$/', $value, $matches);
341            if (!empty($matches['pid'])) {
342                $fixes[$col] = '["' . $matches['pid'] . '",0]';
343            }
344        }
345
346        if (!empty($fixes)) {
347            $fixes = array_map(function ($set, $key) {
348                return "$key = '$set'";
349            }, $fixes, array_keys($fixes));
350        }
351
352        return [$pid, $rid, $rev, $colref, $rowno, $fixes];
353    }
354}
355