xref: /plugin/discussion/admin.php (revision 14382eba0e9088630a1c1e6d68458d054754467c)
1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Esther Brunner <wikidesign@gmail.com>
5 */
6
7// must be run within Dokuwiki
8if (!defined('DOKU_INC')) die();
9
10if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11
12require_once(DOKU_PLUGIN.'admin.php');
13
14class admin_plugin_discussion extends DokuWiki_Admin_Plugin {
15
16    function getInfo() {
17        return array(
18                'author' => 'Gina Häußge, Michael Klier, Esther Brunner',
19                'email'  => 'dokuwiki@chimeric.de',
20                'date'   => @file_get_contents(DOKU_PLUGIN.'discussion/VERSION'),
21                'name'   => 'Discussion Plugin (admin component)',
22                'desc'   => 'Moderate discussions',
23                'url'    => 'http://wiki.splitbrain.org/plugin:discussion',
24                );
25    }
26
27    function getMenuSort() { return 200; }
28    function forAdminOnly() { return false; }
29
30    function handle() {
31        global $lang;
32
33        $cid = $_REQUEST['cid'];
34        if (is_array($cid)) $cid = array_keys($cid);
35
36        $action =& plugin_load('action', 'discussion');
37        if (!$action) return; // couldn't load action plugin component
38
39        switch ($_REQUEST['comment']) {
40            case $lang['btn_delete']:
41                $action->_save($cid, '');
42                break;
43
44            case $this->getLang('btn_show'):
45                $action->_save($cid, '', 'show');
46                break;
47
48            case $this->getLang('btn_hide'):
49                $action->_save($cid, '', 'hide');
50                break;
51
52            case $this->getLang('btn_change'):
53                $this->_changeStatus($_REQUEST['status']);
54                break;
55        }
56    }
57
58    function html() {
59        global $conf;
60
61        $first = $_REQUEST['first'];
62        if (!is_numeric($first)) $first = 0;
63        $num = ($conf['recent']) ? $conf['recent'] : 20;
64
65        ptln('<h1>'.$this->getLang('menu').'</h1>');
66
67        $threads = $this->_getThreads();
68
69        // slice the needed chunk of discussion pages
70        $more = ((count($threads) > ($first + $num)) ? true : false);
71        $threads = array_slice($threads, $first, $num);
72
73        foreach ($threads as $thread) {
74            $comments = $this->_getComments($thread);
75            $this->_threadHead($thread);
76            if ($comments === false) {
77                ptln('</div>', 6); // class="level2"
78                continue;
79            }
80
81            ptln('<form method="post" action="'.wl($thread['id']).'">', 8);
82            ptln('<div class="no">', 10);
83            ptln('<input type="hidden" name="do" value="admin" />', 10);
84            ptln('<input type="hidden" name="page" value="discussion" />', 10);
85            echo html_buildlist($comments, 'admin_discussion', array($this, '_commentItem'), array($this, '_li_comment'));
86            $this->_actionButtons($thread['id']);
87        }
88        $this->_browseDiscussionLinks($more, $first, $num);
89
90    }
91
92    /**
93     * Returns an array of pages with discussion sections, sorted by recent comments
94     */
95    function _getThreads() {
96        global $conf;
97
98        require_once(DOKU_INC.'inc/search.php');
99
100        // returns the list of pages in the given namespace and it's subspaces
101        $items = array();
102        search($items, $conf['datadir'], 'search_allpages', '');
103
104        // add pages with comments to result
105        $result = array();
106        foreach ($items as $item) {
107            $id = $item['id'];
108
109            // some checks
110            $file = metaFN($id, '.comments');
111            if (!@file_exists($file)) continue; // skip if no comments file
112
113            $date = filemtime($file);
114            $result[] = array(
115                    'id'   => $id,
116                    'file' => $file,
117                    'date' => $date,
118                    );
119        }
120
121        // finally sort by time of last comment
122        usort($result, array('admin_plugin_discussion', '_threadCmp'));
123
124        return $result;
125    }
126
127    /**
128     * Callback for comparison of thread data.
129     *
130     * Used for sorting threads in descending order by date of last comment.
131     * If this date happens to be equal for the compared threads, page id
132     * is used as second comparison attribute.
133     */
134    function _threadCmp($a, $b) {
135        if ($a['date'] == $b['date']) {
136            return strcmp($a['id'], $b['id']);
137        }
138        return ($a['date'] < $b['date']) ? 1 : -1;
139    }
140
141    /**
142     * Outputs header, page ID and status of a discussion thread
143     */
144    function _threadHead($thread) {
145        $id = $thread['id'];
146
147        $labels = array(
148                0 => $this->getLang('off'),
149                1 => $this->getLang('open'),
150                2 => $this->getLang('closed')
151                );
152        $title = p_get_metadata($id, 'title');
153        if (!$title) $title = $id;
154        ptln('<h2 name="'.$id.'" id="'.$id.'">'.hsc($title).'</h2>', 6);
155        ptln('<form method="post" action="'.wl($id).'">', 6);
156        ptln('<div class="mediaright">', 8);
157        ptln('<input type="hidden" name="do" value="admin" />', 10);
158        ptln('<input type="hidden" name="page" value="discussion" />', 10);
159        ptln($this->getLang('status').': <select name="status" size="1">', 10);
160        foreach ($labels as $key => $label) {
161            $selected = (($key == $thread['status']) ? ' selected="selected"' : '');
162            ptln('<option value="'.$key.'"'.$selected.'>'.$label.'</option>', 12);
163        }
164        ptln('</select> ', 10);
165        ptln('<input type="submit" class="button" name="comment" value="'.$this->getLang('btn_change').'" class"button" title="'.$this->getLang('btn_change').'" />', 10);
166        ptln('</div>', 8);
167        ptln('</form>', 6);
168        ptln('<div class="level2">', 6);
169        ptln('<a href="'.wl($id).'" class="wikilink1">'.$id.'</a> ', 8);
170        return true;
171    }
172
173    /**
174     * Returns the full comments data for a given wiki page
175     */
176    function _getComments(&$thread) {
177        $id = $thread['id'];
178
179        if (!$thread['file']) $thread['file'] = metaFN($id, '.comments');
180        if (!@file_exists($thread['file'])) return false; // no discussion thread at all
181
182        $data = unserialize(io_readFile($thread['file'], false));
183
184        $thread['status'] = $data['status'];
185        $thread['number'] = $data['number'];
186        if (!$data['status']) return false;   // comments are turned off
187        if (!$data['comments']) return false; // no comments
188
189        $result = array();
190        foreach ($data['comments'] as $cid => $comment) {
191            $this->_addComment($cid, $data, $result);
192        }
193
194        if (empty($result)) return false;
195        else return $result;
196    }
197
198    /**
199     * Recursive function to add the comment hierarchy to the result
200     */
201    function _addComment($cid, &$data, &$result, $parent = '', $level = 1) {
202        if (!is_array($data['comments'][$cid])) return; // corrupt datatype
203        $comment = $data['comments'][$cid];
204        if ($comment['parent'] != $parent) return;      // answer to another comment
205
206        // okay, add the comment to the result
207        $comment['id'] = $cid;
208        $comment['level'] = $level;
209        $result[] = $comment;
210
211        // check answers to this comment
212        if (count($comment['replies'])) {
213            foreach ($comment['replies'] as $rid) {
214                $this->_addComment($rid, $data, $result, $cid, $level + 1);
215            }
216        }
217    }
218
219    /**
220     * Checkbox and info about a comment item
221     */
222    function _commentItem($comment) {
223        global $conf;
224
225        // prepare variables
226        if (is_array($comment['user'])) { // new format
227            $name    = $comment['user']['name'];
228            $mail    = $comment['user']['mail'];
229        } else {                         // old format
230            $name    = $comment['name'];
231            $mail    = $comment['mail'];
232        }
233        if (is_array($comment['date'])) { // new format
234            $created  = $comment['date']['created'];
235        } else {                         // old format
236            $created  = $comment['date'];
237        }
238        $abstract = preg_replace('/\s+?/', ' ', strip_tags($comment['xhtml']));
239        if (utf8_strlen($abstract) > 160) $abstract = utf8_substr($abstract, 0, 160).'...';
240
241        return '<input type="checkbox" name="cid['.$comment['id'].']" value="1" /> '.
242            $this->email($mail, $name, 'email').', '.strftime($conf['dformat'], $created).': '.
243            '<span class="abstract">'.$abstract.'</span>';
244    }
245
246    /**
247     * list item tag
248     */
249    function _li_comment($comment) {
250        $show = ($comment['show'] ? '' : ' hidden');
251        return '<li class="level'.$comment['level'].$show.'">';
252    }
253
254    /**
255     * Show buttons to bulk remove, hide or show comments
256     */
257    function _actionButtons($id) {
258        global $lang;
259
260        ptln('<div class="comment_buttons">', 12);
261        ptln('<input type="submit" name="comment" value="'.$this->getLang('btn_show').'" class="button" title="'.$this->getLang('btn_show').'" />', 14);
262        ptln('<input type="submit" name="comment" value="'.$this->getLang('btn_hide').'" class="button" title="'.$this->getLang('btn_hide').'" />', 14);
263        ptln('<input type="submit" name="comment" value="'.$lang['btn_delete'].'" class="button" title="'.$lang['btn_delete'].'" />', 14);
264        ptln('</div>', 12); // class="comment_buttons"
265        ptln('</div>', 10); // class="no"
266        ptln('</form>', 8);
267        ptln('</div>', 6); // class="level2"
268        return true;
269    }
270
271    /**
272     * Displays links to older newer discussions
273     */
274    function _browseDiscussionLinks($more, $first, $num) {
275        global $ID;
276
277        if (($first == 0) && (!$more)) return true;
278
279        $params = array('do' => 'admin', 'page' => 'discussion');
280        $last = $first+$num;
281        ptln('<div class="level1">', 8);
282        if ($first > 0) {
283            $first -= $num;
284            if ($first < 0) $first = 0;
285            $params['first'] = $first;
286            ptln('<p class="centeralign">', 8);
287            $ret = '<a href="'.wl($ID, $params).'" class="wikilink1">&lt;&lt; '.$this->getLang('newer').'</a>';
288            if ($more) {
289                $ret .= ' | ';
290            } else {
291                ptln($ret, 10);
292                ptln('</p>', 8);
293            }
294        } else if ($more) {
295            ptln('<p class="centeralign">', 8);
296        }
297        if ($more) {
298            $params['first'] = $last;
299            $ret .= '<a href="'.wl($ID, $params).'" class="wikilink1">'.$this->getLang('older').' &gt;&gt;</a>';
300            ptln($ret, 10);
301            ptln('</p>', 8);
302        }
303        ptln('</div>', 6); // class="level1"
304        return true;
305    }
306
307    /**
308     * Changes the status of a comment
309     */
310    function _changeStatus($new) {
311        global $ID;
312
313        // get discussion meta file name
314        $file = metaFN($ID, '.comments');
315        $data = unserialize(io_readFile($file, false));
316
317        $old = $data['status'];
318        if ($old == $new) return true;
319
320        // save the comment metadata file
321        $data['status'] = $new;
322        io_saveFile($file, serialize($data));
323
324        // look for ~~DISCUSSION~~ command in page file and change it accordingly
325        $patterns = array('~~DISCUSSION:off\2~~', '~~DISCUSSION\2~~', '~~DISCUSSION:closed\2~~');
326        $replace = $patterns[$new];
327        $wiki = preg_replace('/~~DISCUSSION([\w:]*)(\|?.*?)~~/', $replace, rawWiki($ID));
328        saveWikiText($ID, $wiki, $this->getLang('statuschanged'), true);
329
330        return true;
331    }
332}
333// vim:ts=4:sw=4:et:enc=utf-8:
334