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