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