xref: /plugin/struct/action/migration.php (revision 48801f6d7d7ad3c56a635066943b271f49b9264e)
1<?php
2/**
3 * DokuWiki Plugin struct (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10
11if(!defined('DOKU_INC')) die();
12
13/**
14 * Class action_plugin_struct_migration
15 *
16 * Handle migrations that need more than just SQL
17 */
18class action_plugin_struct_migration extends DokuWiki_Action_Plugin {
19    /**
20     * @inheritDoc
21     */
22    public function register(Doku_Event_Handler $controller) {
23        $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'BEFORE', $this, 'handle_migrations');
24    }
25
26    /**
27     * Call our custom migrations when defined
28     *
29     * @param Doku_Event $event
30     * @param $param
31     */
32    public function handle_migrations(Doku_Event $event, $param) {
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        /** @noinspection SqlResolve */
54        $sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'multi_%'";
55        $res = $sqlite->query($sql);
56        $tables = $sqlite->res2arr($res);
57        $sqlite->res_close($res);
58
59        foreach($tables as $row) {
60            $sql = 'ALTER TABLE ? ADD COLUMN latest INT DEFAULT 1';
61            $sqlite->query($sql, $row['name']);
62        }
63
64        return true;
65    }
66
67}
68