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