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;
15use dokuwiki\plugin\struct\meta\StructException;
16
17class action_plugin_struct_diff extends ActionPlugin
18{
19    /**
20     * Registers a callback function for a given event
21     *
22     * @param EventHandler $controller DokuWiki's event controller object
23     * @return void
24     */
25    public function register(EventHandler $controller)
26    {
27        $controller->register_hook('IO_WIKIPAGE_READ', 'AFTER', $this, 'handleDiffload');
28    }
29
30    /**
31     * Add structured data to the diff
32     *
33     * This is done by adding pseudo syntax to the page source when it is loaded in diff context
34     *
35     * @param Event $event event object by reference
36     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
37     *                           handler was registered]
38     * @return bool
39     */
40    public function handleDiffload(Event $event, $param)
41    {
42        global $ACT;
43        global $INFO;
44        if ($ACT != 'diff') return;
45        $id = $event->data[2];
46        if (!blank($event->data[1])) {
47            $id = $event->data[1] . ':' . $id;
48        }
49        $rev = $event->data[3] ?: time();
50        if ($INFO['id'] != $id) return;
51
52        $assignments = Assignments::getInstance();
53        $tables = $assignments->getPageAssignments($id);
54        if (!$tables) return;
55
56        $event->result .= "\n---- struct data ----\n";
57        foreach ($tables as $table) {
58            try {
59                $schemadata = AccessTable::getPageAccess($table, $id, $rev);
60            } catch (StructException $ignored) {
61                continue; // no such schema at this revision
62            }
63            $event->result .= $schemadata->getDataPseudoSyntax();
64        }
65        $event->result .= "----\n";
66    }
67}
68
69// vim:ts=4:sw=4:et:
70