xref: /plugin/ireadit/action/ireadit.php (revision d2cf7c78adce1664925be1a0be19ea4fc1239030)
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'])) return;
26        $ireadit_data = $INFO['meta']['plugin_ireadit'];
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'], $readers)) {
39            echo '<a href="' . wl($INFO['id'], ['do' => 'ireadit']) . '">' . $this->getLang('ireadit') . '</a>';
40        }
41
42        if (count($readers) > 0) {
43            echo '<h3>' . $this->getLang('readit_header') . '</h3>';
44            echo '<ul>';
45            foreach ($readers as $reader) {
46                $udata = $auth->getUserData($reader['user'], false);
47                $name = $udata ? $udata['name'] : $reader['user'];
48                $time = strtotime($reader['timestamp']);
49                echo '<li>' . $name . ' - ' . date('d/m/Y H:i', $time) . '</li>';
50            }
51            echo '</ul>';
52        }
53        echo '</div>';
54    }
55
56    public function handle_ireadit_action(Doku_Event $event)
57    {
58        global $INFO;
59        if ($event->data != 'ireadit') return;
60        if (!$INFO['client']) return;
61        if (!isset($INFO['meta']['plugin_ireadit'])) return;
62        $ireadit_data = $INFO['meta']['plugin_ireadit'];
63
64        /** @var helper_plugin_ireadit $helper */
65        $helper = $this->loadHelper('ireadit');
66        if ($helper->user_can_read_page($ireadit_data, $INFO['id'], $INFO['lastmod'], $INFO['client'])) {
67            try {
68                /** @var \helper_plugin_ireadit_db $db_helper */
69                $db_helper = plugin_load('helper', 'ireadit_db');
70                $sqlite = $db_helper->getDB();
71            } catch (Exception $e) {
72                msg($e->getMessage(), -1);
73                return;
74            }
75            $sqlite->storeEntry('ireadit', [
76                'page' => $INFO['id'],
77                'rev' => $INFO['lastmod'], // we use 'lastmod' inseted of 'rev' to get the timestamp also for the current revision
78                'user' => $INFO['client'],
79                'timestamp' => date('c')
80            ]);
81        }
82        $event->data = 'redirect';
83    }
84
85    public function handle_pagesave_after(Doku_Event $event)
86    {
87        global $INFO;
88        if (!isset($INFO['meta']['plugin_ireadit'])) return;
89
90        if ($this->getConf('minor_edit_keeps_readers') &&
91            $event->data['changeType'] == DOKU_CHANGE_TYPE_MINOR_EDIT) {
92            try {
93                /** @var \helper_plugin_ireadit_db $db_helper */
94                $db_helper = plugin_load('helper', 'ireadit_db');
95                $sqlite = $db_helper->getDB();
96            } catch (Exception $e) {
97                msg($e->getMessage(), -1);
98                return;
99            }
100            $sqlite->query('INSERT INTO ireadit (page,rev,user,timestamp)
101                            SELECT page, ?, user, timestamp FROM ireadit WHERE rev=? AND page=?',
102                $event->data['newRevision'], $event->data['oldRevision'], $event->data['id']);
103        }
104    }
105
106    /**
107     * Add a version string to the index so it is rebuilt
108     * whenever the stored data format changes.
109     */
110    public function set_ireadit_index_version(Doku_Event $event) {
111        $event->data['plugin_ireadit'] = '0.1';
112    }
113
114    /**
115     * Add all data of the readers metadata to the metadata index.
116     */
117    public function add_readers_to_index(Doku_Event $event, $param) {
118        $ireadit_data = p_get_metadata($event->data['page'], 'plugin_ireadit');
119        if (!$ireadit_data) return;
120
121        /** @var helper_plugin_ireadit $helper */
122        $helper = $this->loadHelper('ireadit');
123        $event->data['metadata']['ireadit'] = $helper->users_set($ireadit_data);
124    }
125}
126