Blame Identity/Webenv/App/phpBB/3.0.4/includes/diff/renderer.php

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