xref: /plugin/ireadit/action/ireadit.php (revision 13f6c79932364942534c54835bbbb6b994d388d9)
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_ireadit extends DokuWiki_Action_Plugin
10{
11    public function register(Doku_Event_Handler $controller)
12    {
13        $controller->register_hook('TPL_CONTENT_DISPLAY', 'AFTER', $this, 'render_list');
14        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_ireadit_action');
15        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handle_pagesave_after');
16        $controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'set_ireadit_index_version');
17        $controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'add_readers_to_index');
18    }
19
20    public function render_list()
21    {
22        global $INFO, $ACT, $auth;
23
24        if ($ACT != 'show') return;
25        if (!isset($INFO['meta']['plugin_ireadit=0.2'])) return;
26        $ireadit_data = $INFO['meta']['plugin_ireadit=0.2'];
27
28        echo '<div';
29        if ($this->getConf('print') == 0) {
30            echo' class="no-print"';
31        }
32        echo '>';
33
34        /** @var helper_plugin_ireadit $helper */
35        $helper = $this->loadHelper('ireadit');
36
37        // we use 'lastmod' insetead of 'rev' to get the timestamp also for the current revision
38        if ($helper->user_can_read_page($ireadit_data, $INFO['id'], $INFO['lastmod'], $INFO['client'])) {
39            echo '<a href="' . wl($INFO['id'], ['do' => 'ireadit', 'rev' => $INFO['lastmod']]) . '">' . $this->getLang('ireadit') . '</a>';
40        }
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        $res = $sqlite->query('SELECT user, timestamp FROM ireadit
52                                        WHERE page = ?
53                                        AND rev = ?
54                                        ORDER BY timestamp', $INFO['id'], $INFO['lastmod']);
55        $readers = $sqlite->res2arr($res);
56
57        if (count($readers) > 0) {
58            echo '<h3>' . $this->getLang('readit_header') . '</h3>';
59            echo '<ul>';
60            foreach ($readers as $reader) {
61                $udata = $auth->getUserData($reader['user'], false);
62                $name = $udata ? $udata['name'] : $reader['user'];
63                $time = strtotime($reader['timestamp']);
64                echo '<li>' . $name . ' - ' . date('d/m/Y H:i', $time) . '</li>';
65            }
66            echo '</ul>';
67        }
68        echo '</div>';
69    }
70
71    public function handle_ireadit_action(Doku_Event $event)
72    {
73        global $INFO, $INPUT;
74        if ($event->data != 'ireadit') return;
75        if (!$INFO['client']) return;
76        if (!isset($INFO['meta']['plugin_ireadit=0.2'])) return;
77        $ireadit_data = $INFO['meta']['plugin_ireadit=0.2'];
78
79        /** @var helper_plugin_ireadit $helper */
80        $helper = $this->loadHelper('ireadit');
81        if ($helper->user_can_read_page($ireadit_data, $INFO['id'], $INFO['lastmod'], $INFO['client'])) {
82            try {
83                /** @var \helper_plugin_ireadit_db $db_helper */
84                $db_helper = plugin_load('helper', 'ireadit_db');
85                $sqlite = $db_helper->getDB();
86            } catch (Exception $e) {
87                msg($e->getMessage(), -1);
88                return;
89            }
90            $sqlite->storeEntry('ireadit', [
91                'page' => $INFO['id'],
92                'rev' => $INFO['lastmod'], // we use 'lastmod' inseted of 'rev' to get the timestamp also for the current revision
93                'user' => $INFO['client'],
94                'timestamp' => date('c')
95            ]);
96        }
97        $go = wl($INFO['id'], ['rev' => $INFO['lastmod']], true, '&');
98        send_redirect($go);
99    }
100
101    public function handle_pagesave_after(Doku_Event $event)
102    {
103        global $INFO;
104        if (!isset($INFO['meta']['plugin_ireadit=0.2'])) return;
105
106        if ($this->getConf('minor_edit_keeps_readers') &&
107            $event->data['changeType'] == DOKU_CHANGE_TYPE_MINOR_EDIT) {
108            try {
109                /** @var \helper_plugin_ireadit_db $db_helper */
110                $db_helper = plugin_load('helper', 'ireadit_db');
111                $sqlite = $db_helper->getDB();
112            } catch (Exception $e) {
113                msg($e->getMessage(), -1);
114                return;
115            }
116            $sqlite->query('INSERT INTO ireadit (page,rev,user,timestamp)
117                            SELECT page, ?, user, timestamp FROM ireadit WHERE rev=? AND page=?',
118                $event->data['newRevision'], $event->data['oldRevision'], $event->data['id']);
119        }
120    }
121
122    /**
123     * Add a version string to the index so it is rebuilt
124     * whenever the stored data format changes.
125     */
126    public function set_ireadit_index_version(Doku_Event $event) {
127        $event->data['plugin_ireadit'] = '0.2';
128    }
129
130    /**
131     * Add all data of the readers metadata to the metadata index.
132     */
133    public function add_readers_to_index(Doku_Event $event, $param) {
134        $ireadit_data = p_get_metadata($event->data['page'], 'plugin_ireadit=0.2');
135        if (!$ireadit_data) return;
136
137        /** @var helper_plugin_ireadit $helper */
138        $helper = $this->loadHelper('ireadit');
139        $event->data['metadata']['ireadit'] = $helper->users_set($ireadit_data);
140    }
141}
142