1<?php 2/** 3 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 4 * @author Esther Brunner <wikidesign@gmail.com> 5 */ 6 7/** 8 * Class helper_plugin_discussion 9 */ 10class helper_plugin_discussion extends DokuWiki_Plugin { 11 12 /** 13 * @return array 14 */ 15 public function getMethods() { 16 $result = array(); 17 $result[] = array( 18 'name' => 'th', 19 'desc' => 'returns the header of the comments column for pagelist', 20 'return' => array('header' => 'string'), 21 ); 22 $result[] = array( 23 'name' => 'td', 24 'desc' => 'returns the link to the discussion section with number of comments', 25 'params' => array( 26 'id' => 'string', 27 'number of comments (optional)' => 'integer'), 28 'return' => array('link' => 'string'), 29 ); 30 $result[] = array( 31 'name' => 'getThreads', 32 'desc' => 'returns pages with discussion sections, sorted by recent comments', 33 'params' => array( 34 'namespace' => 'string', 35 'number (optional)' => 'integer'), 36 'return' => array('pages' => 'array'), 37 ); 38 $result[] = array( 39 'name' => 'getComments', 40 'desc' => 'returns recently added or edited comments individually', 41 'params' => array( 42 'namespace' => 'string', 43 'number (optional)' => 'integer'), 44 'return' => array('pages' => 'array'), 45 ); 46 $result[] = array( 47 'name' => 'isDiscussionModerator', 48 'desc' => 'check if current user is member of moderator groups', 49 'params' => array(), 50 'return' => array('isModerator' => 'boolean') 51 ); 52 return $result; 53 } 54 55 /** 56 * Returns the column header for the Pagelist Plugin 57 * 58 * @return string 59 */ 60 public function th() { 61 return $this->getLang('discussion'); 62 } 63 64 /** 65 * Returns the link to the discussion section of a page 66 * 67 * @param string $id 68 * @param null|int $num 69 * @return string 70 */ 71 public function td($id, $num = null) { 72 $section = '#discussion__section'; 73 74 if (!isset($num)) { 75 $cfile = metaFN($id, '.comments'); 76 $comments = unserialize(io_readFile($cfile, false)); 77 78 $num = $comments['number']; 79 if ((!$comments['status']) || (($comments['status'] == 2) && (!$num))) return ''; 80 } 81 82 if ($num == 0) { 83 $comment = '0 '.$this->getLang('nocomments'); 84 } elseif ($num == 1) { 85 $comment = '1 '.$this->getLang('comment'); 86 } else { 87 $comment = $num.' '.$this->getLang('comments'); 88 } 89 90 return '<a href="'.wl($id).$section.'" class="wikilink1" title="'.$id.$section.'">'. 91 $comment.'</a>'; 92 } 93 94 /** 95 * Returns an array of pages with discussion sections, sorted by recent comments 96 * Note: also used for content by Feed Plugin 97 * 98 * @param string $ns 99 * @param null|int $num 100 * @param string|bool $skipEmpty 101 * @return array 102 */ 103 public function getThreads($ns, $num = null, $skipEmpty = false) { 104 global $conf; 105 106 require_once(DOKU_INC.'inc/search.php'); 107 108 $dir = $conf['datadir'].utf8_encodeFN(($ns ? '/'.str_replace(':', '/', $ns): '')); 109 110 // returns the list of pages in the given namespace and it's subspaces 111 $items = array(); 112 search($items, $dir, 'search_allpages', array()); 113 114 // add pages with comments to result 115 $result = array(); 116 foreach ($items as $item) { 117 $id = ($ns ? $ns.':' : '').$item['id']; 118 119 // some checks 120 $perm = auth_quickaclcheck($id); 121 if ($perm < AUTH_READ) continue; // skip if no permission 122 $file = metaFN($id, '.comments'); 123 if (!@file_exists($file)) continue; // skip if no comments file 124 $data = unserialize(io_readFile($file, false)); 125 $status = $data['status']; 126 $number = $data['number']; 127 128 if (!$status || (($status == 2) && (!$number))) continue; // skip if comments are off or closed without comments 129 if($skipEmpty == 'y' && $number == 0) continue; // skip if discussion is empty and flag is set 130 131 $date = filemtime($file); 132 $meta = p_get_metadata($id); 133 $result[$date.'_'.$id] = array( 134 'id' => $id, 135 'file' => $file, 136 'title' => $meta['title'], 137 'date' => $date, 138 'user' => $meta['creator'], 139 'desc' => $meta['description']['abstract'], 140 'num' => $number, 141 'comments' => $this->td($id, $number), 142 'status' => $status, 143 'perm' => $perm, 144 'exists' => true, 145 'anchor' => 'discussion__section', 146 ); 147 } 148 149 // finally sort by time of last comment 150 krsort($result); 151 152 if (is_numeric($num)) $result = array_slice($result, 0, $num); 153 154 return $result; 155 } 156 157 /** 158 * Returns an array of recently added comments to a given page or namespace 159 * Note: also used for content by Feed Plugin 160 * 161 * @param string $ns 162 * @param int|null $num 163 * @return array 164 */ 165 public function getComments($ns, $num = NULL) { 166 global $conf; 167 168 $first = $_REQUEST['first']; 169 if (!is_numeric($first)) $first = 0; 170 171 if ((!$num) || (!is_numeric($num))) $num = $conf['recent']; 172 173 $result = array(); 174 $count = 0; 175 176 if (!@file_exists($conf['metadir'].'/_comments.changes')) return $result; 177 178 // read all recent changes. (kept short) 179 $lines = file($conf['metadir'].'/_comments.changes'); 180 181 $seen = array(); //caches seen pages in order to skip them 182 // handle lines 183 $line_num = count($lines); 184 for ($i = ($line_num - 1); $i >= 0; $i--) { 185 $rec = $this->_handleRecentComment($lines[$i], $ns, $seen); 186 if ($rec !== false) { 187 if (--$first >= 0) continue; // skip first entries 188 $result[$rec['date']] = $rec; 189 $count++; 190 // break when we have enough entries 191 if ($count >= $num) break; 192 } 193 } 194 195 // finally sort by time of last comment 196 krsort($result); 197 198 return $result; 199 } 200 201 /* ---------- Changelog function adapted for the Discussion Plugin ---------- */ 202 203 /** 204 * Internal function used by $this->getComments() 205 * 206 * don't call directly 207 * 208 * @see getRecentComments() 209 * @author Andreas Gohr <andi@splitbrain.org> 210 * @author Ben Coburn <btcoburn@silicodon.net> 211 * @author Esther Brunner <wikidesign@gmail.com> 212 * 213 * @param string $line 214 * @param string $ns 215 * @param array $seen 216 * @return array|bool 217 */ 218 protected function _handleRecentComment($line, $ns, &$seen) { 219 if (empty($line)) return false; //skip empty lines 220 221 // split the line into parts 222 $recent = parseChangelogLine($line); 223 if ($recent === false) return false; 224 225 $cid = $recent['extra']; 226 $fullcid = $recent['id'].'#'.$recent['extra']; 227 228 // skip seen ones 229 if (isset($seen[$fullcid])) return false; 230 231 // skip 'show comment' log entries 232 if ($recent['type'] === 'sc') return false; 233 234 // remember in seen to skip additional sights 235 $seen[$fullcid] = 1; 236 237 // check if it's a hidden page or comment 238 if (isHiddenPage($recent['id'])) return false; 239 if ($recent['type'] === 'hc') return false; 240 241 // filter namespace or id 242 if (($ns) && (strpos($recent['id'].':', $ns.':') !== 0)) return false; 243 244 // check ACL 245 $recent['perm'] = auth_quickaclcheck($recent['id']); 246 if ($recent['perm'] < AUTH_READ) return false; 247 248 // check existance 249 $recent['file'] = wikiFN($recent['id']); 250 $recent['exists'] = @file_exists($recent['file']); 251 if (!$recent['exists']) return false; 252 if ($recent['type'] === 'dc') return false; 253 254 // get discussion meta file name 255 $data = unserialize(io_readFile(metaFN($recent['id'], '.comments'), false)); 256 257 // check if discussion is turned off 258 if ($data['status'] === 0) return false; 259 260 $parent_id = $cid; 261 // Check for the comment and all parents if they exist and are visible. 262 do { 263 $tcid = $parent_id; 264 265 // check if the comment still exists 266 if (!isset($data['comments'][$tcid])) return false; 267 // check if the comment is visible 268 if ($data['comments'][$tcid]['show'] != 1) return false; 269 270 $parent_id = $data['comments'][$tcid]['parent']; 271 } while ($parent_id && $parent_id != $tcid); 272 273 // okay, then add some additional info 274 if (is_array($data['comments'][$cid]['user'])) { 275 $recent['name'] = $data['comments'][$cid]['user']['name']; 276 } else { 277 $recent['name'] = $data['comments'][$cid]['name']; 278 } 279 $recent['desc'] = strip_tags($data['comments'][$cid]['xhtml']); 280 $recent['anchor'] = 'comment_'.$cid; 281 282 return $recent; 283 } 284 285 /** 286 * @return bool 287 */ 288 public function isDiscussionModerator() { 289 global $USERINFO; 290 $groups = trim($this->getConf('moderatorgroups')); 291 292 if(auth_ismanager()) return true; 293 // Check if user is member of the moderator groups 294 if(!empty($groups) && auth_isMember($groups, $_SERVER['REMOTE_USER'], (array)$USERINFO['grps'])) return true; 295 296 return false; 297 } 298} 299// vim:ts=4:sw=4:et:enc=utf-8: 300