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