1<?php
2// must be run within Dokuwiki
3if(!defined('DOKU_INC')) die();
4
5if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
6require_once(DOKU_PLUGIN.'syntax.php');
7
8// include git utils, they should be located on same directory with syntax.php
9require_once('git-utils.inc.php');
10
11function find_end($string, $offset = 0)
12{
13    if($offset >= strlen($string))
14        return FALSE;
15    for($i = $offset; $i < strlen($string); $i++)
16    {
17        if(!is_numeric($string[$i]))
18            return $i;
19    }
20    return strlen($string);
21}
22
23class syntax_plugin_dokugitviewer extends DokuWiki_Syntax_Plugin {
24
25    function getType(){
26        return 'substition';
27    }
28
29    function getSort(){
30        return 999;
31    }
32
33    function connectTo($mode) {
34      	$this->Lexer->addSpecialPattern('<dokugitviewer:.+?>',$mode,'plugin_dokugitviewer');
35    }
36
37
38    function handle($match, $state, $pos, &$handler)
39	{
40		$start = strlen('<dokugitviewer:');
41		$end = -1;
42		$params = substr($match, $start, $end);
43		$params = preg_replace('/\s{2,}/', '', $params);
44		$params = preg_replace('/\s[=]/', '=', $params);
45		$params = preg_replace('/[=]\s/', '=', $params);
46
47		$return = array();
48		foreach(explode(' ', $params) as $param)
49		{
50			$val = explode('=', $param);
51			$return[$val[0]] = $val[1];
52		}
53        return $return;
54    }
55
56    function render($mode, &$renderer, $data) {
57		$elements = array('ft' => 'features',
58						  'bug' => 'bugs');
59        if($mode == 'xhtml'){
60			if(isset($data['repository']))
61			{
62				if(isset($data['limit']) && is_numeric($data['limit']))
63					$limit = (int)($data['limit']);
64				else
65					$limit = 10;
66			        if (empty($data['bare']))
67			          $bare=false;
68			        else
69			          $bare=true;
70				$log = git_get_log($data['repository'], $limit,$bare);
71				$renderer->doc .= '<ul class="dokugitviewer">';
72				foreach($log as $row)
73				{
74					$renderer->doc .= '<li>';
75					$message = $row['message'];
76					for($index = 0; $index < strlen($message); $index++)
77					{
78						$char = $message[$index];
79						if($char == '#')
80                        {
81                            foreach(array_keys($elements) as $element)
82                            {
83                                $cmp = '#'.$element;
84                                $src = substr($message, $index, strlen($cmp));
85                                if(strstr($src, $cmp))
86                                {
87                                    $key = substr($message, $index+1, strlen($cmp)-1);
88
89                                    $src= substr($message, $index+1+strlen($key));
90                                    $value = substr($src, 0, find_end($src));
91                                    $index += strlen($element.$value);
92                                    $renderer->internallink($data[$elements[$element]].'#'.$element.$value, '#'.$element.$value);
93
94                                }
95                            }
96						}
97						else
98							$renderer->doc .= $char;
99					}
100					//$renderer->doc .= '<strong>'.$message.'</strong>';
101					$renderer->doc .= '<br />';
102					$renderer->doc .= $row['author'].' on ';
103					$renderer->doc .= date(DATE_FORMAT,$row['timestamp']);
104
105					$renderer->doc .= '</li>';
106
107				}
108				$renderer->doc .= '</ul>';
109			}
110            return true;
111        }
112        return false;
113    }
114}
115
116?>
117
118