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' => '2007-08-22', 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']; 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[$date] = array( 115 'id' => $id, 116 'file' => $file, 117 'date' => $date, 118 ); 119 } 120 121 // finally sort by time of last comment 122 krsort($result); 123 124 return $result; 125 } 126 127 /** 128 * Outputs header, page ID and status of a discussion thread 129 */ 130 function _threadHead($thread){ 131 $id = $thread['id']; 132 133 $labels = array( 134 0 => $this->getLang('off'), 135 1 => $this->getLang('open'), 136 2 => $this->getLang('closed') 137 ); 138 $title = p_get_metadata($id, 'title'); 139 if (!$title) $title = $id; 140 ptln('<h2 name="'.$id.'" id="'.$id.'">'.hsc($title).'</h2>', 6); 141 ptln('<form method="post" action="'.wl($id).'">', 6); 142 ptln('<div class="mediaright">', 8); 143 ptln('<input type="hidden" name="do" value="admin" />', 10); 144 ptln('<input type="hidden" name="page" value="discussion" />', 10); 145 ptln($this->getLang('status').': <select name="status" size="1">', 10); 146 foreach ($labels as $key => $label){ 147 $selected = (($key == $thread['status']) ? ' selected="selected"' : ''); 148 ptln('<option value="'.$key.'"'.$selected.'>'.$label.'</option>', 12); 149 } 150 ptln('</select> ', 10); 151 ptln('<input type="submit" name="comment" value="'.$this->getLang('btn_change').'" class"button" title="'.$this->getLang('btn_change').'" />', 10); 152 ptln('</div>', 8); 153 ptln('</form>', 6); 154 ptln('<div class="level2">', 6); 155 ptln('<a href="'.wl($id).'" class="wikilink1">'.$id.'</a> ', 8); 156 return true; 157 } 158 159 /** 160 * Returns the full comments data for a given wiki page 161 */ 162 function _getComments(&$thread){ 163 $id = $thread['id']; 164 165 if (!$thread['file']) $thread['file'] = metaFN($id, '.comments'); 166 if (!@file_exists($thread['file'])) return false; // no discussion thread at all 167 168 $data = unserialize(io_readFile($thread['file'], false)); 169 170 $thread['status'] = $data['status']; 171 $thread['number'] = $data['number']; 172 if (!$data['status']) return false; // comments are turned off 173 if (!$data['comments']) return false; // no comments 174 175 $result = array(); 176 foreach ($data['comments'] as $cid => $comment){ 177 $this->_addComment($cid, $data, $result); 178 } 179 180 if (empty($result)) return false; 181 else return $result; 182 } 183 184 /** 185 * Recursive function to add the comment hierarchy to the result 186 */ 187 function _addComment($cid, &$data, &$result, $parent = '', $level = 1){ 188 if (!is_array($data['comments'][$cid])) return; // corrupt datatype 189 $comment = $data['comments'][$cid]; 190 if ($comment['parent'] != $parent) return; // answer to another comment 191 192 // okay, add the comment to the result 193 $comment['id'] = $cid; 194 $comment['level'] = $level; 195 $result[] = $comment; 196 197 // check answers to this comment 198 if (count($comment['replies'])){ 199 foreach ($comment['replies'] as $rid){ 200 $this->_addComment($rid, $data, $result, $cid, $level + 1); 201 } 202 } 203 } 204 205 /** 206 * Checkbox and info about a comment item 207 */ 208 function _commentItem($comment){ 209 global $conf; 210 211 // prepare variables 212 if (is_array($comment['user'])){ // new format 213 $name = $comment['user']['name']; 214 $mail = $comment['user']['mail']; 215 } else { // old format 216 $name = $comment['name']; 217 $mail = $comment['mail']; 218 } 219 if (is_array($comment['date'])){ // new format 220 $created = $comment['date']['created']; 221 } else { // old format 222 $created = $comment['date']; 223 } 224 $abstract = preg_replace('/\s+?/', ' ', strip_tags($comment['xhtml'])); 225 if (utf8_strlen($abstract) > 160) $abstract = utf8_substr($abstract, 0, 160).'...'; 226 227 return '<input type="checkbox" name="cid['.$comment['id'].']" value="1" /> '. 228 $this->email($mail, $name, 'email').', '.date($conf['dformat'], $created).': '. 229 '<span class="abstract">'.$abstract.'</span>'; 230 } 231 232 /** 233 * list item tag 234 */ 235 function _li_comment($comment){ 236 $show = ($comment['show'] ? '' : ' hidden'); 237 return '<li class="level'.$comment['level'].$show.'">'; 238 } 239 240 /** 241 * Show buttons to bulk remove, hide or show comments 242 */ 243 function _actionButtons($id){ 244 global $lang; 245 246 ptln('<div class="comment_buttons">', 12); 247 ptln('<input type="submit" name="comment" value="'.$this->getLang('btn_show').'" class="button" title="'.$this->getLang('btn_show').'" />', 14); 248 ptln('<input type="submit" name="comment" value="'.$this->getLang('btn_hide').'" class="button" title="'.$this->getLang('btn_hide').'" />', 14); 249 ptln('<input type="submit" name="comment" value="'.$lang['btn_delete'].'" class="button" title="'.$lang['btn_delete'].'" />', 14); 250 ptln('</div>', 12); // class="comment_buttons" 251 ptln('</div>', 10); // class="no" 252 ptln('</form>', 8); 253 ptln('</div>', 6); // class="level2" 254 return true; 255 } 256 257 /** 258 * Displays links to older newer discussions 259 */ 260 function _browseDiscussionLinks($more, $first, $num){ 261 global $ID; 262 263 if (($first == 0) && (!$more)) return true; 264 265 $params = array('do' => 'admin', 'page' => 'discussion'); 266 $last = $first+$num; 267 ptln('<div class="level1">', 8); 268 if ($first > 0){ 269 $first -= $num; 270 if ($first < 0) $first = 0; 271 $params['first'] = $first; 272 ptln('<p class="centeralign">', 8); 273 $ret = '<a href="'.wl($ID, $params).'" class="wikilink1"><< '.$this->getLang('newer').'</a>'; 274 if ($more){ 275 $ret .= ' | '; 276 } else { 277 ptln($ret, 10); 278 ptln('</p>', 8); 279 } 280 } else if ($more){ 281 ptln('<p class="centeralign">', 8); 282 } 283 if ($more){ 284 $params['first'] = $last; 285 $ret .= '<a href="'.wl($ID, $params).'" class="wikilink1">'.$this->getLang('older').' >></a>'; 286 ptln($ret, 10); 287 ptln('</p>', 8); 288 } 289 ptln('</div>', 6); // class="level1" 290 return true; 291 } 292 293 /** 294 * Changes the status of a comment 295 */ 296 function _changeStatus($new){ 297 global $ID; 298 299 // get discussion meta file name 300 $file = metaFN($ID, '.comments'); 301 $data = unserialize(io_readFile($file, false)); 302 303 $old = $data['status']; 304 if ($old == $new) return true; 305 306 // save the comment metadata file 307 $data['status'] = $new; 308 io_saveFile($file, serialize($data)); 309 310 // look for ~~DISCUSSION~~ command in page file and change it accordingly 311 $patterns = array('~~DISCUSSION:off\2~~', '~~DISCUSSION\2~~', '~~DISCUSSION:closed\2~~'); 312 $replace = $patterns[$new]; 313 $wiki = preg_replace('/~~DISCUSSION([\w:]*)(\|?.*?)~~/', $replace, rawWiki($ID)); 314 saveWikiText($ID, $wiki, $this->getLang('statuschanged'), true); 315 316 return true; 317 } 318 319} 320 321//Setup VIM: ex: et ts=4 enc=utf-8 : 322