xref: /plugin/discussion/helper.php (revision 06ed893a2308b880b9a5d0cc2b0bdfb1088ac20a)
1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Esther Brunner <wikidesign@gmail.com>
5 */
6
7/**
8 * Class helper_plugin_discussion
9 */
10class helper_plugin_discussion extends DokuWiki_Plugin {
11
12    /**
13     * @return array
14     */
15    public function getMethods() {
16        $result = array();
17        $result[] = array(
18                'name'   => 'th',
19                'desc'   => 'returns the header of the comments column for pagelist',
20                'return' => array('header' => 'string'),
21                );
22        $result[] = array(
23                'name'   => 'td',
24                'desc'   => 'returns the link to the discussion section with number of comments',
25                'params' => array(
26                    'id' => 'string',
27                    'number of comments (optional)' => 'integer'),
28                'return' => array('link' => 'string'),
29                );
30        $result[] = array(
31                'name'   => 'getThreads',
32                'desc'   => 'returns pages with discussion sections, sorted by recent comments',
33                'params' => array(
34                    'namespace' => 'string',
35                    'number (optional)' => 'integer'),
36                'return' => array('pages' => 'array'),
37                );
38        $result[] = array(
39                'name'   => 'getComments',
40                'desc'   => 'returns recently added or edited comments individually',
41                'params' => array(
42                    'namespace' => 'string',
43                    'number (optional)' => 'integer'),
44                'return' => array('pages' => 'array'),
45                );
46        $result[] = array(
47                'name' => 'isDiscussionModerator',
48                'desc' => 'check if current user is member of moderator groups',
49                'params' => array(),
50                'return' => array('isModerator' => 'boolean')
51        );
52        return $result;
53    }
54
55    /**
56     * Returns the column header for the Pagelist Plugin
57     *
58     * @return string
59     */
60    public function th() {
61        return $this->getLang('discussion');
62    }
63
64    /**
65     * Returns the link to the discussion section of a page
66     *
67     * @param string $id page id
68     * @param string $col column name, used if more columns needed per plugin
69     * @param string $class class name per cell set by reference
70     * @param null|int $num number of visible comments -- internally used, not by pagelist plugin
71     * @return string
72     */
73    public function td($id, $col = null, &$class=null, $num = null) {
74        $section = '#discussion__section';
75
76        if (!isset($num)) {
77            $cfile = metaFN($id, '.comments');
78            $comments = unserialize(io_readFile($cfile, false));
79
80            $num = $comments['number'];
81            if ((!$comments['status']) || (($comments['status'] == 2) && (!$num))) return '';
82        }
83
84        if ($num == 0) {
85            $comment = '0&nbsp;'.$this->getLang('nocomments');
86        } elseif ($num == 1) {
87            $comment = '1&nbsp;'.$this->getLang('comment');
88        } else {
89            $comment = $num.'&nbsp;'.$this->getLang('comments');
90        }
91
92        return '<a href="'.wl($id).$section.'" class="wikilink1" title="'.$id.$section.'">'.
93            $comment.'</a>';
94    }
95
96    /**
97     * Returns an array of pages with discussion sections, sorted by recent comments
98     * Note: also used for content by Feed Plugin
99     *
100     * @param string $ns
101     * @param null|int $num
102     * @param string|bool $skipEmpty
103     * @return array
104     */
105    public function getThreads($ns, $num = null, $skipEmpty = false) {
106        global $conf;
107
108        require_once(DOKU_INC.'inc/search.php');
109
110        $dir = $conf['datadir'].utf8_encodeFN(($ns ? '/'.str_replace(':', '/', $ns): ''));
111
112        // returns the list of pages in the given namespace and it's subspaces
113        $items = array();
114        search($items, $dir, 'search_allpages', array());
115
116        // add pages with comments to result
117        $result = array();
118        foreach ($items as $item) {
119            $id   = ($ns ? $ns.':' : '').$item['id'];
120
121            // some checks
122            $perm = auth_quickaclcheck($id);
123            if ($perm < AUTH_READ) continue;    // skip if no permission
124            $file = metaFN($id, '.comments');
125            if (!@file_exists($file)) continue; // skip if no comments file
126            $data = unserialize(io_readFile($file, false));
127            $status = $data['status'];
128            $number = $data['number'];
129
130            if (!$status || (($status == 2) && (!$number))) continue; // skip if comments are off or closed without comments
131            if($skipEmpty == 'y' && $number == 0) continue; // skip if discussion is empty and flag is set
132
133            $date = filemtime($file);
134            $meta = p_get_metadata($id);
135            $result[$date.'_'.$id] = array(
136                    'id'       => $id,
137                    'file'     => $file,
138                    'title'    => $meta['title'],
139                    'date'     => $date,
140                    'user'     => $meta['creator'],
141                    'desc'     => $meta['description']['abstract'],
142                    'num'      => $number,
143                    'comments' => $this->td($id, null, $class, $number),
144                    'status'   => $status,
145                    'perm'     => $perm,
146                    'exists'   => true,
147                    'anchor'   => 'discussion__section',
148                    );
149        }
150
151        // finally sort by time of last comment
152        krsort($result);
153
154        if (is_numeric($num)) $result = array_slice($result, 0, $num);
155
156        return $result;
157    }
158
159    /**
160     * Returns an array of recently added comments to a given page or namespace
161     * Note: also used for content by Feed Plugin
162     *
163     * @param string $ns
164     * @param int|null $num
165     * @return array
166     */
167    public function getComments($ns, $num = NULL) {
168        global $conf;
169
170        $first  = $_REQUEST['first'];
171        if (!is_numeric($first)) $first = 0;
172
173        if ((!$num) || (!is_numeric($num))) $num = $conf['recent'];
174
175        $result = array();
176        $count  = 0;
177
178        if (!@file_exists($conf['metadir'].'/_comments.changes')) return $result;
179
180        // read all recent changes. (kept short)
181        $lines = file($conf['metadir'].'/_comments.changes');
182
183        $seen = array(); //caches seen pages in order to skip them
184        // handle lines
185        $line_num = count($lines);
186        for ($i = ($line_num - 1); $i >= 0; $i--) {
187            $rec = $this->_handleRecentComment($lines[$i], $ns, $seen);
188            if ($rec !== false) {
189                if (--$first >= 0) continue; // skip first entries
190                $result[$rec['date']] = $rec;
191                $count++;
192                // break when we have enough entries
193                if ($count >= $num) break;
194            }
195        }
196
197        // finally sort by time of last comment
198        krsort($result);
199
200        return $result;
201    }
202
203    /* ---------- Changelog function adapted for the Discussion Plugin ---------- */
204
205    /**
206     * Internal function used by $this->getComments()
207     *
208     * don't call directly
209     *
210     * @see getRecentComments()
211     * @author Andreas Gohr <andi@splitbrain.org>
212     * @author Ben Coburn <btcoburn@silicodon.net>
213     * @author Esther Brunner <wikidesign@gmail.com>
214     *
215     * @param string $line
216     * @param string $ns
217     * @param array  $seen
218     * @return array|bool
219     */
220    protected function _handleRecentComment($line, $ns, &$seen) {
221        if (empty($line)) return false;  //skip empty lines
222
223        // split the line into parts
224        $recent = parseChangelogLine($line);
225        if ($recent === false) return false;
226
227        $cid     = $recent['extra'];
228        $fullcid = $recent['id'].'#'.$recent['extra'];
229
230        // skip seen ones
231        if (isset($seen[$fullcid])) return false;
232
233        // skip 'show comment' log entries
234        if ($recent['type'] === 'sc') return false;
235
236        // remember in seen to skip additional sights
237        $seen[$fullcid] = 1;
238
239        // check if it's a hidden page or comment
240        if (isHiddenPage($recent['id'])) return false;
241        if ($recent['type'] === 'hc') return false;
242
243        // filter namespace or id
244        if (($ns) && (strpos($recent['id'].':', $ns.':') !== 0)) return false;
245
246        // check ACL
247        $recent['perm'] = auth_quickaclcheck($recent['id']);
248        if ($recent['perm'] < AUTH_READ) return false;
249
250        // check existance
251        $recent['file'] = wikiFN($recent['id']);
252        $recent['exists'] = @file_exists($recent['file']);
253        if (!$recent['exists']) return false;
254        if ($recent['type'] === 'dc') return false;
255
256        // get discussion meta file name
257        $data = unserialize(io_readFile(metaFN($recent['id'], '.comments'), false));
258
259        // check if discussion is turned off
260        if ($data['status'] === 0) return false;
261
262        $parent_id = $cid;
263        // Check for the comment and all parents if they exist and are visible.
264        do  {
265            $tcid = $parent_id;
266
267            // check if the comment still exists
268            if (!isset($data['comments'][$tcid])) return false;
269            // check if the comment is visible
270            if ($data['comments'][$tcid]['show'] != 1) return false;
271
272            $parent_id = $data['comments'][$tcid]['parent'];
273        } while ($parent_id && $parent_id != $tcid);
274
275        // okay, then add some additional info
276        if (is_array($data['comments'][$cid]['user'])) {
277            $recent['name'] = $data['comments'][$cid]['user']['name'];
278        } else {
279            $recent['name'] = $data['comments'][$cid]['name'];
280        }
281        $recent['desc'] = strip_tags($data['comments'][$cid]['xhtml']);
282        $recent['anchor'] = 'comment_'.$cid;
283
284        return $recent;
285    }
286
287    /**
288     * @return bool
289     */
290    public function isDiscussionModerator() {
291        global $USERINFO;
292        $groups = trim($this->getConf('moderatorgroups'));
293
294        if(auth_ismanager()) return true;
295        // Check if user is member of the moderator groups
296        if(!empty($groups) && auth_isMember($groups, $_SERVER['REMOTE_USER'], (array)$USERINFO['grps'])) return true;
297
298        return false;
299    }
300}
301// vim:ts=4:sw=4:et:enc=utf-8:
302