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