Blame Extras/phpBB/3.0.4/includes/diff/renderer.php

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package diff
4c79b5
* @version $Id: renderer.php 8766 2008-08-16 22:24:54Z aptx $
4c79b5
* @copyright (c) 2006 phpBB Group
4c79b5
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
4c79b5
*
4c79b5
*/
4c79b5
4c79b5
/**
4c79b5
* @ignore
4c79b5
*/
4c79b5
if (!defined('IN_PHPBB'))
4c79b5
{
4c79b5
	exit;
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Code from pear.php.net, Text_Diff-1.0.0 package
4c79b5
* http://pear.php.net/package/Text_Diff/
4c79b5
*
4c79b5
* Modified by phpBB Group to meet our coding standards
4c79b5
* and being able to integrate into phpBB
4c79b5
*
4c79b5
* A class to render Diffs in different formats.
4c79b5
*
4c79b5
* This class renders the diff in classic diff format. It is intended that
4c79b5
* this class be customized via inheritance, to obtain fancier outputs.
4c79b5
*
4c79b5
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
4c79b5
*
4c79b5
* @package diff
4c79b5
*/
4c79b5
class diff_renderer
4c79b5
{
4c79b5
	/**
4c79b5
	* Number of leading context "lines" to preserve.
4c79b5
	*
4c79b5
	* This should be left at zero for this class, but subclasses may want to
4c79b5
	* set this to other values.
4c79b5
	*/
4c79b5
	var $_leading_context_lines = 0;
4c79b5
4c79b5
	/**
4c79b5
	* Number of trailing context "lines" to preserve.
4c79b5
	*
4c79b5
	* This should be left at zero for this class, but subclasses may want to
4c79b5
	* set this to other values.
4c79b5
	*/
4c79b5
	var $_trailing_context_lines = 0;
4c79b5
4c79b5
	/**
4c79b5
	* Constructor.
4c79b5
	*/
4c79b5
	function diff_renderer($params = array())
4c79b5
	{
4c79b5
		foreach ($params as $param => $value)
4c79b5
		{
4c79b5
			$v = '_' . $param;
4c79b5
			if (isset($this->$v))
4c79b5
			{
4c79b5
				$this->$v = $value;
4c79b5
			}
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Get any renderer parameters.
4c79b5
	*
4c79b5
	* @return array  All parameters of this renderer object.
4c79b5
	*/
4c79b5
	function get_params()
4c79b5
	{
4c79b5
		$params = array();
4c79b5
		foreach (get_object_vars($this) as $k => $v)
4c79b5
		{
4c79b5
			if ($k[0] == '_')
4c79b5
			{
4c79b5
				$params[substr($k, 1)] = $v;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		return $params;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Renders a diff.
4c79b5
	*
4c79b5
	* @param diff &$diff A diff object.
4c79b5
	*
4c79b5
	* @return string  The formatted output.
4c79b5
	*/
4c79b5
	function render(&$diff)
4c79b5
	{
4c79b5
		$xi = $yi = 1;
4c79b5
		$block = false;
4c79b5
		$context = array();
4c79b5
4c79b5
		// Create a new diff object if it is a 3-way diff
4c79b5
		if (is_a($diff, 'diff3'))
4c79b5
		{
4c79b5
			$diff3 = &$diff;
4c79b5
4c79b5
			$diff_1 = $diff3->get_original();
4c79b5
			$diff_2 = $diff3->merged_output();
4c79b5
4c79b5
			unset($diff3);
4c79b5
4c79b5
			$diff = new diff($diff_1, $diff_2);
4c79b5
		}
4c79b5
4c79b5
		$nlead = $this->_leading_context_lines;
4c79b5
		$ntrail = $this->_trailing_context_lines;
4c79b5
4c79b5
		$output = $this->_start_diff();
4c79b5
		$diffs = $diff->get_diff();
4c79b5
4c79b5
		foreach ($diffs as $i => $edit)
4c79b5
		{
4c79b5
			// If these are unchanged (copied) lines, and we want to keep leading or trailing context lines, extract them from the copy block.
4c79b5
			if (is_a($edit, 'diff_op_copy'))
4c79b5
			{
4c79b5
				// Do we have any diff blocks yet?
4c79b5
				if (is_array($block))
4c79b5
				{
4c79b5
					// How many lines to keep as context from the copy block.
4c79b5
					$keep = ($i == sizeof($diffs) - 1) ? $ntrail : $nlead + $ntrail;
4c79b5
					if (sizeof($edit->orig) <= $keep)
4c79b5
					{
4c79b5
						// We have less lines in the block than we want for context => keep the whole block.
4c79b5
						$block[] = $edit;
4c79b5
					}
4c79b5
					else
4c79b5
					{
4c79b5
						if ($ntrail)
4c79b5
						{
4c79b5
							// Create a new block with as many lines as we need for the trailing context.
4c79b5
							$context = array_slice($edit->orig, 0, $ntrail);
4c79b5
							$block[] = new diff_op_copy($context);
4c79b5
						}
4c79b5
4c79b5
						$output .= $this->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block);
4c79b5
						$block = false;
4c79b5
					}
4c79b5
				}
4c79b5
				// Keep the copy block as the context for the next block.
4c79b5
				$context = $edit->orig;
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				// Don't we have any diff blocks yet?
4c79b5
				if (!is_array($block))
4c79b5
				{
4c79b5
					// Extract context lines from the preceding copy block.
4c79b5
					$context = array_slice($context, sizeof($context) - $nlead);
4c79b5
					$x0 = $xi - sizeof($context);
4c79b5
					$y0 = $yi - sizeof($context);
4c79b5
					$block = array();
4c79b5
4c79b5
					if ($context)
4c79b5
					{
4c79b5
						$block[] = new diff_op_copy($context);
4c79b5
					}
4c79b5
				}
4c79b5
				$block[] = $edit;
4c79b5
			}
4c79b5
4c79b5
			$xi += ($edit->orig) ? sizeof($edit->orig) : 0;
4c79b5
			$yi += ($edit->final) ? sizeof($edit->final) : 0;
4c79b5
		}
4c79b5
4c79b5
		if (is_array($block))
4c79b5
		{
4c79b5
			$output .= $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block);
4c79b5
		}
4c79b5
4c79b5
		return $output . $this->_end_diff();
4c79b5
	}
4c79b5
4c79b5
	function _block($xbeg, $xlen, $ybeg, $ylen, &$edits)
4c79b5
	{
4c79b5
		$output = $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
4c79b5
4c79b5
		foreach ($edits as $edit)
4c79b5
		{
4c79b5
			switch (get_class($edit))
4c79b5
			{
4c79b5
				case 'diff_op_copy':
4c79b5
					$output .= $this->_context($edit->orig);
4c79b5
				break;
4c79b5
4c79b5
				case 'diff_op_add':
4c79b5
					$output .= $this->_added($edit->final);
4c79b5
				break;
4c79b5
4c79b5
				case 'diff_op_delete':
4c79b5
					$output .= $this->_deleted($edit->orig);
4c79b5
				break;
4c79b5
4c79b5
				case 'diff_op_change':
4c79b5
					$output .= $this->_changed($edit->orig, $edit->final);
4c79b5
				break;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		return $output . $this->_end_block();
4c79b5
	}
4c79b5
4c79b5
	function _start_diff()
4c79b5
	{
4c79b5
		return '';
4c79b5
	}
4c79b5
4c79b5
	function _end_diff()
4c79b5
	{
4c79b5
		return '';
4c79b5
	}
4c79b5
4c79b5
	function _block_header($xbeg, $xlen, $ybeg, $ylen)
4c79b5
	{
4c79b5
		if ($xlen > 1)
4c79b5
		{
4c79b5
			$xbeg .= ',' . ($xbeg + $xlen - 1);
4c79b5
		}
4c79b5
4c79b5
		if ($ylen > 1)
4c79b5
		{
4c79b5
			$ybeg .= ',' . ($ybeg + $ylen - 1);
4c79b5
		}
4c79b5
4c79b5
		// this matches the GNU Diff behaviour
4c79b5
		if ($xlen && !$ylen)
4c79b5
		{
4c79b5
			$ybeg--;
4c79b5
		}
4c79b5
		else if (!$xlen)
4c79b5
		{
4c79b5
			$xbeg--;
4c79b5
		}
4c79b5
4c79b5
		return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
4c79b5
	}
4c79b5
4c79b5
	function _start_block($header)
4c79b5
	{
4c79b5
		return $header . "\n";
4c79b5
	}
4c79b5
4c79b5
	function _end_block()
4c79b5
	{
4c79b5
		return '';
4c79b5
	}
4c79b5
4c79b5
	function _lines($lines, $prefix = ' ')
4c79b5
	{
4c79b5
		return $prefix . implode("\n$prefix", $lines) . "\n";
4c79b5
	}
4c79b5
4c79b5
	function _context($lines)
4c79b5
	{
4c79b5
		return $this->_lines($lines, '  ');
4c79b5
	}
4c79b5
4c79b5
	function _added($lines)
4c79b5
	{
4c79b5
		return $this->_lines($lines, '> ');
4c79b5
	}
4c79b5
4c79b5
	function _deleted($lines)
4c79b5
	{
4c79b5
		return $this->_lines($lines, '< ');
4c79b5
	}
4c79b5
4c79b5
	function _changed($orig, $final)
4c79b5
	{
4c79b5
		return $this->_deleted($orig) . "---\n" . $this->_added($final);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Our function to get the diff
4c79b5
	*/
4c79b5
	function get_diff_content($diff)
4c79b5
	{
4c79b5
		return $this->render($diff);
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* Renders a unified diff
4c79b5
* @package diff
4c79b5
*/
4c79b5
class diff_renderer_unified extends diff_renderer
4c79b5
{
4c79b5
	var $_leading_context_lines = 4;
4c79b5
	var $_trailing_context_lines = 4;
4c79b5
4c79b5
	/**
4c79b5
	* Our function to get the diff
4c79b5
	*/
4c79b5
	function get_diff_content($diff)
4c79b5
	{
4c79b5
		return nl2br($this->render($diff));
4c79b5
	}
4c79b5
4c79b5
	function _block_header($xbeg, $xlen, $ybeg, $ylen)
4c79b5
	{
4c79b5
		if ($xlen != 1)
4c79b5
		{
4c79b5
			$xbeg .= ',' . $xlen;
4c79b5
		}
4c79b5
4c79b5
		if ($ylen != 1)
4c79b5
		{
4c79b5
			$ybeg .= ',' . $ylen;
4c79b5
		}
4c79b5
		return '
<big class="info">@@ -' . $xbeg . ' +' . $ybeg . ' @@</big>
';
4c79b5
	}
4c79b5
4c79b5
	function _context($lines)
4c79b5
	{
4c79b5
		return '
' . htmlspecialchars($this->_lines($lines, ' ')) . '
';
4c79b5
	}
4c79b5
4c79b5
	function _added($lines)
4c79b5
	{
4c79b5
		return '
' . htmlspecialchars($this->_lines($lines, '+')) . '
';
4c79b5
	}
4c79b5
4c79b5
	function _deleted($lines)
4c79b5
	{
4c79b5
		return '
' . htmlspecialchars($this->_lines($lines, '-')) . '
';
4c79b5
	}
4c79b5
4c79b5
	function _changed($orig, $final)
4c79b5
	{
4c79b5
		return $this->_deleted($orig) . $this->_added($final);
4c79b5
	}
4c79b5
4c79b5
	function _start_diff()
4c79b5
	{
4c79b5
		$start = '
';
4c79b5
4c79b5
		return $start;
4c79b5
	}
4c79b5
4c79b5
	function _end_diff()
4c79b5
	{
4c79b5
		return '';
4c79b5
	}
4c79b5
4c79b5
	function _end_block()
4c79b5
	{
4c79b5
		return '';
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* "Inline" diff renderer.
4c79b5
*
4c79b5
* This class renders diffs in the Wiki-style "inline" format.
4c79b5
*
4c79b5
* @author  Ciprian Popovici
4c79b5
* @package diff
4c79b5
*/
4c79b5
class diff_renderer_inline extends diff_renderer
4c79b5
{
4c79b5
	var $_leading_context_lines = 10000;
4c79b5
	var $_trailing_context_lines = 10000;
4c79b5
4c79b5
	// Prefix and suffix for inserted text
4c79b5
	var $_ins_prefix = '';
4c79b5
	var $_ins_suffix = '';
4c79b5
4c79b5
	// Prefix and suffix for deleted text
4c79b5
	var $_del_prefix = '';
4c79b5
	var $_del_suffix = '';
4c79b5
4c79b5
	var $_block_head = '';
4c79b5
4c79b5
	// What are we currently splitting on? Used to recurse to show word-level
4c79b5
	var $_split_level = 'lines';
4c79b5
4c79b5
	/**
4c79b5
	* Our function to get the diff
4c79b5
	*/
4c79b5
	function get_diff_content($diff)
4c79b5
	{
4c79b5
		return '
' . nl2br($this->render($diff)) . '
';
4c79b5
	}
4c79b5
4c79b5
	function _start_diff()
4c79b5
	{
4c79b5
		return '';
4c79b5
	}
4c79b5
4c79b5
	function _end_diff()
4c79b5
	{
4c79b5
		return '';
4c79b5
	}
4c79b5
4c79b5
	function _block_header($xbeg, $xlen, $ybeg, $ylen)
4c79b5
	{
4c79b5
		return $this->_block_head;
4c79b5
	}
4c79b5
4c79b5
	function _start_block($header)
4c79b5
	{
4c79b5
		return $header;
4c79b5
	}
4c79b5
4c79b5
	function _lines($lines, $prefix = ' ', $encode = true)
4c79b5
	{
4c79b5
		if ($encode)
4c79b5
		{
4c79b5
			array_walk($lines, array(&$this, '_encode'));
4c79b5
		}
4c79b5
4c79b5
		if ($this->_split_level == 'words')
4c79b5
		{
4c79b5
			return implode('', $lines);
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			return implode("\n", $lines) . "\n";
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	function _added($lines)
4c79b5
	{
4c79b5
		array_walk($lines, array(&$this, '_encode'));
4c79b5
		$lines[0] = $this->_ins_prefix . $lines[0];
4c79b5
		$lines[sizeof($lines) - 1] .= $this->_ins_suffix;
4c79b5
		return $this->_lines($lines, ' ', false);
4c79b5
	}
4c79b5
4c79b5
	function _deleted($lines, $words = false)
4c79b5
	{
4c79b5
		array_walk($lines, array(&$this, '_encode'));
4c79b5
		$lines[0] = $this->_del_prefix . $lines[0];
4c79b5
		$lines[sizeof($lines) - 1] .= $this->_del_suffix;
4c79b5
		return $this->_lines($lines, ' ', false);
4c79b5
	}
4c79b5
4c79b5
	function _changed($orig, $final)
4c79b5
	{
4c79b5
		// If we've already split on words, don't try to do so again - just display.
4c79b5
		if ($this->_split_level == 'words')
4c79b5
		{
4c79b5
			$prefix = '';
4c79b5
			while ($orig[0] !== false && $final[0] !== false && substr($orig[0], 0, 1) == ' ' && substr($final[0], 0, 1) == ' ')
4c79b5
			{
4c79b5
				$prefix .= substr($orig[0], 0, 1);
4c79b5
				$orig[0] = substr($orig[0], 1);
4c79b5
				$final[0] = substr($final[0], 1);
4c79b5
			}
4c79b5
4c79b5
			return $prefix . $this->_deleted($orig) . $this->_added($final);
4c79b5
		}
4c79b5
4c79b5
		$text1 = implode("\n", $orig);
4c79b5
		$text2 = implode("\n", $final);
4c79b5
4c79b5
		// Non-printing newline marker.
4c79b5
		$nl = "\0";
4c79b5
4c79b5
		// We want to split on word boundaries, but we need to preserve whitespace as well.
4c79b5
		// Therefore we split on words, but include all blocks of whitespace in the wordlist.
4c79b5
		$splitted_text_1 = $this->_split_on_words($text1, $nl);
4c79b5
		$splitted_text_2 = $this->_split_on_words($text2, $nl);
4c79b5
4c79b5
		$diff = new diff($splitted_text_1, $splitted_text_2);
4c79b5
		unset($splitted_text_1, $splitted_text_2);
4c79b5
4c79b5
		// Get the diff in inline format.
4c79b5
		$renderer = new diff_renderer_inline(array_merge($this->get_params(), array('split_level' => 'words')));
4c79b5
4c79b5
		// Run the diff and get the output.
4c79b5
		return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
4c79b5
	}
4c79b5
4c79b5
	function _split_on_words($string, $newline_escape = "\n")
4c79b5
	{
4c79b5
		// Ignore \0; otherwise the while loop will never finish.
4c79b5
		$string = str_replace("\0", '', $string);
4c79b5
4c79b5
		$words = array();
4c79b5
		$length = strlen($string);
4c79b5
		$pos = 0;
4c79b5
4c79b5
		$tab_there = true;
4c79b5
		while ($pos < $length)
4c79b5
		{
4c79b5
			// Check for tabs... do not include them
4c79b5
			if ($tab_there && substr($string, $pos, 1) === "\t")
4c79b5
			{
4c79b5
				$words[] = "\t";
4c79b5
				$pos++;
4c79b5
4c79b5
				continue;
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$tab_there = false;
4c79b5
			}
4c79b5
4c79b5
			// Eat a word with any preceding whitespace.
4c79b5
			$spaces = strspn(substr($string, $pos), " \n");
4c79b5
			$nextpos = strcspn(substr($string, $pos + $spaces), " \n");
4c79b5
			$words[] = str_replace("\n", $newline_escape, substr($string, $pos, $spaces + $nextpos));
4c79b5
			$pos += $spaces + $nextpos;
4c79b5
		}
4c79b5
4c79b5
		return $words;
4c79b5
	}
4c79b5
4c79b5
	function _encode(&$string)
4c79b5
	{
4c79b5
		$string = htmlspecialchars($string);
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* "raw" diff renderer.
4c79b5
* This class could be used to output a raw unified patch file
4c79b5
*
4c79b5
* @package diff
4c79b5
*/
4c79b5
class diff_renderer_raw extends diff_renderer
4c79b5
{
4c79b5
	var $_leading_context_lines = 4;
4c79b5
	var $_trailing_context_lines = 4;
4c79b5
4c79b5
	/**
4c79b5
	* Our function to get the diff
4c79b5
	*/
4c79b5
	function get_diff_content($diff)
4c79b5
	{
4c79b5
		return '<textarea style="height: 290px;" class="full">' . htmlspecialchars($this->render($diff)) . '</textarea>';
4c79b5
	}
4c79b5
4c79b5
	function _block_header($xbeg, $xlen, $ybeg, $ylen)
4c79b5
	{
4c79b5
		if ($xlen != 1)
4c79b5
		{
4c79b5
			$xbeg .= ',' . $xlen;
4c79b5
		}
4c79b5
4c79b5
		if ($ylen != 1)
4c79b5
		{
4c79b5
			$ybeg .= ',' . $ylen;
4c79b5
		}
4c79b5
		return '@@ -' . $xbeg . ' +' . $ybeg . ' @@';
4c79b5
	}
4c79b5
4c79b5
	function _context($lines)
4c79b5
	{
4c79b5
		return $this->_lines($lines, ' ');
4c79b5
	}
4c79b5
4c79b5
	function _added($lines)
4c79b5
	{
4c79b5
		return $this->_lines($lines, '+');
4c79b5
	}
4c79b5
4c79b5
	function _deleted($lines)
4c79b5
	{
4c79b5
		return $this->_lines($lines, '-');
4c79b5
	}
4c79b5
4c79b5
	function _changed($orig, $final)
4c79b5
	{
4c79b5
		return $this->_deleted($orig) . $this->_added($final);
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
/**
4c79b5
* "chora (Horde)" diff renderer - similar style.
4c79b5
* This renderer class is a modified human_readable function from the Horde Framework.
4c79b5
*
4c79b5
* @package diff
4c79b5
*/
4c79b5
class diff_renderer_side_by_side extends diff_renderer
4c79b5
{
4c79b5
	var $_leading_context_lines = 3;
4c79b5
	var $_trailing_context_lines = 3;
4c79b5
4c79b5
	var $lines = array();
4c79b5
4c79b5
	// Hold the left and right columns of lines for change blocks.
4c79b5
	var $cols;
4c79b5
	var $state;
4c79b5
4c79b5
	var $data = false;
4c79b5
4c79b5
	/**
4c79b5
	* Our function to get the diff
4c79b5
	*/
4c79b5
	function get_diff_content($diff)
4c79b5
	{
4c79b5
		global $user;
4c79b5
4c79b5
		$output = '';
4c79b5
		$output .= '
4c79b5
<caption>
4c79b5
	  ' . $user->lang['LINE_UNMODIFIED'] . '
4c79b5
	  ' . $user->lang['LINE_ADDED'] . '
4c79b5
	  ' . $user->lang['LINE_MODIFIED'] . '
4c79b5
	  ' . $user->lang['LINE_REMOVED'] . '
4c79b5
</caption>
4c79b5
4c79b5
';
4c79b5
4c79b5
		$this->render($diff);
4c79b5
4c79b5
		// Is the diff empty?
4c79b5
		if (!sizeof($this->lines))
4c79b5
		{
4c79b5
			$output .= '' . $user->lang['NO_VISIBLE_CHANGES'] . '';
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			// Iterate through every header block of changes
4c79b5
			foreach ($this->lines as $header)
4c79b5
			{
4c79b5
				$output .= '' . $user->lang['LINE'] . ' ' . $header['oldline'] . '' . $user->lang['LINE'] . ' ' . $header['newline'] . '';
4c79b5
4c79b5
				// Each header block consists of a number of changes (add, remove, change).
4c79b5
				$current_context = '';
4c79b5
4c79b5
				foreach ($header['contents'] as $change)
4c79b5
				{
4c79b5
					if (!empty($current_context) && $change['type'] != 'empty')
4c79b5
					{
4c79b5
						$line = $current_context;
4c79b5
						$current_context = '';
4c79b5
4c79b5
						$output .= '
' . ((strlen($line)) ? $line : ' ') . '
4c79b5
							
' . ((strlen($line)) ? $line : ' ') . '
';
4c79b5
					}
4c79b5
4c79b5
					switch ($change['type'])
4c79b5
					{
4c79b5
						case 'add':
4c79b5
							$line = '';
4c79b5
4c79b5
							foreach ($change['lines'] as $_line)
4c79b5
							{
4c79b5
								$line .= htmlspecialchars($_line) . '
';
4c79b5
							}
4c79b5
4c79b5
							$output .= ' 
' . ((strlen($line)) ? $line : ' ') . '
';
4c79b5
						break;
4c79b5
4c79b5
						case 'remove':
4c79b5
							$line = '';
4c79b5
4c79b5
							foreach ($change['lines'] as $_line)
4c79b5
							{
4c79b5
								$line .= htmlspecialchars($_line) . '
';
4c79b5
							}
4c79b5
4c79b5
							$output .= '
' . ((strlen($line)) ? $line : ' ') . '
 ';
4c79b5
						break;
4c79b5
4c79b5
						case 'empty':
4c79b5
							$current_context .= htmlspecialchars($change['line']) . '
';
4c79b5
						break;
4c79b5
4c79b5
						case 'change':
4c79b5
							// Pop the old/new stacks one by one, until both are empty.
4c79b5
							$oldsize = sizeof($change['old']);
4c79b5
							$newsize = sizeof($change['new']);
4c79b5
							$left = $right = '';
4c79b5
4c79b5
							for ($row = 0, $row_max = max($oldsize, $newsize); $row < $row_max; ++$row)
4c79b5
							{
4c79b5
								$left .= isset($change['old'][$row]) ? htmlspecialchars($change['old'][$row]) : '';
4c79b5
								$left .= '
';
4c79b5
								$right .= isset($change['new'][$row]) ? htmlspecialchars($change['new'][$row]) : '';
4c79b5
								$right .= '
';
4c79b5
							}
4c79b5
4c79b5
							$output .= '';
4c79b5
4c79b5
							if (!empty($left))
4c79b5
							{
4c79b5
								$output .= '
' . $left . '
';
4c79b5
							}
4c79b5
							else if ($row < $oldsize)
4c79b5
							{
4c79b5
								$output .= ' ';
4c79b5
							}
4c79b5
							else
4c79b5
							{
4c79b5
								$output .= ' ';
4c79b5
							}
4c79b5
4c79b5
							if (!empty($right))
4c79b5
							{
4c79b5
								$output .= '
' . $right . '
';
4c79b5
							}
4c79b5
							else if ($row < $newsize)
4c79b5
							{
4c79b5
								$output .= ' ';
4c79b5
							}
4c79b5
							else
4c79b5
							{
4c79b5
								$output .= ' ';
4c79b5
							}
4c79b5
4c79b5
							$output .= '';
4c79b5
						break;
4c79b5
					}
4c79b5
				}
4c79b5
4c79b5
				if (!empty($current_context))
4c79b5
				{
4c79b5
					$line = $current_context;
4c79b5
					$current_context = '';
4c79b5
4c79b5
					$output .= '
' . ((strlen($line)) ? $line : ' ') . '
';
4c79b5
					$output .= '
' . ((strlen($line)) ? $line : ' ') . '
';
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$output .= '';
4c79b5
4c79b5
		return $output;
4c79b5
	}
4c79b5
4c79b5
	function _start_diff()
4c79b5
	{
4c79b5
		$this->lines = array();
4c79b5
4c79b5
		$this->data = false;
4c79b5
		$this->cols = array(array(), array());
4c79b5
		$this->state = 'empty';
4c79b5
4c79b5
		return '';
4c79b5
	}
4c79b5
4c79b5
	function _end_diff()
4c79b5
	{
4c79b5
		// Just flush any remaining entries in the columns stack.
4c79b5
		switch ($this->state)
4c79b5
		{
4c79b5
			case 'add':
4c79b5
				$this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]);
4c79b5
			break;
4c79b5
4c79b5
			case 'remove':
4c79b5
				// We have some removal lines pending in our stack, so flush them.
4c79b5
				$this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]);
4c79b5
			break;
4c79b5
4c79b5
			case 'change':
4c79b5
				// We have both remove and addition lines, so this is a change block.
4c79b5
				$this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]);
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
		if ($this->data !== false)
4c79b5
		{
4c79b5
			$this->lines[] = $this->data;
4c79b5
		}
4c79b5
4c79b5
		return '';
4c79b5
	}
4c79b5
4c79b5
	function _block_header($xbeg, $xlen, $ybeg, $ylen)
4c79b5
	{
4c79b5
		// Push any previous header information to the return stack.
4c79b5
		if ($this->data !== false)
4c79b5
		{
4c79b5
			$this->lines[] = $this->data;
4c79b5
		}
4c79b5
4c79b5
		$this->data = array('type' => 'header', 'oldline' => $xbeg, 'newline' => $ybeg, 'contents' => array());
4c79b5
		$this->state = 'dump';
4c79b5
	}
4c79b5
4c79b5
	function _added($lines)
4c79b5
	{
4c79b5
		array_walk($lines, array(&$this, '_perform_add'));
4c79b5
	}
4c79b5
4c79b5
	function _perform_add($line)
4c79b5
	{
4c79b5
		if ($this->state == 'empty')
4c79b5
		{
4c79b5
			return '';
4c79b5
		}
4c79b5
4c79b5
		// This is just an addition line.
4c79b5
		if ($this->state == 'dump' || $this->state == 'add')
4c79b5
		{
4c79b5
			// Start adding to the addition stack.
4c79b5
			$this->cols[0][] = $line;
4c79b5
			$this->state = 'add';
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			// This is inside a change block, so start accumulating lines.
4c79b5
			$this->state = 'change';
4c79b5
			$this->cols[1][] = $line;
4c79b5
		}
4c79b5
	}
4c79b5
4c79b5
	function _deleted($lines)
4c79b5
	{
4c79b5
		array_walk($lines, array(&$this, '_perform_delete'));
4c79b5
	}
4c79b5
4c79b5
	function _perform_delete($line)
4c79b5
	{
4c79b5
		// This is a removal line.
4c79b5
		$this->state = 'remove';
4c79b5
		$this->cols[0][] = $line;
4c79b5
	}
4c79b5
4c79b5
	function _context($lines)
4c79b5
	{
4c79b5
		array_walk($lines, array(&$this, '_perform_context'));
4c79b5
	}
4c79b5
4c79b5
	function _perform_context($line)
4c79b5
	{
4c79b5
		// An empty block with no action.
4c79b5
		switch ($this->state)
4c79b5
		{
4c79b5
			case 'add':
4c79b5
				$this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]);
4c79b5
			break;
4c79b5
4c79b5
			case 'remove':
4c79b5
				// We have some removal lines pending in our stack, so flush them.
4c79b5
				$this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]);
4c79b5
			break;
4c79b5
4c79b5
			case 'change':
4c79b5
				// We have both remove and addition lines, so this is a change block.
4c79b5
				$this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]);
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
		$this->cols = array(array(), array());
4c79b5
		$this->data['contents'][] = array('type' => 'empty', 'line' => $line);
4c79b5
		$this->state = 'dump';
4c79b5
	}
4c79b5
4c79b5
	function _changed($orig, $final)
4c79b5
	{
4c79b5
		return $this->_deleted($orig) . $this->_added($final);
4c79b5
	}
4c79b5
4c79b5
}
4c79b5
4c79b5
?>