1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Stephan Dekker <Stephan@SparklingSoftware.com.au>
5 */
6
7require_once(DOKU_PLUGIN.'syntax.php');
8require_once(DOKU_PLUGIN.'git/lib/Git.php');
9require_once(DOKU_INC.'/inc/DifferenceEngine.php');
10
11/**
12 * All DokuWiki plugins to extend the parser/rendering mechanism
13 * need to inherit from this class
14 */
15class syntax_plugin_git_remotestatus extends DokuWiki_Syntax_Plugin {
16
17    var $helper = null;
18
19    function syntax_plugin_git_remotestatus (){
20        $this->helper =& plugin_load('helper', 'git');
21        if(!$this->helper) msg('Loading the git helper in the git_localstatus class failed.', -1);
22    }
23
24    /**
25     * return some info
26     */
27    function getInfo(){
28        return array(
29            'author' => 'Stephan Dekker',
30            'email'  => 'Stephan@SparklingSoftware.com.au',
31            'date'   => @file_get_contents(dirname(__FILE__) . '/VERSION'),
32            'name'   => 'Git Remote Status Plugin',
33            'desc'   => 'xml',
34            'url'    => 'http://dokuwiki.org/plugin:git',
35        );
36    }
37
38    /**
39     * What kind of syntax are we?
40     */
41    function getType(){
42        return 'substition';
43    }
44
45    /**
46     * What about paragraphs?
47     */
48    function getPType(){
49        return 'normal';
50    }
51
52    /**
53     * Where to sort in?
54     */
55    function getSort(){
56        return 990;     //was 990
57    }
58
59
60    /**
61     * Connect pattern to lexer
62     */
63    function connectTo($mode) {
64        $this->Lexer->addSpecialPattern('~~GITRemoteStatus~~',$mode,'plugin_git_remotestatus');
65    }
66
67    /**
68     * Handle the match
69     */
70    function handle($match, $state, $pos, &$handler){
71        $match_array = array();
72        $match = substr($match,11,-2); //strip ~~REVISIONS: from start and ~~ from end
73        // Wolfgang 2007-08-29 suggests commenting out the next line
74        // $match = strtolower($match);
75        //create array, using ! as separator
76        $match_array = explode("!", $match);
77        // $match_array[0] will be all, or syntax error
78        // this return value appears in render() as the $data param there
79        return $match_array;
80    }
81
82
83
84    /**
85     * Create output
86     */
87    function render($format, &$renderer, $data) {
88        global $INFO, $conf;
89
90        if($format == 'xhtml'){
91
92            try
93            {
94                // If not logged in, go bugger off...
95                if (is_array($INFO['userinfo']) === false)
96                {
97                    $renderer->doc .= "<br/><br/>You need to be logged in to view this page. Please login.";
98                    return;
99                }
100
101                // Get GIT commits
102                global $conf;
103                $this->getConf('');
104
105                $git_exe_path = $conf['plugin']['git']['git_exe_path'];
106                $datapath = $conf['savedir'];
107
108                $repo = new GitRepo($datapath);
109                $repo->git_path = $git_exe_path;
110                $repo->fetch();
111                $log = $repo->get_log();
112                if ($log === "")
113                {
114                    $renderer->doc .= "There are no upstream updates for the current workspace. It's up to date!";
115                    return true;
116                }
117
118                $waiting_to_commit = $repo->get_status();
119                if ($waiting_to_commit !== "")
120                {
121                    $gitLocalStatusUrl = wl($conf['plugin']['git']['local_status_page'],'',true);
122                    $renderer->doc .= 'Please commit local changes before you can merge upstream. <a href="'.$gitLocalStatusUrl.'">Local Changes<a/>';
123                    return true;
124                }
125
126                $commits = $repo->get_commits($log);
127
128                // Render select box
129                $renderer->doc .= "Select a commit from upstream to view the changes contained in each: <br/>";
130                $this->helper->render_commit_selector($renderer, $commits);
131                $this->helper->render_changed_files_table($renderer, $commits, $repo);
132                $this->helper->renderChangesMade($renderer, $repo, 'Merge upstream');
133
134                $renderer->doc .= '<form method="post">';
135                $renderer->doc .= '  <input type="submit" name="cmd[ignore]"  value="Ignore this commit" />';
136                $renderer->doc .= '  <input type="submit" name="cmd[merge]"  value="Merge All" />';
137                $renderer->doc .= '</form>';
138                $renderer->doc .= '<br/>';
139
140            }
141            catch(Exception $e)
142            {
143                $renderer->doc .= $e->getMessage();
144            }
145
146            return true;
147        }
148        return false;
149    }
150
151    //function render_commit_selector($renderer, $commits)
152    //{
153    //    $renderer->doc .= "<select id='git_commit' width=\"800\" style=\"width: 800px\" onchange='ChangeGitCommit();'>";
154    //    $index = 1;
155    //    foreach($commits as $commit)
156    //    {
157    //        $renderer->doc .= "<option value=\"".$commit['hash']."\">".$index." - ".$commit['message']."</option>";
158    //        $index++;
159    //    }
160    //    $renderer->doc .= '</select>';
161    //}
162
163    //function render_changed_files_table($renderer, $commits, $repo)
164    //{
165    //    $renderer->doc .= '<h3>This is the content of the selected commit:</h3>';
166    //    $divVisibility = ""; // Make the first div visible as thats the first item in the select box
167    //    foreach($commits as $commit)
168    //    {
169    //        $renderer->doc .= "<div class=\"commit_div\" id='".$commit['hash']."' style=\"".$divVisibility." width: 100%;height: 175px;overflow:-moz-scrollbars-vertical;overflow-y:auto;\">";
170    //        $hash = $commit['hash'];
171    //        $files = explode("\n", $repo->get_files_by_commit($hash));
172
173    //        $renderer->doc .= "<table><tr><th>What happened</th><th>File</th><th>link</th></tr>";
174    //        foreach ($files as $file)
175    //        {
176    //            if ($file === "") continue;
177
178    //            $renderer->doc .= "<tr><td>";
179
180    //            $change = substr($file, 0, 1);
181    //            switch($change)
182    //            {
183    //                case "A":
184    //                    $renderer->doc .= "Added:";
185    //                    break;
186    //                case "M":
187    //                    $renderer->doc .= "Modified:";
188    //                    break;
189    //                case "R":
190    //                    $renderer->doc .= "Removed:";
191    //                    break;
192    //            }
193    //            $renderer->doc .= "</td><td>";
194    //            $file = substr($file, 2);
195    //            $renderer->doc .= $file;
196    //            $renderer->doc .= "</td><td>";
197    //            $renderer->doc .= '   <form method="post">';
198    //            $renderer->doc .= '      <input type="hidden" name="filename"  value="'.$file.'" />';
199    //            $renderer->doc .= '      <input type="hidden" name="hash"  value="'.$commit['hash'].'" />';
200    //            $renderer->doc .= '      <input type="submit" value="View Changes" />';
201    //            $renderer->doc .= '   </form>';
202    //            $renderer->doc .= "</td>";
203    //            $renderer->doc .= "</tr>";
204    //        }
205    //        $renderer->doc .= "</table>";
206    //        $renderer->doc .= "</div>\n";
207    //        $divVisibility = " display:none;";
208    //    }
209    //}
210
211    function getFileContents($filename)
212    {
213        // get contents of a file into a string
214        $handle = fopen($filename, "r");
215        $contents = fread($handle, filesize($filename));
216        fclose($handle);
217
218        return $contents;
219    }
220
221}
222
223
224//Setup VIM: ex: et ts=4 enc=utf-8 :
225?>
226