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