Did base logging stuff
[viewsvn.git] / includes / svnlib.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # ViewSVN [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * Command line interface with the SVN commands
15 *
16 * @package ViewSVN
17 */
18
19 /**
20 * Interacts with the command line subsystem to
21 * access SVN information
22 *
23 * @package ViewSVN
24 * @version $Id$
25 */
26 class SVNLib
27 {
28 /**
29 * Path to the SVN binary
30 * @var string
31 */
32 var $svnpath;
33
34 /**
35 * Constructor: validate SVN path
36 *
37 * @param string Path to SVN binary
38 */
39 function SVNLib($svnpath)
40 {
41 global $viewsvn;
42
43 $this->svnpath = $viewsvn->shell->cmd($svnpath);
44
45 $access = $viewsvn->shell->exec($this->svnpath . ' --version');
46
47 if (!$access)
48 {
49 $viewsvn->trigger->error('svn binary could not be found');
50 }
51
52 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
53 {
54 $viewsvn->trigger->error('svn binary does not pass test');
55 }
56 }
57
58 /**
59 * Executes the SVN binary
60 *
61 * @access private
62 *
63 * @param string Command
64 *
65 * @return array Output
66 */
67 function svn($command)
68 {
69 global $viewsvn;
70
71 return $viewsvn->shell->exec($this->svnpath . ' ' . $command . ' 2>&1');
72 }
73
74 /**
75 * SVN Wrapper: standard command system
76 *
77 * @access private
78 *
79 * @param string SVN command
80 * @param string Repository
81 * @param string Path
82 * @param integer Revision
83 *
84 * @return array Lines of output
85 */
86 function std($command, $repos, $path, $revision)
87 {
88 global $viewsvn;
89
90 $revision = $this->rev($revision);
91 $repospath = $viewsvn->repos->fetch_path($repos, false);
92
93 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
94 }
95
96 /**
97 * SVN Wrapper: blame
98 *
99 * @access protected
100 *
101 * @param string Repository
102 * @param string Path
103 * @param integer Revision
104 *
105 * @return array Lines of blame output
106 */
107 function blame($repos, $path, $revision)
108 {
109 return $this->std('blame', $repos, $path, $revision);
110 }
111
112 /**
113 * SVN Wrapper: cat
114 *
115 * @access protected
116 *
117 * @param string Repository
118 * @param string Path
119 * @param integer Revision
120 *
121 * @return array Lines of cat output
122 */
123 function cat($repos, $path, $revision)
124 {
125 return $this->std('cat', $repos, $path, $revision);
126 }
127
128 /**
129 * SVN Wrapper: log
130 *
131 * @access protected
132 *
133 * @param string Repository
134 * @param string Path
135 * @param integer Lower revision
136 * @param integer Higher revision
137 *
138 * @return array Lines of log output
139 */
140 function log($repos, $path, $lorev, $hirev)
141 {
142 global $viewsvn;
143
144 $hirev = $this->rev($hirev);
145 $lorev = $this->rev($hirev);
146 if ($lorev == 'HEAD')
147 {
148 $lorev = 0;
149 }
150
151 if (is_integer($hirev) AND is_integer($lorev))
152 {
153 if ($lorev > $hirev)
154 {
155 $lorev = $hirev - 1;
156 }
157 }
158
159 $repospath = $viewsvn->repos->fetch_path($repos, false);
160
161 return $this->svn('log -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
162 }
163
164 /**
165 * SVN Wrapper: ls (list)
166 *
167 * @access protected
168 *
169 * @param string Repository
170 * @param string Path
171 * @param integer Revision
172 *
173 * @return array Lines of list output
174 */
175 function ls($repos, $path, $revision)
176 {
177 return $this->std('list', $repos, $path, $revision);
178 }
179
180 /**
181 * Generates a clean revision number
182 *
183 * @access public
184 *
185 * @param integer Revision number
186 *
187 * @return mixed Cleaned revision or HEAD
188 */
189 function rev($revision)
190 {
191 if (($revision = intval($revision)) < 1)
192 {
193 $revision = 'HEAD';
194 }
195 return $revision;
196 }
197 }
198
199 /**
200 * Log management system; creates a complex list
201 * of SVN log information
202 *
203 * @package ViewSVN
204 * @version $Id$
205 */
206 class SVNLog
207 {
208 /**
209 * Array of logs
210 * @var array
211 */
212 var $logs = array();
213
214 /**
215 * Raw "svn log" output
216 * @var array
217 */
218 var $rawoutput;
219
220 /**
221 * Constructor: create log store for the given file
222 *
223 * @param string Repository
224 * @param string Path
225 * @param integer Lower revision
226 * @param integer Higher revision
227 */
228 function SVNLog($repos, $path, $lorev, $hirev)
229 {
230 global $viewsvn;
231
232 $this->rawoutput = $viewsvn->svn->log($repos, $path, $lorev, $hirev);
233 $this->process();
234 }
235
236 /**
237 * Returns logs for display
238 *
239 * @access public
240 *
241 * @return array Log data
242 */
243 function fetch()
244 {
245 return $this->logs;
246 }
247
248 /**
249 * Splits up the raw output into a usable log
250 *
251 * @access private
252 */
253 function process()
254 {
255 $lastrev = 0;
256
257 for ($i = 1; $i <= count($this->rawoutput) - 1; $i++)
258 {
259 $line = $this->rawoutput["$i"];
260
261 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
262 {
263 if (isset($this->logs["$lastrev"]))
264 {
265 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
266 }
267
268 $this->logs["$matches[1]"] = array(
269 'rev' => $matches[1],
270 'author' => $matches[2],
271 'date' => $matches[3],
272 'timezone' => $matches[4],
273 'lines' => $matches[6],
274 'message' => ''
275 );
276
277 $lastrev = $matches[1];
278 }
279 else
280 {
281 $this->logs["$lastrev"]['message'] .= $line . "\n";
282 }
283 }
284
285 if (isset($this->logs["$lastrev"]))
286 {
287 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
288 }
289 }
290
291 /**
292 * Trims the last dash line off a message
293 *
294 * @access private
295 *
296 * @param string Message with annoying-ass line
297 *
298 * @return string Clean string
299 */
300 function strip_last_line($string)
301 {
302 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
303 }
304 }
305
306 /*=====================================================================*\
307 || ###################################################################
308 || # $HeadURL$
309 || # $Id$
310 || ###################################################################
311 \*=====================================================================*/
312 ?>