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