Added more checks for svnlib::log() revision 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 * Prepares data for output
60 *
61 * @access public
62 *
63 * @param string Standard data
64 *
65 * @return string Output-ready data
66 */
67 function format($string)
68 {
69 // convert entities
70 $string = htmlspecialchars($string);
71
72 // tabs to 5 spaces
73 $string = str_replace("\t", ' ', $string);
74
75 // spaces to nbsp
76 $string = str_replace(' ', '&nbsp;', $string);
77
78 // nl2br
79 $string = nl2br($string);
80
81 return $string;
82 }
83
84 /**
85 * Executes the SVN binary
86 *
87 * @access private
88 *
89 * @param string Command
90 *
91 * @return array Output
92 */
93 function svn($command)
94 {
95 global $viewsvn;
96
97 return $viewsvn->shell->exec($this->svnpath . ' ' . $command . ' 2>&1');
98 }
99
100 /**
101 * SVN Wrapper: standard command system
102 *
103 * @access private
104 *
105 * @param string SVN command
106 * @param string Repository
107 * @param string Path
108 * @param integer Revision
109 *
110 * @return array Lines of output
111 */
112 function std($command, $repos, $path, $revision)
113 {
114 global $viewsvn;
115
116 $revision = $this->rev($revision);
117 $repospath = $viewsvn->repos->fetch_path($repos, false);
118
119 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
120 }
121
122 /**
123 * SVN Wrapper: blame
124 *
125 * @access protected
126 *
127 * @param string Repository
128 * @param string Path
129 * @param integer Revision
130 *
131 * @return array Lines of blame output
132 */
133 function blame($repos, $path, $revision)
134 {
135 return $this->std('blame', $repos, $path, $revision);
136 }
137
138 /**
139 * SVN Wrapper: cat
140 *
141 * @access protected
142 *
143 * @param string Repository
144 * @param string Path
145 * @param integer Revision
146 *
147 * @return array Lines of cat output
148 */
149 function cat($repos, $path, $revision)
150 {
151 global $viewsvn;
152
153 $repospath = $viewsvn->repos->fetch_path($repos, false);
154
155 return $this->svn('cat ' . $repospath . $path . '@' . $revision);
156 }
157
158 /**
159 * SVN Wrapper: log
160 *
161 * @access protected
162 *
163 * @param string Repository
164 * @param string Path
165 * @param integer Lower revision
166 * @param integer Higher revision
167 *
168 * @return array Lines of log output
169 */
170 function log($repos, $path, $lorev, $hirev)
171 {
172 global $viewsvn;
173
174 $hirev = $this->rev($hirev);
175 $lorev = $this->rev($hirev);
176 if ($lorev == 'HEAD')
177 {
178 $lorev = 0;
179 }
180
181 if (is_integer($hirev) AND is_integer($lorev))
182 {
183 if ($lorev > $hirev)
184 {
185 $lorev = $hirev - 1;
186 }
187 if ($lorev == $hirev)
188 {
189 $lorev = 0;
190 }
191 }
192
193 $repospath = $viewsvn->repos->fetch_path($repos, false);
194
195 return $this->svn('log -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
196 }
197
198 /**
199 * SVN Wrapper: ls (list)
200 *
201 * @access protected
202 *
203 * @param string Repository
204 * @param string Path
205 * @param integer Revision
206 *
207 * @return array Lines of list output
208 */
209 function ls($repos, $path, $revision)
210 {
211 return $this->std('list', $repos, $path, $revision);
212 }
213
214 /**
215 * Generates a clean revision number
216 *
217 * @access public
218 *
219 * @param integer Revision number
220 *
221 * @return mixed Cleaned revision or HEAD
222 */
223 function rev($revision)
224 {
225 if (($revision = intval($revision)) < 1)
226 {
227 $revision = 'HEAD';
228 }
229 return $revision;
230 }
231 }
232
233 /**
234 * Annotation/blame system; constructs an array
235 * that is ready for output
236 *
237 * @package ViewSVN
238 * @version $Id$
239 */
240 class SVNBlame
241 {
242 /**
243 * Array of blame information
244 * @var array
245 */
246 var $blame = array();
247
248 /**
249 * Raw "svn blame" output
250 */
251 var $rawoutput;
252
253 /**
254 * Constructor: create blame and store data
255 *
256 * @param string Repository
257 * @param string Path
258 * @param integer Revision
259 */
260 function SVNBlame($repos, $path, $revision)
261 {
262 global $viewsvn;
263
264 $this->rawoutput = $viewsvn->svn->blame($repos, $path, $revision);
265 $this->process();
266 }
267
268 /**
269 * Returns blame for display
270 *
271 * @access public
272 *
273 * @return array Blame data
274 */
275 function fetch()
276 {
277 return $this->blame;
278 }
279
280 /**
281 * Parses the blame data
282 *
283 * @access private
284 */
285 function process()
286 {
287 $lineno = 1;
288
289 foreach ($this->rawoutput AS $line)
290 {
291 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
292 {
293 $this->blame[] = array(
294 'rev' => $matches[1],
295 'author' => $matches[2],
296 'line' => $matches[3],
297 'lineno' => $lineno++
298 );
299 }
300 // a blank line
301 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
302 {
303 $this->blame[] = array(
304 'rev' => $matches[1],
305 'author' => $matches[2],
306 'line' => '',
307 'lineno' => $lineno++
308 );
309 }
310 }
311 }
312 }
313
314 /**
315 * Log management system; creates a complex list
316 * of SVN log information
317 *
318 * @package ViewSVN
319 * @version $Id$
320 */
321 class SVNLog
322 {
323 /**
324 * Array of logs
325 * @var array
326 */
327 var $logs = array();
328
329 /**
330 * Raw "svn log" output
331 * @var array
332 */
333 var $rawoutput;
334
335 /**
336 * Constructor: create log store for the given file
337 *
338 * @param string Repository
339 * @param string Path
340 * @param integer Lower revision
341 * @param integer Higher revision
342 */
343 function SVNLog($repos, $path, $lorev, $hirev)
344 {
345 global $viewsvn;
346
347 $this->rawoutput = $viewsvn->svn->log($repos, $path, $lorev, $hirev);
348 $this->process();
349 }
350
351 /**
352 * Returns logs for display
353 *
354 * @access public
355 *
356 * @return array Log data
357 */
358 function fetch()
359 {
360 return $this->logs;
361 }
362
363 /**
364 * Splits up the raw output into a usable log
365 *
366 * @access private
367 */
368 function process()
369 {
370 $lastrev = 0;
371
372 for ($i = 1; $i <= count($this->rawoutput) - 1; $i++)
373 {
374 $line = $this->rawoutput["$i"];
375
376 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
377 {
378 if (isset($this->logs["$lastrev"]))
379 {
380 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
381 }
382
383 $this->logs["$matches[1]"] = array(
384 'rev' => $matches[1],
385 'author' => $matches[2],
386 'date' => $matches[3],
387 'timezone' => $matches[4],
388 'lines' => $matches[6],
389 'message' => ''
390 );
391
392 $lastrev = $matches[1];
393 }
394 else
395 {
396 $this->logs["$lastrev"]['message'] .= $line . "\n";
397 }
398 }
399
400 if (isset($this->logs["$lastrev"]))
401 {
402 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
403 }
404 }
405
406 /**
407 * Trims the last dash line off a message
408 *
409 * @access private
410 *
411 * @param string Message with annoying-ass line
412 *
413 * @return string Clean string
414 */
415 function strip_last_line($string)
416 {
417 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
418 }
419 }
420
421 /*=====================================================================*\
422 || ###################################################################
423 || # $HeadURL$
424 || # $Id$
425 || ###################################################################
426 \*=====================================================================*/
427 ?>