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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
|
<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2006 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Code from pear.php.net, Text_Diff-0.2.1 (beta) package
* http://pear.php.net/package/Text_Diff/
*
* Modified by phpBB Group to meet our coding standards
* and being able to integrate into phpBB
*
* A class to render Diffs in different formats.
*
* This class renders the diff in classic diff format. It is intended that
* this class be customized via inheritance, to obtain fancier outputs.
*
* @package phpBB3
*/
class diff_renderer
{
/**
* Number of leading context "lines" to preserve.
*
* This should be left at zero for this class, but subclasses may want to
* set this to other values.
*/
var $_leading_context_lines = 0;
/**
* Number of trailing context "lines" to preserve.
*
* This should be left at zero for this class, but subclasses may want to
* set this to other values.
*/
var $_trailing_context_lines = 0;
/**
* Constructor.
*/
function diff_renderer($params = array())
{
foreach ($params as $param => $value)
{
$v = '_' . $param;
if (isset($this->$v))
{
$this->$v = $value;
}
}
}
/**
* Get any renderer parameters.
*
* @return array All parameters of this renderer object.
*/
function get_params()
{
$params = array();
foreach (get_object_vars($this) as $k => $v)
{
if ($k[0] == '_')
{
$params[substr($k, 1)] = $v;
}
}
return $params;
}
/**
* Renders a diff.
*
* @param diff &$diff A diff object.
*
* @return string The formatted output.
*/
function render(&$diff)
{
$xi = $yi = 1;
$block = false;
$context = array();
// Create a new diff object if it is a 3-way diff
if (is_a($diff, 'diff3'))
{
$diff3 = &$diff;
$diff = &new diff($diff3->get_original(), $diff3->merged_output());
unset($diff3);
}
$nlead = $this->_leading_context_lines;
$ntrail = $this->_trailing_context_lines;
$output = $this->_start_diff();
$diffs = $diff->get_diff();
foreach ($diffs as $i => $edit)
{
if (is_a($edit, 'diff_op_copy'))
{
if (is_array($block))
{
$keep = ($i == sizeof($diffs) - 1) ? $ntrail : $nlead + $ntrail;
if (sizeof($edit->orig) <= $keep)
{
$block[] = $edit;
}
else
{
if ($ntrail)
{
$context = array_slice($edit->orig, 0, $ntrail);
$block[] = &new diff_op_copy($context);
}
$output .= $this->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block);
$block = false;
}
}
$context = $edit->orig;
}
else
{
if (!is_array($block))
{
$context = array_slice($context, sizeof($context) - $nlead);
$x0 = $xi - sizeof($context);
$y0 = $yi - sizeof($context);
$block = array();
if ($context)
{
$block[] = &new diff_op_copy($context);
}
}
$block[] = $edit;
}
$xi += ($edit->orig) ? sizeof($edit->orig) : 0;
$yi += ($edit->final) ? sizeof($edit->final) : 0;
}
if (is_array($block))
{
$output .= $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block);
}
return $output . $this->_end_diff();
}
function _block($xbeg, $xlen, $ybeg, $ylen, &$edits)
{
$output = $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
foreach ($edits as $edit)
{
switch (get_class($edit))
{
case 'diff_op_copy':
$output .= $this->_context($edit->orig);
break;
case 'diff_op_add':
$output .= $this->_added($edit->final);
break;
case 'diff_op_delete':
$output .= $this->_deleted($edit->orig);
break;
case 'diff_op_change':
$output .= $this->_changed($edit->orig, $edit->final);
break;
}
}
return $output . $this->_end_block();
}
function _start_diff()
{
return '';
}
function _end_diff()
{
return '';
}
function _block_header($xbeg, $xlen, $ybeg, $ylen)
{
if ($xlen > 1)
{
$xbeg .= ',' . ($xbeg + $xlen - 1);
}
if ($ylen > 1)
{
$ybeg .= ',' . ($ybeg + $ylen - 1);
}
return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
}
function _start_block($header)
{
return $header . "\n";
}
function _end_block()
{
return '';
}
function _lines($lines, $prefix = ' ')
{
return $prefix . implode("\n$prefix", $lines) . "\n";
}
function _context($lines)
{
return $this->_lines($lines, ' ');
}
function _added($lines)
{
return $this->_lines($lines, '> ');
}
function _deleted($lines)
{
return $this->_lines($lines, '< ');
}
function _changed($orig, $final)
{
return $this->_deleted($orig) . "---\n" . $this->_added($final);
}
/**
* Our function to get the diff
*/
function get_diff_content($diff)
{
return $this->render($diff);
}
}
/**
* Renders a unified diff
* @package phpBB3
*/
class diff_renderer_unified extends diff_renderer
{
var $_leading_context_lines = 4;
var $_trailing_context_lines = 4;
/**
* Our function to get the diff
*/
function get_diff_content($diff)
{
return nl2br($this->render($diff));
}
function _block_header($xbeg, $xlen, $ybeg, $ylen)
{
if ($xlen != 1)
{
$xbeg .= ',' . $xlen;
}
if ($ylen != 1)
{
$ybeg .= ',' . $ylen;
}
return '<div class="diff"><big class="info">@@ -' . $xbeg . ' +' . $ybeg . ' @@</big></div>';
}
function _context($lines)
{
return '<pre class="diff context">' . htmlspecialchars($this->_lines($lines, ' ')) . '</pre>';
}
function _added($lines)
{
return '<pre class="diff added">' . htmlspecialchars($this->_lines($lines, '+')) . '</pre>';
}
function _deleted($lines)
{
return '<pre class="diff removed">' . htmlspecialchars($this->_lines($lines, '-')) . '</pre>';
}
function _changed($orig, $final)
{
return $this->_deleted($orig) . $this->_added($final);
}
function _start_diff()
{
$start = '<div class="file">';
return $start;
}
function _end_diff()
{
return '</div>';
}
function _end_block()
{
return '';
}
}
/**
* "Inline" diff renderer.
*
* This class renders diffs in the Wiki-style "inline" format.
*
* @author Ciprian Popovici
* @package phpBB3
*/
class diff_renderer_inline extends diff_renderer
{
var $_leading_context_lines = 10000;
var $_trailing_context_lines = 10000;
// Prefix and suffix for inserted text
var $_ins_prefix = '<span class="ins">';
var $_ins_suffix = '</span>';
// Prefix and suffix for deleted text
var $_del_prefix = '<span class="del">';
var $_del_suffix = '</span>';
var $_block_head = '';
// What are we currently splitting on? Used to recurse to show word-level
var $_split_level = 'lines';
/**
* Our function to get the diff
*/
function get_diff_content($diff)
{
return '<pre>' . nl2br($this->render($diff)) . '</pre>';
}
function _start_diff()
{
return '';
}
function _end_diff()
{
return '';
}
function _block_header($xbeg, $xlen, $ybeg, $ylen)
{
return $this->_block_head;
}
function _start_block($header)
{
return $header;
}
function _lines($lines, $prefix = ' ', $encode = true)
{
if ($encode)
{
array_walk($lines, array(&$this, '_encode'));
}
if ($this->_split_level == 'words')
{
return implode('', $lines);
}
else
{
return implode("\n", $lines) . "\n";
}
}
function _added($lines)
{
array_walk($lines, array(&$this, '_encode'));
$lines[0] = $this->_ins_prefix . $lines[0];
$lines[sizeof($lines) - 1] .= $this->_ins_suffix;
return $this->_lines($lines, ' ', false);
}
function _deleted($lines, $words = false)
{
array_walk($lines, array(&$this, '_encode'));
$lines[0] = $this->_del_prefix . $lines[0];
$lines[sizeof($lines) - 1] .= $this->_del_suffix;
return $this->_lines($lines, ' ', false);
}
function _changed($orig, $final)
{
// If we've already split on words, don't try to do so again - just display.
if ($this->_split_level == 'words')
{
$prefix = '';
while ($orig[0] !== false && $final[0] !== false && substr($orig[0], 0, 1) == ' ' && substr($final[0], 0, 1) == ' ')
{
$prefix .= substr($orig[0], 0, 1);
$orig[0] = substr($orig[0], 1);
$final[0] = substr($final[0], 1);
}
return $prefix . $this->_deleted($orig) . $this->_added($final);
}
$text1 = implode("\n", $orig);
$text2 = implode("\n", $final);
// Non-printing newline marker.
$nl = "\0";
// We want to split on word boundaries, but we need to preserve whitespace as well.
// Therefore we split on words, but include all blocks of whitespace in the wordlist.
$diff = &new diff($this->_split_on_words($text1, $nl), $this->_split_on_words($text2, $nl));
// Get the diff in inline format.
$renderer = &new diff_renderer_inline(array_merge($this->get_params(), array('split_level' => 'words')));
// Run the diff and get the output.
return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
}
function _split_on_words($string, $newline_escape = "\n")
{
// Ignore \0; otherwise the while loop will never finish.
$string = str_replace("\0", '', $string);
$words = array();
$length = strlen($string);
$pos = 0;
$tab_there = true;
while ($pos < $length)
{
// Check for tabs... do not include them
if ($tab_there && substr($string, $pos, 1) === "\t")
{
$words[] = "\t";
$pos++;
continue;
}
else
{
$tab_there = false;
}
// Eat a word with any preceding whitespace.
$spaces = strspn(substr($string, $pos), " \n");
$nextpos = strcspn(substr($string, $pos + $spaces), " \n");
$words[] = str_replace("\n", $newline_escape, substr($string, $pos, $spaces + $nextpos));
$pos += $spaces + $nextpos;
}
return $words;
}
function _encode(&$string)
{
$string = htmlspecialchars($string);
}
}
/**
* "raw" diff renderer.
* This class could be used to output a raw unified patch file
*
* @package phpBB3
*/
class diff_renderer_raw extends diff_renderer
{
var $_leading_context_lines = 4;
var $_trailing_context_lines = 4;
/**
* Our function to get the diff
*/
function get_diff_content($diff)
{
return '<textarea style="height: 400px;" class="full">' . htmlspecialchars($this->render($diff)) . '</textarea>';
}
function _block_header($xbeg, $xlen, $ybeg, $ylen)
{
if ($xlen != 1)
{
$xbeg .= ',' . $xlen;
}
if ($ylen != 1)
{
$ybeg .= ',' . $ylen;
}
return '@@ -' . $xbeg . ' +' . $ybeg . ' @@';
}
function _context($lines)
{
return $this->_lines($lines, ' ');
}
function _added($lines)
{
return $this->_lines($lines, '+');
}
function _deleted($lines)
{
return $this->_lines($lines, '-');
}
function _changed($orig, $final)
{
return $this->_deleted($orig) . $this->_added($final);
}
}
/**
* "chora (Horde)" diff renderer - similar style.
* This renderer class is a modified human_readable function from the Horde Framework.
*
* @package phpBB3
*/
class diff_renderer_side_by_side extends diff_renderer
{
var $_leading_context_lines = 3;
var $_trailing_context_lines = 3;
var $lines = array();
// Hold the left and right columns of lines for change blocks.
var $cols;
var $state;
var $data = false;
/**
* Our function to get the diff
*/
function get_diff_content($diff)
{
global $user;
$output = '';
$output .= '<table cellspacing="0" class="hrdiff">
<caption>
<span class="unmodified"> </span> ' . $user->lang['LINE_UNMODIFIED'] . '
<span class="added"> </span> ' . $user->lang['LINE_ADDED'] . '
<span class="modified"> </span> ' . $user->lang['LINE_MODIFIED'] . '
<span class="removed"> </span> ' . $user->lang['LINE_REMOVED'] . '
</caption>
<tbody>
';
$this->render($diff);
// Is the diff empty?
if (!sizeof($this->lines))
{
$output .= '<tr><th colspan="2">' . $user->lang['NO_VISIBLE_CHANGES'] . '</th></tr>';
}
else
{
// Iterate through every header block of changes
foreach ($this->lines as $header)
{
$output .= '<tr><th>Line ' . $header['oldline'] . '</th><th>' . $user->lang['LINE'] . ' ' . $header['newline'] . '</th></tr>';
// Each header block consists of a number of changes (add, remove, change).
$current_context = '';
foreach ($header['contents'] as $change)
{
if (!empty($current_context) && $change['type'] != 'empty')
{
$line = $current_context;
$current_context = '';
$output .= '<tr class="unmodified"><td><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td>
<td><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td></tr>';
}
switch ($change['type'])
{
case 'add':
$line = '';
foreach ($change['lines'] as $_line)
{
$line .= htmlspecialchars($_line) . '<br />';
}
$output .= '<tr><td class="added_empty"> </td><td class="added"><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td></tr>';
break;
case 'remove':
$line = '';
foreach ($change['lines'] as $_line)
{
$line .= htmlspecialchars($_line) . '<br />';
}
$output .= '<tr><td class="removed"><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td><td class="removed_empty"> </td></tr>';
break;
case 'empty':
$current_context .= htmlspecialchars($change['line']) . '<br />';
break;
case 'change':
// Pop the old/new stacks one by one, until both are empty.
$oldsize = sizeof($change['old']);
$newsize = sizeof($change['new']);
$left = $right = '';
for ($row = 0, $row_max = max($oldsize, $newsize); $row < $row_max; ++$row)
{
$left .= isset($change['old'][$row]) ? htmlspecialchars($change['old'][$row]) : '';
$left .= '<br />';
$right .= isset($change['new'][$row]) ? htmlspecialchars($change['new'][$row]) : '';
$right .= '<br />';
}
$output .= '<tr>';
if (!empty($left))
{
$output .= '<td class="modified"><pre>' . $left . '</pre></td>';
}
else if ($row < $oldsize)
{
$output .= '<td class="modified"> </td>';
}
else
{
$output .= '<td class="unmodified"> </td>';
}
if (!empty($right))
{
$output .= '<td class="modified"><pre>' . $right . '</pre></td>';
}
else if ($row < $newsize)
{
$output .= '<td class="modified"> </td>';
}
else
{
$output .= '<td class="unmodified"> </td>';
}
$output .= '</tr>';
break;
}
}
if (!empty($current_context))
{
$line = $current_context;
$current_context = '';
$output .= '<tr class="unmodified"><td><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td>';
$output .= '<td><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td></tr>';
}
}
}
$output .= '</tbody></table>';
return $output;
}
function _start_diff()
{
$this->lines = array();
$this->data = false;
$this->cols = array(array(), array());
$this->state = 'empty';
return '';
}
function _end_diff()
{
// Just flush any remaining entries in the columns stack.
switch ($this->state)
{
case 'add':
$this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]);
break;
case 'remove':
// We have some removal lines pending in our stack, so flush them.
$this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]);
break;
case 'change':
// We have both remove and addition lines, so this is a change block.
$this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]);
break;
}
if ($this->data !== false)
{
$this->lines[] = $this->data;
}
return '';
}
function _block_header($xbeg, $xlen, $ybeg, $ylen)
{
// Push any previous header information to the return stack.
if ($this->data !== false)
{
$this->lines[] = $this->data;
}
$this->data = array('type' => 'header', 'oldline' => $xbeg, 'newline' => $ybeg, 'contents' => array());
$this->state = 'dump';
}
function _added($lines)
{
array_walk($lines, array(&$this, '_perform_add'));
}
function _perform_add($line)
{
if ($this->state == 'empty')
{
return '';
}
// This is just an addition line.
if ($this->state == 'dump' || $this->state == 'add')
{
// Start adding to the addition stack.
$this->cols[0][] = $line;
$this->state = 'add';
}
else
{
// This is inside a change block, so start accumulating lines.
$this->state = 'change';
$this->cols[1][] = $line;
}
}
function _deleted($lines)
{
array_walk($lines, array(&$this, '_perform_delete'));
}
function _perform_delete($line)
{
// This is a removal line.
$this->state = 'remove';
$this->cols[0][] = $line;
}
function _context($lines)
{
array_walk($lines, array(&$this, '_perform_context'));
}
function _perform_context($line)
{
// An empty block with no action.
switch ($this->state)
{
case 'add':
$this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]);
break;
case 'remove':
// We have some removal lines pending in our stack, so flush them.
$this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]);
break;
case 'change':
// We have both remove and addition lines, so this is a change block.
$this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]);
break;
}
$this->cols = array(array(), array());
$this->data['contents'][] = array('type' => 'empty', 'line' => $line);
$this->state = 'dump';
}
function _changed($orig, $final)
{
return $this->_deleted($orig) . $this->_added($final);
}
}
?>
|