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

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