xref: /plugin/struct/action/migration.php (revision 8f2594674dc864a5f8a2455d5836ec024b2a45f2)
1*8f259467SAndreas Gohr<?php
2*8f259467SAndreas Gohr/**
3*8f259467SAndreas Gohr * DokuWiki Plugin struct (Action Component)
4*8f259467SAndreas Gohr *
5*8f259467SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*8f259467SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7*8f259467SAndreas Gohr */
8*8f259467SAndreas Gohr
9*8f259467SAndreas Gohr// must be run within Dokuwiki
10*8f259467SAndreas Gohr
11*8f259467SAndreas Gohrif(!defined('DOKU_INC')) die();
12*8f259467SAndreas Gohr
13*8f259467SAndreas Gohr/**
14*8f259467SAndreas Gohr * Class action_plugin_struct_migration
15*8f259467SAndreas Gohr *
16*8f259467SAndreas Gohr * Handle migrations that need more than just SQL
17*8f259467SAndreas Gohr */
18*8f259467SAndreas Gohrclass action_plugin_struct_migration extends DokuWiki_Action_Plugin {
19*8f259467SAndreas Gohr    /**
20*8f259467SAndreas Gohr     * @inheritDoc
21*8f259467SAndreas Gohr     */
22*8f259467SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
23*8f259467SAndreas Gohr        $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'BEFORE', $this, 'handle_migrations');
24*8f259467SAndreas Gohr    }
25*8f259467SAndreas Gohr
26*8f259467SAndreas Gohr    /**
27*8f259467SAndreas Gohr     * Call our custom migrations when defined
28*8f259467SAndreas Gohr     *
29*8f259467SAndreas Gohr     * @param Doku_Event $event
30*8f259467SAndreas Gohr     * @param $param
31*8f259467SAndreas Gohr     */
32*8f259467SAndreas Gohr    public function handle_migrations(Doku_Event $event, $param) {
33*8f259467SAndreas Gohr        $to = $event->data['to'];
34*8f259467SAndreas Gohr
35*8f259467SAndreas Gohr        if(is_callable(array($this, "migration$to"))) {
36*8f259467SAndreas Gohr            $event->preventDefault();
37*8f259467SAndreas Gohr            $event->result = call_user_func(array($this, "migration$to"), $event->data['sqlite']);
38*8f259467SAndreas Gohr        }
39*8f259467SAndreas Gohr    }
40*8f259467SAndreas Gohr
41*8f259467SAndreas Gohr    /**
42*8f259467SAndreas Gohr     * Executes Migration 12
43*8f259467SAndreas Gohr     *
44*8f259467SAndreas Gohr     * Add a latest column to all existing multi tables
45*8f259467SAndreas Gohr     *
46*8f259467SAndreas Gohr     * @param helper_plugin_sqlite $sqlite
47*8f259467SAndreas Gohr     * @return bool
48*8f259467SAndreas Gohr     */
49*8f259467SAndreas Gohr    protected function migration12(helper_plugin_sqlite $sqlite) {
50*8f259467SAndreas Gohr        /** @noinspection SqlResolve */
51*8f259467SAndreas Gohr        $sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'multi_%'";
52*8f259467SAndreas Gohr        $res = $sqlite->query($sql);
53*8f259467SAndreas Gohr        $tables = $sqlite->res2arr($res);
54*8f259467SAndreas Gohr        $sqlite->res_close($res);
55*8f259467SAndreas Gohr
56*8f259467SAndreas Gohr        foreach($tables as $row) {
57*8f259467SAndreas Gohr            $sql = 'ALTER TABLE ? ADD COLUMN latest INT DEFAULT 1';
58*8f259467SAndreas Gohr            $sqlite->query($sql, $row['name']);
59*8f259467SAndreas Gohr        }
60*8f259467SAndreas Gohr
61*8f259467SAndreas Gohr        return true;
62*8f259467SAndreas Gohr    }
63*8f259467SAndreas Gohr
64*8f259467SAndreas Gohr}
65