*/ // must be run within Dokuwiki if (!defined('DOKU_INC')) die(); if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'admin.php'); class admin_plugin_discussion extends DokuWiki_Admin_Plugin { function getInfo(){ return array( 'author' => 'Esther Brunner', 'email' => 'wikidesign@gmail.com', 'date' => '2007-03-01', 'name' => 'Discussion Plugin (admin component)', 'desc' => 'Moderate discussions', 'url' => 'http://www.wikidesign.ch/en/plugin/discussion/start', ); } function getMenuSort(){ return 200; } function forAdminOnly(){ return false; } function handle(){ global $lang; $cid = $_REQUEST['cid']; if (is_array($cid)) $cid = array_keys($cid); $action =& plugin_load('action', 'discussion'); if (!$action) return; // couldn't load action plugin component switch ($_REQUEST['comment']){ case $lang['btn_delete']: $action->_save($cid, ''); break; case $this->getLang('btn_show'): $action->_save($cid, '', 'show'); break; case $this->getLang('btn_hide'): $action->_save($cid, '', 'hide'); break; case $this->getLang('btn_change'): $new = $_REQUEST['status']; $this->_changeStatus($new); break; } } function html(){ global $conf; $first = $_REQUEST['first']; if (!is_numeric($first)) $first = 0; $num = $conf['recent']; ptln('

'.$this->getLang('menu').'

'); $threads = $this->_getThreads(); // slice the needed chunk of discussion pages $more = ((count($threads) > ($first + $num)) ? true : false); $threads = array_slice($threads, $first, $num); foreach ($threads as $thread){ $comments = $this->_getComments($thread); $this->_threadHead($thread); if ($comments === false){ ptln(''); // class="level2" continue; } ptln('
', 2); ptln('
', 4); ptln('', 6); ptln('', 6); echo html_buildlist($comments, 'admin_discussion', array($this, '_commentItem'), array($this, '_li_comment')); $this->_actionButtons($thread['id']); } $this->_browseDiscussionLinks($more, $first, $num); } /** * Returns an array of pages with discussion sections, sorted by recent comments */ function _getThreads(){ global $conf; require_once(DOKU_INC.'inc/search.php'); // returns the list of pages in the given namespace and it's subspaces $items = array(); search($items, $conf['datadir'], 'search_allpages', ''); // add pages with comments to result $result = array(); foreach ($items as $item){ $id = $item['id']; // some checks $file = metaFN($id, '.comments'); if (!@file_exists($file)) continue; // skip if no comments file $date = filemtime($file); $result[$date] = array( 'id' => $id, 'file' => $file, 'date' => $date, ); } // finally sort by time of last comment krsort($result); return $result; } /** * Outputs header, page ID and status of a discussion thread */ function _threadHead($thread){ $id = $thread['id']; $labels = array( 0 => $this->getLang('off'), 1 => $this->getLang('open'), 2 => $this->getLang('closed') ); ptln('

'.hsc(p_get_metadata($id, 'title')).'

'); ptln(''); ptln('
', 2); ptln('', 4); ptln('', 4); ptln($this->getLang('status').': ', 4); ptln('', 4); ptln('
', 2); ptln(''); ptln('
'); ptln(''.$id.' ', 2); return true; } /** * Returns the full comments data for a given wiki page */ function _getComments(&$thread){ $id = $thread['id']; if (!$thread['file']) $thread['file'] = metaFN($id, '.comments'); if (!@file_exists($thread['file'])) return false; // no discussion thread at all $data = unserialize(io_readFile($thread['file'], false)); $thread['status'] = $data['status']; $thread['number'] = $data['number']; if (!$data['status']) return false; // comments are turned off if (!isset($data['comments']) || !$data['number']) return false; // no comments $result = array(); foreach ($data['comments'] as $cid => $comment){ $this->_addComment($cid, $data, $result); } return $result; } /** * Recursive function to add the comment hierarchy to the result */ function _addComment($cid, &$data, &$result, $parent = '', $level = 1){ if (!is_array($data['comments'][$cid])) return; // corrupt datatype $comment = $data['comments'][$cid]; if ($comment['parent'] != $parent) return; // answer to another comment // okay, add the comment to the result $comment['id'] = $cid; $comment['level'] = $level; $result[] = $comment; // check answers to this comment if (count($comment['replies'])){ foreach ($comment['replies'] as $rid){ $this->_addComment($rid, $data, $result, $cid, $level + 1); } } } /** * Checkbox and info about a comment item */ function _commentItem($comment){ global $conf; // prepare variables if (is_array($comment['user'])){ // new format $name = $comment['user']['name']; $mail = $comment['user']['mail']; } else { // old format $name = $comment['name']; $mail = $comment['mail']; } if (is_array($comment['date'])){ // new format $created = $comment['date']['created']; } else { // old format $created = $comment['date']; } $abstract = preg_replace('/\s+?/', ' ', strip_tags($comment['xhtml'])); if (utf8_strlen($abstract) > 160) $abstract = utf8_substr($abstract, 0, 160).'...'; return ' '. $this->email($mail, $name, 'email').', '.date($conf['dformat'], $created).': '. ''.$abstract.''; } /** * list item tag */ function _li_comment($comment){ $show = ($comment['show'] ? '' : ' hidden'); return '
  • '; } /** * Show buttons to bulk remove, hide or show comments */ function _actionButtons($id){ global $lang; ptln('
    ', 6); ptln('', 8); ptln('', 8); ptln('', 8); ptln('
    ', 6); // class="comment_buttons" ptln('
  • ', 4); // class="no" ptln('', 2); ptln('
    '); // class="level2" return true; } /** * Displays links to older newer discussions */ function _browseDiscussionLinks($more, $first, $num){ global $ID; $params = array('do' => 'admin', 'page' => 'discussion'); $last = $first+$num; if ($first > 0){ $first -= $num; if ($first < 0) $first = 0; $params['first'] = $first; ptln('

    ', 2); $ret = '<< '.$this->getLang('newer').''; if ($more){ $ret .= ' | '; } else { ptln($ret, 4); ptln('

    ', 2); } } else if ($more){ ptln('

    ', 2); } if ($more){ $params['first'] = $last; $ret .= ''.$this->getLang('older').' >>'; ptln($ret, 4); ptln('

    ', 2); } return true; } /** * Changes the status of a comment */ function _changeStatus($new){ global $ID; // get discussion meta file name $file = metaFN($ID, '.comments'); $data = unserialize(io_readFile($file, false)); $old = $data['status']; if ($old == $new) return true; // save the comment metadata file $data['status'] = $new; io_saveFile($file, serialize($data)); // look for ~~DISCUSSION~~ command in page file and change it accordingly $patterns = array('~~DISCUSSION:off~~', '~~DISCUSSION~~', '~~DISCUSSION:closed~~'); $replace = $patterns[$new]; $wiki = preg_replace('/~~DISCUSSION(?:|:off|:closed)~~/', $replace, rawWiki($ID)); saveWikiText($ID, $wiki, $this->getLang('statuschanged'), true); return true; } /** * function by iDo */ function _html() { require_once(DOKU_PLUGIN.'action.php'); $actionDiscussion= new action_plugin_discussion(); global $conf; global $INFO; global $ID; global $ADMDISCUSSION; $oID=$ID; $ADMDISCUSSION['page']="adm"; //Execute action for page if (isset($_REQUEST['comment'])) { if ($_REQUEST['comment']!='edit') { if (($_REQUEST['comment']=='add') && (isset($_REQUEST['cid']))) { } else { $obj=new unusedclass(); $actionDiscussion->comments($obj, null); } } } $chem=DOKU_INC.$conf['savedir']."/meta/"; $arr=$this->globr($chem,"*.comments"); $com =array(); foreach ($arr as $v) { $ap=unserialize(io_readFile($v, false)); if (isset($ap['comments'])){ $ID=substr(str_replace(array($chem,".comments",'/'),array("","",':'),$v),1); $ADMDISCUSSION['page']=' : '.str_replace("/doku.php/","",wl($ID,'')).''; if ((isset($_REQUEST['comment'])) && ($_REQUEST['comment']=='edit')) $actionDiscussion->_show(NULL, $_REQUEST['cid']); else $actionDiscussion->_show((($oID==$ID)?@$_REQUEST['cid']:null)); } } $ID = $oID; $ADMDISCUSSION['breakaction']=true; } /** * Recursive version of glob * * @return array containing all pattern-matched files. * * @param string $sDir Directory to start with. * @param string $sPattern Pattern to glob for. * @param int $nFlags Flags sent to glob. */ function _globr($sDir, $sPattern, $nFlags = NULL) { $sDir = escapeshellcmd($sDir); // Get the list of all matching files currently in the // directory. $aFiles = glob("$sDir/$sPattern", $nFlags); // Then get a list of all directories in this directory, and // run ourselves on the resulting array. This is the // recursion step, which will not execute if there are no // directories. foreach (glob("$sDir/*", GLOB_ONLYDIR) as $sSubDir) { $aSubFiles = $this->globr($sSubDir, $sPattern, $nFlags); $aFiles = array_merge($aFiles, $aSubFiles); } // The array we return contains the files we found, and the // files all of our children found. return $aFiles; } } //Setup VIM: ex: et ts=4 enc=utf-8 :