2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
23 * Common functions that aren't Xquery-related and advanced query systems
29 * Interacts with the command line subsystem to
30 * access SVN information
38 * Path to the SVN binary
44 * Constructor: validate SVN path
46 * @param string Path to SVN binary
48 function SVNLib($svnpath)
52 $this->svnpath
= $viewsvn->shell
->cmd($svnpath);
54 $access = $viewsvn->shell
->exec($this->svnpath
. ' --version');
58 $viewsvn->trigger
->error($viewsvn->lang
->string('The SVN binary could not be located'));
61 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
63 $viewsvn->trigger
->error($viewsvn->lang
->string('The SVN binary does not appear to be valid (it failed our tests)'));
68 * Prepares data for output
72 * @param string Standard data
74 * @return string Output-ready data
76 function format($string)
79 $string = htmlspecialchars($string);
82 $string = str_replace("\t", ' ', $string);
87 $string = preg_replace('#( +)#e', '$this->format_spaces("\1")', $string);
92 $string = str_replace(' ', ' ', $string);
95 // convert advanced diff
96 $string = str_replace(array('{@+' . '+}', '{@-' . '-}'), array('<span class="diff_add_bit">', '<span class="diff_del_bit">'), $string);
97 $string = str_replace(array('{/@+' . '+}', '{/@-' . '-}'), '</span>', $string);
100 $string = nl2br($string);
106 * Formats a SVN log message
110 * @param string Unformatted log message
112 * @return string Output-ready log message
114 function format_log_message($message)
118 $message = $viewsvn->entity_encode($message);
120 $message = preg_replace('#r([0-9]+)#e', '"<a href=\"" . $viewsvn->path . "/" . $viewsvn->paths->out("diff.php" . $viewsvn->paths->fetch_rev_str(true, "\1", 0), $viewsvn->paths->fetch_repos($viewsvn->paths->parse()) . "/") . "\">r\1</a>"', $message);
123 $lines = explode("\n", $message);
125 foreach ($lines AS $line)
127 if (preg_match('#^\s*?(\*|\-)\s?(.*)#', $line, $matches))
131 $message .= '<li>' . $matches[2] . '</li>';
135 $message .= '<ul class="list">';
136 $message .= '<li>' . $matches[2] . '</li>';
150 $message .= '<br />';
163 $message = preg_replace('#(<br />)*$#', '', $message);
168 // ###################################################################
170 * Parses a date from Xquery XML outut
174 * @param string Date string
176 * @return string Formatted and readable date string
178 function format_date_string($string)
180 // 2005-01-23T20:42:53.703057Z
181 return preg_replace('#(....)\-(..)\-(..)T(..):(..):(..).(.*)Z#e', 'gmdate("r", mktime(\4, \5, \6, \2, \3, \1))', $string);
185 * Counts the spaces and replaces two or more ones
189 * @param string Spaced string
191 * @return string 'd string
193 function format_spaces($thestring)
195 if (strlen($thestring) >= 2)
197 $thestring = str_replace(' ', ' ', $thestring);
204 * Prints the file changed list
208 * @public array List of file changes
209 * @public string The repository
210 * @public integer Current revision
212 * @return string Processed HTML
214 function construct_file_changes($changes, $repos, $revision)
220 foreach ($changes AS $file)
222 switch ($file['action'])
226 $tooltip = $viewsvn->lang
->string('Added');
229 $class = 'file_delete';
230 $tooltip = $viewsvn->lang
->string('Deleted');
233 $class = 'file_modify';
234 $tooltip = $viewsvn->lang
->string('Modified');
237 $class = 'file_replace';
238 $tooltip = $viewsvn->lang
->string('Replaced');
242 $show['from'] = (bool
)$file['from'];
246 $class = 'file_move';
247 $tooltip = 'Moved/Copied';
248 preg_match('#(.*):([0-9]+)#', $file['from'], $matches);
249 $link['from'] = $viewsvn->paths
->out('view.php' . $viewsvn->paths
->fetch_rev_str(false
, $matches[2]), $repos . $matches[1]);
252 $link['file'] = $viewsvn->paths
->out('view.php' . $viewsvn->paths
->fetch_rev_str(false
, $revision), $repos . $file['file']);
254 eval('$files .= "' . $viewsvn->template
->fetch('file_change') . '";');
262 * Annotation/blame system; constructs an array
263 * that is ready for output
271 * Array of blame information
274 var $blame = array();
277 * Raw "svn blame" output
283 * Constructor: create blame and store data
285 * @param string Repository
287 * @param integer Revision
289 function SVNBlame($repos, $path, $revision)
293 $this->rawoutput
= $viewsvn->svn
->blame($repos, $path, $revision);
298 * Returns blame for display
302 * @return array Blame data
310 * Parses the blame data
318 foreach ($this->rawoutput
AS $line)
320 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
322 $this->blame
[] = array(
323 'rev' => $matches[1],
324 'author' => $matches[2],
325 'line' => $matches[3],
326 'lineno' => $lineno++
330 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
332 $this->blame
[] = array(
333 'rev' => $matches[1],
334 'author' => $matches[2],
336 'lineno' => $lineno++
344 * Log management system; creates a complex list
345 * of SVN log information
359 * Raw "svn log" output
365 * Constructor: create log store for the given file
367 * @param string Repository
369 * @param integer Lower revision
370 * @param integer Higher revision
372 function SVNLog($repos, $path, $lorev, $hirev)
376 $this->rawoutput
= $viewsvn->svn
->log($repos, $path, $lorev, $hirev);
381 * Returns logs for display
385 * @return array Log data
393 * Splits up the raw output into a usable log
401 for ($i = 1; $i <= count($this->rawoutput
) - 1; $i++
)
403 $line = $this->rawoutput
["$i"];
405 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
407 if (isset($this->logs
["$lastrev"]))
409 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
412 $this->logs
["$matches[1]"] = array(
413 'rev' => $matches[1],
414 'author' => $matches[2],
415 'date' => $matches[3],
416 'timezone' => $matches[4],
417 'lines' => $matches[6],
421 $lastrev = $matches[1];
423 else if (preg_match('#^\s+([ADMR])\s(.*)#', $line, $matches))
425 if (preg_match('#(.*) \(from (.*?)\)$#', $matches[2], $amatches))
427 $matches[2] = $amatches[1];
430 $this->logs
["$lastrev"]['files'][] = array(
431 'action' => $matches[1],
432 'file' => trim($matches[2]),
433 'from' => (isset($amatches[2]) ?
$amatches[2] : '')
438 if (trim($line) != 'Changed paths:')
440 $this->logs
["$lastrev"]['message'] .= $line . "\n";
445 if (isset($this->logs
["$lastrev"]))
447 $this->logs
["$lastrev"]['message'] = $this->strip_last_line($this->logs
["$lastrev"]['message']);
452 * Trims the last dash line off a message
456 * @param string Message with annoying-ass line
458 * @return string Clean string
460 function strip_last_line($string)
462 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
467 * Diff system; constructs a diff array that
468 * is ready for output
475 * Array of diff information
481 * Raw "svn diff" output
487 * Constructor: create and store diff data
489 * @param string Repository
491 * @param integer Lower revision
492 * @param integer Higher revision
494 function SVNDiff($repos, $path, $lorev, $hirev)
498 $this->rawoutput
= $viewsvn->svn
->diff($repos, $path, $lorev, $hirev);
503 * Returns diffs for display
507 * @return array Diff data
515 * Processes and prepares diff data
524 $indexcounter = null
;
529 foreach ($this->rawoutput
AS $line)
531 if (preg_match('#^@@ \-([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@$#', $line, $bits))
535 $this->diff
["$index"][ ++
$chunk ]['hunk'] = array('old' => array('line' => $bits[1], 'count' => $bits[2]), 'new' => array('line' => $bits[3], 'count' => $bits[4]));
536 $lines['old'] = $this->diff
["$index"]["$chunk"]['hunk']['old']['line'] - 1;
537 $lines['new'] = $this->diff
["$index"]["$chunk"]['hunk']['new']['line'] - 1;
540 else if (preg_match('#^Property changes on: (.*?)$#', $line, $bits))
544 $this->diff
["$index"]['props'] = array();
548 if ($indexcounter <= 3 AND $indexcounter !== null
)
553 else if ($indexcounter == 3)
555 $indexcounter = null
;
559 if (preg_match('#^([\+\- ])(.*)#', $line, $matches) AND !$property)
562 $content = $matches[2];
566 $this->diff
["$index"]["$chunk"][] = array(
569 'oldlineno' => ++
$lines['old'],
570 'newlineno' => ++
$lines['new']
575 else if ($act == '+')
577 // potential line delta
578 if (count($delstack) > 0)
580 $lastline = array_shift($delstack);
582 if ($delta = @$this->fetch_diff_extent($lastline['line'], $content))
584 if (strlen($lastline['line']) > ($delta['start'] - $delta['end']))
586 $end = strlen($lastline['line']) +
$delta['end'];
587 $viewsvn->debug("RM delta- = " . $end);
588 $change = '{@-' . '-}' . substr($lastline['line'], $delta['start'], $end - $delta['start']) . '{/@-' . '-}';
589 $this->diff
["$index"]["$chunk"]["$lastline[INDEX]"]['line'] = substr($lastline['line'], 0, $delta['start']) . $change . substr($lastline['line'], $end);
592 if (strlen($content) > $delta['start'] - $delta['end'])
594 $end = strlen($content) +
$delta['end'];
595 $viewsvn->debug("MK delta+ = " . $end);
596 $change = '{@+' . '+}' . substr($content, $delta['start'], $end - $delta['start']) . '{/@+' . '+}';
597 $content = substr($content, 0, $delta['start']) . $change . substr($content, $end);
602 $this->diff
["$index"]["$chunk"][] = array(
606 'newlineno' => ++
$lines['new']
609 else if ($act == '-')
611 $this->diff
["$index"]["$chunk"][] = $thearray = array(
614 'oldlineno' => ++
$lines['old'],
618 $key = count($this->diff
["$index"]["$chunk"]) - 2;
619 $thearray['INDEX'] = $key;
621 array_push($delstack, $thearray);
627 if (preg_match('#^Index: (.*?)$#', $line, $matches))
629 $index = $matches[1];
637 if (preg_match('#^__*_$#', trim($line)))
639 $viewsvn->debug("skipping: $line");
643 if (preg_match('#Name: (.*?)$#', $line, $matches))
645 $curprop = $matches[1];
646 $viewsvn->debug("prop: $curprop");
651 if (preg_match('#^\s+?\+(.*)#', $line, $matches))
654 $this->diff
["$index"]['props']["$curprop"]['add'] .= $matches[1];
656 else if (preg_match('#^\s+?\-(.*)#', $line, $matches))
659 $this->diff
["$index"]['props']["$curprop"]['del'] .= $matches[1];
661 else if (!preg_match('#^\s+[\+\- ](.*)#', $line) AND trim($line) != '')
663 $this->diff
["$index"]['props']["$curprop"]["$mode"] .= "\n" . $line;
669 $this->diff
["$index"]["$chunk"][] = array(
672 'oldlineno' => ++
$lines['old'],
673 'newlineno' => ++
$lines['new']
682 * Returns the amount of change that occured
687 * @param string Old line
688 * @param string New line
690 * @return array Difference of positions
692 function fetch_diff_extent($old, $new)
697 $min = min(strlen($old), strlen($new));
699 $viewsvn->debug("min1 = $min");
701 while ($start < $min AND $old["$start"] == $new["$start"])
707 $min = $min - $start;
709 $viewsvn->debug("min2 = $min");
711 $viewsvn->debug("checking: " . $old[ strlen($old) +
$end ] . ' == ' . $new[ strlen($new) +
$end ]);
713 while (-$end <= $min AND $old[ strlen($old) +
$end ] == $new[ strlen($new) +
$end ])
718 return array('start' => $start, 'end' => $end +
1);
722 /*=====================================================================*\
723 || ###################################################################
726 || ###################################################################
727 \*=====================================================================*/