xref: /plugin/discussion/helper.php (revision 573e23a1826b1683fa668aede546befbffcfcb10)
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-02-28',
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){
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 (!$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/* ---------- Changelog function adapted for the Discussion Plugin ---------- */
181
182  /**
183   * Internal function used by $this->getComments()
184   *
185   * don't call directly
186   *
187   * @see getRecentComments()
188   * @author Andreas Gohr <andi@splitbrain.org>
189   * @author Ben Coburn <btcoburn@silicodon.net>
190   * @author Esther Brunner <wikidesign@gmail.com>
191   */
192  function _handleRecentComment($line, $ns){
193    static $seen  = array();         //caches seen pages and skip them
194    if (empty($line)) return false;  //skip empty lines
195
196    // split the line into parts
197    $recent = parseChangelogLine($line);
198    if ($recent === false) return false;
199
200    $cid     = $recent['extra'];
201    $fullcid = $recent['id'].'#'.$recent['extra'];
202
203    // skip seen ones
204    if (isset($seen[$fullcid])) return false;
205
206    // skip 'show comment' log entries
207    if ($recent['type'] === 'sc') return false;
208
209    // remember in seen to skip additional sights
210    $seen[$fullcid] = 1;
211
212    // check if it's a hidden page or comment
213    if (isHiddenPage($recent['id'])) return false;
214    if ($recent['type'] === 'hc') return false;
215
216    // filter namespace or id
217    if (($ns) && (strpos($recent['id'].':', $ns.':') !== 0)) return false;
218
219    // check ACL
220    $recent['perm'] = auth_quickaclcheck($recent['id']);
221    if ($recent['perm'] < AUTH_READ) return false;
222
223    // check existance
224    $recent['file'] = wikiFN($recent['id']);
225    $recent['exists'] = @file_exists($recent['file']);
226    if (!$recent['exists']) return false;
227    if ($recent['type'] === 'dc') return false;
228
229    // get discussion meta file name
230    $data = unserialize(io_readFile(metaFN($recent['id'], '.comments'), false));
231
232    // check if discussion is turned off
233    if ($data['status'] === 0) return false;
234
235    // okay, then add some additional info
236    if (is_array($data['comments'][$cid]['user']))
237      $recent['name'] = $data['comments'][$cid]['user']['name'];
238    else $recent['name'] = $data['comments'][$cid]['name'];
239    $recent['desc'] = strip_tags($data['comments'][$cid]['xhtml']);
240
241    return $recent;
242  }
243
244}
245
246//Setup VIM: ex: et ts=4 enc=utf-8 :
247