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 * Inject struct data into indexed pages and search result snippets
15 */
16class action_plugin_struct_search extends DokuWiki_Action_Plugin
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('INDEXER_PAGE_ADD', 'BEFORE', $this, 'handleIndexing');
27        $controller->register_hook('FULLTEXT_SNIPPET_CREATE', 'BEFORE', $this, 'handleSnippets');
28    }
29
30    /**
31     * Adds the structured data to the page body to be indexed
32     *
33     * @param Doku_Event $event event object by reference
34     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
35     *                           handler was registered]
36     * @return bool
37     */
38    public function handleIndexing(Doku_Event $event, $param)
39    {
40        $id = $event->data['page'];
41        $assignments = Assignments::getInstance();
42        $tables = $assignments->getPageAssignments($id);
43        if (!$tables) return;
44
45        foreach ($tables as $table) {
46            $schemadata = AccessTable::getPageAccess($table, $id);
47            $event->data['body'] .= $schemadata->getDataPseudoSyntax();
48        }
49    }
50
51    /**
52     * Adds the structured data to the page body to be snippeted
53     *
54     * @param Doku_Event $event event object by reference
55     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
56     *                           handler was registered]
57     * @return bool
58     */
59    public function handleSnippets(Doku_Event $event, $param)
60    {
61        $id = $event->data['id'];
62        $assignments = Assignments::getInstance();
63        $tables = $assignments->getPageAssignments($id);
64        if (!$tables) return;
65
66        foreach ($tables as $table) {
67            $schemadata = AccessTable::getPageAccess($table, $id);
68            $event->data['text'] .= $schemadata->getDataPseudoSyntax();
69        }
70    }
71}
72
73// vim:ts=4:sw=4:et:
74