xref: /plugin/struct/action/revert.php (revision f392071e5199424895ee8c9c1720ed98413e5410)
187dc1344SAndreas Gohr<?php
287dc1344SAndreas Gohr/**
387dc1344SAndreas Gohr * DokuWiki Plugin struct (Action Component)
487dc1344SAndreas Gohr *
587dc1344SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
687dc1344SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
787dc1344SAndreas Gohr */
887dc1344SAndreas Gohr
987dc1344SAndreas Gohr// must be run within Dokuwiki
1087dc1344SAndreas Gohrif(!defined('DOKU_INC')) die();
1187dc1344SAndreas Gohr
12*f392071eSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
1387dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
1487dc1344SAndreas Gohr
1587dc1344SAndreas Gohr/**
1687dc1344SAndreas Gohr * Class action_plugin_struct_entry
1787dc1344SAndreas Gohr *
1887dc1344SAndreas Gohr * Handles reverting to old data via revert action
1987dc1344SAndreas Gohr */
2087dc1344SAndreas Gohrclass action_plugin_struct_revert extends DokuWiki_Action_Plugin {
2187dc1344SAndreas Gohr
2287dc1344SAndreas Gohr    /**
2387dc1344SAndreas Gohr     * Registers a callback function for a given event
2487dc1344SAndreas Gohr     *
2587dc1344SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
2687dc1344SAndreas Gohr     * @return void
2787dc1344SAndreas Gohr     */
2887dc1344SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
2987dc1344SAndreas Gohr        // ensure a page revision is created when struct data changes:
3087dc1344SAndreas Gohr        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_pagesave_before');
3187dc1344SAndreas Gohr        // save struct data after page has been saved:
3287dc1344SAndreas Gohr        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handle_pagesave_after');
3387dc1344SAndreas Gohr    }
3487dc1344SAndreas Gohr
3587dc1344SAndreas Gohr    /**
3687dc1344SAndreas Gohr     * Check if the page has to be changed
3787dc1344SAndreas Gohr     *
3887dc1344SAndreas Gohr     * @param Doku_Event $event event object by reference
3987dc1344SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
4087dc1344SAndreas Gohr     *                           handler was registered]
4187dc1344SAndreas Gohr     * @return bool
4287dc1344SAndreas Gohr     */
4387dc1344SAndreas Gohr    public function handle_pagesave_before(Doku_Event $event, $param) {
4487dc1344SAndreas Gohr        if($event->data['contentChanged']) return false; // will be saved for page changes already
4587dc1344SAndreas Gohr        global $ACT;
4687dc1344SAndreas Gohr        global $REV;
4787dc1344SAndreas Gohr        if($ACT != 'revert' || !$REV) return false;
4887dc1344SAndreas Gohr
4987dc1344SAndreas Gohr        // force changes for revert if there are assignments
5087dc1344SAndreas Gohr        $assignments = new Assignments();
5187dc1344SAndreas Gohr        $tosave = $assignments->getPageAssignments($event->data['id']);
5287dc1344SAndreas Gohr        if(count($tosave)) {
5387dc1344SAndreas Gohr            $event->data['contentChanged'] = true; // save for data changes
5487dc1344SAndreas Gohr        }
5587dc1344SAndreas Gohr
5687dc1344SAndreas Gohr        return true;
5787dc1344SAndreas Gohr    }
5887dc1344SAndreas Gohr
5987dc1344SAndreas Gohr    /**
6087dc1344SAndreas Gohr     * Save the data, by loading it from the old revision and storing it as a new revision
6187dc1344SAndreas Gohr     *
6287dc1344SAndreas Gohr     * @param Doku_Event $event event object by reference
6387dc1344SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
6487dc1344SAndreas Gohr     *                           handler was registered]
6587dc1344SAndreas Gohr     * @return bool
6687dc1344SAndreas Gohr     */
6787dc1344SAndreas Gohr    public function handle_pagesave_after(Doku_Event $event, $param) {
6887dc1344SAndreas Gohr        global $ACT;
6987dc1344SAndreas Gohr        global $REV;
7087dc1344SAndreas Gohr        if($ACT != 'revert' || !$REV) return false;
7187dc1344SAndreas Gohr
7287dc1344SAndreas Gohr        $assignments = new Assignments();
7387dc1344SAndreas Gohr
7487dc1344SAndreas Gohr        //  we load the data to restore from DB:
7587dc1344SAndreas Gohr        $tosave = $assignments->getPageAssignments($event->data['id']);
7687dc1344SAndreas Gohr        foreach($tosave as $table) {
77*f392071eSAndreas Gohr            $accessOld = AccessTable::byTableName($table, $event->data['id'], $REV);
78*f392071eSAndreas Gohr            $accessNew = AccessTable::byTableName($table, $event->data['id'], $event->data['newRevision']);
7987dc1344SAndreas Gohr            $accessNew->saveData($accessOld->getDataArray());
8087dc1344SAndreas Gohr
8187dc1344SAndreas Gohr            // make sure this schema is assigned
8287dc1344SAndreas Gohr            $assignments->assignPageSchema($event->data['id'], $table);
8387dc1344SAndreas Gohr        }
8487dc1344SAndreas Gohr        return true;
8587dc1344SAndreas Gohr    }
8687dc1344SAndreas Gohr
8787dc1344SAndreas Gohr}
88