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) { 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']; // skip if comments are off or closed without comments 105 if (!$status || (($status == 2) && (!$number))) continue; 106 107 $date = filemtime($file); 108 $meta = p_get_metadata($id); 109 $result[$date.'_'.$id] = array( 110 'id' => $id, 111 'file' => $file, 112 'title' => $meta['title'], 113 'date' => $date, 114 'user' => $meta['creator'], 115 'desc' => $meta['description']['abstract'], 116 'num' => $number, 117 'comments' => $this->td($id, $number), 118 'status' => $status, 119 'perm' => $perm, 120 'exists' => true, 121 'anchor' => 'discussion__section', 122 ); 123 } 124 125 // finally sort by time of last comment 126 krsort($result); 127 128 if (is_numeric($num)) $result = array_slice($result, 0, $num); 129 130 return $result; 131 } 132 133 /** 134 * Returns an array of recently added comments to a given page or namespace 135 */ 136 function getComments($ns, $num = NULL) { 137 global $conf; 138 139 $first = $_REQUEST['first']; 140 if (!is_numeric($first)) $first = 0; 141 142 if ((!$num) || (!is_numeric($num))) $num = $conf['recent']; 143 144 $result = array(); 145 $count = 0; 146 147 if (!@file_exists($conf['metadir'].'/_comments.changes')) return $result; 148 149 // read all recent changes. (kept short) 150 $lines = file($conf['metadir'].'/_comments.changes'); 151 152 $seen = array(); //caches seen pages in order to skip them 153 // handle lines 154 $line_num = count($lines); 155 for ($i = ($line_num - 1); $i >= 0; $i--) { 156 $rec = $this->_handleRecentComment($lines[$i], $ns, $seen); 157 if ($rec !== false) { 158 if (--$first >= 0) continue; // skip first entries 159 $result[$rec['date']] = $rec; 160 $count++; 161 // break when we have enough entries 162 if ($count >= $num) break; 163 } 164 } 165 166 // finally sort by time of last comment 167 krsort($result); 168 169 return $result; 170 } 171 172 /* ---------- Changelog function adapted for the Discussion Plugin ---------- */ 173 174 /** 175 * Internal function used by $this->getComments() 176 * 177 * don't call directly 178 * 179 * @see getRecentComments() 180 * @author Andreas Gohr <andi@splitbrain.org> 181 * @author Ben Coburn <btcoburn@silicodon.net> 182 * @author Esther Brunner <wikidesign@gmail.com> 183 */ 184 function _handleRecentComment($line, $ns, &$seen) { 185 if (empty($line)) return false; //skip empty lines 186 187 // split the line into parts 188 $recent = parseChangelogLine($line); 189 if ($recent === false) return false; 190 191 $cid = $recent['extra']; 192 $fullcid = $recent['id'].'#'.$recent['extra']; 193 194 // skip seen ones 195 if (isset($seen[$fullcid])) return false; 196 197 // skip 'show comment' log entries 198 if ($recent['type'] === 'sc') return false; 199 200 // remember in seen to skip additional sights 201 $seen[$fullcid] = 1; 202 203 // check if it's a hidden page or comment 204 if (isHiddenPage($recent['id'])) return false; 205 if ($recent['type'] === 'hc') return false; 206 207 // filter namespace or id 208 if (($ns) && (strpos($recent['id'].':', $ns.':') !== 0)) return false; 209 210 // check ACL 211 $recent['perm'] = auth_quickaclcheck($recent['id']); 212 if ($recent['perm'] < AUTH_READ) return false; 213 214 // check existance 215 $recent['file'] = wikiFN($recent['id']); 216 $recent['exists'] = @file_exists($recent['file']); 217 if (!$recent['exists']) return false; 218 if ($recent['type'] === 'dc') return false; 219 220 // get discussion meta file name 221 $data = unserialize(io_readFile(metaFN($recent['id'], '.comments'), false)); 222 223 // check if discussion is turned off 224 if ($data['status'] === 0) return false; 225 226 // check if the comment still exists 227 if (!isset($data['comments'][$cid])) return false; 228 229 // okay, then add some additional info 230 if (is_array($data['comments'][$cid]['user'])) 231 $recent['name'] = $data['comments'][$cid]['user']['name']; 232 else $recent['name'] = $data['comments'][$cid]['name']; 233 $recent['desc'] = strip_tags($data['comments'][$cid]['xhtml']); 234 $recent['anchor'] = 'comment_'.$cid; 235 236 return $recent; 237 } 238} 239// vim:ts=4:sw=4:et:enc=utf-8: 240