xref: /plugin/struct/action/migration.php (revision 748e747f37aa44250ee32847b5fc3ff1e47f0835)
18f259467SAndreas Gohr<?php
2d6d97f60SAnna Dabrowska
38f259467SAndreas Gohr/**
48f259467SAndreas Gohr * DokuWiki Plugin struct (Action Component)
58f259467SAndreas Gohr *
68f259467SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
78f259467SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
88f259467SAndreas Gohr */
98f259467SAndreas Gohr
108f259467SAndreas Gohr/**
118f259467SAndreas Gohr * Class action_plugin_struct_migration
128f259467SAndreas Gohr *
138f259467SAndreas Gohr * Handle migrations that need more than just SQL
148f259467SAndreas Gohr */
15d6d97f60SAnna Dabrowskaclass action_plugin_struct_migration extends DokuWiki_Action_Plugin
16d6d97f60SAnna Dabrowska{
178f259467SAndreas Gohr    /**
188f259467SAndreas Gohr     * @inheritDoc
198f259467SAndreas Gohr     */
20d6d97f60SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
21d6d97f60SAnna Dabrowska    {
22*748e747fSAnna Dabrowska        $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'BEFORE', $this, 'handleMigrations');
238f259467SAndreas Gohr    }
248f259467SAndreas Gohr
258f259467SAndreas Gohr    /**
268f259467SAndreas Gohr     * Call our custom migrations when defined
278f259467SAndreas Gohr     *
288f259467SAndreas Gohr     * @param Doku_Event $event
298f259467SAndreas Gohr     * @param $param
308f259467SAndreas Gohr     */
31*748e747fSAnna Dabrowska    public function handleMigrations(Doku_Event $event, $param)
32d6d97f60SAnna Dabrowska    {
333eafcbabSMichael Große        if ($event->data['sqlite']->getAdapter()->getDbname() !== 'struct') {
343eafcbabSMichael Große            return;
353eafcbabSMichael Große        }
368f259467SAndreas Gohr        $to = $event->data['to'];
378f259467SAndreas Gohr
388f259467SAndreas Gohr        if (is_callable(array($this, "migration$to"))) {
398f259467SAndreas Gohr            $event->preventDefault();
408f259467SAndreas Gohr            $event->result = call_user_func(array($this, "migration$to"), $event->data['sqlite']);
418f259467SAndreas Gohr        }
428f259467SAndreas Gohr    }
438f259467SAndreas Gohr
448f259467SAndreas Gohr    /**
458f259467SAndreas Gohr     * Executes Migration 12
468f259467SAndreas Gohr     *
478f259467SAndreas Gohr     * Add a latest column to all existing multi tables
488f259467SAndreas Gohr     *
498f259467SAndreas Gohr     * @param helper_plugin_sqlite $sqlite
508f259467SAndreas Gohr     * @return bool
518f259467SAndreas Gohr     */
52d6d97f60SAnna Dabrowska    protected function migration12(helper_plugin_sqlite $sqlite)
53d6d97f60SAnna Dabrowska    {
548f259467SAndreas Gohr        /** @noinspection SqlResolve */
558f259467SAndreas Gohr        $sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'multi_%'";
568f259467SAndreas Gohr        $res = $sqlite->query($sql);
578f259467SAndreas Gohr        $tables = $sqlite->res2arr($res);
588f259467SAndreas Gohr        $sqlite->res_close($res);
598f259467SAndreas Gohr
608f259467SAndreas Gohr        foreach ($tables as $row) {
618f259467SAndreas Gohr            $sql = 'ALTER TABLE ? ADD COLUMN latest INT DEFAULT 1';
628f259467SAndreas Gohr            $sqlite->query($sql, $row['name']);
638f259467SAndreas Gohr        }
648f259467SAndreas Gohr
658f259467SAndreas Gohr        return true;
668f259467SAndreas Gohr    }
678f259467SAndreas Gohr}
68