-
Notifications
You must be signed in to change notification settings - Fork 28
/
admin.php
411 lines (357 loc) · 13 KB
/
admin.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
/**
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Esther Brunner <[email protected]>
*/
use dokuwiki\Utf8\PhpString;
/**
* Class admin_plugin_discussion
*/
class admin_plugin_discussion extends DokuWiki_Admin_Plugin
{
/**
* @return int
*/
public function getMenuSort()
{
return 200;
}
/**
* @return bool
*/
public function forAdminOnly()
{
return false;
}
public function handle()
{
global $lang, $INPUT;
$cids = $INPUT->post->arr('cid');
if (is_array($cids)) {
$cids = array_keys($cids);
}
/** @var action_plugin_discussion $action */
$action = plugin_load('action', 'discussion');
if (!$action) return; // couldn't load action plugin component
$act = $INPUT->post->str('comment');
if ($act && !checkSecurityToken()) {
return;
}
switch ($act) {
case $lang['btn_delete']:
$action->save($cids, '');
break;
case $this->getLang('btn_show'):
$action->save($cids, '', 'show');
break;
case $this->getLang('btn_hide'):
$action->save($cids, '', 'hide');
break;
case $this->getLang('btn_change'):
$this->changeStatus($INPUT->post->str('status'));
break;
}
}
public function html()
{
global $conf, $INPUT;
$first = $INPUT->int('first');
$num = $conf['recent'] ?: 20;
ptln('<h1>' . $this->getLang('menu') . '</h1>');
$threads = $this->getThreads();
// slice the needed chunk of discussion pages
$isMore = count($threads) > ($first + $num);
$threads = array_slice($threads, $first, $num);
foreach ($threads as $thread) {
$comments = $this->getComments($thread);
$this->threadHead($thread);
if ($comments === false) {
ptln('</div>', 6); // class="level2"
continue;
}
ptln('<form method="post" action="' . wl($thread['id']) . '">', 8);
ptln('<div class="no">', 10);
ptln('<input type="hidden" name="do" value="admin" />', 10);
ptln('<input type="hidden" name="page" value="discussion" />', 10);
ptln('<input type="hidden" name="sectok" value="'.getSecurityToken().'" />', 10);
echo html_buildlist($comments, 'admin_discussion', [$this, 'commentItem'], [$this, 'liComment']);
$this->actionButtons();
ptln('</div>', 10); // class="no"
ptln('</form>', 8);
ptln('</div>', 6); // class="level2"
}
$this->browseDiscussionLinks($isMore, $first, $num);
}
/**
* Returns an array of pages with discussion sections, sorted by recent comments
*
* @return array
*/
protected function getThreads()
{
global $conf;
// returns the list of pages in the given namespace and it's subspaces
$items = [];
search($items, $conf['datadir'], 'search_allpages', []);
// add pages with comments to result
$result = [];
foreach ($items as $item) {
$id = $item['id'];
// some checks
$file = metaFN($id, '.comments');
if (!@file_exists($file)) continue; // skip if no comments file
$date = filemtime($file);
$result[] = [
'id' => $id,
'file' => $file,
'date' => $date,
];
}
// finally sort by time of last comment
usort($result, ['admin_plugin_discussion', 'threadCmp']);
return $result;
}
/**
* Callback for comparison of thread data.
*
* Used for sorting threads in descending order by date of last comment.
* If this date happens to be equal for the compared threads, page id
* is used as second comparison attribute.
*
* @param array $a
* @param array $b
* @return int
*/
protected function threadCmp($a, $b)
{
if ($a['date'] == $b['date']) {
return strcmp($a['id'], $b['id']);
}
if ($a['date'] < $b['date']) {
return 1;
} else {
return -1;
}
}
/**
* Outputs header, page ID and status of a discussion thread
*
* @param array $thread
* @return bool
*/
protected function threadHead($thread)
{
global $lang;
$id = $thread['id'];
$labels = [
0 => $this->getLang('off'),
1 => $this->getLang('open'),
2 => $this->getLang('closed')
];
$title = p_get_metadata($id, 'title');
if (!$title) {
$title = $id;
}
$align = $lang['direction'] === 'rtl' ? 'left' : 'right';
echo '<h2 name="' . $id . '" id="' . $id . '">' . hsc($title) . '</h2>'
. '<form method="post" action="' . wl($id) . '">'
. '<div class="media' . $align . '">'
. '<input type="hidden" name="do" value="admin" />'
. '<input type="hidden" name="page" value="discussion" />'
. '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />'
. $this->getLang('status') . ': '
. '<select name="status" size="1">';
foreach ($labels as $key => $label) {
$selected = ($key == $thread['status'] ? ' selected="selected"' : '');
echo '<option value="' . $key . '"' . $selected . '>' . $label . '</option>';
}
echo '</select> '
. '<input type="submit" class="button" name="comment" value="' . $this->getLang('btn_change') . '" '
. 'title="' . $this->getLang('btn_change') . '" />'
. '</div>'
. '</form>'
. '<div class="level2">'
. '<a href="' . wl($id) . '" class="wikilink1">' . $id . '</a> ';
return true;
}
/**
* Returns the full comments data for a given wiki page
*
* @param array $thread by reference with:
* 'id' => string page id,
* 'file' => string file location of .comments metadata file
* 'status' => int
* 'number' => int number of visible comments
*
* @return array|bool
*/
protected function getComments(&$thread)
{
$id = $thread['id'];
if (!$thread['file']) {
$thread['file'] = metaFN($id, '.comments');
}
if (!@file_exists($thread['file'])) return false; // no discussion thread at all
$data = unserialize(io_readFile($thread['file'], false));
$thread['status'] = $data['status'];
$thread['number'] = $data['number'];
if (empty($data['status'])) return false; // comments are turned off
if (empty($data['comments'])) return false; // no comments
$result = [];
foreach ($data['comments'] as $cid => $comment) {
$this->addComment($cid, $data, $result, $id);
}
if (empty($result)) {
return false;
} else {
return $result;
}
}
/**
* Recursive function to add the comment hierarchy to the result
*
* @param string $cid comment id of current comment
* @param array $data array with all comments by reference
* @param array $result array with all comments by reference enhanced with level
* @param string $id page id
* @param string $parent comment id of parent or empty
* @param int $level level of current comment, higher is deeper
*/
protected function addComment($cid, &$data, &$result, $id, $parent = '', $level = 1)
{
if (!isset($data['comments'][$cid]) || !is_array($data['comments'][$cid])) return; // corrupt datatype
$comment = $data['comments'][$cid];
// handle only replies to given parent comment
if ($comment['parent'] != $parent) return;
// okay, add the comment to the result
$comment['id'] = $id;
$comment['level'] = $level;
$result[] = $comment;
// check answers to this comment
if (count($comment['replies'])) {
foreach ($comment['replies'] as $rid) {
$this->addComment($rid, $data, $result, $id, $cid, $level + 1);
}
}
}
/**
* Returns html of checkbox and info about a comment item
*
* @param array $comment array with comment data
* @return string html of checkbox and info
*/
public function commentItem($comment)
{
global $conf;
// prepare variables
if (is_array($comment['user'])) { // new format
$name = $comment['user']['name'];
$mail = $comment['user']['mail'];
} else { // old format
$name = $comment['name'];
$mail = $comment['mail'];
}
if (is_array($comment['date'])) { // new format
$created = $comment['date']['created'];
} else { // old format
$created = $comment['date'];
}
$abstract = preg_replace('/\s+?/', ' ', strip_tags($comment['xhtml']));
if (PhpString::strlen($abstract) > 160) {
$abstract = PhpString::substr($abstract, 0, 160) . '...';
}
return '<input type="checkbox" name="cid[' . $comment['cid'] . ']" value="1" /> '
. $this->email($mail, $name, 'email')
. ', <a href="' . wl($comment['id']) . '#comment_' . $comment['cid'] . '" class="wikilink1">'
. strftime($conf['dformat'], $created) . ': '
. '</a>'
. '<span class="abstract">' . $abstract . '</span>';
}
/**
* Returns html of list item openings tag
*
* @param array $comment
* @return string
*/
public function liComment($comment)
{
$showclass = ($comment['show'] ? '' : ' hidden');
return '<li class="level' . $comment['level'] . $showclass . '">';
}
/**
* Show buttons to bulk remove, hide or show comments
*/
protected function actionButtons()
{
global $lang;
ptln('<div class="comment_buttons">', 12);
ptln('<input type="submit" name="comment" value="' . $this->getLang('btn_show') . '" class="button" title="' . $this->getLang('btn_show') . '" />', 14);
ptln('<input type="submit" name="comment" value="' . $this->getLang('btn_hide') . '" class="button" title="' . $this->getLang('btn_hide') . '" />', 14);
ptln('<input type="submit" name="comment" value="' . $lang['btn_delete'] . '" class="button" title="' . $lang['btn_delete'] . '" />', 14);
ptln('</div>', 12); // class="comment_buttons"
}
/**
* Displays links to older newer discussions
*
* @param bool $isMore whether there are more pages needed
* @param int $first first entry on this page
* @param int $num number of entries per page
*/
protected function browseDiscussionLinks($isMore, $first, $num)
{
global $ID;
if ($first == 0 && !$isMore) return;
$params = ['do' => 'admin', 'page' => 'discussion'];
$last = $first + $num;
ptln('<div class="level1">', 8);
$return = '';
if ($first > 0) {
$first -= $num;
if ($first < 0) {
$first = 0;
}
$params['first'] = $first;
ptln('<p class="centeralign">', 8);
$return = '<a href="' . wl($ID, $params) . '" class="wikilink1"><< ' . $this->getLang('newer') . '</a>';
if ($isMore) {
$return .= ' | ';
} else {
ptln($return, 10);
ptln('</p>', 8);
}
} elseif ($isMore) {
ptln('<p class="centeralign">', 8);
}
if ($isMore) {
$params['first'] = $last;
$return .= '<a href="' . wl($ID, $params) . '" class="wikilink1">' . $this->getLang('older') . ' >></a>';
ptln($return, 10);
ptln('</p>', 8);
}
ptln('</div>', 6); // class="level1"
}
/**
* Changes the status of a comment section
*
* @param int $new 0=disabled, 1=enabled, 2=closed
*/
protected function changeStatus($new)
{
global $ID;
// get discussion meta file name
$file = metaFN($ID, '.comments');
$data = unserialize(io_readFile($file, false));
$old = $data['status'];
if ($old == $new) {
return;
}
// save the comment metadata file
$data['status'] = $new;
io_saveFile($file, serialize($data));
// look for ~~DISCUSSION~~ command in page file and change it accordingly
$patterns = ['~~DISCUSSION:off\2~~', '~~DISCUSSION\2~~', '~~DISCUSSION:closed\2~~'];
$replace = $patterns[$new];
$wiki = preg_replace('/~~DISCUSSION([\w:]*)(\|?.*?)~~/', $replace, rawWiki($ID));
saveWikiText($ID, $wiki, $this->getLang('statuschanged'), true);
}
}