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