xref: /plugin/struct/action/migration.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
18f259467SAndreas Gohr<?php
2*d6d97f60SAnna 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// must be run within Dokuwiki
118f259467SAndreas Gohr
128f259467SAndreas Gohrif (!defined('DOKU_INC')) die();
138f259467SAndreas Gohr
148f259467SAndreas Gohr/**
158f259467SAndreas Gohr * Class action_plugin_struct_migration
168f259467SAndreas Gohr *
178f259467SAndreas Gohr * Handle migrations that need more than just SQL
188f259467SAndreas Gohr */
19*d6d97f60SAnna Dabrowskaclass action_plugin_struct_migration extends DokuWiki_Action_Plugin
20*d6d97f60SAnna Dabrowska{
218f259467SAndreas Gohr    /**
228f259467SAndreas Gohr     * @inheritDoc
238f259467SAndreas Gohr     */
24*d6d97f60SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
25*d6d97f60SAnna Dabrowska    {
268f259467SAndreas Gohr        $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'BEFORE', $this, 'handle_migrations');
278f259467SAndreas Gohr    }
288f259467SAndreas Gohr
298f259467SAndreas Gohr    /**
308f259467SAndreas Gohr     * Call our custom migrations when defined
318f259467SAndreas Gohr     *
328f259467SAndreas Gohr     * @param Doku_Event $event
338f259467SAndreas Gohr     * @param $param
348f259467SAndreas Gohr     */
35*d6d97f60SAnna Dabrowska    public function handle_migrations(Doku_Event $event, $param)
36*d6d97f60SAnna Dabrowska    {
373eafcbabSMichael Große        if ($event->data['sqlite']->getAdapter()->getDbname() !== 'struct') {
383eafcbabSMichael Große            return;
393eafcbabSMichael Große        }
408f259467SAndreas Gohr        $to = $event->data['to'];
418f259467SAndreas Gohr
428f259467SAndreas Gohr        if (is_callable(array($this, "migration$to"))) {
438f259467SAndreas Gohr            $event->preventDefault();
448f259467SAndreas Gohr            $event->result = call_user_func(array($this, "migration$to"), $event->data['sqlite']);
458f259467SAndreas Gohr        }
468f259467SAndreas Gohr    }
478f259467SAndreas Gohr
488f259467SAndreas Gohr    /**
498f259467SAndreas Gohr     * Executes Migration 12
508f259467SAndreas Gohr     *
518f259467SAndreas Gohr     * Add a latest column to all existing multi tables
528f259467SAndreas Gohr     *
538f259467SAndreas Gohr     * @param helper_plugin_sqlite $sqlite
548f259467SAndreas Gohr     * @return bool
558f259467SAndreas Gohr     */
56*d6d97f60SAnna Dabrowska    protected function migration12(helper_plugin_sqlite $sqlite)
57*d6d97f60SAnna Dabrowska    {
588f259467SAndreas Gohr        /** @noinspection SqlResolve */
598f259467SAndreas Gohr        $sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'multi_%'";
608f259467SAndreas Gohr        $res = $sqlite->query($sql);
618f259467SAndreas Gohr        $tables = $sqlite->res2arr($res);
628f259467SAndreas Gohr        $sqlite->res_close($res);
638f259467SAndreas Gohr
648f259467SAndreas Gohr        foreach ($tables as $row) {
658f259467SAndreas Gohr            $sql = 'ALTER TABLE ? ADD COLUMN latest INT DEFAULT 1';
668f259467SAndreas Gohr            $sqlite->query($sql, $row['name']);
678f259467SAndreas Gohr        }
688f259467SAndreas Gohr
698f259467SAndreas Gohr        return true;
708f259467SAndreas Gohr    }
718f259467SAndreas Gohr}
72