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