5
* @version $Id: renderer.php,v 1.8 2007/10/05 14:36:33 acydburn Exp $
6
* @copyright (c) 2006 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
if (!defined('IN_PHPBB'))
20
* Code from pear.php.net, Text_Diff-0.2.1 (beta) package
21
* http://pear.php.net/package/Text_Diff/
23
* Modified by phpBB Group to meet our coding standards
24
* and being able to integrate into phpBB
26
* A class to render Diffs in different formats.
28
* This class renders the diff in classic diff format. It is intended that
29
* this class be customized via inheritance, to obtain fancier outputs.
36
* Number of leading context "lines" to preserve.
38
* This should be left at zero for this class, but subclasses may want to
39
* set this to other values.
41
var $_leading_context_lines = 0;
44
* Number of trailing context "lines" to preserve.
46
* This should be left at zero for this class, but subclasses may want to
47
* set this to other values.
49
var $_trailing_context_lines = 0;
54
function diff_renderer($params = array())
56
foreach ($params as $param => $value)
67
* Get any renderer parameters.
69
* @return array All parameters of this renderer object.
74
foreach (get_object_vars($this) as $k => $v)
78
$params[substr($k, 1)] = $v;
88
* @param diff &$diff A diff object.
90
* @return string The formatted output.
92
function render(&$diff)
98
// Create a new diff object if it is a 3-way diff
99
if (is_a($diff, 'diff3'))
103
$diff_1 = $diff3->get_original();
104
$diff_2 = $diff3->merged_output();
108
$diff = &new diff($diff_1, $diff_2);
111
$nlead = $this->_leading_context_lines;
112
$ntrail = $this->_trailing_context_lines;
114
$output = $this->_start_diff();
115
$diffs = $diff->get_diff();
117
foreach ($diffs as $i => $edit)
119
if (is_a($edit, 'diff_op_copy'))
121
if (is_array($block))
123
$keep = ($i == sizeof($diffs) - 1) ? $ntrail : $nlead + $ntrail;
124
if (sizeof($edit->orig) <= $keep)
132
$context = array_slice($edit->orig, 0, $ntrail);
133
$block[] = &new diff_op_copy($context);
136
$output .= $this->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block);
140
$context = $edit->orig;
144
if (!is_array($block))
146
$context = array_slice($context, sizeof($context) - $nlead);
147
$x0 = $xi - sizeof($context);
148
$y0 = $yi - sizeof($context);
153
$block[] = &new diff_op_copy($context);
159
$xi += ($edit->orig) ? sizeof($edit->orig) : 0;
160
$yi += ($edit->final) ? sizeof($edit->final) : 0;
163
if (is_array($block))
165
$output .= $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block);
168
return $output . $this->_end_diff();
171
function _block($xbeg, $xlen, $ybeg, $ylen, &$edits)
173
$output = $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
175
foreach ($edits as $edit)
177
switch (get_class($edit))
180
$output .= $this->_context($edit->orig);
184
$output .= $this->_added($edit->final);
187
case 'diff_op_delete':
188
$output .= $this->_deleted($edit->orig);
191
case 'diff_op_change':
192
$output .= $this->_changed($edit->orig, $edit->final);
197
return $output . $this->_end_block();
200
function _start_diff()
210
function _block_header($xbeg, $xlen, $ybeg, $ylen)
214
$xbeg .= ',' . ($xbeg + $xlen - 1);
219
$ybeg .= ',' . ($ybeg + $ylen - 1);
222
return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
225
function _start_block($header)
227
return $header . "\n";
230
function _end_block()
235
function _lines($lines, $prefix = ' ')
237
return $prefix . implode("\n$prefix", $lines) . "\n";
240
function _context($lines)
242
return $this->_lines($lines, ' ');
245
function _added($lines)
247
return $this->_lines($lines, '> ');
250
function _deleted($lines)
252
return $this->_lines($lines, '< ');
255
function _changed($orig, $final)
257
return $this->_deleted($orig) . "---\n" . $this->_added($final);
261
* Our function to get the diff
263
function get_diff_content($diff)
265
return $this->render($diff);
270
* Renders a unified diff
273
class diff_renderer_unified extends diff_renderer
275
var $_leading_context_lines = 4;
276
var $_trailing_context_lines = 4;
279
* Our function to get the diff
281
function get_diff_content($diff)
283
return nl2br($this->render($diff));
286
function _block_header($xbeg, $xlen, $ybeg, $ylen)
290
$xbeg .= ',' . $xlen;
295
$ybeg .= ',' . $ylen;
297
return '<div class="diff"><big class="info">@@ -' . $xbeg . ' +' . $ybeg . ' @@</big></div>';
300
function _context($lines)
302
return '<pre class="diff context">' . htmlspecialchars($this->_lines($lines, ' ')) . '<br /></pre>';
305
function _added($lines)
307
return '<pre class="diff added">' . htmlspecialchars($this->_lines($lines, '+')) . '<br /></pre>';
310
function _deleted($lines)
312
return '<pre class="diff removed">' . htmlspecialchars($this->_lines($lines, '-')) . '<br /></pre>';
315
function _changed($orig, $final)
317
return $this->_deleted($orig) . $this->_added($final);
320
function _start_diff()
322
$start = '<div class="file">';
332
function _end_block()
339
* "Inline" diff renderer.
341
* This class renders diffs in the Wiki-style "inline" format.
343
* @author Ciprian Popovici
346
class diff_renderer_inline extends diff_renderer
348
var $_leading_context_lines = 10000;
349
var $_trailing_context_lines = 10000;
351
// Prefix and suffix for inserted text
352
var $_ins_prefix = '<span class="ins">';
353
var $_ins_suffix = '</span>';
355
// Prefix and suffix for deleted text
356
var $_del_prefix = '<span class="del">';
357
var $_del_suffix = '</span>';
359
var $_block_head = '';
361
// What are we currently splitting on? Used to recurse to show word-level
362
var $_split_level = 'lines';
365
* Our function to get the diff
367
function get_diff_content($diff)
369
return '<pre>' . nl2br($this->render($diff)) . '<br /></pre>';
372
function _start_diff()
382
function _block_header($xbeg, $xlen, $ybeg, $ylen)
384
return $this->_block_head;
387
function _start_block($header)
392
function _lines($lines, $prefix = ' ', $encode = true)
396
array_walk($lines, array(&$this, '_encode'));
399
if ($this->_split_level == 'words')
401
return implode('', $lines);
405
return implode("\n", $lines) . "\n";
409
function _added($lines)
411
array_walk($lines, array(&$this, '_encode'));
412
$lines[0] = $this->_ins_prefix . $lines[0];
413
$lines[sizeof($lines) - 1] .= $this->_ins_suffix;
414
return $this->_lines($lines, ' ', false);
417
function _deleted($lines, $words = false)
419
array_walk($lines, array(&$this, '_encode'));
420
$lines[0] = $this->_del_prefix . $lines[0];
421
$lines[sizeof($lines) - 1] .= $this->_del_suffix;
422
return $this->_lines($lines, ' ', false);
425
function _changed($orig, $final)
427
// If we've already split on words, don't try to do so again - just display.
428
if ($this->_split_level == 'words')
431
while ($orig[0] !== false && $final[0] !== false && substr($orig[0], 0, 1) == ' ' && substr($final[0], 0, 1) == ' ')
433
$prefix .= substr($orig[0], 0, 1);
434
$orig[0] = substr($orig[0], 1);
435
$final[0] = substr($final[0], 1);
438
return $prefix . $this->_deleted($orig) . $this->_added($final);
441
$text1 = implode("\n", $orig);
442
$text2 = implode("\n", $final);
444
// Non-printing newline marker.
447
// We want to split on word boundaries, but we need to preserve whitespace as well.
448
// Therefore we split on words, but include all blocks of whitespace in the wordlist.
449
$splitted_text_1 = $this->_split_on_words($text1, $nl);
450
$splitted_text_2 = $this->_split_on_words($text2, $nl);
452
$diff = &new diff($splitted_text_1, $splitted_text_2);
453
unset($splitted_text_1, $splitted_text_2);
455
// Get the diff in inline format.
456
$renderer = &new diff_renderer_inline(array_merge($this->get_params(), array('split_level' => 'words')));
458
// Run the diff and get the output.
459
return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
462
function _split_on_words($string, $newline_escape = "\n")
464
// Ignore \0; otherwise the while loop will never finish.
465
$string = str_replace("\0", '', $string);
468
$length = strlen($string);
472
while ($pos < $length)
474
// Check for tabs... do not include them
475
if ($tab_there && substr($string, $pos, 1) === "\t")
487
// Eat a word with any preceding whitespace.
488
$spaces = strspn(substr($string, $pos), " \n");
489
$nextpos = strcspn(substr($string, $pos + $spaces), " \n");
490
$words[] = str_replace("\n", $newline_escape, substr($string, $pos, $spaces + $nextpos));
491
$pos += $spaces + $nextpos;
497
function _encode(&$string)
499
$string = htmlspecialchars($string);
504
* "raw" diff renderer.
505
* This class could be used to output a raw unified patch file
509
class diff_renderer_raw extends diff_renderer
511
var $_leading_context_lines = 4;
512
var $_trailing_context_lines = 4;
515
* Our function to get the diff
517
function get_diff_content($diff)
519
return '<textarea style="height: 290px;" class="full">' . htmlspecialchars($this->render($diff)) . '</textarea>';
522
function _block_header($xbeg, $xlen, $ybeg, $ylen)
526
$xbeg .= ',' . $xlen;
531
$ybeg .= ',' . $ylen;
533
return '@@ -' . $xbeg . ' +' . $ybeg . ' @@';
536
function _context($lines)
538
return $this->_lines($lines, ' ');
541
function _added($lines)
543
return $this->_lines($lines, '+');
546
function _deleted($lines)
548
return $this->_lines($lines, '-');
551
function _changed($orig, $final)
553
return $this->_deleted($orig) . $this->_added($final);
558
* "chora (Horde)" diff renderer - similar style.
559
* This renderer class is a modified human_readable function from the Horde Framework.
563
class diff_renderer_side_by_side extends diff_renderer
565
var $_leading_context_lines = 3;
566
var $_trailing_context_lines = 3;
568
var $lines = array();
570
// Hold the left and right columns of lines for change blocks.
577
* Our function to get the diff
579
function get_diff_content($diff)
584
$output .= '<table cellspacing="0" class="hrdiff">
586
<span class="unmodified"> </span> ' . $user->lang['LINE_UNMODIFIED'] . '
587
<span class="added"> </span> ' . $user->lang['LINE_ADDED'] . '
588
<span class="modified"> </span> ' . $user->lang['LINE_MODIFIED'] . '
589
<span class="removed"> </span> ' . $user->lang['LINE_REMOVED'] . '
594
$this->render($diff);
596
// Is the diff empty?
597
if (!sizeof($this->lines))
599
$output .= '<tr><th colspan="2">' . $user->lang['NO_VISIBLE_CHANGES'] . '</th></tr>';
603
// Iterate through every header block of changes
604
foreach ($this->lines as $header)
606
$output .= '<tr><th>Line ' . $header['oldline'] . '</th><th>' . $user->lang['LINE'] . ' ' . $header['newline'] . '</th></tr>';
608
// Each header block consists of a number of changes (add, remove, change).
609
$current_context = '';
611
foreach ($header['contents'] as $change)
613
if (!empty($current_context) && $change['type'] != 'empty')
615
$line = $current_context;
616
$current_context = '';
618
$output .= '<tr class="unmodified"><td><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td>
619
<td><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td></tr>';
622
switch ($change['type'])
627
foreach ($change['lines'] as $_line)
629
$line .= htmlspecialchars($_line) . '<br />';
632
$output .= '<tr><td class="added_empty"> </td><td class="added"><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td></tr>';
638
foreach ($change['lines'] as $_line)
640
$line .= htmlspecialchars($_line) . '<br />';
643
$output .= '<tr><td class="removed"><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td><td class="removed_empty"> </td></tr>';
647
$current_context .= htmlspecialchars($change['line']) . '<br />';
651
// Pop the old/new stacks one by one, until both are empty.
652
$oldsize = sizeof($change['old']);
653
$newsize = sizeof($change['new']);
656
for ($row = 0, $row_max = max($oldsize, $newsize); $row < $row_max; ++$row)
658
$left .= isset($change['old'][$row]) ? htmlspecialchars($change['old'][$row]) : '';
660
$right .= isset($change['new'][$row]) ? htmlspecialchars($change['new'][$row]) : '';
668
$output .= '<td class="modified"><pre>' . $left . '<br /></pre></td>';
670
else if ($row < $oldsize)
672
$output .= '<td class="modified"> </td>';
676
$output .= '<td class="unmodified"> </td>';
681
$output .= '<td class="modified"><pre>' . $right . '<br /></pre></td>';
683
else if ($row < $newsize)
685
$output .= '<td class="modified"> </td>';
689
$output .= '<td class="unmodified"> </td>';
697
if (!empty($current_context))
699
$line = $current_context;
700
$current_context = '';
702
$output .= '<tr class="unmodified"><td><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td>';
703
$output .= '<td><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td></tr>';
708
$output .= '</tbody></table>';
713
function _start_diff()
715
$this->lines = array();
718
$this->cols = array(array(), array());
719
$this->state = 'empty';
726
// Just flush any remaining entries in the columns stack.
727
switch ($this->state)
730
$this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]);
734
// We have some removal lines pending in our stack, so flush them.
735
$this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]);
739
// We have both remove and addition lines, so this is a change block.
740
$this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]);
744
if ($this->data !== false)
746
$this->lines[] = $this->data;
752
function _block_header($xbeg, $xlen, $ybeg, $ylen)
754
// Push any previous header information to the return stack.
755
if ($this->data !== false)
757
$this->lines[] = $this->data;
760
$this->data = array('type' => 'header', 'oldline' => $xbeg, 'newline' => $ybeg, 'contents' => array());
761
$this->state = 'dump';
764
function _added($lines)
766
array_walk($lines, array(&$this, '_perform_add'));
769
function _perform_add($line)
771
if ($this->state == 'empty')
776
// This is just an addition line.
777
if ($this->state == 'dump' || $this->state == 'add')
779
// Start adding to the addition stack.
780
$this->cols[0][] = $line;
781
$this->state = 'add';
785
// This is inside a change block, so start accumulating lines.
786
$this->state = 'change';
787
$this->cols[1][] = $line;
791
function _deleted($lines)
793
array_walk($lines, array(&$this, '_perform_delete'));
796
function _perform_delete($line)
798
// This is a removal line.
799
$this->state = 'remove';
800
$this->cols[0][] = $line;
803
function _context($lines)
805
array_walk($lines, array(&$this, '_perform_context'));
808
function _perform_context($line)
810
// An empty block with no action.
811
switch ($this->state)
814
$this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]);
818
// We have some removal lines pending in our stack, so flush them.
819
$this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]);
823
// We have both remove and addition lines, so this is a change block.
824
$this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]);
828
$this->cols = array(array(), array());
829
$this->data['contents'][] = array('type' => 'empty', 'line' => $line);
830
$this->state = 'dump';
833
function _changed($orig, $final)
835
return $this->_deleted($orig) . $this->_added($final);
b'\\ No newline at end of file'