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