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) { 159 $title = $id; 160 } 161 ptln('<h2 name="'.$id.'" id="'.$id.'">'.hsc($title).'</h2>', 6); 162 ptln('<form method="post" action="'.wl($id).'">', 6); 163 ptln('<div class="mediaright">', 8); 164 ptln('<input type="hidden" name="do" value="admin" />', 10); 165 ptln('<input type="hidden" name="page" value="discussion" />', 10); 166 ptln($this->getLang('status').': <select name="status" size="1">', 10); 167 foreach ($labels as $key => $label) { 168 $selected = (($key == $thread['status']) ? ' selected="selected"' : ''); 169 ptln('<option value="'.$key.'"'.$selected.'>'.$label.'</option>', 12); 170 } 171 ptln('</select> ', 10); 172 ptln('<input type="submit" class="button" name="comment" value="'.$this->getLang('btn_change').'" class"button" title="'.$this->getLang('btn_change').'" />', 10); 173 ptln('</div>', 8); 174 ptln('</form>', 6); 175 ptln('<div class="level2">', 6); 176 ptln('<a href="'.wl($id).'" class="wikilink1">'.$id.'</a> ', 8); 177 return true; 178 } 179 180 /** 181 * Returns the full comments data for a given wiki page 182 * 183 * @param array $thread 184 * @return array|bool 185 */ 186 function _getComments(&$thread) { 187 $id = $thread['id']; 188 189 if (!$thread['file']) { 190 $thread['file'] = metaFN($id, '.comments'); 191 } 192 if (!@file_exists($thread['file'])) return false; // no discussion thread at all 193 194 $data = unserialize(io_readFile($thread['file'], false)); 195 196 $thread['status'] = $data['status']; 197 $thread['number'] = $data['number']; 198 if (!$data['status']) return false; // comments are turned off 199 if (!$data['comments']) return false; // no comments 200 201 $result = array(); 202 foreach ($data['comments'] as $cid => $comment) { 203 $this->_addComment($cid, $data, $result); 204 } 205 206 if (empty($result)) { 207 return false; 208 } else { 209 return $result; 210 } 211 } 212 213 /** 214 * Recursive function to add the comment hierarchy to the result 215 * 216 * @param string $cid 217 * @param array $data 218 * @param array $result 219 * @param string $parent 220 * @param int $level 221 */ 222 function _addComment($cid, &$data, &$result, $parent = '', $level = 1) { 223 if (!is_array($data['comments'][$cid])) return; // corrupt datatype 224 $comment = $data['comments'][$cid]; 225 if ($comment['parent'] != $parent) return; // answer to another comment 226 227 // okay, add the comment to the result 228 $comment['id'] = $cid; 229 $comment['level'] = $level; 230 $result[] = $comment; 231 232 // check answers to this comment 233 if (count($comment['replies'])) { 234 foreach ($comment['replies'] as $rid) { 235 $this->_addComment($rid, $data, $result, $cid, $level + 1); 236 } 237 } 238 } 239 240 /** 241 * Checkbox and info about a comment item 242 * 243 * @param array $comment 244 * @return string 245 */ 246 function _commentItem($comment) { 247 global $conf; 248 249 // prepare variables 250 if (is_array($comment['user'])) { // new format 251 $name = $comment['user']['name']; 252 $mail = $comment['user']['mail']; 253 } else { // old format 254 $name = $comment['name']; 255 $mail = $comment['mail']; 256 } 257 if (is_array($comment['date'])) { // new format 258 $created = $comment['date']['created']; 259 } else { // old format 260 $created = $comment['date']; 261 } 262 $abstract = preg_replace('/\s+?/', ' ', strip_tags($comment['xhtml'])); 263 if (utf8_strlen($abstract) > 160) { 264 $abstract = utf8_substr($abstract, 0, 160).'...'; 265 } 266 267 return '<input type="checkbox" name="cid['.$comment['id'].']" value="1" /> '. 268 $this->email($mail, $name, 'email').', '.strftime($conf['dformat'], $created).': '. 269 '<span class="abstract">'.$abstract.'</span>'; 270 } 271 272 /** 273 * list item tag 274 * 275 * @param array $comment 276 * @return string 277 */ 278 function _li_comment($comment) { 279 $show = ($comment['show'] ? '' : ' hidden'); 280 return '<li class="level'.$comment['level'].$show.'">'; 281 } 282 283 /** 284 * Show buttons to bulk remove, hide or show comments 285 * 286 * @param string $id 287 * @return bool 288 */ 289 function _actionButtons($id) { 290 global $lang; 291 292 ptln('<div class="comment_buttons">', 12); 293 ptln('<input type="submit" name="comment" value="'.$this->getLang('btn_show').'" class="button" title="'.$this->getLang('btn_show').'" />', 14); 294 ptln('<input type="submit" name="comment" value="'.$this->getLang('btn_hide').'" class="button" title="'.$this->getLang('btn_hide').'" />', 14); 295 ptln('<input type="submit" name="comment" value="'.$lang['btn_delete'].'" class="button" title="'.$lang['btn_delete'].'" />', 14); 296 ptln('</div>', 12); // class="comment_buttons" 297 ptln('</div>', 10); // class="no" 298 ptln('</form>', 8); 299 ptln('</div>', 6); // class="level2" 300 return true; 301 } 302 303 /** 304 * Displays links to older newer discussions 305 * 306 * @param bool $more 307 * @param int $first 308 * @param int $num 309 * @return bool 310 */ 311 function _browseDiscussionLinks($more, $first, $num) { 312 global $ID; 313 314 if (($first == 0) && (!$more)) return true; 315 316 $params = array('do' => 'admin', 'page' => 'discussion'); 317 $last = $first+$num; 318 ptln('<div class="level1">', 8); 319 $ret = ''; 320 if ($first > 0) { 321 $first -= $num; 322 if ($first < 0) { 323 $first = 0; 324 } 325 $params['first'] = $first; 326 ptln('<p class="centeralign">', 8); 327 $ret = '<a href="'.wl($ID, $params).'" class="wikilink1"><< '.$this->getLang('newer').'</a>'; 328 if ($more) { 329 $ret .= ' | '; 330 } else { 331 ptln($ret, 10); 332 ptln('</p>', 8); 333 } 334 } else if ($more) { 335 ptln('<p class="centeralign">', 8); 336 } 337 if ($more) { 338 $params['first'] = $last; 339 $ret .= '<a href="'.wl($ID, $params).'" class="wikilink1">'.$this->getLang('older').' >></a>'; 340 ptln($ret, 10); 341 ptln('</p>', 8); 342 } 343 ptln('</div>', 6); // class="level1" 344 return true; 345 } 346 347 /** 348 * Changes the status of a comment 349 * 350 * @param $new 351 * @return bool 352 */ 353 function _changeStatus($new) { 354 global $ID; 355 356 // get discussion meta file name 357 $file = metaFN($ID, '.comments'); 358 $data = unserialize(io_readFile($file, false)); 359 360 $old = $data['status']; 361 if ($old == $new) return true; 362 363 // save the comment metadata file 364 $data['status'] = $new; 365 io_saveFile($file, serialize($data)); 366 367 // look for ~~DISCUSSION~~ command in page file and change it accordingly 368 $patterns = array('~~DISCUSSION:off\2~~', '~~DISCUSSION\2~~', '~~DISCUSSION:closed\2~~'); 369 $replace = $patterns[$new]; 370 $wiki = preg_replace('/~~DISCUSSION([\w:]*)(\|?.*?)~~/', $replace, rawWiki($ID)); 371 saveWikiText($ID, $wiki, $this->getLang('statuschanged'), true); 372 373 return true; 374 } 375} 376// vim:ts=4:sw=4:et:enc=utf-8: 377