xref: /plugin/struct/action/migration.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
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// must be run within Dokuwiki
11
12if (!defined('DOKU_INC')) die();
13
14/**
15 * Class action_plugin_struct_migration
16 *
17 * Handle migrations that need more than just SQL
18 */
19class action_plugin_struct_migration extends DokuWiki_Action_Plugin
20{
21    /**
22     * @inheritDoc
23     */
24    public function register(Doku_Event_Handler $controller)
25    {
26        $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'BEFORE', $this, 'handle_migrations');
27    }
28
29    /**
30     * Call our custom migrations when defined
31     *
32     * @param Doku_Event $event
33     * @param $param
34     */
35    public function handle_migrations(Doku_Event $event, $param)
36    {
37        if ($event->data['sqlite']->getAdapter()->getDbname() !== 'struct') {
38            return;
39        }
40        $to = $event->data['to'];
41
42        if (is_callable(array($this, "migration$to"))) {
43            $event->preventDefault();
44            $event->result = call_user_func(array($this, "migration$to"), $event->data['sqlite']);
45        }
46    }
47
48    /**
49     * Executes Migration 12
50     *
51     * Add a latest column to all existing multi tables
52     *
53     * @param helper_plugin_sqlite $sqlite
54     * @return bool
55     */
56    protected function migration12(helper_plugin_sqlite $sqlite)
57    {
58        /** @noinspection SqlResolve */
59        $sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'multi_%'";
60        $res = $sqlite->query($sql);
61        $tables = $sqlite->res2arr($res);
62        $sqlite->res_close($res);
63
64        foreach ($tables as $row) {
65            $sql = 'ALTER TABLE ? ADD COLUMN latest INT DEFAULT 1';
66            $sqlite->query($sql, $row['name']);
67        }
68
69        return true;
70    }
71}
72