| <?php |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if (!defined('IN_PHPBB')) |
| { |
| exit; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class diff_renderer |
| { |
| |
| |
| |
| |
| |
| |
| var $_leading_context_lines = 0; |
| |
| |
| |
| |
| |
| |
| |
| var $_trailing_context_lines = 0; |
| |
| |
| |
| |
| function diff_renderer($params = array()) |
| { |
| foreach ($params as $param => $value) |
| { |
| $v = '_' . $param; |
| if (isset($this->$v)) |
| { |
| $this->$v = $value; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| function get_params() |
| { |
| $params = array(); |
| foreach (get_object_vars($this) as $k => $v) |
| { |
| if ($k[0] == '_') |
| { |
| $params[substr($k, 1)] = $v; |
| } |
| } |
| |
| return $params; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| function render(&$diff) |
| { |
| $xi = $yi = 1; |
| $block = false; |
| $context = array(); |
| |
| |
| if (is_a($diff, 'diff3')) |
| { |
| $diff3 = &$diff; |
| |
| $diff_1 = $diff3->get_original(); |
| $diff_2 = $diff3->merged_output(); |
| |
| unset($diff3); |
| |
| $diff = new diff($diff_1, $diff_2); |
| } |
| |
| $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); |
| } |
| |
| |
| if ($xlen && !$ylen) |
| { |
| $ybeg--; |
| } |
| else if (!$xlen) |
| { |
| $xbeg--; |
| } |
| |
| 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); |
| } |
| |
| |
| |
| |
| function get_diff_content($diff) |
| { |
| return $this->render($diff); |
| } |
| } |
| |
| |
| |
| |
| |
| class diff_renderer_unified extends diff_renderer |
| { |
| var $_leading_context_lines = 4; |
| var $_trailing_context_lines = 4; |
| |
| |
| |
| |
| 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, ' ')) . '<br /></pre>'; |
| } |
| |
| function _added($lines) |
| { |
| return '<pre class="diff added">' . htmlspecialchars($this->_lines($lines, '+')) . '<br /></pre>'; |
| } |
| |
| function _deleted($lines) |
| { |
| return '<pre class="diff removed">' . htmlspecialchars($this->_lines($lines, '-')) . '<br /></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 ''; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class diff_renderer_inline extends diff_renderer |
| { |
| var $_leading_context_lines = 10000; |
| var $_trailing_context_lines = 10000; |
| |
| |
| var $_ins_prefix = '<span class="ins">'; |
| var $_ins_suffix = '</span>'; |
| |
| |
| var $_del_prefix = '<span class="del">'; |
| var $_del_suffix = '</span>'; |
| |
| var $_block_head = ''; |
| |
| |
| var $_split_level = 'lines'; |
| |
| |
| |
| |
| function get_diff_content($diff) |
| { |
| return '<pre>' . nl2br($this->render($diff)) . '<br /></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 ($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); |
| |
| |
| $nl = "\0"; |
| |
| |
| |
| $splitted_text_1 = $this->_split_on_words($text1, $nl); |
| $splitted_text_2 = $this->_split_on_words($text2, $nl); |
| |
| $diff = new diff($splitted_text_1, $splitted_text_2); |
| unset($splitted_text_1, $splitted_text_2); |
| |
| |
| $renderer = new diff_renderer_inline(array_merge($this->get_params(), array('split_level' => 'words'))); |
| |
| |
| return str_replace($nl, "\n", $renderer->render($diff)) . "\n"; |
| } |
| |
| function _split_on_words($string, $newline_escape = "\n") |
| { |
| |
| $string = str_replace("\0", '', $string); |
| |
| $words = array(); |
| $length = strlen($string); |
| $pos = 0; |
| |
| $tab_there = true; |
| while ($pos < $length) |
| { |
| |
| if ($tab_there && substr($string, $pos, 1) === "\t") |
| { |
| $words[] = "\t"; |
| $pos++; |
| |
| continue; |
| } |
| else |
| { |
| $tab_there = false; |
| } |
| |
| |
| $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); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| class diff_renderer_raw extends diff_renderer |
| { |
| var $_leading_context_lines = 4; |
| var $_trailing_context_lines = 4; |
| |
| |
| |
| |
| function get_diff_content($diff) |
| { |
| return '<textarea style="height: 290px;" 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); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| class diff_renderer_side_by_side extends diff_renderer |
| { |
| var $_leading_context_lines = 3; |
| var $_trailing_context_lines = 3; |
| |
| var $lines = array(); |
| |
| |
| var $cols; |
| var $state; |
| |
| var $data = false; |
| |
| |
| |
| |
| 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); |
| |
| |
| if (!sizeof($this->lines)) |
| { |
| $output .= '<tr><th colspan="2">' . $user->lang['NO_VISIBLE_CHANGES'] . '</th></tr>'; |
| } |
| else |
| { |
| |
| foreach ($this->lines as $header) |
| { |
| $output .= '<tr><th>' . $user->lang['LINE'] . ' ' . $header['oldline'] . '</th><th>' . $user->lang['LINE'] . ' ' . $header['newline'] . '</th></tr>'; |
| |
| |
| $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 : ' ') . '<br /></pre></td> |
| <td><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></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 : ' ') . '<br /></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 : ' ') . '<br /></pre></td><td class="removed_empty"> </td></tr>'; |
| break; |
| |
| case 'empty': |
| $current_context .= htmlspecialchars($change['line']) . '<br />'; |
| break; |
| |
| case 'change': |
| |
| $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 . '<br /></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 . '<br /></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 : ' ') . '<br /></pre></td>'; |
| $output .= '<td><pre>' . ((strlen($line)) ? $line : ' ') . '<br /></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() |
| { |
| |
| switch ($this->state) |
| { |
| case 'add': |
| $this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]); |
| break; |
| |
| case 'remove': |
| |
| $this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]); |
| break; |
| |
| case 'change': |
| |
| $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) |
| { |
| |
| 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 ''; |
| } |
| |
| |
| if ($this->state == 'dump' || $this->state == 'add') |
| { |
| |
| $this->cols[0][] = $line; |
| $this->state = 'add'; |
| } |
| else |
| { |
| |
| $this->state = 'change'; |
| $this->cols[1][] = $line; |
| } |
| } |
| |
| function _deleted($lines) |
| { |
| array_walk($lines, array(&$this, '_perform_delete')); |
| } |
| |
| function _perform_delete($line) |
| { |
| |
| $this->state = 'remove'; |
| $this->cols[0][] = $line; |
| } |
| |
| function _context($lines) |
| { |
| array_walk($lines, array(&$this, '_perform_context')); |
| } |
| |
| function _perform_context($line) |
| { |
| |
| switch ($this->state) |
| { |
| case 'add': |
| $this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]); |
| break; |
| |
| case 'remove': |
| |
| $this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]); |
| break; |
| |
| case 'change': |
| |
| $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); |
| } |
| |
| } |
| |
| ?> |