xref: /plugin/struct/action/migration.php (revision 3eafcbabeb88f1fc6d6d206798b1e0e7a8b870c0)
18f259467SAndreas Gohr<?php
28f259467SAndreas Gohr/**
38f259467SAndreas Gohr * DokuWiki Plugin struct (Action Component)
48f259467SAndreas Gohr *
58f259467SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
68f259467SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
78f259467SAndreas Gohr */
88f259467SAndreas Gohr
98f259467SAndreas Gohr// must be run within Dokuwiki
108f259467SAndreas Gohr
118f259467SAndreas Gohrif(!defined('DOKU_INC')) die();
128f259467SAndreas Gohr
138f259467SAndreas Gohr/**
148f259467SAndreas Gohr * Class action_plugin_struct_migration
158f259467SAndreas Gohr *
168f259467SAndreas Gohr * Handle migrations that need more than just SQL
178f259467SAndreas Gohr */
188f259467SAndreas Gohrclass action_plugin_struct_migration extends DokuWiki_Action_Plugin {
198f259467SAndreas Gohr    /**
208f259467SAndreas Gohr     * @inheritDoc
218f259467SAndreas Gohr     */
228f259467SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
238f259467SAndreas Gohr        $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'BEFORE', $this, 'handle_migrations');
248f259467SAndreas Gohr    }
258f259467SAndreas Gohr
268f259467SAndreas Gohr    /**
278f259467SAndreas Gohr     * Call our custom migrations when defined
288f259467SAndreas Gohr     *
298f259467SAndreas Gohr     * @param Doku_Event $event
308f259467SAndreas Gohr     * @param $param
318f259467SAndreas Gohr     */
328f259467SAndreas Gohr    public function handle_migrations(Doku_Event $event, $param) {
33*3eafcbabSMichael Große        if ($event->data['sqlite']->getAdapter()->getDbname() !== 'struct') {
34*3eafcbabSMichael Große            return;
35*3eafcbabSMichael 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     */
528f259467SAndreas Gohr    protected function migration12(helper_plugin_sqlite $sqlite) {
538f259467SAndreas Gohr        /** @noinspection SqlResolve */
548f259467SAndreas Gohr        $sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'multi_%'";
558f259467SAndreas Gohr        $res = $sqlite->query($sql);
568f259467SAndreas Gohr        $tables = $sqlite->res2arr($res);
578f259467SAndreas Gohr        $sqlite->res_close($res);
588f259467SAndreas Gohr
598f259467SAndreas Gohr        foreach($tables as $row) {
608f259467SAndreas Gohr            $sql = 'ALTER TABLE ? ADD COLUMN latest INT DEFAULT 1';
618f259467SAndreas Gohr            $sqlite->query($sql, $row['name']);
628f259467SAndreas Gohr        }
638f259467SAndreas Gohr
648f259467SAndreas Gohr        return true;
658f259467SAndreas Gohr    }
668f259467SAndreas Gohr
678f259467SAndreas Gohr}
68