*/
class HTMLDiff {
/**
* Holds the style names and basic CSS
*/
static public $styles = array(
'diff-addedline' => 'background-color: #ddffdd;',
'diff-deletedline' => 'background-color: #ffdddd;',
'diff-context' => 'background-color: #f5f5f5;',
'diff-mark' => 'color: #ff0000;',
);
/**
* Return a class or style parameter
*
* @param string $classname
*
* @return string
*/
static function css($classname){
global $DIFF_INLINESTYLES;
if($DIFF_INLINESTYLES){
if(!isset(self::$styles[$classname])) return '';
return 'style="'.self::$styles[$classname].'"';
}else{
return 'class="'.$classname.'"';
}
}
}
/**
* Additions by Axel Boldt follow, partly taken from diff.php, phpwiki-1.3.3
*
*/
define('NBSP', "\xC2\xA0"); // utf-8 non-breaking space.
class _HWLDF_WordAccumulator {
function __construct() {
$this->_lines = array();
$this->_line = '';
$this->_group = '';
$this->_tag = '';
}
function _flushGroup($new_tag) {
if ($this->_group !== '') {
if ($this->_tag == 'mark')
$this->_line .= ''.$this->_escape($this->_group).'';
elseif ($this->_tag == 'add')
$this->_line .= ''.$this->_escape($this->_group).'';
elseif ($this->_tag == 'del')
$this->_line .= ''.$this->_escape($this->_group).'';
else
$this->_line .= $this->_escape($this->_group);
}
$this->_group = '';
$this->_tag = $new_tag;
}
/**
* @param string $new_tag
*/
function _flushLine($new_tag) {
$this->_flushGroup($new_tag);
if ($this->_line != '')
$this->_lines[] = $this->_line;
$this->_line = '';
}
function addWords($words, $tag = '') {
if ($tag != $this->_tag)
$this->_flushGroup($tag);
foreach ($words as $word) {
// new-line should only come as first char of word.
if ($word == '')
continue;
if ($word[0] == "\n") {
$this->_group .= NBSP;
$this->_flushLine($tag);
$word = substr($word, 1);
}
assert(!strstr($word, "\n"));
$this->_group .= $word;
}
}
function getLines() {
$this->_flushLine('~done');
return $this->_lines;
}
function _escape($str){
return hsc($str);
}
}
class WordLevelDiff extends MappedDiff {
function __construct($orig_lines, $closing_lines) {
list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
parent::__construct($orig_words, $closing_words, $orig_stripped, $closing_stripped);
}
function _split($lines) {
if (!preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xsu',
implode("\n", $lines), $m)) {
return array(array(''), array(''));
}
return array($m[0], $m[1]);
}
function orig() {
$orig = new _HWLDF_WordAccumulator;
foreach ($this->edits as $edit) {
if ($edit->type == 'copy')
$orig->addWords($edit->orig);
elseif ($edit->orig)
$orig->addWords($edit->orig, 'mark');
}
return $orig->getLines();
}
function closing() {
$closing = new _HWLDF_WordAccumulator;
foreach ($this->edits as $edit) {
if ($edit->type == 'copy')
$closing->addWords($edit->closing);
elseif ($edit->closing)
$closing->addWords($edit->closing, 'mark');
}
return $closing->getLines();
}
}
class InlineWordLevelDiff extends MappedDiff {
function __construct($orig_lines, $closing_lines) {
list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
list ($closing_words, $closing_stripped) = $this->_split($closing_lines);
parent::__construct($orig_words, $closing_words, $orig_stripped, $closing_stripped);
}
function _split($lines) {
if (!preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xsu',
implode("\n", $lines), $m)) {
return array(array(''), array(''));
}
return array($m[0], $m[1]);
}
function inline() {
$orig = new _HWLDF_WordAccumulator;
foreach ($this->edits as $edit) {
if ($edit->type == 'copy')
$orig->addWords($edit->closing);
elseif ($edit->type == 'change'){
$orig->addWords($edit->orig, 'del');
$orig->addWords($edit->closing, 'add');
} elseif ($edit->type == 'delete')
$orig->addWords($edit->orig, 'del');
elseif ($edit->type == 'add')
$orig->addWords($edit->closing, 'add');
elseif ($edit->orig)
$orig->addWords($edit->orig, 'del');
}
return $orig->getLines();
}
}
/**
* "Unified" diff formatter.
*
* This class formats the diff in classic "unified diff" format.
*
* NOTE: output is plain text and unsafe for use in HTML without escaping.
*/
class UnifiedDiffFormatter extends DiffFormatter {
function __construct($context_lines = 4) {
$this->leading_context_lines = $context_lines;
$this->trailing_context_lines = $context_lines;
}
function _block_header($xbeg, $xlen, $ybeg, $ylen) {
if ($xlen != 1)
$xbeg .= "," . $xlen;
if ($ylen != 1)
$ybeg .= "," . $ylen;
return "@@ -$xbeg +$ybeg @@\n";
}
function _added($lines) {
$this->_lines($lines, "+");
}
function _deleted($lines) {
$this->_lines($lines, "-");
}
function _changed($orig, $final) {
$this->_deleted($orig);
$this->_added($final);
}
}
/**
* Wikipedia Table style diff formatter.
*
*/
class TableDiffFormatter extends DiffFormatter {
var $colspan = 2;
function __construct() {
$this->leading_context_lines = 2;
$this->trailing_context_lines = 2;
}
/**
* @param Diff $diff
* @return string
*/
function format($diff) {
// Preserve whitespaces by converting some to non-breaking spaces.
// Do not convert all of them to allow word-wrap.
$val = parent::format($diff);
$val = str_replace(' ',' ', $val);
$val = preg_replace('/ (?=<)|(?<=[ >]) /', ' ', $val);
return $val;
}
function _pre($text){
$text = htmlspecialchars($text);
return $text;
}
function _block_header($xbeg, $xlen, $ybeg, $ylen) {
global $lang;
$l1 = $lang['line'].' '.$xbeg;
$l2 = $lang['line'].' '.$ybeg;
$r = '| '.$l1.": | \n".
''.$l2.": | \n".
"
\n";
return $r;
}
function _start_block($header) {
print($header);
}
function _end_block() {
}
function _lines($lines, $prefix=' ', $color="white") {
}
function addedLine($line,$escaped=false) {
if (!$escaped){
$line = $this->_escape($line);
}
return '+ | '.
'' . $line.' | ';
}
function deletedLine($line,$escaped=false) {
if (!$escaped){
$line = $this->_escape($line);
}
return '- | '.
'' . $line.' | ';
}
function emptyLine() {
return ' | ';
}
function contextLine($line) {
return ' | '.
''.$this->_escape($line).' | ';
}
function _added($lines) {
$this->_addedLines($lines,false);
}
function _addedLines($lines,$escaped=false){
foreach ($lines as $line) {
print('' . $this->emptyLine() . $this->addedLine($line,$escaped) . "
\n");
}
}
function _deleted($lines) {
foreach ($lines as $line) {
print('' . $this->deletedLine($line) . $this->emptyLine() . "
\n");
}
}
function _context($lines) {
foreach ($lines as $line) {
print('' . $this->contextLine($line) . $this->contextLine($line) . "
\n");
}
}
function _changed($orig, $closing) {
$diff = new WordLevelDiff($orig, $closing); // this escapes the diff data
$del = $diff->orig();
$add = $diff->closing();
while ($line = array_shift($del)) {
$aline = array_shift($add);
print('' . $this->deletedLine($line,true) . $this->addedLine($aline,true) . "
\n");
}
$this->_addedLines($add,true); # If any leftovers
}
function _escape($str) {
return hsc($str);
}
}
/**
* Inline style diff formatter.
*
*/
class InlineDiffFormatter extends DiffFormatter {
var $colspan = 2;
function __construct() {
$this->leading_context_lines = 2;
$this->trailing_context_lines = 2;
}
/**
* @param Diff $diff
* @return string
*/
function format($diff) {
// Preserve whitespaces by converting some to non-breaking spaces.
// Do not convert all of them to allow word-wrap.
$val = parent::format($diff);
$val = str_replace(' ',' ', $val);
$val = preg_replace('/ (?=<)|(?<=[ >]) /', ' ', $val);
return $val;
}
function _pre($text){
$text = htmlspecialchars($text);
return $text;
}
function _block_header($xbeg, $xlen, $ybeg, $ylen) {
global $lang;
if ($xlen != 1)
$xbeg .= "," . $xlen;
if ($ylen != 1)
$ybeg .= "," . $ylen;
$r = '@@ '.$lang['line']." -$xbeg +$ybeg @@";
$r .= ' '.$lang['deleted'].'';
$r .= ' '.$lang['created'].'';
$r .= " |
\n";
return $r;
}
function _start_block($header) {
print($header."\n");
}
function _end_block() {
}
function _lines($lines, $prefix=' ', $color="white") {
}
function _added($lines) {
foreach ($lines as $line) {
print('| | '. $this->_escape($line) . " |
\n");
}
}
function _deleted($lines) {
foreach ($lines as $line) {
print('| | ' . $this->_escape($line) . " |
\n");
}
}
function _context($lines) {
foreach ($lines as $line) {
print('| | '. $this->_escape($line) ." |
\n");
}
}
function _changed($orig, $closing) {
$diff = new InlineWordLevelDiff($orig, $closing); // this escapes the diff data
$add = $diff->inline();
foreach ($add as $line)
print('| | '.$line." |
\n");
}
function _escape($str) {
return hsc($str);
}
}
/**
* A class for computing three way diffs.
*
* @author Geoffrey T. Dairiki
*/
class Diff3 extends Diff {
/**
* Conflict counter.
*
* @var integer
*/
var $_conflictingBlocks = 0;
/**
* Computes diff between 3 sequences of strings.
*
* @param array $orig The original lines to use.
* @param array $final1 The first version to compare to.
* @param array $final2 The second version to compare to.
*/
function __construct($orig, $final1, $final2) {
$engine = new _DiffEngine();
$this->_edits = $this->_diff3($engine->diff($orig, $final1),
$engine->diff($orig, $final2));
}
/**
* Returns the merged lines
*
* @param string $label1 label for first version
* @param string $label2 label for second version
* @param string $label3 separator between versions
* @return array lines of the merged text
*/
function mergedOutput($label1='<<<<<<<',$label2='>>>>>>>',$label3='=======') {
$lines = array();
foreach ($this->_edits as $edit) {
if ($edit->isConflict()) {
/* FIXME: this should probably be moved somewhere else. */
$lines = array_merge($lines,
array($label1),
$edit->final1,
array($label3),
$edit->final2,
array($label2));
$this->_conflictingBlocks++;
} else {
$lines = array_merge($lines, $edit->merged());
}
}
return $lines;
}
/**
* @access private
*
* @param array $edits1
* @param array $edits2
*
* @return array
*/
function _diff3($edits1, $edits2) {
$edits = array();
$bb = new _Diff3_BlockBuilder();
$e1 = current($edits1);
$e2 = current($edits2);
while ($e1 || $e2) {
if ($e1 && $e2 && is_a($e1, '_DiffOp_copy') && is_a($e2, '_DiffOp_copy')) {
/* We have copy blocks from both diffs. This is the (only)
* time we want to emit a diff3 copy block. Flush current
* diff3 diff block, if any. */
if ($edit = $bb->finish()) {
$edits[] = $edit;
}
$ncopy = min($e1->norig(), $e2->norig());
assert($ncopy > 0);
$edits[] = new _Diff3_Op_copy(array_slice($e1->orig, 0, $ncopy));
if ($e1->norig() > $ncopy) {
array_splice($e1->orig, 0, $ncopy);
array_splice($e1->closing, 0, $ncopy);
} else {
$e1 = next($edits1);
}
if ($e2->norig() > $ncopy) {
array_splice($e2->orig, 0, $ncopy);
array_splice($e2->closing, 0, $ncopy);
} else {
$e2 = next($edits2);
}
} else {
if ($e1 && $e2) {
if ($e1->orig && $e2->orig) {
$norig = min($e1->norig(), $e2->norig());
$orig = array_splice($e1->orig, 0, $norig);
array_splice($e2->orig, 0, $norig);
$bb->input($orig);
}
if (is_a($e1, '_DiffOp_copy')) {
$bb->out1(array_splice($e1->closing, 0, $norig));
}
if (is_a($e2, '_DiffOp_copy')) {
$bb->out2(array_splice($e2->closing, 0, $norig));
}
}
if ($e1 && ! $e1->orig) {
$bb->out1($e1->closing);
$e1 = next($edits1);
}
if ($e2 && ! $e2->orig) {
$bb->out2($e2->closing);
$e2 = next($edits2);
}
}
}
if ($edit = $bb->finish()) {
$edits[] = $edit;
}
return $edits;
}
}
/**
* @author Geoffrey T. Dairiki
*
* @access private
*/
class _Diff3_Op {
function __construct($orig = false, $final1 = false, $final2 = false) {
$this->orig = $orig ? $orig : array();
$this->final1 = $final1 ? $final1 : array();
$this->final2 = $final2 ? $final2 : array();
}
function merged() {
if (!isset($this->_merged)) {
if ($this->final1 === $this->final2) {
$this->_merged = &$this->final1;
} elseif ($this->final1 === $this->orig) {
$this->_merged = &$this->final2;
} elseif ($this->final2 === $this->orig) {
$this->_merged = &$this->final1;
} else {
$this->_merged = false;
}
}
return $this->_merged;
}
function isConflict() {
return $this->merged() === false;
}
}
/**
* @author Geoffrey T. Dairiki
*
* @access private
*/
class _Diff3_Op_copy extends _Diff3_Op {
function __construct($lines = false) {
$this->orig = $lines ? $lines : array();
$this->final1 = &$this->orig;
$this->final2 = &$this->orig;
}
function merged() {
return $this->orig;
}
function isConflict() {
return false;
}
}
/**
* @author Geoffrey T. Dairiki
*
* @access private
*/
class _Diff3_BlockBuilder {
function __construct() {
$this->_init();
}
function input($lines) {
if ($lines) {
$this->_append($this->orig, $lines);
}
}
function out1($lines) {
if ($lines) {
$this->_append($this->final1, $lines);
}
}
function out2($lines) {
if ($lines) {
$this->_append($this->final2, $lines);
}
}
function isEmpty() {
return !$this->orig && !$this->final1 && !$this->final2;
}
function finish() {
if ($this->isEmpty()) {
return false;
} else {
$edit = new _Diff3_Op($this->orig, $this->final1, $this->final2);
$this->_init();
return $edit;
}
}
function _init() {
$this->orig = $this->final1 = $this->final2 = array();
}
function _append(&$array, $lines) {
array_splice($array, sizeof($array), 0, $lines);
}
}
//Setup VIM: ex: et ts=4 :