1<?php
2// must be run within DokuWiki
3if (!defined('DOKU_INC')) die();
4
5/**
6 * All DokuWiki plugins to extend the parser/rendering mechanism
7 * need to inherit from this class
8 */
9class action_plugin_ireadit_notification extends DokuWiki_Action_Plugin
10{
11    public function register(Doku_Event_Handler $controller)
12    {
13        $controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'add_notifications_source');
14        $controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'add_notifications');
15        $controller->register_hook('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', 'AFTER', $this, 'add_notification_cache_dependencies');
16    }
17
18    public function add_notifications_source(Doku_Event $event)
19    {
20        $event->data[] = 'ireadit';
21    }
22
23    public function add_notification_cache_dependencies(Doku_Event $event)
24    {
25        if (!in_array('ireadit', $event->data['plugins'])) return;
26
27        try {
28            /** @var \helper_plugin_ireadit_db $db_helper */
29            $db_helper = plugin_load('helper', 'ireadit_db');
30            $sqlite = $db_helper->getDB();
31            $event->data['dependencies'][] = $sqlite->getAdapter()->getDbFile();
32        } catch (Exception $e) {
33            msg($e->getMessage(), -1);
34            return;
35        }
36    }
37
38    public function add_notifications(Doku_Event $event)
39    {
40        if (!in_array('ireadit', $event->data['plugins'])) return;
41
42        try {
43            /** @var \helper_plugin_ireadit_db $db_helper */
44            $db_helper = plugin_load('helper', 'ireadit_db');
45            $sqlite = $db_helper->getDB();
46        } catch (Exception $e) {
47            msg($e->getMessage(), -1);
48            return;
49        }
50
51        $user = $event->data['user'];
52
53        $res = $sqlite->query('SELECT page, rev FROM ireadit
54                                        WHERE timestamp IS NULL
55                                        AND user = ?
56                                        ORDER BY timestamp', $user);
57
58        $notifications = $sqlite->res2arr($res);
59
60        foreach ($notifications as $notification) {
61            $page = $notification['page'];
62            $rev = $notification['rev'];
63
64            $link = '<a class="wikilink1" href="' . wl($page, '', true) . '">';
65            if (useHeading('content')) {
66                $heading = p_get_first_heading($page);
67                if (!blank($heading)) {
68                    $link .= $heading;
69                } else {
70                    $link .= noNSorNS($page);
71                }
72            } else {
73                $link .= noNSorNS($page);
74            }
75            $link .= '</a>';
76            $full = sprintf($this->getLang('notification full'), $link);
77            $event->data['notifications'][] = [
78                'plugin' => 'ireadit',
79                'id' => $page . ':' . $rev,
80                'full' => $full,
81                'brief' => $link,
82                'timestamp' => (int)$rev
83            ];
84        }
85    }
86}
87