1 | <?php
|
---|
2 | /**
|
---|
3 | *
|
---|
4 | * @package diff
|
---|
5 | * @version $Id: diff.php 8765 2008-08-16 22:18:25Z aptx $
|
---|
6 | * @copyright (c) 2006 phpBB Group
|
---|
7 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
---|
8 | *
|
---|
9 | */
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * @ignore
|
---|
13 | */
|
---|
14 | if (!defined('IN_PHPBB'))
|
---|
15 | {
|
---|
16 | exit;
|
---|
17 | }
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Code from pear.php.net, Text_Diff-1.0.0 package
|
---|
21 | * http://pear.php.net/package/Text_Diff/
|
---|
22 | *
|
---|
23 | * Modified by phpBB Group to meet our coding standards
|
---|
24 | * and being able to integrate into phpBB
|
---|
25 | *
|
---|
26 | * General API for generating and formatting diffs - the differences between
|
---|
27 | * two sequences of strings.
|
---|
28 | *
|
---|
29 | * Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
30 | * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
|
---|
31 | *
|
---|
32 | * @package diff
|
---|
33 | * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
34 | */
|
---|
35 | class diff
|
---|
36 | {
|
---|
37 | /**
|
---|
38 | * Array of changes.
|
---|
39 | * @var array
|
---|
40 | */
|
---|
41 | var $_edits;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Computes diffs between sequences of strings.
|
---|
45 | *
|
---|
46 | * @param array $from_lines An array of strings. Typically these are lines from a file.
|
---|
47 | * @param array $to_lines An array of strings.
|
---|
48 | */
|
---|
49 | function diff(&$from_content, &$to_content, $preserve_cr = true)
|
---|
50 | {
|
---|
51 | $diff_engine = new diff_engine();
|
---|
52 | $this->_edits = $diff_engine->diff($from_content, $to_content, $preserve_cr);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Returns the array of differences.
|
---|
57 | */
|
---|
58 | function get_diff()
|
---|
59 | {
|
---|
60 | return $this->_edits;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Computes a reversed diff.
|
---|
65 | *
|
---|
66 | * Example:
|
---|
67 | * <code>
|
---|
68 | * $diff = new diff($lines1, $lines2);
|
---|
69 | * $rev = $diff->reverse();
|
---|
70 | * </code>
|
---|
71 | *
|
---|
72 | * @return diff A Diff object representing the inverse of the original diff.
|
---|
73 | * Note that we purposely don't return a reference here, since
|
---|
74 | * this essentially is a clone() method.
|
---|
75 | */
|
---|
76 | function reverse()
|
---|
77 | {
|
---|
78 | if (version_compare(zend_version(), '2', '>'))
|
---|
79 | {
|
---|
80 | $rev = clone($this);
|
---|
81 | }
|
---|
82 | else
|
---|
83 | {
|
---|
84 | $rev = $this;
|
---|
85 | }
|
---|
86 |
|
---|
87 | $rev->_edits = array();
|
---|
88 |
|
---|
89 | foreach ($this->_edits as $edit)
|
---|
90 | {
|
---|
91 | $rev->_edits[] = $edit->reverse();
|
---|
92 | }
|
---|
93 |
|
---|
94 | return $rev;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Checks for an empty diff.
|
---|
99 | *
|
---|
100 | * @return boolean True if two sequences were identical.
|
---|
101 | */
|
---|
102 | function is_empty()
|
---|
103 | {
|
---|
104 | foreach ($this->_edits as $edit)
|
---|
105 | {
|
---|
106 | if (!is_a($edit, 'diff_op_copy'))
|
---|
107 | {
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Computes the length of the Longest Common Subsequence (LCS).
|
---|
116 | *
|
---|
117 | * This is mostly for diagnostic purposes.
|
---|
118 | *
|
---|
119 | * @return integer The length of the LCS.
|
---|
120 | */
|
---|
121 | function lcs()
|
---|
122 | {
|
---|
123 | $lcs = 0;
|
---|
124 |
|
---|
125 | foreach ($this->_edits as $edit)
|
---|
126 | {
|
---|
127 | if (is_a($edit, 'diff_op_copy'))
|
---|
128 | {
|
---|
129 | $lcs += sizeof($edit->orig);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | return $lcs;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Gets the original set of lines.
|
---|
137 | *
|
---|
138 | * This reconstructs the $from_lines parameter passed to the constructor.
|
---|
139 | *
|
---|
140 | * @return array The original sequence of strings.
|
---|
141 | */
|
---|
142 | function get_original()
|
---|
143 | {
|
---|
144 | $lines = array();
|
---|
145 |
|
---|
146 | foreach ($this->_edits as $edit)
|
---|
147 | {
|
---|
148 | if ($edit->orig)
|
---|
149 | {
|
---|
150 | array_splice($lines, sizeof($lines), 0, $edit->orig);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | return $lines;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Gets the final set of lines.
|
---|
158 | *
|
---|
159 | * This reconstructs the $to_lines parameter passed to the constructor.
|
---|
160 | *
|
---|
161 | * @return array The sequence of strings.
|
---|
162 | */
|
---|
163 | function get_final()
|
---|
164 | {
|
---|
165 | $lines = array();
|
---|
166 |
|
---|
167 | foreach ($this->_edits as $edit)
|
---|
168 | {
|
---|
169 | if ($edit->final)
|
---|
170 | {
|
---|
171 | array_splice($lines, sizeof($lines), 0, $edit->final);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | return $lines;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Removes trailing newlines from a line of text. This is meant to be used with array_walk().
|
---|
179 | *
|
---|
180 | * @param string &$line The line to trim.
|
---|
181 | * @param integer $key The index of the line in the array. Not used.
|
---|
182 | */
|
---|
183 | function trim_newlines(&$line, $key)
|
---|
184 | {
|
---|
185 | $line = str_replace(array("\n", "\r"), '', $line);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Checks a diff for validity.
|
---|
190 | *
|
---|
191 | * This is here only for debugging purposes.
|
---|
192 | */
|
---|
193 | function _check($from_lines, $to_lines)
|
---|
194 | {
|
---|
195 | if (serialize($from_lines) != serialize($this->get_original()))
|
---|
196 | {
|
---|
197 | trigger_error("[diff] Reconstructed original doesn't match", E_USER_ERROR);
|
---|
198 | }
|
---|
199 |
|
---|
200 | if (serialize($to_lines) != serialize($this->get_final()))
|
---|
201 | {
|
---|
202 | trigger_error("[diff] Reconstructed final doesn't match", E_USER_ERROR);
|
---|
203 | }
|
---|
204 |
|
---|
205 | $rev = $this->reverse();
|
---|
206 |
|
---|
207 | if (serialize($to_lines) != serialize($rev->get_original()))
|
---|
208 | {
|
---|
209 | trigger_error("[diff] Reversed original doesn't match", E_USER_ERROR);
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (serialize($from_lines) != serialize($rev->get_final()))
|
---|
213 | {
|
---|
214 | trigger_error("[diff] Reversed final doesn't match", E_USER_ERROR);
|
---|
215 | }
|
---|
216 |
|
---|
217 | $prevtype = null;
|
---|
218 |
|
---|
219 | foreach ($this->_edits as $edit)
|
---|
220 | {
|
---|
221 | if ($prevtype == get_class($edit))
|
---|
222 | {
|
---|
223 | trigger_error("[diff] Edit sequence is non-optimal", E_USER_ERROR);
|
---|
224 | }
|
---|
225 | $prevtype = get_class($edit);
|
---|
226 | }
|
---|
227 |
|
---|
228 | return true;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * @package diff
|
---|
234 | * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
235 | */
|
---|
236 | class mapped_diff extends diff
|
---|
237 | {
|
---|
238 | /**
|
---|
239 | * Computes a diff between sequences of strings.
|
---|
240 | *
|
---|
241 | * This can be used to compute things like case-insensitve diffs, or diffs
|
---|
242 | * which ignore changes in white-space.
|
---|
243 | *
|
---|
244 | * @param array $from_lines An array of strings.
|
---|
245 | * @param array $to_lines An array of strings.
|
---|
246 | * @param array $mapped_from_lines This array should have the same size number of elements as $from_lines.
|
---|
247 | * The elements in $mapped_from_lines and $mapped_to_lines are what is actually
|
---|
248 | * compared when computing the diff.
|
---|
249 | * @param array $mapped_to_lines This array should have the same number of elements as $to_lines.
|
---|
250 | */
|
---|
251 | function mapped_diff(&$from_lines, &$to_lines, &$mapped_from_lines, &$mapped_to_lines)
|
---|
252 | {
|
---|
253 | if (sizeof($from_lines) != sizeof($mapped_from_lines) || sizeof($to_lines) != sizeof($mapped_to_lines))
|
---|
254 | {
|
---|
255 | return false;
|
---|
256 | }
|
---|
257 |
|
---|
258 | parent::diff($mapped_from_lines, $mapped_to_lines);
|
---|
259 |
|
---|
260 | $xi = $yi = 0;
|
---|
261 | for ($i = 0; $i < sizeof($this->_edits); $i++)
|
---|
262 | {
|
---|
263 | $orig = &$this->_edits[$i]->orig;
|
---|
264 | if (is_array($orig))
|
---|
265 | {
|
---|
266 | $orig = array_slice($from_lines, $xi, sizeof($orig));
|
---|
267 | $xi += sizeof($orig);
|
---|
268 | }
|
---|
269 |
|
---|
270 | $final = &$this->_edits[$i]->final;
|
---|
271 | if (is_array($final))
|
---|
272 | {
|
---|
273 | $final = array_slice($to_lines, $yi, sizeof($final));
|
---|
274 | $yi += sizeof($final);
|
---|
275 | }
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * @package diff
|
---|
282 | * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
283 | *
|
---|
284 | * @access private
|
---|
285 | */
|
---|
286 | class diff_op
|
---|
287 | {
|
---|
288 | var $orig;
|
---|
289 | var $final;
|
---|
290 |
|
---|
291 | function &reverse()
|
---|
292 | {
|
---|
293 | trigger_error('[diff] Abstract method', E_USER_ERROR);
|
---|
294 | }
|
---|
295 |
|
---|
296 | function norig()
|
---|
297 | {
|
---|
298 | return ($this->orig) ? sizeof($this->orig) : 0;
|
---|
299 | }
|
---|
300 |
|
---|
301 | function nfinal()
|
---|
302 | {
|
---|
303 | return ($this->final) ? sizeof($this->final) : 0;
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * @package diff
|
---|
309 | * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
310 | *
|
---|
311 | * @access private
|
---|
312 | */
|
---|
313 | class diff_op_copy extends diff_op
|
---|
314 | {
|
---|
315 | function diff_op_copy($orig, $final = false)
|
---|
316 | {
|
---|
317 | if (!is_array($final))
|
---|
318 | {
|
---|
319 | $final = $orig;
|
---|
320 | }
|
---|
321 | $this->orig = $orig;
|
---|
322 | $this->final = $final;
|
---|
323 | }
|
---|
324 |
|
---|
325 | function &reverse()
|
---|
326 | {
|
---|
327 | $reverse = new diff_op_copy($this->final, $this->orig);
|
---|
328 | return $reverse;
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * @package diff
|
---|
334 | * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
335 | *
|
---|
336 | * @access private
|
---|
337 | */
|
---|
338 | class diff_op_delete extends diff_op
|
---|
339 | {
|
---|
340 | function diff_op_delete($lines)
|
---|
341 | {
|
---|
342 | $this->orig = $lines;
|
---|
343 | $this->final = false;
|
---|
344 | }
|
---|
345 |
|
---|
346 | function &reverse()
|
---|
347 | {
|
---|
348 | $reverse = new diff_op_add($this->orig);
|
---|
349 | return $reverse;
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * @package diff
|
---|
355 | * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
356 | *
|
---|
357 | * @access private
|
---|
358 | */
|
---|
359 | class diff_op_add extends diff_op
|
---|
360 | {
|
---|
361 | function diff_op_add($lines)
|
---|
362 | {
|
---|
363 | $this->final = $lines;
|
---|
364 | $this->orig = false;
|
---|
365 | }
|
---|
366 |
|
---|
367 | function &reverse()
|
---|
368 | {
|
---|
369 | $reverse = new diff_op_delete($this->final);
|
---|
370 | return $reverse;
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * @package diff
|
---|
376 | * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
377 | *
|
---|
378 | * @access private
|
---|
379 | */
|
---|
380 | class diff_op_change extends diff_op
|
---|
381 | {
|
---|
382 | function diff_op_change($orig, $final)
|
---|
383 | {
|
---|
384 | $this->orig = $orig;
|
---|
385 | $this->final = $final;
|
---|
386 | }
|
---|
387 |
|
---|
388 | function &reverse()
|
---|
389 | {
|
---|
390 | $reverse = new diff_op_change($this->final, $this->orig);
|
---|
391 | return $reverse;
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 |
|
---|
396 | /**
|
---|
397 | * A class for computing three way diffs.
|
---|
398 | *
|
---|
399 | * @package diff
|
---|
400 | * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
401 | */
|
---|
402 | class diff3 extends diff
|
---|
403 | {
|
---|
404 | /**
|
---|
405 | * Conflict counter.
|
---|
406 | * @var integer
|
---|
407 | */
|
---|
408 | var $_conflicting_blocks = 0;
|
---|
409 |
|
---|
410 | /**
|
---|
411 | * Computes diff between 3 sequences of strings.
|
---|
412 | *
|
---|
413 | * @param array $orig The original lines to use.
|
---|
414 | * @param array $final1 The first version to compare to.
|
---|
415 | * @param array $final2 The second version to compare to.
|
---|
416 | */
|
---|
417 | function diff3(&$orig, &$final1, &$final2)
|
---|
418 | {
|
---|
419 | $diff_engine = new diff_engine();
|
---|
420 |
|
---|
421 | $diff_1 = $diff_engine->diff($orig, $final1);
|
---|
422 | $diff_2 = $diff_engine->diff($orig, $final2);
|
---|
423 |
|
---|
424 | unset($engine);
|
---|
425 |
|
---|
426 | $this->_edits = $this->_diff3($diff_1, $diff_2);
|
---|
427 | }
|
---|
428 |
|
---|
429 | /**
|
---|
430 | * Return merged output
|
---|
431 | *
|
---|
432 | * @param string $label1 the cvs file version/label from the original set of lines
|
---|
433 | * @param string $label2 the cvs file version/label from the new set of lines
|
---|
434 | * @param string $label_sep the explanation between label1 and label2 - more of a helper for the user
|
---|
435 | * @param bool $get_conflicts if set to true only the number of conflicts is returned
|
---|
436 | * @param bool $merge_new if set to true the merged output will have the new file contents on a conflicting merge
|
---|
437 | *
|
---|
438 | * @return mixed the merged output
|
---|
439 | */
|
---|
440 | function merged_output($label1 = 'CURRENT_FILE', $label2 = 'NEW_FILE', $label_sep = 'DIFF_SEP_EXPLAIN', $get_conflicts = false, $merge_new = false)
|
---|
441 | {
|
---|
442 | global $user;
|
---|
443 |
|
---|
444 | if ($get_conflicts)
|
---|
445 | {
|
---|
446 | foreach ($this->_edits as $edit)
|
---|
447 | {
|
---|
448 | if ($edit->is_conflict())
|
---|
449 | {
|
---|
450 | $this->_conflicting_blocks++;
|
---|
451 | }
|
---|
452 | }
|
---|
453 |
|
---|
454 | return $this->_conflicting_blocks;
|
---|
455 | }
|
---|
456 |
|
---|
457 | $label1 = (!empty($user->lang[$label1])) ? $user->lang[$label1] : $label1;
|
---|
458 | $label2 = (!empty($user->lang[$label2])) ? $user->lang[$label2] : $label2;
|
---|
459 | $label_sep = (!empty($user->lang[$label_sep])) ? $user->lang[$label_sep] : $label_sep;
|
---|
460 |
|
---|
461 | $lines = array();
|
---|
462 |
|
---|
463 | foreach ($this->_edits as $edit)
|
---|
464 | {
|
---|
465 | if ($edit->is_conflict())
|
---|
466 | {
|
---|
467 | if (!$merge_new)
|
---|
468 | {
|
---|
469 | $lines = array_merge($lines, array('<<<<<<<' . ($label1 ? ' ' . $label1 : '')), $edit->final1, array('=======' . ($label_sep ? ' ' . $label_sep : '')), $edit->final2, array('>>>>>>>' . ($label2 ? ' ' . $label2 : '')));
|
---|
470 | }
|
---|
471 | else
|
---|
472 | {
|
---|
473 | $lines = array_merge($lines, $edit->final1);
|
---|
474 | }
|
---|
475 | $this->_conflicting_blocks++;
|
---|
476 | }
|
---|
477 | else
|
---|
478 | {
|
---|
479 | $lines = array_merge($lines, $edit->merged());
|
---|
480 | }
|
---|
481 | }
|
---|
482 |
|
---|
483 | return $lines;
|
---|
484 | }
|
---|
485 |
|
---|
486 | /**
|
---|
487 | * Merge the output and use the new file code for conflicts
|
---|
488 | */
|
---|
489 | function merged_new_output()
|
---|
490 | {
|
---|
491 | $lines = array();
|
---|
492 |
|
---|
493 | foreach ($this->_edits as $edit)
|
---|
494 | {
|
---|
495 | if ($edit->is_conflict())
|
---|
496 | {
|
---|
497 | $lines = array_merge($lines, $edit->final2);
|
---|
498 | }
|
---|
499 | else
|
---|
500 | {
|
---|
501 | $lines = array_merge($lines, $edit->merged());
|
---|
502 | }
|
---|
503 | }
|
---|
504 |
|
---|
505 | return $lines;
|
---|
506 | }
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * Merge the output and use the original file code for conflicts
|
---|
510 | */
|
---|
511 | function merged_orig_output()
|
---|
512 | {
|
---|
513 | $lines = array();
|
---|
514 |
|
---|
515 | foreach ($this->_edits as $edit)
|
---|
516 | {
|
---|
517 | if ($edit->is_conflict())
|
---|
518 | {
|
---|
519 | $lines = array_merge($lines, $edit->final1);
|
---|
520 | }
|
---|
521 | else
|
---|
522 | {
|
---|
523 | $lines = array_merge($lines, $edit->merged());
|
---|
524 | }
|
---|
525 | }
|
---|
526 |
|
---|
527 | return $lines;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Get conflicting block(s)
|
---|
532 | */
|
---|
533 | function get_conflicts()
|
---|
534 | {
|
---|
535 | $conflicts = array();
|
---|
536 |
|
---|
537 | foreach ($this->_edits as $edit)
|
---|
538 | {
|
---|
539 | if ($edit->is_conflict())
|
---|
540 | {
|
---|
541 | $conflicts[] = array($edit->final1, $edit->final2);
|
---|
542 | }
|
---|
543 | }
|
---|
544 |
|
---|
545 | return $conflicts;
|
---|
546 | }
|
---|
547 |
|
---|
548 | /**
|
---|
549 | * @access private
|
---|
550 | */
|
---|
551 | function _diff3(&$edits1, &$edits2)
|
---|
552 | {
|
---|
553 | $edits = array();
|
---|
554 | $bb = new diff3_block_builder();
|
---|
555 |
|
---|
556 | $e1 = current($edits1);
|
---|
557 | $e2 = current($edits2);
|
---|
558 |
|
---|
559 | while ($e1 || $e2)
|
---|
560 | {
|
---|
561 | if ($e1 && $e2 && is_a($e1, 'diff_op_copy') && is_a($e2, 'diff_op_copy'))
|
---|
562 | {
|
---|
563 | // We have copy blocks from both diffs. This is the (only) time we want to emit a diff3 copy block.
|
---|
564 | // Flush current diff3 diff block, if any.
|
---|
565 | if ($edit = $bb->finish())
|
---|
566 | {
|
---|
567 | $edits[] = $edit;
|
---|
568 | }
|
---|
569 |
|
---|
570 | $ncopy = min($e1->norig(), $e2->norig());
|
---|
571 | $edits[] = new diff3_op_copy(array_slice($e1->orig, 0, $ncopy));
|
---|
572 |
|
---|
573 | if ($e1->norig() > $ncopy)
|
---|
574 | {
|
---|
575 | array_splice($e1->orig, 0, $ncopy);
|
---|
576 | array_splice($e1->final, 0, $ncopy);
|
---|
577 | }
|
---|
578 | else
|
---|
579 | {
|
---|
580 | $e1 = next($edits1);
|
---|
581 | }
|
---|
582 |
|
---|
583 | if ($e2->norig() > $ncopy)
|
---|
584 | {
|
---|
585 | array_splice($e2->orig, 0, $ncopy);
|
---|
586 | array_splice($e2->final, 0, $ncopy);
|
---|
587 | }
|
---|
588 | else
|
---|
589 | {
|
---|
590 | $e2 = next($edits2);
|
---|
591 | }
|
---|
592 | }
|
---|
593 | else
|
---|
594 | {
|
---|
595 | if ($e1 && $e2)
|
---|
596 | {
|
---|
597 | if ($e1->orig && $e2->orig)
|
---|
598 | {
|
---|
599 | $norig = min($e1->norig(), $e2->norig());
|
---|
600 | $orig = array_splice($e1->orig, 0, $norig);
|
---|
601 | array_splice($e2->orig, 0, $norig);
|
---|
602 | $bb->input($orig);
|
---|
603 | }
|
---|
604 | else
|
---|
605 | {
|
---|
606 | $norig = 0;
|
---|
607 | }
|
---|
608 |
|
---|
609 | if (is_a($e1, 'diff_op_copy'))
|
---|
610 | {
|
---|
611 | $bb->out1(array_splice($e1->final, 0, $norig));
|
---|
612 | }
|
---|
613 |
|
---|
614 | if (is_a($e2, 'diff_op_copy'))
|
---|
615 | {
|
---|
616 | $bb->out2(array_splice($e2->final, 0, $norig));
|
---|
617 | }
|
---|
618 | }
|
---|
619 |
|
---|
620 | if ($e1 && ! $e1->orig)
|
---|
621 | {
|
---|
622 | $bb->out1($e1->final);
|
---|
623 | $e1 = next($edits1);
|
---|
624 | }
|
---|
625 |
|
---|
626 | if ($e2 && ! $e2->orig)
|
---|
627 | {
|
---|
628 | $bb->out2($e2->final);
|
---|
629 | $e2 = next($edits2);
|
---|
630 | }
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 | if ($edit = $bb->finish())
|
---|
635 | {
|
---|
636 | $edits[] = $edit;
|
---|
637 | }
|
---|
638 |
|
---|
639 | return $edits;
|
---|
640 | }
|
---|
641 | }
|
---|
642 |
|
---|
643 | /**
|
---|
644 | * @package diff
|
---|
645 | * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
646 | *
|
---|
647 | * @access private
|
---|
648 | */
|
---|
649 | class diff3_op
|
---|
650 | {
|
---|
651 | function diff3_op($orig = false, $final1 = false, $final2 = false)
|
---|
652 | {
|
---|
653 | $this->orig = $orig ? $orig : array();
|
---|
654 | $this->final1 = $final1 ? $final1 : array();
|
---|
655 | $this->final2 = $final2 ? $final2 : array();
|
---|
656 | }
|
---|
657 |
|
---|
658 | function merged()
|
---|
659 | {
|
---|
660 | if (!isset($this->_merged))
|
---|
661 | {
|
---|
662 | if ($this->final1 === $this->final2)
|
---|
663 | {
|
---|
664 | $this->_merged = &$this->final1;
|
---|
665 | }
|
---|
666 | else if ($this->final1 === $this->orig)
|
---|
667 | {
|
---|
668 | $this->_merged = &$this->final2;
|
---|
669 | }
|
---|
670 | else if ($this->final2 === $this->orig)
|
---|
671 | {
|
---|
672 | $this->_merged = &$this->final1;
|
---|
673 | }
|
---|
674 | else
|
---|
675 | {
|
---|
676 | $this->_merged = false;
|
---|
677 | }
|
---|
678 | }
|
---|
679 |
|
---|
680 | return $this->_merged;
|
---|
681 | }
|
---|
682 |
|
---|
683 | function is_conflict()
|
---|
684 | {
|
---|
685 | return ($this->merged() === false) ? true : false;
|
---|
686 | }
|
---|
687 | }
|
---|
688 |
|
---|
689 | /**
|
---|
690 | * @package diff
|
---|
691 | * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
692 | *
|
---|
693 | * @access private
|
---|
694 | */
|
---|
695 | class diff3_op_copy extends diff3_op
|
---|
696 | {
|
---|
697 | function diff3_op_copy($lines = false)
|
---|
698 | {
|
---|
699 | $this->orig = $lines ? $lines : array();
|
---|
700 | $this->final1 = &$this->orig;
|
---|
701 | $this->final2 = &$this->orig;
|
---|
702 | }
|
---|
703 |
|
---|
704 | function merged()
|
---|
705 | {
|
---|
706 | return $this->orig;
|
---|
707 | }
|
---|
708 |
|
---|
709 | function is_conflict()
|
---|
710 | {
|
---|
711 | return false;
|
---|
712 | }
|
---|
713 | }
|
---|
714 |
|
---|
715 | /**
|
---|
716 | * @package diff
|
---|
717 | * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
---|
718 | *
|
---|
719 | * @access private
|
---|
720 | */
|
---|
721 | class diff3_block_builder
|
---|
722 | {
|
---|
723 | function diff3_block_builder()
|
---|
724 | {
|
---|
725 | $this->_init();
|
---|
726 | }
|
---|
727 |
|
---|
728 | function input($lines)
|
---|
729 | {
|
---|
730 | if ($lines)
|
---|
731 | {
|
---|
732 | $this->_append($this->orig, $lines);
|
---|
733 | }
|
---|
734 | }
|
---|
735 |
|
---|
736 | function out1($lines)
|
---|
737 | {
|
---|
738 | if ($lines)
|
---|
739 | {
|
---|
740 | $this->_append($this->final1, $lines);
|
---|
741 | }
|
---|
742 | }
|
---|
743 |
|
---|
744 | function out2($lines)
|
---|
745 | {
|
---|
746 | if ($lines)
|
---|
747 | {
|
---|
748 | $this->_append($this->final2, $lines);
|
---|
749 | }
|
---|
750 | }
|
---|
751 |
|
---|
752 | function is_empty()
|
---|
753 | {
|
---|
754 | return !$this->orig && !$this->final1 && !$this->final2;
|
---|
755 | }
|
---|
756 |
|
---|
757 | function finish()
|
---|
758 | {
|
---|
759 | if ($this->is_empty())
|
---|
760 | {
|
---|
761 | return false;
|
---|
762 | }
|
---|
763 | else
|
---|
764 | {
|
---|
765 | $edit = new diff3_op($this->orig, $this->final1, $this->final2);
|
---|
766 | $this->_init();
|
---|
767 | return $edit;
|
---|
768 | }
|
---|
769 | }
|
---|
770 |
|
---|
771 | function _init()
|
---|
772 | {
|
---|
773 | $this->orig = $this->final1 = $this->final2 = array();
|
---|
774 | }
|
---|
775 |
|
---|
776 | function _append(&$array, $lines)
|
---|
777 | {
|
---|
778 | array_splice($array, sizeof($array), 0, $lines);
|
---|
779 | }
|
---|
780 | }
|
---|
781 |
|
---|
782 | ?>
|
---|