5
* @version $Id: diff.php,v 1.8 2007/10/05 14:36:33 acydburn Exp $
6
* @copyright (c) 2006 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
if (!defined('IN_PHPBB'))
20
* Code from pear.php.net, Text_Diff-0.2.1 (beta) package
21
* http://pear.php.net/package/Text_Diff/
23
* Modified by phpBB Group to meet our coding standards
24
* and being able to integrate into phpBB
26
* General API for generating and formatting diffs - the differences between
27
* two sequences of strings.
30
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
41
* Computes diffs between sequences of strings.
43
* @param array $from_lines An array of strings. Typically these are lines from a file.
44
* @param array $to_lines An array of strings.
46
function diff(&$from_content, &$to_content, $preserve_cr = true)
48
$diff_engine = &new diff_engine();
49
$this->_edits = $diff_engine->diff($from_content, $to_content, $preserve_cr);
53
* Returns the array of differences.
61
* Computes a reversed diff.
65
* $diff = &new diff($lines1, $lines2);
66
* $rev = $diff->reverse();
69
* @return diff A Diff object representing the inverse of the original diff.
70
* Note that we purposely don't return a reference here, since
71
* this essentially is a clone() method.
75
if (version_compare(zend_version(), '2', '>'))
84
$rev->_edits = array();
86
foreach ($this->_edits as $edit)
88
$rev->_edits[] = $edit->reverse();
95
* Checks for an empty diff.
97
* @return boolean True if two sequences were identical.
101
foreach ($this->_edits as $edit)
103
if (!is_a($edit, 'diff_op_copy'))
112
* Computes the length of the Longest Common Subsequence (LCS).
114
* This is mostly for diagnostic purposes.
116
* @return integer The length of the LCS.
122
foreach ($this->_edits as $edit)
124
if (is_a($edit, 'diff_op_copy'))
126
$lcs += sizeof($edit->orig);
133
* Gets the original set of lines.
135
* This reconstructs the $from_lines parameter passed to the constructor.
137
* @return array The original sequence of strings.
139
function get_original()
143
foreach ($this->_edits as $edit)
147
array_splice($lines, sizeof($lines), 0, $edit->orig);
154
* Gets the final set of lines.
156
* This reconstructs the $to_lines parameter passed to the constructor.
158
* @return array The sequence of strings.
164
foreach ($this->_edits as $edit)
168
array_splice($lines, sizeof($lines), 0, $edit->final);
175
* Removes trailing newlines from a line of text. This is meant to be used with array_walk().
177
* @param string &$line The line to trim.
178
* @param integer $key The index of the line in the array. Not used.
180
function trim_newlines(&$line, $key)
182
$line = str_replace(array("\n", "\r"), '', $line);
186
* Checks a diff for validity.
188
* This is here only for debugging purposes.
190
function _check($from_lines, $to_lines)
192
if (serialize($from_lines) != serialize($this->get_original()))
194
trigger_error("[diff] Reconstructed original doesn't match", E_USER_ERROR);
197
if (serialize($to_lines) != serialize($this->get_final()))
199
trigger_error("[diff] Reconstructed final doesn't match", E_USER_ERROR);
202
$rev = $this->reverse();
204
if (serialize($to_lines) != serialize($rev->get_original()))
206
trigger_error("[diff] Reversed original doesn't match", E_USER_ERROR);
209
if (serialize($from_lines) != serialize($rev->get_final()))
211
trigger_error("[diff] Reversed final doesn't match", E_USER_ERROR);
216
foreach ($this->_edits as $edit)
218
if ($prevtype == get_class($edit))
220
trigger_error("[diff] Edit sequence is non-optimal", E_USER_ERROR);
222
$prevtype = get_class($edit);
231
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
233
class mapped_diff extends diff
236
* Computes a diff between sequences of strings.
238
* This can be used to compute things like case-insensitve diffs, or diffs
239
* which ignore changes in white-space.
241
* @param array $from_lines An array of strings.
242
* @param array $to_lines An array of strings.
243
* @param array $mapped_from_lines This array should have the same size number of elements as $from_lines.
244
* The elements in $mapped_from_lines and $mapped_to_lines are what is actually
245
* compared when computing the diff.
246
* @param array $mapped_to_lines This array should have the same number of elements as $to_lines.
248
function mapped_diff(&$from_lines, &$to_lines, &$mapped_from_lines, &$mapped_to_lines)
250
if (sizeof($from_lines) != sizeof($mapped_from_lines) || sizeof($to_lines) != sizeof($mapped_to_lines))
255
parent::diff($mapped_from_lines, $mapped_to_lines);
258
for ($i = 0; $i < sizeof($this->_edits); $i++)
260
$orig = &$this->_edits[$i]->orig;
263
$orig = array_slice($from_lines, $xi, sizeof($orig));
264
$xi += sizeof($orig);
267
$final = &$this->_edits[$i]->final;
268
if (is_array($final))
270
$final = array_slice($to_lines, $yi, sizeof($final));
271
$yi += sizeof($final);
279
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
290
trigger_error('[diff] Abstract method', E_USER_ERROR);
295
return ($this->orig) ? sizeof($this->orig) : 0;
300
return ($this->final) ? sizeof($this->final) : 0;
306
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
310
class diff_op_copy extends diff_op
312
function diff_op_copy($orig, $final = false)
314
if (!is_array($final))
319
$this->final = $final;
324
$reverse = &new diff_op_copy($this->final, $this->orig);
331
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
335
class diff_op_delete extends diff_op
337
function diff_op_delete($lines)
339
$this->orig = $lines;
340
$this->final = false;
345
$reverse = &new diff_op_add($this->orig);
352
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
356
class diff_op_add extends diff_op
358
function diff_op_add($lines)
360
$this->final = $lines;
366
$reverse = &new diff_op_delete($this->final);
373
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
377
class diff_op_change extends diff_op
379
function diff_op_change($orig, $final)
382
$this->final = $final;
387
$reverse = &new diff_op_change($this->final, $this->orig);
394
* A class for computing three way diffs.
397
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
399
class diff3 extends diff
405
var $_conflicting_blocks = 0;
408
* Computes diff between 3 sequences of strings.
410
* @param array $orig The original lines to use.
411
* @param array $final1 The first version to compare to.
412
* @param array $final2 The second version to compare to.
414
function diff3(&$orig, &$final1, &$final2)
416
$diff_engine = &new diff_engine();
418
$diff_1 = $diff_engine->diff($orig, $final1);
419
$diff_2 = $diff_engine->diff($orig, $final2);
423
$this->_edits = $this->_diff3($diff_1, $diff_2);
427
* Return merged output
429
* @param string $label1 the cvs file version/label from the original set of lines
430
* @param string $label2 the cvs file version/label from the new set of lines
431
* @param string $label_sep the explanation between label1 and label2 - more of a helper for the user
432
* @param bool $get_conflicts if set to true only the number of conflicts is returned
433
* @param bool $merge_new if set to true the merged output will have the new file contents on a conflicting merge
435
* @return mixed the merged output
437
function merged_output($label1 = 'CURRENT_FILE', $label2 = 'NEW_FILE', $label_sep = 'DIFF_SEP_EXPLAIN', $get_conflicts = false, $merge_new = false)
443
foreach ($this->_edits as $edit)
445
if ($edit->is_conflict())
447
$this->_conflicting_blocks++;
451
return $this->_conflicting_blocks;
454
$label1 = (!empty($user->lang[$label1])) ? $user->lang[$label1] : $label1;
455
$label2 = (!empty($user->lang[$label2])) ? $user->lang[$label2] : $label2;
456
$label_sep = (!empty($user->lang[$label_sep])) ? $user->lang[$label_sep] : $label_sep;
460
foreach ($this->_edits as $edit)
462
if ($edit->is_conflict())
466
$lines = array_merge($lines, array('<<<<<<<' . ($label1 ? ' ' . $label1 : '')), $edit->final1, array('=======' . ($label_sep ? ' ' . $label_sep : '')), $edit->final2, array('>>>>>>>' . ($label2 ? ' ' . $label2 : '')));
470
$lines = array_merge($lines, $edit->final1);
472
$this->_conflicting_blocks++;
476
$lines = array_merge($lines, $edit->merged());
484
* Merge the output and use the new file code for conflicts
486
function merged_new_output()
490
foreach ($this->_edits as $edit)
492
if ($edit->is_conflict())
494
$lines = array_merge($lines, $edit->final2);
498
$lines = array_merge($lines, $edit->merged());
506
* Merge the output and use the original file code for conflicts
508
function merged_orig_output()
512
foreach ($this->_edits as $edit)
514
if ($edit->is_conflict())
516
$lines = array_merge($lines, $edit->final1);
520
$lines = array_merge($lines, $edit->merged());
528
* Get conflicting block(s)
530
function get_conflicts()
532
$conflicts = array();
534
foreach ($this->_edits as $edit)
536
if ($edit->is_conflict())
538
$conflicts[] = array($edit->final1, $edit->final2);
548
function _diff3(&$edits1, &$edits2)
551
$bb = &new diff3_block_builder();
553
$e1 = current($edits1);
554
$e2 = current($edits2);
558
if ($e1 && $e2 && is_a($e1, 'diff_op_copy') && is_a($e2, 'diff_op_copy'))
560
// We have copy blocks from both diffs. This is the (only) time we want to emit a diff3 copy block.
561
// Flush current diff3 diff block, if any.
562
if ($edit = $bb->finish())
567
$ncopy = min($e1->norig(), $e2->norig());
568
$edits[] = &new diff3_op_copy(array_slice($e1->orig, 0, $ncopy));
570
if ($e1->norig() > $ncopy)
572
array_splice($e1->orig, 0, $ncopy);
573
array_splice($e1->final, 0, $ncopy);
580
if ($e2->norig() > $ncopy)
582
array_splice($e2->orig, 0, $ncopy);
583
array_splice($e2->final, 0, $ncopy);
594
if ($e1->orig && $e2->orig)
596
$norig = min($e1->norig(), $e2->norig());
597
$orig = array_splice($e1->orig, 0, $norig);
598
array_splice($e2->orig, 0, $norig);
606
if (is_a($e1, 'diff_op_copy'))
608
$bb->out1(array_splice($e1->final, 0, $norig));
611
if (is_a($e2, 'diff_op_copy'))
613
$bb->out2(array_splice($e2->final, 0, $norig));
617
if ($e1 && ! $e1->orig)
619
$bb->out1($e1->final);
623
if ($e2 && ! $e2->orig)
625
$bb->out2($e2->final);
631
if ($edit = $bb->finish())
642
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
648
function diff3_op($orig = false, $final1 = false, $final2 = false)
650
$this->orig = $orig ? $orig : array();
651
$this->final1 = $final1 ? $final1 : array();
652
$this->final2 = $final2 ? $final2 : array();
657
if (!isset($this->_merged))
659
if ($this->final1 === $this->final2)
661
$this->_merged = &$this->final1;
663
else if ($this->final1 === $this->orig)
665
$this->_merged = &$this->final2;
667
else if ($this->final2 === $this->orig)
669
$this->_merged = &$this->final1;
673
$this->_merged = false;
677
return $this->_merged;
680
function is_conflict()
682
return ($this->merged() === false) ? true : false;
688
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
692
class diff3_op_copy extends diff3_op
694
function diff3_op_copy($lines = false)
696
$this->orig = $lines ? $lines : array();
697
$this->final1 = &$this->orig;
698
$this->final2 = &$this->orig;
706
function is_conflict()
714
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
718
class diff3_block_builder
720
function diff3_block_builder()
725
function input($lines)
729
$this->_append($this->orig, $lines);
733
function out1($lines)
737
$this->_append($this->final1, $lines);
741
function out2($lines)
745
$this->_append($this->final2, $lines);
751
return !$this->orig && !$this->final1 && !$this->final2;
756
if ($this->is_empty())
762
$edit = &new diff3_op($this->orig, $this->final1, $this->final2);
770
$this->orig = $this->final1 = $this->final2 = array();
773
function _append(&$array, $lines)
775
array_splice($array, sizeof($array), 0, $lines);
b'\\ No newline at end of file'