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