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