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-1.1.0 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.
31 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
38 * Number of leading context "lines" to preserve.
40 * This should be left at zero for this class, but subclasses may want to
41 * set this to other values.
43 var $_leading_context_lines = 0;
46 * Number of trailing context "lines" to preserve.
48 * This should be left at zero for this class, but subclasses may want to
49 * set this to other values.
51 var $_trailing_context_lines = 0;
56 function diff_renderer($params = array())
58 foreach ($params as $param => $value)
69 * Get any renderer parameters.
71 * @return array All parameters of this renderer object.
76 foreach (get_object_vars($this) as $k => $v)
80 $params[substr($k, 1)] = $v;
90 * @param diff &$diff A diff object.
92 * @return string The formatted output.
94 function render(&$diff)
100 // Create a new diff object if it is a 3-way diff
101 if (is_a($diff, 'diff3'))
105 $diff_1 = $diff3->get_original();
106 $diff_2 = $diff3->merged_output();
110 $diff = new diff($diff_1, $diff_2);
113 $nlead = $this->_leading_context_lines;
114 $ntrail = $this->_trailing_context_lines;
116 $output = $this->_start_diff();
117 $diffs = $diff->get_diff();
119 foreach ($diffs as $i => $edit)
121 // If these are unchanged (copied) lines, and we want to keep leading or trailing context lines, extract them from the copy block.
122 if (is_a($edit, 'diff_op_copy'))
124 // Do we have any diff blocks yet?
125 if (is_array($block))
127 // How many lines to keep as context from the copy block.
128 $keep = ($i == sizeof($diffs) - 1) ? $ntrail : $nlead + $ntrail;
129 if (sizeof($edit->orig) <= $keep)
131 // We have less lines in the block than we want for context => keep the whole block.
138 // Create a new block with as many lines as we need for the trailing context.
139 $context = array_slice($edit->orig, 0, $ntrail);
140 $block[] = new diff_op_copy($context);
143 $output .= $this->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block);
147 // Keep the copy block as the context for the next block.
148 $context = $edit->orig;
152 // Don't we have any diff blocks yet?
153 if (!is_array($block))
155 // Extract context lines from the preceding copy block.
156 $context = array_slice($context, sizeof($context) - $nlead);
157 $x0 = $xi - sizeof($context);
158 $y0 = $yi - sizeof($context);
163 $block[] = new diff_op_copy($context);
169 $xi += ($edit->orig) ? sizeof($edit->orig) : 0;
170 $yi += ($edit->final) ? sizeof($edit->final) : 0;
173 if (is_array($block))
175 $output .= $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block);
178 return $output . $this->_end_diff();
181 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits)
183 $output = $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
185 foreach ($edits as $edit)
187 switch (get_class($edit))
190 $output .= $this->_context($edit->orig);
194 $output .= $this->_added($edit->final);
197 case 'diff_op_delete':
198 $output .= $this->_deleted($edit->orig);
201 case 'diff_op_change':
202 $output .= $this->_changed($edit->orig, $edit->final);
207 return $output . $this->_end_block();
210 function _start_diff()
220 function _block_header($xbeg, $xlen, $ybeg, $ylen)
224 $xbeg .= ',' . ($xbeg + $xlen - 1);
229 $ybeg .= ',' . ($ybeg + $ylen - 1);
232 // this matches the GNU Diff behaviour
242 return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
245 function _start_block($header)
247 return $header . "\n";
250 function _end_block()
255 function _lines($lines, $prefix = ' ')
257 return $prefix . implode("\n$prefix", $lines) . "\n";
260 function _context($lines)
262 return $this->_lines($lines, ' ');
265 function _added($lines)
267 return $this->_lines($lines, '> ');
270 function _deleted($lines)
272 return $this->_lines($lines, '< ');
275 function _changed($orig, $final)
277 return $this->_deleted($orig) . "---\n" . $this->_added($final);
281 * Our function to get the diff
283 function get_diff_content($diff)
285 return $this->render($diff);
290 * Renders a unified diff
293 class diff_renderer_unified extends diff_renderer
295 var $_leading_context_lines = 4;
296 var $_trailing_context_lines = 4;
299 * Our function to get the diff
301 function get_diff_content($diff)
303 return nl2br($this->render($diff));
306 function _block_header($xbeg, $xlen, $ybeg, $ylen)
310 $xbeg .= ',' . $xlen;
315 $ybeg .= ',' . $ylen;
317 return '<div class="diff"><big class="info">@@ -' . $xbeg . ' +' . $ybeg . ' @@</big></div>';
320 function _context($lines)
322 return '<pre class="diff context">' . htmlspecialchars($this->_lines($lines, ' ')) . '<br /></pre>';
325 function _added($lines)
327 return '<pre class="diff added">' . htmlspecialchars($this->_lines($lines, '+')) . '<br /></pre>';
330 function _deleted($lines)
332 return '<pre class="diff removed">' . htmlspecialchars($this->_lines($lines, '-')) . '<br /></pre>';
335 function _changed($orig, $final)
337 return $this->_deleted($orig) . $this->_added($final);
340 function _start_diff()
342 $start = '<div class="file">';
352 function _end_block()
359 * "Inline" diff renderer.
361 * This class renders diffs in the Wiki-style "inline" format.
363 * @author Ciprian Popovici
366 class diff_renderer_inline extends diff_renderer
368 var $_leading_context_lines = 10000;
369 var $_trailing_context_lines = 10000;
371 // Prefix and suffix for inserted text
372 var $_ins_prefix = '<span class="ins">';
373 var $_ins_suffix = '</span>';
375 // Prefix and suffix for deleted text
376 var $_del_prefix = '<span class="del">';
377 var $_del_suffix = '</span>';
379 var $_block_head = '';
381 // What are we currently splitting on? Used to recurse to show word-level
382 var $_split_level = 'lines';
385 * Our function to get the diff
387 function get_diff_content($diff)
389 return '<pre>' . nl2br($this->render($diff)) . '<br /></pre>';
392 function _start_diff()
402 function _block_header($xbeg, $xlen, $ybeg, $ylen)
404 return $this->_block_head;
407 function _start_block($header)
412 function _lines($lines, $prefix = ' ', $encode = true)
416 array_walk($lines, array(&$this, '_encode'));
419 if ($this->_split_level == 'words')
421 return implode('', $lines);
425 return implode("\n", $lines) . "\n";
429 function _added($lines)
431 array_walk($lines, array(&$this, '_encode'));
432 $lines[0] = $this->_ins_prefix . $lines[0];
433 $lines[sizeof($lines) - 1] .= $this->_ins_suffix;
434 return $this->_lines($lines, ' ', false);
437 function _deleted($lines, $words = false)
439 array_walk($lines, array(&$this, '_encode'));
440 $lines[0] = $this->_del_prefix . $lines[0];
441 $lines[sizeof($lines) - 1] .= $this->_del_suffix;
442 return $this->_lines($lines, ' ', false);
445 function _changed($orig, $final)
447 // If we've already split on words, don't try to do so again - just display.
448 if ($this->_split_level == 'words')
451 while ($orig[0] !== false && $final[0] !== false && substr($orig[0], 0, 1) == ' ' && substr($final[0], 0, 1) == ' ')
453 $prefix .= substr($orig[0], 0, 1);
454 $orig[0] = substr($orig[0], 1);
455 $final[0] = substr($final[0], 1);
458 return $prefix . $this->_deleted($orig) . $this->_added($final);
461 $text1 = implode("\n", $orig);
462 $text2 = implode("\n", $final);
464 // Non-printing newline marker.
467 // We want to split on word boundaries, but we need to preserve whitespace as well.
468 // Therefore we split on words, but include all blocks of whitespace in the wordlist.
469 $splitted_text_1 = $this->_split_on_words($text1, $nl);
470 $splitted_text_2 = $this->_split_on_words($text2, $nl);
472 $diff = new diff($splitted_text_1, $splitted_text_2);
473 unset($splitted_text_1, $splitted_text_2);
475 // Get the diff in inline format.
476 $renderer = new diff_renderer_inline(array_merge($this->get_params(), array('split_level' => 'words')));
478 // Run the diff and get the output.
479 return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
482 function _split_on_words($string, $newline_escape = "\n")
484 // Ignore \0; otherwise the while loop will never finish.
485 $string = str_replace("\0", '', $string);
488 $length = strlen($string);
492 while ($pos < $length)
494 // Check for tabs... do not include them
495 if ($tab_there && substr($string, $pos, 1) === "\t")
507 // Eat a word with any preceding whitespace.
508 $spaces = strspn(substr($string, $pos), " \n");
509 $nextpos = strcspn(substr($string, $pos + $spaces), " \n");
510 $words[] = str_replace("\n", $newline_escape, substr($string, $pos, $spaces + $nextpos));
511 $pos += $spaces + $nextpos;
517 function _encode(&$string)
519 $string = htmlspecialchars($string);
524 * "raw" diff renderer.
525 * This class could be used to output a raw unified patch file
529 class diff_renderer_raw extends diff_renderer
531 var $_leading_context_lines = 4;
532 var $_trailing_context_lines = 4;
535 * Our function to get the diff
537 function get_diff_content($diff)
539 return '<textarea style="height: 290px;" rows="15" cols="76" class="full">' . htmlspecialchars($this->render($diff)) . '</textarea>';
542 function _block_header($xbeg, $xlen, $ybeg, $ylen)
546 $xbeg .= ',' . $xlen;
551 $ybeg .= ',' . $ylen;
553 return '@@ -' . $xbeg . ' +' . $ybeg . ' @@';
556 function _context($lines)
558 return $this->_lines($lines, ' ');
561 function _added($lines)
563 return $this->_lines($lines, '+');
566 function _deleted($lines)
568 return $this->_lines($lines, '-');
571 function _changed($orig, $final)
573 return $this->_deleted($orig) . $this->_added($final);
578 * "chora (Horde)" diff renderer - similar style.
579 * This renderer class is a modified human_readable function from the Horde Framework.
583 class diff_renderer_side_by_side extends diff_renderer
585 var $_leading_context_lines = 3;
586 var $_trailing_context_lines = 3;
588 var $lines = array();
590 // Hold the left and right columns of lines for change blocks.
597 * Our function to get the diff
599 function get_diff_content($diff)
604 $output .= '<table cellspacing="0" class="hrdiff">
606 <span class="unmodified"> </span> ' . $user->lang['LINE_UNMODIFIED'] . '
607 <span class="added"> </span> ' . $user->lang['LINE_ADDED'] . '
608 <span class="modified"> </span> ' . $user->lang['LINE_MODIFIED'] . '
609 <span class="removed"> </span> ' . $user->lang['LINE_REMOVED'] . '
614 $this->render($diff);
616 // Is the diff empty?
617 if (!sizeof($this->lines))
619 $output .= '<tr><th colspan="2">' . $user->lang['NO_VISIBLE_CHANGES'] . '</th></tr>';
623 // Iterate through every header block of changes
624 foreach ($this->lines as $header)
626 $output .= '<tr><th>' . $user->lang['LINE'] . ' ' . $header['oldline'] . '</th><th>' . $user->lang['LINE'] . ' ' . $header['newline'] . '</th></tr>';
628 // Each header block consists of a number of changes (add, remove, change).
629 $current_context = '';
631 foreach ($header['contents'] as $change)
633 if (!empty($current_context) && $change['type'] != 'empty')
635 $line = $current_context;
636 $current_context = '';
638 $output .= '<tr class="unmodified"><td><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td>
639 <td><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td></tr>';
642 switch ($change['type'])
647 foreach ($change['lines'] as $_line)
649 $line .= htmlspecialchars($_line) . '<br />';
652 $output .= '<tr><td class="added_empty"> </td><td class="added"><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td></tr>';
658 foreach ($change['lines'] as $_line)
660 $line .= htmlspecialchars($_line) . '<br />';
663 $output .= '<tr><td class="removed"><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td><td class="removed_empty"> </td></tr>';
667 $current_context .= htmlspecialchars($change['line']) . '<br />';
671 // Pop the old/new stacks one by one, until both are empty.
672 $oldsize = sizeof($change['old']);
673 $newsize = sizeof($change['new']);
676 for ($row = 0, $row_max = max($oldsize, $newsize); $row < $row_max; ++$row)
678 $left .= isset($change['old'][$row]) ? htmlspecialchars($change['old'][$row]) : '';
680 $right .= isset($change['new'][$row]) ? htmlspecialchars($change['new'][$row]) : '';
688 $output .= '<td class="modified"><pre>' . $left . '<br /></pre></td>';
690 else if ($row < $oldsize)
692 $output .= '<td class="modified"> </td>';
696 $output .= '<td class="unmodified"> </td>';
701 $output .= '<td class="modified"><pre>' . $right . '<br /></pre></td>';
703 else if ($row < $newsize)
705 $output .= '<td class="modified"> </td>';
709 $output .= '<td class="unmodified"> </td>';
717 if (!empty($current_context))
719 $line = $current_context;
720 $current_context = '';
722 $output .= '<tr class="unmodified"><td><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td>';
723 $output .= '<td><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></pre></td></tr>';
728 $output .= '</tbody></table>';
733 function _start_diff()
735 $this->lines = array();
738 $this->cols = array(array(), array());
739 $this->state = 'empty';
746 // Just flush any remaining entries in the columns stack.
747 switch ($this->state)
750 $this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]);
754 // We have some removal lines pending in our stack, so flush them.
755 $this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]);
759 // We have both remove and addition lines, so this is a change block.
760 $this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]);
764 if ($this->data !== false)
766 $this->lines[] = $this->data;
772 function _block_header($xbeg, $xlen, $ybeg, $ylen)
774 // Push any previous header information to the return stack.
775 if ($this->data !== false)
777 $this->lines[] = $this->data;
780 $this->data = array('type' => 'header', 'oldline' => $xbeg, 'newline' => $ybeg, 'contents' => array());
781 $this->state = 'dump';
784 function _added($lines)
786 array_walk($lines, array(&$this, '_perform_add'));
789 function _perform_add($line)
791 if ($this->state == 'empty')
796 // This is just an addition line.
797 if ($this->state == 'dump' || $this->state == 'add')
799 // Start adding to the addition stack.
800 $this->cols[0][] = $line;
801 $this->state = 'add';
805 // This is inside a change block, so start accumulating lines.
806 $this->state = 'change';
807 $this->cols[1][] = $line;
811 function _deleted($lines)
813 array_walk($lines, array(&$this, '_perform_delete'));
816 function _perform_delete($line)
818 // This is a removal line.
819 $this->state = 'remove';
820 $this->cols[0][] = $line;
823 function _context($lines)
825 array_walk($lines, array(&$this, '_perform_context'));
828 function _perform_context($line)
830 // An empty block with no action.
831 switch ($this->state)
834 $this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]);
838 // We have some removal lines pending in our stack, so flush them.
839 $this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]);
843 // We have both remove and addition lines, so this is a change block.
844 $this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]);
848 $this->cols = array(array(), array());
849 $this->data['contents'][] = array('type' => 'empty', 'line' => $line);
850 $this->state = 'dump';
853 function _changed($orig, $final)
855 return $this->_deleted($orig) . $this->_added($final);