xref: /plugin/struct/action/revert.php (revision 87dc1344fb5ebba89b6bf5f09fe8e70f0793e751)
1*87dc1344SAndreas Gohr<?php
2*87dc1344SAndreas Gohr/**
3*87dc1344SAndreas Gohr * DokuWiki Plugin struct (Action Component)
4*87dc1344SAndreas Gohr *
5*87dc1344SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*87dc1344SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7*87dc1344SAndreas Gohr */
8*87dc1344SAndreas Gohr
9*87dc1344SAndreas Gohr// must be run within Dokuwiki
10*87dc1344SAndreas Gohrif(!defined('DOKU_INC')) die();
11*87dc1344SAndreas Gohr
12*87dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTableData;
13*87dc1344SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
14*87dc1344SAndreas Gohr
15*87dc1344SAndreas Gohr/**
16*87dc1344SAndreas Gohr * Class action_plugin_struct_entry
17*87dc1344SAndreas Gohr *
18*87dc1344SAndreas Gohr * Handles reverting to old data via revert action
19*87dc1344SAndreas Gohr */
20*87dc1344SAndreas Gohrclass action_plugin_struct_revert extends DokuWiki_Action_Plugin {
21*87dc1344SAndreas Gohr
22*87dc1344SAndreas Gohr    /**
23*87dc1344SAndreas Gohr     * Registers a callback function for a given event
24*87dc1344SAndreas Gohr     *
25*87dc1344SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
26*87dc1344SAndreas Gohr     * @return void
27*87dc1344SAndreas Gohr     */
28*87dc1344SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
29*87dc1344SAndreas Gohr        // ensure a page revision is created when struct data changes:
30*87dc1344SAndreas Gohr        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_pagesave_before');
31*87dc1344SAndreas Gohr        // save struct data after page has been saved:
32*87dc1344SAndreas Gohr        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handle_pagesave_after');
33*87dc1344SAndreas Gohr    }
34*87dc1344SAndreas Gohr
35*87dc1344SAndreas Gohr    /**
36*87dc1344SAndreas Gohr     * Check if the page has to be changed
37*87dc1344SAndreas Gohr     *
38*87dc1344SAndreas Gohr     * @param Doku_Event $event event object by reference
39*87dc1344SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
40*87dc1344SAndreas Gohr     *                           handler was registered]
41*87dc1344SAndreas Gohr     * @return bool
42*87dc1344SAndreas Gohr     */
43*87dc1344SAndreas Gohr    public function handle_pagesave_before(Doku_Event $event, $param) {
44*87dc1344SAndreas Gohr        if($event->data['contentChanged']) return false; // will be saved for page changes already
45*87dc1344SAndreas Gohr        global $ACT;
46*87dc1344SAndreas Gohr        global $REV;
47*87dc1344SAndreas Gohr        if($ACT != 'revert' || !$REV) return false;
48*87dc1344SAndreas Gohr
49*87dc1344SAndreas Gohr        // force changes for revert if there are assignments
50*87dc1344SAndreas Gohr        $assignments = new Assignments();
51*87dc1344SAndreas Gohr        $tosave = $assignments->getPageAssignments($event->data['id']);
52*87dc1344SAndreas Gohr        if(count($tosave)) {
53*87dc1344SAndreas Gohr            $event->data['contentChanged'] = true; // save for data changes
54*87dc1344SAndreas Gohr        }
55*87dc1344SAndreas Gohr
56*87dc1344SAndreas Gohr        return true;
57*87dc1344SAndreas Gohr    }
58*87dc1344SAndreas Gohr
59*87dc1344SAndreas Gohr    /**
60*87dc1344SAndreas Gohr     * Save the data, by loading it from the old revision and storing it as a new revision
61*87dc1344SAndreas Gohr     *
62*87dc1344SAndreas Gohr     * @param Doku_Event $event event object by reference
63*87dc1344SAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
64*87dc1344SAndreas Gohr     *                           handler was registered]
65*87dc1344SAndreas Gohr     * @return bool
66*87dc1344SAndreas Gohr     */
67*87dc1344SAndreas Gohr    public function handle_pagesave_after(Doku_Event $event, $param) {
68*87dc1344SAndreas Gohr        global $ACT;
69*87dc1344SAndreas Gohr        global $REV;
70*87dc1344SAndreas Gohr        if($ACT != 'revert' || !$REV) return false;
71*87dc1344SAndreas Gohr
72*87dc1344SAndreas Gohr        $assignments = new Assignments();
73*87dc1344SAndreas Gohr
74*87dc1344SAndreas Gohr        //  we load the data to restore from DB:
75*87dc1344SAndreas Gohr        $tosave = $assignments->getPageAssignments($event->data['id']);
76*87dc1344SAndreas Gohr        foreach($tosave as $table) {
77*87dc1344SAndreas Gohr            $accessOld = new AccessTableData($table, $event->data['id'], $REV);
78*87dc1344SAndreas Gohr            $accessNew = new AccessTableData($table, $event->data['id'], $event->data['newRevision']);
79*87dc1344SAndreas Gohr            $accessNew->saveData($accessOld->getDataArray());
80*87dc1344SAndreas Gohr
81*87dc1344SAndreas Gohr            // make sure this schema is assigned
82*87dc1344SAndreas Gohr            $assignments->assignPageSchema($event->data['id'], $table);
83*87dc1344SAndreas Gohr        }
84*87dc1344SAndreas Gohr        return true;
85*87dc1344SAndreas Gohr    }
86*87dc1344SAndreas Gohr
87*87dc1344SAndreas Gohr}
88