xref: /plugin/struct/action/migration.php (revision 2e12ac22cf806595364023b831794693655a9912)
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        $to = $event->data['to'];
34
35        if(is_callable(array($this, "migration$to"))) {
36            $event->preventDefault();
37            $event->result = call_user_func(array($this, "migration$to"), $event->data['sqlite']);
38        }
39    }
40
41    /**
42     * Executes Migration 12
43     *
44     * Add a latest column to all existing multi tables
45     *
46     * @param helper_plugin_sqlite $sqlite
47     * @return bool
48     */
49    protected function migration12(helper_plugin_sqlite $sqlite) {
50        /** @noinspection SqlResolve */
51        $sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'multi_%'";
52        $res = $sqlite->query($sql);
53        $tables = $sqlite->res2arr($res);
54        $sqlite->res_close($res);
55
56        foreach($tables as $row) {
57            $sql = 'ALTER TABLE ? ADD COLUMN latest INT DEFAULT 1';
58            $sqlite->query($sql, $row['name']);
59        }
60
61        return true;
62    }
63
64}
65