forked from gorhill/PHP-FineDiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewdiff-ex.php
311 lines (305 loc) · 13.1 KB
/
viewdiff-ex.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
body {
margin: 0;
border: 0;
padding: 0;
font-size: 11pt;
}
body > h1 {
margin:0 0 0.5em 0;
font: 2em sans-serif;
background-color: #def
}
body > div {
padding:2px;
}
p {
margin-top: 0;
}
ins {
color: green;
background: #dfd;
text-decoration: none;
}
del {
color: red;
background: #fdd;
text-decoration: none;
}
code {
font-size: smaller;
}
#params {
margin: 1em 0;
font: 14px sans-serif;
}
.code {
margin-left: 2em;
font: 12px monospace;
}
.ins {
background:#dfd;
}
.del {
background:#fdd;
}
.rep {
color: #008;
background: #eef;
}
.panecontainer {
display: inline-block;
width: 49.5%;
vertical-align: top;
}
.panecontainer > p {
margin: 0;
border: 1px solid #bcd;
border-bottom: none;
padding: 1px 3px;
background: #def;
font: 14px sans-serif
}
.panecontainer > p + div {
margin: 0;
padding: 2px 0 2px 2px;
border: 1px solid #bcd;
border-top: none;
}
.pane {
margin: 0;
padding: 0;
border: 0;
width: 100%;
min-height: 20em;
overflow:auto;
font: 12px monospace;
}
#htmldiff.onlyDeletions ins {display:none}
#htmldiff.onlyInsertions del {display:none}
</style>
<title>PHP Fine Diff</title>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-5586753-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'http://www.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<a href="https://github.com/gorhill/PHP-FineDiff"><img style="position:absolute;top:0;right:0;border:0;" src="https://d3nwyuy0nl342s.cloudfront.net/img/7afbc8b248c68eb468279e8c17986ad46549fb71/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub"></a>
<h1>PHP Fine Diff</h1>
<div>
<p style="margin-right:5em">This page demonstrate the <a href="finediff-code.php"><code>FineDiff</code></a> class (as in “<b>fine</b> granularity <b>diff</b>”) I wrote – starting from scratch – to generate a <u>lossless</u> (won't eat your line breaks), <u>compact</u> opcodes string listing the sequence of atomic actions (copy/delete/insert) necessary to transform one string into another (thereafter referred as the “From” and “To” string). The “To” string can be rebuilt by running the opcodes string on the “From” string. The <code>FineDiff</code> class allows to specify the granularity, and up to character-level granularity is possible, in order to generate the smallest diff possible (at the <i>potential</i> cost of increased CPU cycles.)</p>
<p>Typical usage:</p>
<p class="code">
include '<a href="finediff-code.php">finediff.php</a>';<br>
$opcodes = FineDiff::getDiffOpcodes($from_text, $to_text /* , default granularity is set to character */);<br>
// store opcodes for later use...</p>
<p>Later, <code>$to_text</code> can be re-created from <code>$from_text</code> using <code>$opcodes</code> as follow:</p>
<p class="code">
include '<a href="finediff-code.php">finediff.php</a>';<br>
$to_text = FineDiff::renderToTextFromOpcodes($from_text, $opcodes);
</p>
<p>Try it by inserting your own text, or <a href="viewdiff-ex?sample=1">Use sample text</a>, or <a href="viewdiff-ex">Start from scratch</a>, or just use the plain <a href="viewdiff.php">Online diff viewer</a>:</p>
<?php
// http://www.php.net/manual/en/function.get-magic-quotes-gpc.php#82524
function stripslashes_deep(&$value) {
$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
return $value;
}
if ( (function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc()) || (ini_get('magic_quotes_sybase') && strtolower(ini_get('magic_quotes_sybase'))!="off") ) {
stripslashes_deep($_GET);
stripslashes_deep($_POST);
stripslashes_deep($_REQUEST);
}
require_once 'finediff.php';
$from = '';
$to = '';
$granularity = 2;
if ( isset($_REQUEST['granularity']) && ctype_digit($_REQUEST['granularity']) ) {
$granularity = max(min(intval($_REQUEST['granularity']),3),0);
}
$rendered_diff = '';
if ( !empty($_REQUEST['from']) || !empty($_REQUEST['to'])) {
if ( !empty($_REQUEST['from']) ) {
$from = $_REQUEST['from'];
}
if ( !empty($_REQUEST['to']) ) {
$to = $_REQUEST['to'];
}
}
else if ( !empty($_REQUEST['sample']) ) { // use sample text?
// Sample text:
// http://en.wikipedia.org/w/index.php?title=Universe&action=historysubmit&diff=414830579&oldid=378547111
$from = file_get_contents('sample_from.txt');
$to = file_get_contents('sample_to.txt');
}
$from_len = strlen($from);
$to_len = strlen($to);
$use_stdlib_diff = !empty($_REQUEST['stdlib']) && ctype_digit($_REQUEST['stdlib']) && intval($_REQUEST['stdlib']) === 1;
require_once 'Text/Diff.php';
$start_time = gettimeofday(true);
if ( $use_stdlib_diff ) {
if ( $granularity < 3 ) {
$delimiters = array(
FineDiff::paragraphDelimiters,
FineDiff::sentenceDelimiters,
FineDiff::wordDelimiters,
FineDiff::characterDelimiters
);
function extractFragments($text, $delimiter) {
$text = str_replace(array("\n","\r"), array("\1","\2"), $text);
$delimiter = str_replace(array("\n","\r"), array("\1","\2"), $delimiter);
if ( empty($delimiter) ) {
return str_split($text, 1);
}
$fragments = array();
$start = $end = 0;
for (;;) {
$end += strcspn($text, $delimiter, $end);
$end += strspn($text, $delimiter, $end);
if ( $end === $start ) {
break;
}
$fragments[] = substr($text, $start, $end - $start);
$start = $end;
}
return $fragments;
}
$from_fragments = extractFragments($from, $delimiters[$granularity]);
$to_fragments = extractFragments($to, $delimiters[$granularity]);
$diff = new Text_Diff('native', array($from_fragments, $to_fragments));
$exec_time = sprintf('%.3f sec', gettimeofday(true) - $start_time);
$edits = array();
ob_start();
foreach ( $diff->getDiff() as $edit ) {
if ( $edit instanceof Text_Diff_Op_copy ) {
$orig = str_replace(array("\1","\2"), array("\n","\r"), implode('', $edit->orig));
$edits[] = new fineDiffCopyOp(strlen($orig));
echo htmlentities($orig);
}
else if ( $edit instanceof Text_Diff_Op_delete ) {
$orig = str_replace(array("\1","\2"), array("\n","\r"), implode('', $edit->orig));
$edits[] = new fineDiffDeleteOp(strlen($orig));
echo '<del>', htmlentities($orig), '</del>';
}
else if ( $edit instanceof Text_Diff_Op_add ) {
$final = str_replace(array("\1","\2"), array("\n","\r"), implode('', $edit->final));
$edits[] = new fineDiffInsertOp($final);
echo '<ins>', htmlentities($final), '</ins>';
}
else if ( $edit instanceof Text_Diff_Op_change ) {
$orig = str_replace(array("\1","\2"), array("\n","\r"), implode('', $edit->orig));
$final = str_replace(array("\1","\2"), array("\n","\r"), implode('', $edit->final));
$edits[] = new fineDiffReplaceOp(strlen($orig), $final);
echo '<del>', htmlentities($orig), '</del>';
echo '<ins>', htmlentities($final), '</ins>';
}
}
$rendered_diff = ob_get_clean();
$rendering_time = sprintf('%.3f sec', gettimeofday(true) - $start_time);
}
// character-level granularity not allowed
else {
$edits = false;
$exec_time = '?';
$rendering_time = '?';
$rendered_diff = '<span style="color:gray">Character-level granularity not allowed when using <code>Text_Diff</code>, due to performance issues.</span>';
}
}
else {
$granularityStacks = array(
FineDiff::$paragraphGranularity,
FineDiff::$sentenceGranularity,
FineDiff::$wordGranularity,
FineDiff::$characterGranularity
);
$diff = new FineDiff($from, $to, $granularityStacks[$granularity]);
$edits = $diff->getOps();
$exec_time = sprintf('%.3f sec', gettimeofday(true) - $start_time);
$rendered_diff = $diff->renderDiffToHTML();
$rendering_time = sprintf('%.3f sec', gettimeofday(true) - $start_time);
}
if ( $edits !== false ) {
$opcodes = array();
$opcodes_len = 0;
foreach ( $edits as $edit ) {
$opcode = $edit->getOpcode();
$opcodes_len += strlen($opcode);
$opcode = htmlentities($opcode);
if ( $edit instanceof FineDiffCopyOp ) {
$opcodes[] = "{$opcode}";
}
else if ( $edit instanceof FineDiffDeleteOp ) {
$opcodes[] = "<span class=\"del\">{$opcode}</span>";
}
else if ( $edit instanceof FineDiffInsertOp ) {
$opcodes[] = "<span class=\"ins\">{$opcode}</span>";
}
else /* if ( $edit instanceof FineDiffReplaceOp ) */ {
$opcodes[] = "<span class=\"rep\">{$opcode}</span>";
}
}
$opcodes = implode("", $opcodes);
$opcodes_len = sprintf('%d bytes (%.1f %% of "To")', $opcodes_len, $to_len ? $opcodes_len * 100 / $to_len : 0);
}
else {
$opcodes = '?';
$opcodes_len = '?';
}
?>
<form action="viewdiff-ex.php" method="post">
<div class="panecontainer"><p>From:</p><div><textarea name="from" class="pane"><?php echo htmlentities($from); ?></textarea></div></div>
<div class="panecontainer"><p>To:</p><div><textarea name="to" class="pane"><?php echo htmlentities($to); ?></textarea></div></div>
<p id="params">Granularity:<input name="granularity" type="radio" value="0"<?php if ( $granularity === 0 ) { echo ' checked="checked"'; } ?>> Paragraph/lines <input name="granularity" type="radio" value="1"<?php if ( $granularity === 1 ) { echo ' checked="checked"'; } ?>> Sentence <input name="granularity" type="radio" value="2"<?php if ( $granularity === 2 ) { echo ' checked="checked"'; } ?>> Word <input name="granularity" type="radio" value="3"<?php if ( $granularity === 3 ) { echo ' checked="checked"'; } ?>> Character <!-- <input name="XDEBUG_PROFILE" type="hidden" value=""> --><input type="submit" value="Compute diff"> <input name="stdlib" type="checkbox" value="1"<?php if ( $use_stdlib_diff ) { echo ' checked="checked"'; } ?>><a href="http://pear.php.net/package/Text_Diff/"><code>Text_Diff</code></a> lib (for comparison purpose) <sup style="font-size:x-small"><a href="#notes">see notes</a></sup></p>
</form>
<div class="panecontainer"><p>Diff stats:</p><div><div class="pane">
<b>Diff execution time:</b> <?php echo $exec_time; ?><br>
<b>Diff execution + rendering time:</b> <?php echo $rendering_time; ?><br>
<b>"From" size:</b> <?php echo $from_len; ?> bytes<br>
<b>"To" size:</b> <?php echo $to_len; ?> bytes<br>
<b>Diff opcodes size:</b> <?php echo $opcodes_len; ?><br>
<b>Diff opcodes (<span style="border:1px solid #ccc;display:inline-block;width:16px"> </span>=copy, <span class="del" style="display:inline-block;width:16px"> </span>=delete, <span class="ins" style="display:inline-block;width:16px"> </span>=insert, <span class="rep" style="display:inline-block;width:16px"> </span>=replace):</b>
<div style="margin:2px 0 2px 0;border:0;border-top:1px dotted #aaa;padding-top:4px;word-wrap:break-word"><?php echo $opcodes; ?></div>
</div></div></div>
<div class="panecontainer"><p>Rendered Diff: <span style="font-size:smaller">Show <input type="radio" name="htmldiffshow" onclick="setHTMLDiffVisibility('deletions');">Deletions only <input type="radio" name="htmldiffshow" checked="checked" onclick="setHTMLDiffVisibility();">All <input type="radio" name="htmldiffshow" onclick="setHTMLDiffVisibility('insertions');">Insertions only</span></p>
<div id="htmldiff">
<div class="pane" style="white-space:pre-line"><?php echo $rendered_diff; ?></div>
</div>
</div>
<script type="text/javascript">
<!--
function setHTMLDiffVisibility(what) {
var htmldiffEl = document.getElementById('htmldiff');
if ( what === 'deletions' ) {
htmldiffEl.className = 'onlyDeletions';
}
else if ( what === 'insertions' ) {
htmldiffEl.className = 'onlyInsertions';
}
else {
htmldiffEl.className = '';
}
}
// -->
</script>
</div>
<div style="margin:0.5em 0;border-top:1px solid #ccc;height:2px"></div>
<div id="notes" style="font:11px sans-serif"><h3 style="margin-top:0">Notes</h3>
<p>The PHP-based engine of <code><a href="http://pear.php.net/package/Text_Diff/">Text_Diff</a></code> is forced, in order to meaningfully compare results with PHP-based <code>FineDiff</code>. <code>Text_Diff</code> is naturally geared toward line-level granularity, and to compute diff for a higher granularity (sequences, words, characters), line break characters (\n, \r) are replaced in order to avoid having <code>Text_Diff</code> from eating our line breaks — so extra steps are required.</p>
<p><code>FineDiff</code> is natively better equipped to generate diff at granularity higher than line levels. An example of this is that using the above built-in sample text, for word and character-level granularity, <code>FineDiff</code> roughly executes in 25 ms and 30 ms, respectively, while <code>Text_Diff</code> roughly executes in 75 ms and 6.5 seconds, respectively (on my development computer, a run of the mill Intel i5 core desktop computer).</p>
<p>If you wish to comment on this page, head to the associated blog entry: <a href="http://www.raymondhill.net/blog/?p=441">FineDiff, a character-level diff algorithm in PHP</a></p>
</div>
</body>
</html>