Diff.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)
  4. *
  5. * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
  6. * You may copy this code freely under the conditions of the GPL.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. * @ingroup DifferenceEngine
  25. */
  26. /**
  27. * Class representing a 'diff' between two sequences of strings.
  28. * @private
  29. * @ingroup DifferenceEngine
  30. */
  31. class Diff {
  32. /**
  33. * @var DiffOp[]
  34. */
  35. public $edits;
  36. /**
  37. * @var int If this diff complexity is exceeded, a ComplexityException is thrown
  38. * 0 means no limit.
  39. */
  40. protected $bailoutComplexity = 0;
  41. /**
  42. * Computes diff between sequences of strings.
  43. *
  44. * @param string[] $from_lines An array of strings.
  45. * Typically these are lines from a file.
  46. * @param string[] $to_lines An array of strings.
  47. * @throws \MediaWiki\Diff\ComplexityException
  48. */
  49. public function __construct( $from_lines, $to_lines ) {
  50. $eng = new DiffEngine;
  51. $eng->setBailoutComplexity( $this->bailoutComplexity );
  52. $this->edits = $eng->diff( $from_lines, $to_lines );
  53. }
  54. /**
  55. * @return DiffOp[]
  56. */
  57. public function getEdits() {
  58. return $this->edits;
  59. }
  60. /**
  61. * Compute reversed Diff.
  62. *
  63. * SYNOPSIS:
  64. *
  65. * $diff = new Diff($lines1, $lines2);
  66. * $rev = $diff->reverse();
  67. *
  68. * @return Object A Diff object representing the inverse of the
  69. * original diff.
  70. */
  71. public function reverse() {
  72. $rev = $this;
  73. $rev->edits = [];
  74. /** @var DiffOp $edit */
  75. foreach ( $this->edits as $edit ) {
  76. $rev->edits[] = $edit->reverse();
  77. }
  78. return $rev;
  79. }
  80. /**
  81. * Check for empty diff.
  82. *
  83. * @return bool True if two sequences were identical.
  84. */
  85. public function isEmpty() {
  86. foreach ( $this->edits as $edit ) {
  87. if ( $edit->type != 'copy' ) {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. /**
  94. * Compute the length of the Longest Common Subsequence (LCS).
  95. *
  96. * This is mostly for diagnostic purposed.
  97. *
  98. * @return int The length of the LCS.
  99. */
  100. public function lcs() {
  101. $lcs = 0;
  102. foreach ( $this->edits as $edit ) {
  103. if ( $edit->type == 'copy' ) {
  104. $lcs += count( $edit->orig );
  105. }
  106. }
  107. return $lcs;
  108. }
  109. /**
  110. * Get the original set of lines.
  111. *
  112. * This reconstructs the $from_lines parameter passed to the
  113. * constructor.
  114. *
  115. * @return string[] The original sequence of strings.
  116. */
  117. public function orig() {
  118. $lines = [];
  119. foreach ( $this->edits as $edit ) {
  120. if ( $edit->orig ) {
  121. array_splice( $lines, count( $lines ), 0, $edit->orig );
  122. }
  123. }
  124. return $lines;
  125. }
  126. /**
  127. * Get the closing set of lines.
  128. *
  129. * This reconstructs the $to_lines parameter passed to the
  130. * constructor.
  131. *
  132. * @return string[] The sequence of strings.
  133. */
  134. public function closing() {
  135. $lines = [];
  136. foreach ( $this->edits as $edit ) {
  137. if ( $edit->closing ) {
  138. array_splice( $lines, count( $lines ), 0, $edit->closing );
  139. }
  140. }
  141. return $lines;
  142. }
  143. }