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