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