DairikiDiff.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. * @defgroup DifferenceEngine DifferenceEngine
  26. */
  27. /**
  28. * The base class for all other DiffOp classes.
  29. *
  30. * The classes that extend DiffOp are: DiffOpCopy, DiffOpDelete, DiffOpAdd and
  31. * DiffOpChange. FakeDiffOp also extends DiffOp, but it is not located in this file.
  32. *
  33. * @private
  34. * @ingroup DifferenceEngine
  35. */
  36. abstract class DiffOp {
  37. /**
  38. * @var string
  39. */
  40. public $type;
  41. /**
  42. * @var string[]
  43. */
  44. public $orig;
  45. /**
  46. * @var string[]
  47. */
  48. public $closing;
  49. /**
  50. * @return string
  51. */
  52. public function getType() {
  53. return $this->type;
  54. }
  55. /**
  56. * @return string[]
  57. */
  58. public function getOrig() {
  59. return $this->orig;
  60. }
  61. /**
  62. * @param int|null $i
  63. * @return string[]|string|null
  64. */
  65. public function getClosing( $i = null ) {
  66. if ( $i === null ) {
  67. return $this->closing;
  68. }
  69. if ( array_key_exists( $i, $this->closing ) ) {
  70. return $this->closing[$i];
  71. }
  72. return null;
  73. }
  74. abstract public function reverse();
  75. /**
  76. * @return int
  77. */
  78. public function norig() {
  79. return $this->orig ? count( $this->orig ) : 0;
  80. }
  81. /**
  82. * @return int
  83. */
  84. public function nclosing() {
  85. return $this->closing ? count( $this->closing ) : 0;
  86. }
  87. }
  88. /**
  89. * Extends DiffOp. Used to mark strings that have been
  90. * copied from one string array to the other.
  91. *
  92. * @private
  93. * @ingroup DifferenceEngine
  94. */
  95. class DiffOpCopy extends DiffOp {
  96. public $type = 'copy';
  97. public function __construct( $orig, $closing = false ) {
  98. if ( !is_array( $closing ) ) {
  99. $closing = $orig;
  100. }
  101. $this->orig = $orig;
  102. $this->closing = $closing;
  103. }
  104. /**
  105. * @return DiffOpCopy
  106. */
  107. public function reverse() {
  108. return new DiffOpCopy( $this->closing, $this->orig );
  109. }
  110. }
  111. /**
  112. * Extends DiffOp. Used to mark strings that have been
  113. * deleted from the first string array.
  114. *
  115. * @private
  116. * @ingroup DifferenceEngine
  117. */
  118. class DiffOpDelete extends DiffOp {
  119. public $type = 'delete';
  120. public function __construct( $lines ) {
  121. $this->orig = $lines;
  122. $this->closing = false;
  123. }
  124. /**
  125. * @return DiffOpAdd
  126. */
  127. public function reverse() {
  128. return new DiffOpAdd( $this->orig );
  129. }
  130. }
  131. /**
  132. * Extends DiffOp. Used to mark strings that have been
  133. * added from the first string array.
  134. *
  135. * @private
  136. * @ingroup DifferenceEngine
  137. */
  138. class DiffOpAdd extends DiffOp {
  139. public $type = 'add';
  140. public function __construct( $lines ) {
  141. $this->closing = $lines;
  142. $this->orig = false;
  143. }
  144. /**
  145. * @return DiffOpDelete
  146. */
  147. public function reverse() {
  148. return new DiffOpDelete( $this->closing );
  149. }
  150. }
  151. /**
  152. * Extends DiffOp. Used to mark strings that have been
  153. * changed from the first string array (both added and subtracted).
  154. *
  155. * @private
  156. * @ingroup DifferenceEngine
  157. */
  158. class DiffOpChange extends DiffOp {
  159. public $type = 'change';
  160. public function __construct( $orig, $closing ) {
  161. $this->orig = $orig;
  162. $this->closing = $closing;
  163. }
  164. /**
  165. * @return DiffOpChange
  166. */
  167. public function reverse() {
  168. return new DiffOpChange( $this->closing, $this->orig );
  169. }
  170. }
  171. /**
  172. * Class representing a 'diff' between two sequences of strings.
  173. * @todo document
  174. * @private
  175. * @ingroup DifferenceEngine
  176. */
  177. class Diff {
  178. /**
  179. * @var DiffOp[]
  180. */
  181. public $edits;
  182. /**
  183. * @var int If this diff complexity is exceeded, a ComplexityException is thrown
  184. * 0 means no limit.
  185. */
  186. protected $bailoutComplexity = 0;
  187. /**
  188. * Computes diff between sequences of strings.
  189. *
  190. * @param string[] $from_lines An array of strings.
  191. * Typically these are lines from a file.
  192. * @param string[] $to_lines An array of strings.
  193. * @throws \MediaWiki\Diff\ComplexityException
  194. */
  195. public function __construct( $from_lines, $to_lines ) {
  196. $eng = new DiffEngine;
  197. $eng->setBailoutComplexity( $this->bailoutComplexity );
  198. $this->edits = $eng->diff( $from_lines, $to_lines );
  199. }
  200. /**
  201. * @return DiffOp[]
  202. */
  203. public function getEdits() {
  204. return $this->edits;
  205. }
  206. /**
  207. * Compute reversed Diff.
  208. *
  209. * SYNOPSIS:
  210. *
  211. * $diff = new Diff($lines1, $lines2);
  212. * $rev = $diff->reverse();
  213. *
  214. * @return Object A Diff object representing the inverse of the
  215. * original diff.
  216. */
  217. public function reverse() {
  218. $rev = $this;
  219. $rev->edits = [];
  220. /** @var DiffOp $edit */
  221. foreach ( $this->edits as $edit ) {
  222. $rev->edits[] = $edit->reverse();
  223. }
  224. return $rev;
  225. }
  226. /**
  227. * Check for empty diff.
  228. *
  229. * @return bool True if two sequences were identical.
  230. */
  231. public function isEmpty() {
  232. foreach ( $this->edits as $edit ) {
  233. if ( $edit->type != 'copy' ) {
  234. return false;
  235. }
  236. }
  237. return true;
  238. }
  239. /**
  240. * Compute the length of the Longest Common Subsequence (LCS).
  241. *
  242. * This is mostly for diagnostic purposed.
  243. *
  244. * @return int The length of the LCS.
  245. */
  246. public function lcs() {
  247. $lcs = 0;
  248. foreach ( $this->edits as $edit ) {
  249. if ( $edit->type == 'copy' ) {
  250. $lcs += count( $edit->orig );
  251. }
  252. }
  253. return $lcs;
  254. }
  255. /**
  256. * Get the original set of lines.
  257. *
  258. * This reconstructs the $from_lines parameter passed to the
  259. * constructor.
  260. *
  261. * @return string[] The original sequence of strings.
  262. */
  263. public function orig() {
  264. $lines = [];
  265. foreach ( $this->edits as $edit ) {
  266. if ( $edit->orig ) {
  267. array_splice( $lines, count( $lines ), 0, $edit->orig );
  268. }
  269. }
  270. return $lines;
  271. }
  272. /**
  273. * Get the closing set of lines.
  274. *
  275. * This reconstructs the $to_lines parameter passed to the
  276. * constructor.
  277. *
  278. * @return string[] The sequence of strings.
  279. */
  280. public function closing() {
  281. $lines = [];
  282. foreach ( $this->edits as $edit ) {
  283. if ( $edit->closing ) {
  284. array_splice( $lines, count( $lines ), 0, $edit->closing );
  285. }
  286. }
  287. return $lines;
  288. }
  289. }
  290. /**
  291. * @deprecated Alias for WordAccumulator, to be soon removed
  292. */
  293. class HWLDFWordAccumulator extends MediaWiki\Diff\WordAccumulator {
  294. }