Diff.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Drupal\Component\Diff;
  3. use Drupal\Component\Diff\Engine\DiffEngine;
  4. /**
  5. * Class representing a 'diff' between two sequences of strings.
  6. * @todo document
  7. * @subpackage DifferenceEngine
  8. *
  9. * Copied from https://www.drupal.org/project/diff which was based PHP diff
  10. * engine for phpwiki. (Taken from phpwiki-1.3.3) The original code in phpwiki
  11. * was copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org> and
  12. * licensed under GPL.
  13. */
  14. class Diff {
  15. /**
  16. * The list of differences as an array of diff operations.
  17. *
  18. * @var \Drupal\Component\Diff\Engine\DiffOp[]
  19. */
  20. protected $edits;
  21. /**
  22. * Constructor.
  23. * Computes diff between sequences of strings.
  24. *
  25. * @param array $from_lines
  26. * An array of strings.
  27. * (Typically these are lines from a file.)
  28. * @param array $to_lines
  29. * An array of strings.
  30. */
  31. public function __construct($from_lines, $to_lines) {
  32. $eng = new DiffEngine();
  33. $this->edits = $eng->diff($from_lines, $to_lines);
  34. //$this->_check($from_lines, $to_lines);
  35. }
  36. /**
  37. * Compute reversed Diff.
  38. *
  39. * SYNOPSIS:
  40. *
  41. * $diff = new Diff($lines1, $lines2);
  42. * $rev = $diff->reverse();
  43. * @return object
  44. * A Diff object representing the inverse of the original diff.
  45. */
  46. public function reverse() {
  47. $rev = $this;
  48. $rev->edits = [];
  49. foreach ($this->edits as $edit) {
  50. $rev->edits[] = $edit->reverse();
  51. }
  52. return $rev;
  53. }
  54. /**
  55. * Check for empty diff.
  56. *
  57. * @return bool True iff two sequences were identical.
  58. */
  59. public function isEmpty() {
  60. foreach ($this->edits as $edit) {
  61. if ($edit->type != 'copy') {
  62. return FALSE;
  63. }
  64. }
  65. return TRUE;
  66. }
  67. /**
  68. * Compute the length of the Longest Common Subsequence (LCS).
  69. *
  70. * This is mostly for diagnostic purposed.
  71. *
  72. * @return int The length of the LCS.
  73. */
  74. public function lcs() {
  75. $lcs = 0;
  76. foreach ($this->edits as $edit) {
  77. if ($edit->type == 'copy') {
  78. $lcs += sizeof($edit->orig);
  79. }
  80. }
  81. return $lcs;
  82. }
  83. /**
  84. * Gets the original set of lines.
  85. *
  86. * This reconstructs the $from_lines parameter passed to the
  87. * constructor.
  88. *
  89. * @return array The original sequence of strings.
  90. */
  91. public function orig() {
  92. $lines = [];
  93. foreach ($this->edits as $edit) {
  94. if ($edit->orig) {
  95. array_splice($lines, sizeof($lines), 0, $edit->orig);
  96. }
  97. }
  98. return $lines;
  99. }
  100. /**
  101. * Gets the closing set of lines.
  102. *
  103. * This reconstructs the $to_lines parameter passed to the
  104. * constructor.
  105. *
  106. * @return array The sequence of strings.
  107. */
  108. public function closing() {
  109. $lines = [];
  110. foreach ($this->edits as $edit) {
  111. if ($edit->closing) {
  112. array_splice($lines, sizeof($lines), 0, $edit->closing);
  113. }
  114. }
  115. return $lines;
  116. }
  117. /**
  118. * Check a Diff for validity.
  119. *
  120. * This is here only for debugging purposes.
  121. */
  122. public function check($from_lines, $to_lines) {
  123. if (serialize($from_lines) != serialize($this->orig())) {
  124. trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
  125. }
  126. if (serialize($to_lines) != serialize($this->closing())) {
  127. trigger_error("Reconstructed closing doesn't match", E_USER_ERROR);
  128. }
  129. $rev = $this->reverse();
  130. if (serialize($to_lines) != serialize($rev->orig())) {
  131. trigger_error("Reversed original doesn't match", E_USER_ERROR);
  132. }
  133. if (serialize($from_lines) != serialize($rev->closing())) {
  134. trigger_error("Reversed closing doesn't match", E_USER_ERROR);
  135. }
  136. $prevtype = 'none';
  137. foreach ($this->edits as $edit) {
  138. if ( $prevtype == $edit->type ) {
  139. trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
  140. }
  141. $prevtype = $edit->type;
  142. }
  143. $lcs = $this->lcs();
  144. trigger_error('Diff okay: LCS = ' . $lcs, E_USER_NOTICE);
  145. }
  146. /**
  147. * Gets the list of differences as an array of diff operations.
  148. *
  149. * @return \Drupal\Component\Diff\Engine\DiffOp[]
  150. * The list of differences as an array of diff operations.
  151. */
  152. public function getEdits() {
  153. return $this->edits;
  154. }
  155. }