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