xref: /plugin/renderrevisions/action/revisions.php (revision 3e4faa7909287fd936f946fb2a5d1faecafacfb3)
1ab3c4129SAndreas Gohr<?php
2ab3c4129SAndreas Gohr
3ab3c4129SAndreas Gohruse dokuwiki\Extension\ActionPlugin;
4ab3c4129SAndreas Gohruse dokuwiki\Extension\Event;
5ab3c4129SAndreas Gohruse dokuwiki\Extension\EventHandler;
6ab3c4129SAndreas Gohruse dokuwiki\Form\HTMLElement;
7ab3c4129SAndreas Gohr
8ab3c4129SAndreas Gohr/**
9ab3c4129SAndreas Gohr * DokuWiki Plugin renderrevisions (Action Component)
10ab3c4129SAndreas Gohr *
11ab3c4129SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
12ab3c4129SAndreas Gohr * @author Andreas Gohr <dokuwiki@cosmocode.de>
13ab3c4129SAndreas Gohr */
14ab3c4129SAndreas Gohrclass action_plugin_renderrevisions_revisions extends ActionPlugin
15ab3c4129SAndreas Gohr{
16ab3c4129SAndreas Gohr    /** @inheritDoc */
17ab3c4129SAndreas Gohr    public function register(EventHandler $controller)
18ab3c4129SAndreas Gohr    {
19ab3c4129SAndreas Gohr        if($this->getConf('store')) {
20ab3c4129SAndreas Gohr            $controller->register_hook('FORM_REVISIONS_OUTPUT', 'BEFORE', $this, 'handleRevisions');
21ab3c4129SAndreas Gohr            $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleActPreprocess');
22ab3c4129SAndreas Gohr            $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'handleActUnknown');
23ab3c4129SAndreas Gohr        }
24ab3c4129SAndreas Gohr    }
25ab3c4129SAndreas Gohr
26ab3c4129SAndreas Gohr    /**
27ab3c4129SAndreas Gohr     * Event handler for FORM_REVISIONS_OUTPUT
28ab3c4129SAndreas Gohr     *
29ab3c4129SAndreas Gohr     * Link revisions to their rendered output when available
30ab3c4129SAndreas Gohr     *
31ab3c4129SAndreas Gohr     * @see https://www.dokuwiki.org/devel:events:FORM_REVISIONS_OUTPUT
32ab3c4129SAndreas Gohr     * @param Event $event Event object
33ab3c4129SAndreas Gohr     * @param mixed $param optional parameter passed when event was registered
34ab3c4129SAndreas Gohr     * @return void
35ab3c4129SAndreas Gohr     */
36ab3c4129SAndreas Gohr    public function handleRevisions(Event $event, $param)
37ab3c4129SAndreas Gohr    {
38ab3c4129SAndreas Gohr        global $INFO;
39ab3c4129SAndreas Gohr
40ab3c4129SAndreas Gohr        /** @var dokuwiki\Form\Form $form */
41ab3c4129SAndreas Gohr        $form = $event->data;
42ab3c4129SAndreas Gohr
43ab3c4129SAndreas Gohr        /** @var helper_plugin_renderrevisions_storage $storage */
44ab3c4129SAndreas Gohr        $storage = plugin_load('helper', 'renderrevisions_storage');
45ab3c4129SAndreas Gohr
46ab3c4129SAndreas Gohr        $id = $INFO['id'];
47ab3c4129SAndreas Gohr        $elementCount = $form->elementCount();
48ab3c4129SAndreas Gohr
49ab3c4129SAndreas Gohr        for ($i = 0; $i < $elementCount; $i++) {
50ab3c4129SAndreas Gohr            $element = $form->getElementAt($i);
51ab3c4129SAndreas Gohr            if (!$element instanceof HTMLElement) continue;
52*3e4faa79SAndreas Gohr            if (!preg_match('/[;&?]rev=(\d+)/', $element->val(), $match)) continue;
53ab3c4129SAndreas Gohr            $rev = (int)$match[1];
54ab3c4129SAndreas Gohr            if (!$rev) continue;
55ab3c4129SAndreas Gohr            if (!$storage->hasRevision($id, $rev)) continue;
56ab3c4129SAndreas Gohr
57ab3c4129SAndreas Gohr            $html = $element->val();
58*3e4faa79SAndreas Gohr            $html = preg_replace('/([;&?]rev=\d+)/', '\\1&amp;do=renderrevisions', $html);
59ab3c4129SAndreas Gohr            $html = preg_replace('/class="wikilink1"/', 'class="wikilink1 renderrevisions"', $html);
60ab3c4129SAndreas Gohr            $element->val($html);
61ab3c4129SAndreas Gohr        }
62ab3c4129SAndreas Gohr    }
63ab3c4129SAndreas Gohr
64ab3c4129SAndreas Gohr    /**
65ab3c4129SAndreas Gohr     * Event handler for ACTION_ACT_PREPROCESS
66ab3c4129SAndreas Gohr     *
67ab3c4129SAndreas Gohr     * @see https://www.dokuwiki.org/devel:event:ACTION_ACT_PREPROCESS
68ab3c4129SAndreas Gohr     * @param Event $event Event object
69ab3c4129SAndreas Gohr     * @param mixed $param optional parameter passed when event was registered
70ab3c4129SAndreas Gohr     * @return void
71ab3c4129SAndreas Gohr     */
72ab3c4129SAndreas Gohr    public function handleActPreprocess(Event $event, $param)
73ab3c4129SAndreas Gohr    {
74ab3c4129SAndreas Gohr        global $REV;
75ab3c4129SAndreas Gohr        global $INFO;
76ab3c4129SAndreas Gohr
77ab3c4129SAndreas Gohr        // not our circus?
78ab3c4129SAndreas Gohr        if ($event->data !== 'renderrevisions') return;
79ab3c4129SAndreas Gohr
80ab3c4129SAndreas Gohr        // no revision? show the page. id should always be set, but just in case
81ab3c4129SAndreas Gohr        if (!$REV || !$INFO['id']) {
82ab3c4129SAndreas Gohr            $event->data = 'show';
83ab3c4129SAndreas Gohr            return;
84ab3c4129SAndreas Gohr        }
85ab3c4129SAndreas Gohr
86ab3c4129SAndreas Gohr        // check permissions
87ab3c4129SAndreas Gohr        if(auth_quickaclcheck($INFO['id']) < AUTH_READ) {
88ab3c4129SAndreas Gohr            $event->data = 'denied';
89ab3c4129SAndreas Gohr            return;
90ab3c4129SAndreas Gohr        }
91ab3c4129SAndreas Gohr
92ab3c4129SAndreas Gohr        // no stored revision? show the page
93ab3c4129SAndreas Gohr        /** @var helper_plugin_renderrevisions_storage $storage */
94ab3c4129SAndreas Gohr        $storage = plugin_load('helper', 'renderrevisions_storage');
95ab3c4129SAndreas Gohr        if (!$storage->hasRevision($INFO['id'], (int)$REV)) {
96ab3c4129SAndreas Gohr            $event->data = 'show';
97ab3c4129SAndreas Gohr            return;
98ab3c4129SAndreas Gohr        }
99ab3c4129SAndreas Gohr
100ab3c4129SAndreas Gohr        // we can handle it!
101ab3c4129SAndreas Gohr        $event->preventDefault();
102ab3c4129SAndreas Gohr        $event->stopPropagation();
103ab3c4129SAndreas Gohr    }
104ab3c4129SAndreas Gohr
105ab3c4129SAndreas Gohr
106ab3c4129SAndreas Gohr    /**
107ab3c4129SAndreas Gohr     * Event handler for TPL_ACT_UNKNOWN
108ab3c4129SAndreas Gohr     *
109ab3c4129SAndreas Gohr     * @see https://www.dokuwiki.org/devel:event:TPL_ACT_UNKNOWN
110ab3c4129SAndreas Gohr     * @param Event $event Event object
111ab3c4129SAndreas Gohr     * @param mixed $param optional parameter passed when event was registered
112ab3c4129SAndreas Gohr     * @return void
113ab3c4129SAndreas Gohr     */
114ab3c4129SAndreas Gohr    public function handleActUnknown(Event $event, $param)
115ab3c4129SAndreas Gohr    {
116ab3c4129SAndreas Gohr        global $REV;
117ab3c4129SAndreas Gohr        global $INFO;
118ab3c4129SAndreas Gohr        global $ACT;
119ab3c4129SAndreas Gohr
120ab3c4129SAndreas Gohr        if ($event->data !== 'renderrevisions') return;
121ab3c4129SAndreas Gohr        $event->preventDefault();
122ab3c4129SAndreas Gohr        $event->stopPropagation();
123ab3c4129SAndreas Gohr
124ab3c4129SAndreas Gohr        /** @var helper_plugin_renderrevisions_storage $storage */
125ab3c4129SAndreas Gohr        $storage = plugin_load('helper', 'renderrevisions_storage');
126ab3c4129SAndreas Gohr        $content = $storage->getRevision($INFO['id'], (int)$REV);
127ab3c4129SAndreas Gohr        $intro = sprintf(
128ab3c4129SAndreas Gohr            $this->getLang('intro'),
129ab3c4129SAndreas Gohr            dformat($REV),
130ab3c4129SAndreas Gohr            '<a href="' . wl($INFO['id'], ['rev' => $REV]) . '">' . $this->getLang('rerender') . '</a>'
131ab3c4129SAndreas Gohr        );
132ab3c4129SAndreas Gohr
133ab3c4129SAndreas Gohr        echo '<div class="renderrevisions">';
134ab3c4129SAndreas Gohr        echo $intro;
135ab3c4129SAndreas Gohr        echo '</div>';
136ab3c4129SAndreas Gohr        echo $content;
137ab3c4129SAndreas Gohr    }
138ab3c4129SAndreas Gohr}
139