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 * @return array 92 */ 93 function _getThreads() { 94 global $conf; 95 96 require_once(DOKU_INC.'inc/search.php'); 97 98 // returns the list of pages in the given namespace and it's subspaces 99 $items = array(); 100 search($items, $conf['datadir'], 'search_allpages', array()); 101 102 // add pages with comments to result 103 $result = array(); 104 foreach ($items as $item) { 105 $id = $item['id']; 106 107 // some checks 108 $file = metaFN($id, '.comments'); 109 if (!@file_exists($file)) continue; // skip if no comments file 110 111 $date = filemtime($file); 112 $result[] = array( 113 'id' => $id, 114 'file' => $file, 115 'date' => $date, 116 ); 117 } 118 119 // finally sort by time of last comment 120 usort($result, array('admin_plugin_discussion', '_threadCmp')); 121 122 return $result; 123 } 124 125 /** 126 * Callback for comparison of thread data. 127 * 128 * Used for sorting threads in descending order by date of last comment. 129 * If this date happens to be equal for the compared threads, page id 130 * is used as second comparison attribute. 131 * 132 * @param array $a 133 * @param array $b 134 * @return int 135 */ 136 function _threadCmp($a, $b) { 137 if ($a['date'] == $b['date']) { 138 return strcmp($a['id'], $b['id']); 139 } 140 return ($a['date'] < $b['date']) ? 1 : -1; 141 } 142 143 /** 144 * Outputs header, page ID and status of a discussion thread 145 * 146 * @param array $thread 147 * @return bool 148 */ 149 function _threadHead($thread) { 150 $id = $thread['id']; 151 152 $labels = array( 153 0 => $this->getLang('off'), 154 1 => $this->getLang('open'), 155 2 => $this->getLang('closed') 156 ); 157 $title = p_get_metadata($id, 'title'); 158 if (!$title) $title = $id; 159 ptln('<h2 name="'.$id.'" id="'.$id.'">'.hsc($title).'</h2>', 6); 160 ptln('<form method="post" action="'.wl($id).'">', 6); 161 ptln('<div class="mediaright">', 8); 162 ptln('<input type="hidden" name="do" value="admin" />', 10); 163 ptln('<input type="hidden" name="page" value="discussion" />', 10); 164 ptln($this->getLang('status').': <select name="status" size="1">', 10); 165 foreach ($labels as $key => $label) { 166 $selected = (($key == $thread['status']) ? ' selected="selected"' : ''); 167 ptln('<option value="'.$key.'"'.$selected.'>'.$label.'</option>', 12); 168 } 169 ptln('</select> ', 10); 170 ptln('<input type="submit" class="button" name="comment" value="'.$this->getLang('btn_change').'" class"button" title="'.$this->getLang('btn_change').'" />', 10); 171 ptln('</div>', 8); 172 ptln('</form>', 6); 173 ptln('<div class="level2">', 6); 174 ptln('<a href="'.wl($id).'" class="wikilink1">'.$id.'</a> ', 8); 175 return true; 176 } 177 178 /** 179 * Returns the full comments data for a given wiki page 180 * 181 * @param array $thread 182 * @return array|bool 183 */ 184 function _getComments(&$thread) { 185 $id = $thread['id']; 186 187 if (!$thread['file']) $thread['file'] = metaFN($id, '.comments'); 188 if (!@file_exists($thread['file'])) return false; // no discussion thread at all 189 190 $data = unserialize(io_readFile($thread['file'], false)); 191 192 $thread['status'] = $data['status']; 193 $thread['number'] = $data['number']; 194 if (!$data['status']) return false; // comments are turned off 195 if (!$data['comments']) return false; // no comments 196 197 $result = array(); 198 foreach ($data['comments'] as $cid => $comment) { 199 $this->_addComment($cid, $data, $result); 200 } 201 202 if (empty($result)) return false; 203 else return $result; 204 } 205 206 /** 207 * Recursive function to add the comment hierarchy to the result 208 * 209 * @param string $cid 210 * @param array $data 211 * @param array $result 212 * @param string $parent 213 * @param int $level 214 */ 215 function _addComment($cid, &$data, &$result, $parent = '', $level = 1) { 216 if (!is_array($data['comments'][$cid])) return; // corrupt datatype 217 $comment = $data['comments'][$cid]; 218 if ($comment['parent'] != $parent) return; // answer to another comment 219 220 // okay, add the comment to the result 221 $comment['id'] = $cid; 222 $comment['level'] = $level; 223 $result[] = $comment; 224 225 // check answers to this comment 226 if (count($comment['replies'])) { 227 foreach ($comment['replies'] as $rid) { 228 $this->_addComment($rid, $data, $result, $cid, $level + 1); 229 } 230 } 231 } 232 233 /** 234 * Checkbox and info about a comment item 235 * 236 * @param array $comment 237 * @return string 238 */ 239 function _commentItem($comment) { 240 global $conf; 241 242 // prepare variables 243 if (is_array($comment['user'])) { // new format 244 $name = $comment['user']['name']; 245 $mail = $comment['user']['mail']; 246 } else { // old format 247 $name = $comment['name']; 248 $mail = $comment['mail']; 249 } 250 if (is_array($comment['date'])) { // new format 251 $created = $comment['date']['created']; 252 } else { // old format 253 $created = $comment['date']; 254 } 255 $abstract = preg_replace('/\s+?/', ' ', strip_tags($comment['xhtml'])); 256 if (utf8_strlen($abstract) > 160) $abstract = utf8_substr($abstract, 0, 160).'...'; 257 258 return '<input type="checkbox" name="cid['.$comment['id'].']" value="1" /> '. 259 $this->email($mail, $name, 'email').', '.strftime($conf['dformat'], $created).': '. 260 '<span class="abstract">'.$abstract.'</span>'; 261 } 262 263 /** 264 * list item tag 265 * 266 * @param array $comment 267 * @return string 268 */ 269 function _li_comment($comment) { 270 $show = ($comment['show'] ? '' : ' hidden'); 271 return '<li class="level'.$comment['level'].$show.'">'; 272 } 273 274 /** 275 * Show buttons to bulk remove, hide or show comments 276 * 277 * @param string $id 278 * @return bool 279 */ 280 function _actionButtons($id) { 281 global $lang; 282 283 ptln('<div class="comment_buttons">', 12); 284 ptln('<input type="submit" name="comment" value="'.$this->getLang('btn_show').'" class="button" title="'.$this->getLang('btn_show').'" />', 14); 285 ptln('<input type="submit" name="comment" value="'.$this->getLang('btn_hide').'" class="button" title="'.$this->getLang('btn_hide').'" />', 14); 286 ptln('<input type="submit" name="comment" value="'.$lang['btn_delete'].'" class="button" title="'.$lang['btn_delete'].'" />', 14); 287 ptln('</div>', 12); // class="comment_buttons" 288 ptln('</div>', 10); // class="no" 289 ptln('</form>', 8); 290 ptln('</div>', 6); // class="level2" 291 return true; 292 } 293 294 /** 295 * Displays links to older newer discussions 296 * 297 * @param bool $more 298 * @param int $first 299 * @param int $num 300 * @return bool 301 */ 302 function _browseDiscussionLinks($more, $first, $num) { 303 global $ID; 304 305 if (($first == 0) && (!$more)) return true; 306 307 $params = array('do' => 'admin', 'page' => 'discussion'); 308 $last = $first+$num; 309 ptln('<div class="level1">', 8); 310 $ret = ''; 311 if ($first > 0) { 312 $first -= $num; 313 if ($first < 0) $first = 0; 314 $params['first'] = $first; 315 ptln('<p class="centeralign">', 8); 316 $ret = '<a href="'.wl($ID, $params).'" class="wikilink1"><< '.$this->getLang('newer').'</a>'; 317 if ($more) { 318 $ret .= ' | '; 319 } else { 320 ptln($ret, 10); 321 ptln('</p>', 8); 322 } 323 } else if ($more) { 324 ptln('<p class="centeralign">', 8); 325 } 326 if ($more) { 327 $params['first'] = $last; 328 $ret .= '<a href="'.wl($ID, $params).'" class="wikilink1">'.$this->getLang('older').' >></a>'; 329 ptln($ret, 10); 330 ptln('</p>', 8); 331 } 332 ptln('</div>', 6); // class="level1" 333 return true; 334 } 335 336 /** 337 * Changes the status of a comment 338 * 339 * @param string $new 340 * @return bool 341 */ 342 function _changeStatus($new) { 343 global $ID; 344 345 // get discussion meta file name 346 $file = metaFN($ID, '.comments'); 347 $data = unserialize(io_readFile($file, false)); 348 349 $old = $data['status']; 350 if ($old == $new) return true; 351 352 // save the comment metadata file 353 $data['status'] = $new; 354 io_saveFile($file, serialize($data)); 355 356 // look for ~~DISCUSSION~~ command in page file and change it accordingly 357 $patterns = array('~~DISCUSSION:off\2~~', '~~DISCUSSION\2~~', '~~DISCUSSION:closed\2~~'); 358 $replace = $patterns[$new]; 359 $wiki = preg_replace('/~~DISCUSSION([\w:]*)(\|?.*?)~~/', $replace, rawWiki($ID)); 360 saveWikiText($ID, $wiki, $this->getLang('statuschanged'), true); 361 362 return true; 363 } 364} 365// vim:ts=4:sw=4:et:enc=utf-8: 366