1<?php
2/**
3 * User annotations plugin
4 *
5 * @author     Gabriel Birke <birke@d-scribe.de>
6 */
7
8if(!defined('DOKU_INC')) die();
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10require_once(DOKU_PLUGIN.'action.php');
11
12class action_plugin_userannotations extends DokuWiki_Action_Plugin {
13
14  /**
15   * return some info
16   */
17  function getInfo(){
18    return array(
19		 'author' => 'Gabriel Birke',
20		 'email'  => 'birke@d-scribe.de',
21		 'date'   => '2009-02-23',
22		 'name'   => 'User annotations (action plugin component)',
23		 'desc'   => 'Displays user annotations below regular wiki text.'
24		 );
25  }
26
27  /*
28   * Register its handlers with the dokuwiki's event controller
29   */
30  function register(&$controller) {
31	  $controller->register_hook('TPL_ACT_RENDER', 'BEFORE',  $this, '_displayMessage');
32      $controller->register_hook('TPL_ACT_RENDER', 'AFTER',  $this, '_displayAnnotations');
33  }
34
35  /**
36   * Displays a hint if a page in the annotation namespace is displayed.
37   */
38  function _displayMessage(&$event, $param) {
39	  global $ID;
40	  $privatepath = $this->getConf('annotation_namespace');
41	  $pattern = '/^'.preg_quote($privatepath.':').'[^:]+\:/';
42	  if(preg_match($pattern, $ID, $matches))
43		  {
44			  $annotated_page = str_replace($matches[0], '', $ID);
45			  $title = p_get_first_heading($annotated_page);
46			  $title = empty($title)?noNS($annotated_page):$title;
47			echo sprintf('<p class="annotation-info">'.$this->getLang('annotation_info').'</p>', '<a href="'.wl($annotated_page).'">'.$title.'</a>');
48
49		  }
50  }
51
52
53  /**
54   * Collect all annotations from the annotation namespace that match the current
55   * page ID and display the annotations.
56   */
57  function _displayAnnotations(&$event, $param) {
58
59	if($event->data != "show")
60		return;
61	global $ID;
62
63	$privatepath = $this->getConf('annotation_namespace');
64	$anonymous = $this->getConf('anonymous_user');
65	$current = empty($_SERVER['REMOTE_USER']) ? $anonymous : $_SERVER['REMOTE_USER'];
66	$annotation_ns = array($current);
67	// Namespaces for each user
68	if($this->getConf('all_namespaces'))
69	{
70		// Collect names of users that have already posted something in the annotation namespace
71		// For wikis with big user lists and less annotations this may be more efficient than iterating all users
72		foreach ( new DirectoryIterator(preg_replace('/\.txt$/', '', wikiFN($privatepath))) as $item )
73        {
74			if($item->isDir() && !$item->isDot()) {
75			    $user = $item->getBasename();
76			    if($user != $current)
77    				$annotation_ns[] = $user;
78
79            }
80        }
81	}
82	foreach($annotation_ns as $user)
83	{
84		$annotation_id = $privatepath.':'.$user.':'.$ID;
85		$perm = auth_quickaclcheck($annotation_id);
86		// Only show annotation pages that exist. Maybe this user has not posted anything here
87		if(file_exists(wikiFN($annotation_id, '', false)) &&  $perm >= AUTH_READ)
88		{
89			echo '<div class="annotation annotation-'.preg_replace('/[^a-z0-9]/', '', $ns).'">';
90			echo '<h2>'.sprintf($this->getLang('annotationfrom'), $user).'</h2>';
91			if($perm >= AUTH_EDIT)
92    			echo '<p class="editannotation"><a href="'.DOKU_SCRIPT.'?id='.$annotation_id.'&do=edit">'.$this->getLang('edit_annotation').'</a></p>';
93			echo p_wiki_xhtml($annotation_id, '', false);
94			echo '</div>';
95		}
96		// The currently logged in user can create his own annotation
97		elseif($user == $current && auth_quickaclcheck($privatepath.':'.$user) >= AUTH_CREATE)
98		{
99			echo '<div class="annotation"><p class="editannotation createannotation"><a href="'.DOKU_SCRIPT;
100			echo '?id='.$annotation_id.'&do=edit" class="editlink">'.sprintf($this->getLang('create_annotation'), $current).'</a></p></div>';
101		}
102	}
103  }
104}
105