xref: /plugin/discussion/admin.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_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11
12require_once(DOKU_PLUGIN.'admin.php');
13
14class admin_plugin_discussion extends DokuWiki_Admin_Plugin {
15
16  function getInfo(){
17    return array(
18      'author' => 'Esther Brunner',
19      'email'  => 'wikidesign@gmail.com',
20      'date'   => '2007-03-01',
21      'name'   => 'Discussion Plugin (admin component)',
22      'desc'   => 'Moderate discussions',
23      'url'    => 'http://www.wikidesign.ch/en/plugin/discussion/start',
24    );
25  }
26
27  function getMenuSort(){ return 200; }
28  function forAdminOnly(){ return false; }
29
30  function handle(){
31    global $lang;
32
33    $cid = $_REQUEST['cid'];
34    if (is_array($cid)) $cid = array_keys($cid);
35
36    $action =& plugin_load('action', 'discussion');
37    if (!$action) return; // couldn't load action plugin component
38
39    switch ($_REQUEST['comment']){
40      case $lang['btn_delete']:
41        $action->_save($cid, '');
42        break;
43
44      case $this->getLang('btn_show'):
45        $action->_save($cid, '', 'show');
46        break;
47
48      case $this->getLang('btn_hide'):
49        $action->_save($cid, '', 'hide');
50        break;
51
52      case $this->getLang('btn_change'):
53        $new = $_REQUEST['status'];
54        $this->_changeStatus($new);
55        break;
56    }
57  }
58
59  function html(){
60    global $conf;
61
62    $first = $_REQUEST['first'];
63    if (!is_numeric($first)) $first = 0;
64    $num = $conf['recent'];
65
66    ptln('<h1>'.$this->getLang('menu').'</h1>');
67
68    $threads = $this->_getThreads();
69
70    // slice the needed chunk of discussion pages
71    $more = ((count($threads) > ($first + $num)) ? true : false);
72    $threads = array_slice($threads, $first, $num);
73
74    foreach ($threads as $thread){
75      $comments = $this->_getComments($thread);
76      $this->_threadHead($thread);
77      if ($comments === false){
78        ptln('</div>'); // class="level2"
79        continue;
80      }
81
82      ptln('<form method="post" action="'.wl($thread['id']).'">', 2);
83      ptln('<div class="no">', 4);
84      ptln('<input type="hidden" name="do" value="admin" />', 6);
85      ptln('<input type="hidden" name="page" value="discussion" />', 6);
86      echo html_buildlist($comments, 'admin_discussion', array($this, '_commentItem'), array($this, '_li_comment'));
87      $this->_actionButtons($thread['id']);
88    }
89    $this->_browseDiscussionLinks($more, $first, $num);
90  }
91
92  /**
93   * Returns an array of pages with discussion sections, sorted by recent comments
94   */
95  function _getThreads(){
96    global $conf;
97
98    require_once(DOKU_INC.'inc/search.php');
99
100    // returns the list of pages in the given namespace and it's subspaces
101    $items = array();
102    search($items, $conf['datadir'], 'search_allpages', '');
103
104    // add pages with comments to result
105    $result = array();
106    foreach ($items as $item){
107      $id = $item['id'];
108
109      // some checks
110      $file = metaFN($id, '.comments');
111      if (!@file_exists($file)) continue; // skip if no comments file
112
113      $date = filemtime($file);
114      $result[$date] = array(
115        'id'   => $id,
116        'file' => $file,
117        'date' => $date,
118      );
119    }
120
121    // finally sort by time of last comment
122    krsort($result);
123
124    return $result;
125  }
126
127  /**
128   * Outputs header, page ID and status of a discussion thread
129   */
130  function _threadHead($thread){
131    $id = $thread['id'];
132
133    $labels = array(
134      0 => $this->getLang('off'),
135      1 => $this->getLang('open'),
136      2 => $this->getLang('closed')
137    );
138    ptln('<h2 name="'.$id.'" id="'.$id.'">'.hsc(p_get_metadata($id, 'title')).'</h2>');
139    ptln('<form method="post" action="'.wl($id).'">');
140    ptln('<div class="mediaright">', 2);
141    ptln('<input type="hidden" name="do" value="admin" />', 4);
142    ptln('<input type="hidden" name="page" value="discussion" />', 4);
143    ptln($this->getLang('status').': <select name="status" size="1">', 4);
144    foreach ($labels as $key => $label){
145      $selected = (($key == $thread['status']) ? ' selected="selected"' : '');
146      ptln('<option value="'.$key.'"'.$selected.'>'.$label.'</option>', 6);
147    }
148    ptln('</select> ', 4);
149    ptln('<input type="submit" value="'.$this->getLang('btn_change').'" />', 4);
150    ptln('</div>', 2);
151    ptln('</form>');
152    ptln('<div class="level2">');
153    ptln('<a href="'.wl($id).'" class="wikilink1">'.$id.'</a> ', 2);
154    return true;
155  }
156
157  /**
158   * Returns the full comments data for a given wiki page
159   */
160  function _getComments(&$thread){
161    $id = $thread['id'];
162
163    if (!$thread['file']) $thread['file'] = metaFN($id, '.comments');
164    if (!@file_exists($thread['file'])) return false; // no discussion thread at all
165
166    $data = unserialize(io_readFile($thread['file'], false));
167
168    $thread['status'] = $data['status'];
169    $thread['number'] = $data['number'];
170    if (!$data['status']) return false;               // comments are turned off
171    if (!isset($data['comments']) || !$data['number']) return false; // no comments
172
173    $result = array();
174    foreach ($data['comments'] as $cid => $comment){
175      $this->_addComment($cid, $data, $result);
176    }
177
178    return $result;
179  }
180
181  /**
182   * Recursive function to add the comment hierarchy to the result
183   */
184  function _addComment($cid, &$data, &$result, $parent = '', $level = 1){
185    if (!is_array($data['comments'][$cid])) return; // corrupt datatype
186    $comment = $data['comments'][$cid];
187    if ($comment['parent'] != $parent) return;      // answer to another comment
188
189    // okay, add the comment to the result
190    $comment['id'] = $cid;
191    $comment['level'] = $level;
192    $result[] = $comment;
193
194    // check answers to this comment
195    if (count($comment['replies'])){
196      foreach ($comment['replies'] as $rid){
197        $this->_addComment($rid, $data, $result, $cid, $level + 1);
198      }
199    }
200  }
201
202  /**
203   * Checkbox and info about a comment item
204   */
205  function _commentItem($comment){
206    global $conf;
207
208    // prepare variables
209    if (is_array($comment['user'])){ // new format
210      $name    = $comment['user']['name'];
211      $mail    = $comment['user']['mail'];
212    } else {                         // old format
213      $name    = $comment['name'];
214      $mail    = $comment['mail'];
215    }
216    if (is_array($comment['date'])){ // new format
217      $created  = $comment['date']['created'];
218    } else {                         // old format
219      $created  = $comment['date'];
220    }
221    $abstract = preg_replace('/\s+?/', ' ', strip_tags($comment['xhtml']));
222    if (utf8_strlen($abstract) > 160) $abstract = utf8_substr($abstract, 0, 160).'...';
223
224    return '<input type="checkbox" name="cid['.$comment['id'].']" value="1" /> '.
225      $this->email($mail, $name, 'email').', '.date($conf['dformat'], $created).': '.
226      '<span class="abstract">'.$abstract.'</span>';
227  }
228
229  /**
230   * list item tag
231   */
232  function _li_comment($comment){
233    $show = ($comment['show'] ? '' : ' hidden');
234    return '<li class="level'.$comment['level'].$show.'">';
235  }
236
237  /**
238   * Show buttons to bulk remove, hide or show comments
239   */
240  function _actionButtons($id){
241    global $lang;
242
243    ptln('<div class="comment_buttons">', 6);
244    ptln('<input type="submit" name="comment" value="'.$this->getLang('btn_show').'" class="button" title="'.$this->getLang('btn_show').'" />', 8);
245    ptln('<input type="submit" name="comment" value="'.$this->getLang('btn_hide').'" class="button" title="'.$this->getLang('btn_hide').'" />', 8);
246    ptln('<input type="submit" name="comment" value="'.$lang['btn_delete'].'" class="button" title="'.$lang['btn_delete'].'" />', 8);
247    ptln('</div>', 6); // class="comment_buttons"
248    ptln('</div>', 4); // class="no"
249    ptln('</form>', 2);
250    ptln('</div>'); // class="level2"
251    return true;
252  }
253
254  /**
255   * Displays links to older newer discussions
256   */
257  function _browseDiscussionLinks($more, $first, $num){
258    global $ID;
259
260    $params = array('do' => 'admin', 'page' => 'discussion');
261    $last = $first+$num;
262    if ($first > 0){
263      $first -= $num;
264      if ($first < 0) $first = 0;
265      $params['first'] = $first;
266      ptln('<p class="centeralign">', 2);
267      $ret = '<a href="'.wl($ID, $params).'" class="wikilink1">&lt;&lt; '.$this->getLang('newer').'</a>';
268      if ($more){
269        $ret .= ' | ';
270      } else {
271        ptln($ret, 4);
272        ptln('</p>', 2);
273      }
274    } else if ($more){
275      ptln('<p class="centeralign">', 2);
276    }
277    if ($more){
278      $params['first'] = $last;
279      $ret .= '<a href="'.wl($ID, $params).'" class="wikilink1">'.$this->getLang('older').' &gt;&gt;</a>';
280      ptln($ret, 4);
281      ptln('</p>', 2);
282    }
283    return true;
284  }
285
286  /**
287   * Changes the status of a comment
288   */
289  function _changeStatus($new){
290    global $ID;
291
292    // get discussion meta file name
293    $file = metaFN($ID, '.comments');
294    $data = unserialize(io_readFile($file, false));
295
296    $old = $data['status'];
297    if ($old == $new) return true;
298
299    // save the comment metadata file
300    $data['status'] = $new;
301    io_saveFile($file, serialize($data));
302
303    // look for ~~DISCUSSION~~ command in page file and change it accordingly
304    $patterns = array('~~DISCUSSION:off~~', '~~DISCUSSION~~', '~~DISCUSSION:closed~~');
305    $replace = $patterns[$new];
306    $wiki = preg_replace('/~~DISCUSSION(?:|:off|:closed)~~/', $replace, rawWiki($ID));
307    saveWikiText($ID, $wiki, $this->getLang('statuschanged'), true);
308
309    return true;
310  }
311
312    /**
313     * function by iDo
314     */
315    function _html() {
316    	require_once(DOKU_PLUGIN.'action.php');
317		$actionDiscussion= new action_plugin_discussion();
318
319		global $conf;
320		global $INFO;
321		global $ID;
322		global $ADMDISCUSSION;
323
324		$oID=$ID;
325		$ADMDISCUSSION['page']="adm";
326		//Execute action for page
327		if (isset($_REQUEST['comment'])) {
328			if ($_REQUEST['comment']!='edit') {
329
330				if (($_REQUEST['comment']=='add') && (isset($_REQUEST['cid']))) {
331
332				} else {
333					$obj=new unusedclass();
334					$actionDiscussion->comments($obj, null);
335				}
336			}
337		}
338
339		$chem=DOKU_INC.$conf['savedir']."/meta/";
340		$arr=$this->globr($chem,"*.comments");
341		$com =array();
342		foreach ($arr as $v) {
343			$ap=unserialize(io_readFile($v, false));
344			if (isset($ap['comments'])){
345				$ID=substr(str_replace(array($chem,".comments",'/'),array("","",':'),$v),1);
346				$ADMDISCUSSION['page']=' : <a href="'.wl($ID,'').'">'.str_replace("/doku.php/","",wl($ID,'')).'</a>';
347
348				if ((isset($_REQUEST['comment'])) && ($_REQUEST['comment']=='edit'))
349					$actionDiscussion->_show(NULL, $_REQUEST['cid']);
350				else
351					$actionDiscussion->_show((($oID==$ID)?@$_REQUEST['cid']:null));
352
353			}
354		}
355		$ID = $oID;
356		$ADMDISCUSSION['breakaction']=true;
357    }
358
359	/**
360	 * Recursive version of glob
361	 *
362	 * @return array containing all pattern-matched files.
363	 *
364	 * @param string $sDir      Directory to start with.
365	 * @param string $sPattern  Pattern to glob for.
366	 * @param int $nFlags      Flags sent to glob.
367	 */
368	function _globr($sDir, $sPattern, $nFlags = NULL) {
369	  $sDir = escapeshellcmd($sDir);
370	  // Get the list of all matching files currently in the
371	  // directory.
372	  $aFiles = glob("$sDir/$sPattern", $nFlags);
373	  // Then get a list of all directories in this directory, and
374	  // run ourselves on the resulting array.  This is the
375	  // recursion step, which will not execute if there are no
376	  // directories.
377	  foreach (glob("$sDir/*", GLOB_ONLYDIR) as $sSubDir)  {
378	   $aSubFiles = $this->globr($sSubDir, $sPattern, $nFlags);
379	   $aFiles = array_merge($aFiles, $aSubFiles);
380	  }
381	  // The array we return contains the files we found, and the
382	  // files all of our children found.
383	  return $aFiles;
384	}
385
386}
387
388//Setup VIM: ex: et ts=4 enc=utf-8 :