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-1.1.0 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.
29 * Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
30 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
33 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
44 * Computes diffs between sequences of strings.
46 * @param array $from_lines An array of strings. Typically these are lines from a file.
47 * @param array $to_lines An array of strings.
49 function diff(&$from_content, &$to_content, $preserve_cr = true)
51 $diff_engine = new diff_engine();
52 $this->_edits = $diff_engine->diff($from_content, $to_content, $preserve_cr);
56 * Returns the array of differences.
64 * returns the number of new (added) lines in a given diff.
66 * @since Text_Diff 1.1.0
68 * @return integer The number of new lines
70 function count_added_lines()
74 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
76 $edit = $this->_edits[$i];
78 if (is_a($edit, 'diff_op_add') || is_a($edit, 'diff_op_change'))
80 $count += $edit->nfinal();
87 * Returns the number of deleted (removed) lines in a given diff.
89 * @since Text_Diff 1.1.0
91 * @return integer The number of deleted lines
93 function count_deleted_lines()
97 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
99 $edit = $this->_edits[$i];
101 if (is_a($edit, 'diff_op_delete') || is_a($edit, 'diff_op_change'))
103 $count += $edit->norig();
110 * Computes a reversed diff.
114 * $diff = new diff($lines1, $lines2);
115 * $rev = $diff->reverse();
118 * @return diff A Diff object representing the inverse of the original diff.
119 * Note that we purposely don't return a reference here, since
120 * this essentially is a clone() method.
124 if (version_compare(zend_version(), '2', '>'))
133 $rev->_edits = array();
135 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
137 $edit = $this->_edits[$i];
138 $rev->_edits[] = $edit->reverse();
145 * Checks for an empty diff.
147 * @return boolean True if two sequences were identical.
151 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
153 $edit = $this->_edits[$i];
156 if (is_a($edit, 'diff_op_copy'))
161 if (is_a($edit, 'diff_op_delete') || is_a($edit, 'diff_op_add'))
164 $final = $edit->final;
166 // We can simplify one case where the array is usually supposed to be empty...
167 if (sizeof($orig) == 1 && trim($orig[0]) === '') $orig = array();
168 if (sizeof($final) == 1 && trim($final[0]) === '') $final = array();
170 if (!$orig && !$final)
185 * Computes the length of the Longest Common Subsequence (LCS).
187 * This is mostly for diagnostic purposes.
189 * @return integer The length of the LCS.
195 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
197 $edit = $this->_edits[$i];
199 if (is_a($edit, 'diff_op_copy'))
201 $lcs += sizeof($edit->orig);
208 * Gets the original set of lines.
210 * This reconstructs the $from_lines parameter passed to the constructor.
212 * @return array The original sequence of strings.
214 function get_original()
218 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
220 $edit = $this->_edits[$i];
224 array_splice($lines, sizeof($lines), 0, $edit->orig);
231 * Gets the final set of lines.
233 * This reconstructs the $to_lines parameter passed to the constructor.
235 * @return array The sequence of strings.
241 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
243 $edit = $this->_edits[$i];
247 array_splice($lines, sizeof($lines), 0, $edit->final);
254 * Removes trailing newlines from a line of text. This is meant to be used with array_walk().
256 * @param string &$line The line to trim.
257 * @param integer $key The index of the line in the array. Not used.
259 function trim_newlines(&$line, $key)
261 $line = str_replace(array("\n", "\r"), '', $line);
265 * Checks a diff for validity.
267 * This is here only for debugging purposes.
269 function _check($from_lines, $to_lines)
271 if (serialize($from_lines) != serialize($this->get_original()))
273 trigger_error("[diff] Reconstructed original doesn't match", E_USER_ERROR);
276 if (serialize($to_lines) != serialize($this->get_final()))
278 trigger_error("[diff] Reconstructed final doesn't match", E_USER_ERROR);
281 $rev = $this->reverse();
283 if (serialize($to_lines) != serialize($rev->get_original()))
285 trigger_error("[diff] Reversed original doesn't match", E_USER_ERROR);
288 if (serialize($from_lines) != serialize($rev->get_final()))
290 trigger_error("[diff] Reversed final doesn't match", E_USER_ERROR);
295 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
297 $edit = $this->_edits[$i];
299 if ($prevtype == get_class($edit))
301 trigger_error("[diff] Edit sequence is non-optimal", E_USER_ERROR);
303 $prevtype = get_class($edit);
312 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
314 class mapped_diff extends diff
317 * Computes a diff between sequences of strings.
319 * This can be used to compute things like case-insensitve diffs, or diffs
320 * which ignore changes in white-space.
322 * @param array $from_lines An array of strings.
323 * @param array $to_lines An array of strings.
324 * @param array $mapped_from_lines This array should have the same size number of elements as $from_lines.
325 * The elements in $mapped_from_lines and $mapped_to_lines are what is actually
326 * compared when computing the diff.
327 * @param array $mapped_to_lines This array should have the same number of elements as $to_lines.
329 function mapped_diff(&$from_lines, &$to_lines, &$mapped_from_lines, &$mapped_to_lines)
331 if (sizeof($from_lines) != sizeof($mapped_from_lines) || sizeof($to_lines) != sizeof($mapped_to_lines))
336 parent::diff($mapped_from_lines, $mapped_to_lines);
339 for ($i = 0; $i < sizeof($this->_edits); $i++)
341 $orig = &$this->_edits[$i]->orig;
344 $orig = array_slice($from_lines, $xi, sizeof($orig));
345 $xi += sizeof($orig);
348 $final = &$this->_edits[$i]->final;
349 if (is_array($final))
351 $final = array_slice($to_lines, $yi, sizeof($final));
352 $yi += sizeof($final);
360 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
371 trigger_error('[diff] Abstract method', E_USER_ERROR);
376 return ($this->orig) ? sizeof($this->orig) : 0;
381 return ($this->final) ? sizeof($this->final) : 0;
387 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
391 class diff_op_copy extends diff_op
393 function diff_op_copy($orig, $final = false)
395 if (!is_array($final))
400 $this->final = $final;
405 $reverse = new diff_op_copy($this->final, $this->orig);
412 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
416 class diff_op_delete extends diff_op
418 function diff_op_delete($lines)
420 $this->orig = $lines;
421 $this->final = false;
426 $reverse = new diff_op_add($this->orig);
433 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
437 class diff_op_add extends diff_op
439 function diff_op_add($lines)
441 $this->final = $lines;
447 $reverse = new diff_op_delete($this->final);
454 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
458 class diff_op_change extends diff_op
460 function diff_op_change($orig, $final)
463 $this->final = $final;
468 $reverse = new diff_op_change($this->final, $this->orig);
475 * A class for computing three way diffs.
478 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
480 class diff3 extends diff
486 var $_conflicting_blocks = 0;
489 * Computes diff between 3 sequences of strings.
491 * @param array $orig The original lines to use.
492 * @param array $final1 The first version to compare to.
493 * @param array $final2 The second version to compare to.
495 function diff3(&$orig, &$final1, &$final2, $preserve_cr = true)
497 $diff_engine = new diff_engine();
499 $diff_1 = $diff_engine->diff($orig, $final1, $preserve_cr);
500 $diff_2 = $diff_engine->diff($orig, $final2, $preserve_cr);
504 $this->_edits = $this->_diff3($diff_1, $diff_2);
508 * Return number of conflicts
510 function get_num_conflicts()
514 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
516 $edit = $this->_edits[$i];
518 if ($edit->is_conflict())
528 * Get conflicts content for download. This is generally a merged file, but preserving conflicts and adding explanations to it.
529 * A user could then go through this file, search for the conflicts and changes the code accordingly.
531 * @param string $label1 the cvs file version/label from the original set of lines
532 * @param string $label2 the cvs file version/label from the new set of lines
533 * @param string $label_sep the explanation between label1 and label2 - more of a helper for the user
535 * @return mixed the merged output
537 function get_conflicts_content($label1 = 'CURRENT_FILE', $label2 = 'NEW_FILE', $label_sep = 'DIFF_SEP_EXPLAIN')
541 $label1 = (!empty($user->lang[$label1])) ? $user->lang[$label1] : $label1;
542 $label2 = (!empty($user->lang[$label2])) ? $user->lang[$label2] : $label2;
543 $label_sep = (!empty($user->lang[$label_sep])) ? $user->lang[$label_sep] : $label_sep;
547 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
549 $edit = $this->_edits[$i];
551 if ($edit->is_conflict())
553 // Start conflict label
554 $label_start = array('<<<<<<< ' . $label1);
555 $label_mid = array('======= ' . $label_sep);
556 $label_end = array('>>>>>>> ' . $label2);
558 $lines = array_merge($lines, $label_start, $edit->final1, $label_mid, $edit->final2, $label_end);
559 $this->_conflicting_blocks++;
563 $lines = array_merge($lines, $edit->merged());
571 * Return merged output (used by the renderer)
573 * @return mixed the merged output
575 function merged_output()
577 return $this->get_conflicts_content();
581 * Merge the output and use the new file code for conflicts
583 function merged_new_output()
587 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
589 $edit = $this->_edits[$i];
591 if ($edit->is_conflict())
593 $lines = array_merge($lines, $edit->final2);
597 $lines = array_merge($lines, $edit->merged());
605 * Merge the output and use the original file code for conflicts
607 function merged_orig_output()
611 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
613 $edit = $this->_edits[$i];
615 if ($edit->is_conflict())
617 $lines = array_merge($lines, $edit->final1);
621 $lines = array_merge($lines, $edit->merged());
629 * Get conflicting block(s)
631 function get_conflicts()
633 $conflicts = array();
635 for ($i = 0, $size = sizeof($this->_edits); $i < $size; $i++)
637 $edit = $this->_edits[$i];
639 if ($edit->is_conflict())
641 $conflicts[] = array($edit->final1, $edit->final2);
651 function _diff3(&$edits1, &$edits2)
654 $bb = new diff3_block_builder();
656 $e1 = current($edits1);
657 $e2 = current($edits2);
661 if ($e1 && $e2 && is_a($e1, 'diff_op_copy') && is_a($e2, 'diff_op_copy'))
663 // We have copy blocks from both diffs. This is the (only) time we want to emit a diff3 copy block.
664 // Flush current diff3 diff block, if any.
665 if ($edit = $bb->finish())
670 $ncopy = min($e1->norig(), $e2->norig());
671 $edits[] = new diff3_op_copy(array_slice($e1->orig, 0, $ncopy));
673 if ($e1->norig() > $ncopy)
675 array_splice($e1->orig, 0, $ncopy);
676 array_splice($e1->final, 0, $ncopy);
683 if ($e2->norig() > $ncopy)
685 array_splice($e2->orig, 0, $ncopy);
686 array_splice($e2->final, 0, $ncopy);
697 if ($e1->orig && $e2->orig)
699 $norig = min($e1->norig(), $e2->norig());
700 $orig = array_splice($e1->orig, 0, $norig);
701 array_splice($e2->orig, 0, $norig);
709 if (is_a($e1, 'diff_op_copy'))
711 $bb->out1(array_splice($e1->final, 0, $norig));
714 if (is_a($e2, 'diff_op_copy'))
716 $bb->out2(array_splice($e2->final, 0, $norig));
720 if ($e1 && ! $e1->orig)
722 $bb->out1($e1->final);
726 if ($e2 && ! $e2->orig)
728 $bb->out2($e2->final);
734 if ($edit = $bb->finish())
745 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
751 function diff3_op($orig = false, $final1 = false, $final2 = false)
753 $this->orig = $orig ? $orig : array();
754 $this->final1 = $final1 ? $final1 : array();
755 $this->final2 = $final2 ? $final2 : array();
760 if (!isset($this->_merged))
762 // Prepare the arrays before we compare them. ;)
763 $this->solve_prepare();
765 if ($this->final1 === $this->final2)
767 $this->_merged = &$this->final1;
769 else if ($this->final1 === $this->orig)
771 $this->_merged = &$this->final2;
773 else if ($this->final2 === $this->orig)
775 $this->_merged = &$this->final1;
779 // The following tries to aggressively solve conflicts...
780 $this->_merged = false;
781 $this->solve_conflict();
785 return $this->_merged;
788 function is_conflict()
790 return ($this->merged() === false) ? true : false;
794 * Function to prepare the arrays for comparing - we want to skip over newline changes
797 function solve_prepare()
799 // We can simplify one case where the array is usually supposed to be empty...
800 if (sizeof($this->orig) == 1 && trim($this->orig[0]) === '') $this->orig = array();
801 if (sizeof($this->final1) == 1 && trim($this->final1[0]) === '') $this->final1 = array();
802 if (sizeof($this->final2) == 1 && trim($this->final2[0]) === '') $this->final2 = array();
804 // Now we only can have the case where the only difference between arrays are newlines, so compare all cases
806 // First, some strings we can compare...
807 $orig = $final1 = $final2 = '';
809 foreach ($this->orig as $null => $line) $orig .= trim($line);
810 foreach ($this->final1 as $null => $line) $final1 .= trim($line);
811 foreach ($this->final2 as $null => $line) $final2 .= trim($line);
814 if ($final1 === $final2)
816 // We preserve the part which will be used in the merge later
817 $this->final2 = $this->final1;
820 else if ($final1 === $orig)
822 // Here it does not really matter what we choose, but we will use the new code
823 $this->orig = $this->final1;
826 else if ($final2 === $orig)
828 // Here it does not really matter too (final1 will be used), but we will use the new code
829 $this->orig = $this->final2;
834 * Find code portions from $orig in $final1 and use $final2 as merged instance if provided
837 function _compare_conflict_seq($orig, $final1, $final2 = false)
839 $result = array('merge_found' => false, 'merge' => array());
841 $_orig = &$this->$orig;
842 $_final1 = &$this->$final1;
844 // Ok, we basically search for $orig in $final1
845 $compare_seq = sizeof($_orig);
847 // Go through the conflict code
848 for ($i = 0, $j = 0, $size = sizeof($_final1); $i < $size; $i++, $j = $i)
850 $line = $_final1[$i];
853 for ($x = 0; $x < $compare_seq; $x++)
855 // Try to skip all matching lines
856 if (trim($line) === trim($_orig[$x]))
858 $line = (++$j < $size) ? $_final1[$j] : $line;
863 if ($skip === $compare_seq)
865 $result['merge_found'] = true;
867 if ($final2 !== false)
869 $result['merge'] = array_merge($result['merge'], $this->$final2);
873 else if ($final2 !== false)
875 $result['merge'][] = $line;
883 * Tries to solve conflicts aggressively based on typical "assumptions"
886 function solve_conflict()
888 $this->_merged = false;
890 // CASE ONE: orig changed into final2, but modified/unknown code in final1.
891 // IF orig is found "as is" in final1 we replace the code directly in final1 and populate this as final2/merge
892 if (sizeof($this->orig) && sizeof($this->final2))
894 $result = $this->_compare_conflict_seq('orig', 'final1', 'final2');
896 if ($result['merge_found'])
898 $this->final2 = $result['merge'];
899 $this->_merged = &$this->final2;
903 $result = $this->_compare_conflict_seq('final2', 'final1');
905 if ($result['merge_found'])
907 $this->_merged = &$this->final1;
911 // Try to solve $Id$ issues. ;)
912 if (sizeof($this->orig) == 1 && sizeof($this->final1) == 1 && sizeof($this->final2) == 1)
914 $match = '#^' . preg_quote('* @version $Id: ', '#') . '[a-z\._\- ]+[0-9]+ [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9\:Z]+ [a-z0-9_\- ]+\$$#';
916 if (preg_match($match, $this->orig[0]) && preg_match($match, $this->final1[0]) && preg_match($match, $this->final2[0]))
918 $this->_merged = &$this->final2;
925 // Try to solve issues where the only reason why the above did not work is a newline being removed in the final1 code but exist in the orig/final2 code
926 if (trim($this->orig[0]) === '' && trim($this->final2[0]) === '')
928 unset($this->orig[0], $this->final2[0]);
929 $this->orig = array_values($this->orig);
930 $this->final2 = array_values($this->final2);
935 // The same is true for a line at the end. ;)
936 if (sizeof($this->orig) && sizeof($this->final2) && sizeof($this->orig) === sizeof($this->final2) && trim($this->orig[sizeof($this->orig)-1]) === '' && trim($this->final2[sizeof($this->final2)-1]) === '')
938 unset($this->orig[sizeof($this->orig)-1], $this->final2[sizeof($this->final2)-1]);
939 $this->orig = array_values($this->orig);
940 $this->final2 = array_values($this->final2);
947 $result = $this->_compare_conflict_seq('orig', 'final1', 'final2');
949 if ($result['merge_found'])
951 $this->final2 = $result['merge'];
952 $this->_merged = &$this->final2;
956 $result = $this->_compare_conflict_seq('final2', 'final1');
958 if ($result['merge_found'])
960 $this->_merged = &$this->final1;
968 // CASE TWO: Added lines from orig to final2 but final1 had added lines too. Just merge them.
969 if (!sizeof($this->orig) && $this->final1 !== $this->final2 && sizeof($this->final1) && sizeof($this->final2))
971 $result = $this->_compare_conflict_seq('final2', 'final1');
973 if ($result['merge_found'])
975 $this->final2 = $this->final1;
976 $this->_merged = &$this->final1;
980 $result = $this->_compare_conflict_seq('final1', 'final2');
982 if (!$result['merge_found'])
984 $this->final2 = array_merge($this->final1, $this->final2);
985 $this->_merged = &$this->final2;
989 $this->final2 = $this->final1;
990 $this->_merged = &$this->final1;
997 // CASE THREE: Removed lines (orig has the to-remove line(s), but final1 has additional lines which does not need to be removed). Just remove orig from final1 and then use final1 as final2/merge
998 if (!sizeof($this->final2) && sizeof($this->orig) && sizeof($this->final1) && $this->orig !== $this->final1)
1000 $result = $this->_compare_conflict_seq('orig', 'final1');
1002 if (!$result['merge_found'])
1007 // First of all, try to find the code in orig in final1. ;)
1008 $compare_seq = sizeof($this->orig);
1012 for ($i = 0, $size = sizeof($this->final1); $i < $size; $i++)
1014 $line = $this->final1[$i];
1016 if (trim($line) === trim($this->orig[$j]))
1024 // End is always $i, the last found line
1027 if (isset($this->orig[$j+1]))
1034 if ($begin !== -1 && $begin + ($compare_seq - 1) == $end)
1036 foreach ($this->final1 as $i => $line)
1038 if ($i < $begin || $i > $end)
1044 $this->final2 = $merged;
1045 $this->_merged = &$this->final2;
1057 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
1061 class diff3_op_copy extends diff3_op
1063 function diff3_op_copy($lines = false)
1065 $this->orig = $lines ? $lines : array();
1066 $this->final1 = &$this->orig;
1067 $this->final2 = &$this->orig;
1075 function is_conflict()
1083 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
1087 class diff3_block_builder
1089 function diff3_block_builder()
1094 function input($lines)
1098 $this->_append($this->orig, $lines);
1102 function out1($lines)
1106 $this->_append($this->final1, $lines);
1110 function out2($lines)
1114 $this->_append($this->final2, $lines);
1120 return !$this->orig && !$this->final1 && !$this->final2;
1125 if ($this->is_empty())
1131 $edit = new diff3_op($this->orig, $this->final1, $this->final2);
1139 $this->orig = $this->final1 = $this->final2 = array();
1142 function _append(&$array, $lines)
1144 array_splice($array, sizeof($array), 0, $lines);