includes/diff/renderer.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:53:57 +0400
changeset 17 049ab39a9b9c
parent 0 ceef6d64c21d
permissions -rw-r--r--
Show gamers online everywhere
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package diff
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2006 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* Code from pear.php.net, Text_Diff-1.1.0 package
indvdum@0
    21
* http://pear.php.net/package/Text_Diff/
indvdum@0
    22
*
indvdum@0
    23
* Modified by phpBB Group to meet our coding standards
indvdum@0
    24
* and being able to integrate into phpBB
indvdum@0
    25
*
indvdum@0
    26
* A class to render Diffs in different formats.
indvdum@0
    27
*
indvdum@0
    28
* This class renders the diff in classic diff format. It is intended that
indvdum@0
    29
* this class be customized via inheritance, to obtain fancier outputs.
indvdum@0
    30
*
indvdum@0
    31
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
indvdum@0
    32
*
indvdum@0
    33
* @package diff
indvdum@0
    34
*/
indvdum@0
    35
class diff_renderer
indvdum@0
    36
{
indvdum@0
    37
	/**
indvdum@0
    38
	* Number of leading context "lines" to preserve.
indvdum@0
    39
	*
indvdum@0
    40
	* This should be left at zero for this class, but subclasses may want to
indvdum@0
    41
	* set this to other values.
indvdum@0
    42
	*/
indvdum@0
    43
	var $_leading_context_lines = 0;
indvdum@0
    44
indvdum@0
    45
	/**
indvdum@0
    46
	* Number of trailing context "lines" to preserve.
indvdum@0
    47
	*
indvdum@0
    48
	* This should be left at zero for this class, but subclasses may want to
indvdum@0
    49
	* set this to other values.
indvdum@0
    50
	*/
indvdum@0
    51
	var $_trailing_context_lines = 0;
indvdum@0
    52
indvdum@0
    53
	/**
indvdum@0
    54
	* Constructor.
indvdum@0
    55
	*/
indvdum@0
    56
	function diff_renderer($params = array())
indvdum@0
    57
	{
indvdum@0
    58
		foreach ($params as $param => $value)
indvdum@0
    59
		{
indvdum@0
    60
			$v = '_' . $param;
indvdum@0
    61
			if (isset($this->$v))
indvdum@0
    62
			{
indvdum@0
    63
				$this->$v = $value;
indvdum@0
    64
			}
indvdum@0
    65
		}
indvdum@0
    66
	}
indvdum@0
    67
indvdum@0
    68
	/**
indvdum@0
    69
	* Get any renderer parameters.
indvdum@0
    70
	*
indvdum@0
    71
	* @return array  All parameters of this renderer object.
indvdum@0
    72
	*/
indvdum@0
    73
	function get_params()
indvdum@0
    74
	{
indvdum@0
    75
		$params = array();
indvdum@0
    76
		foreach (get_object_vars($this) as $k => $v)
indvdum@0
    77
		{
indvdum@0
    78
			if ($k[0] == '_')
indvdum@0
    79
			{
indvdum@0
    80
				$params[substr($k, 1)] = $v;
indvdum@0
    81
			}
indvdum@0
    82
		}
indvdum@0
    83
indvdum@0
    84
		return $params;
indvdum@0
    85
	}
indvdum@0
    86
indvdum@0
    87
	/**
indvdum@0
    88
	* Renders a diff.
indvdum@0
    89
	*
indvdum@0
    90
	* @param diff &$diff A diff object.
indvdum@0
    91
	*
indvdum@0
    92
	* @return string  The formatted output.
indvdum@0
    93
	*/
indvdum@0
    94
	function render(&$diff)
indvdum@0
    95
	{
indvdum@0
    96
		$xi = $yi = 1;
indvdum@0
    97
		$block = false;
indvdum@0
    98
		$context = array();
indvdum@0
    99
indvdum@0
   100
		// Create a new diff object if it is a 3-way diff
indvdum@0
   101
		if (is_a($diff, 'diff3'))
indvdum@0
   102
		{
indvdum@0
   103
			$diff3 = &$diff;
indvdum@0
   104
indvdum@0
   105
			$diff_1 = $diff3->get_original();
indvdum@0
   106
			$diff_2 = $diff3->merged_output();
indvdum@0
   107
indvdum@0
   108
			unset($diff3);
indvdum@0
   109
indvdum@0
   110
			$diff = new diff($diff_1, $diff_2);
indvdum@0
   111
		}
indvdum@0
   112
indvdum@0
   113
		$nlead = $this->_leading_context_lines;
indvdum@0
   114
		$ntrail = $this->_trailing_context_lines;
indvdum@0
   115
indvdum@0
   116
		$output = $this->_start_diff();
indvdum@0
   117
		$diffs = $diff->get_diff();
indvdum@0
   118
indvdum@0
   119
		foreach ($diffs as $i => $edit)
indvdum@0
   120
		{
indvdum@0
   121
			// If these are unchanged (copied) lines, and we want to keep leading or trailing context lines, extract them from the copy block.
indvdum@0
   122
			if (is_a($edit, 'diff_op_copy'))
indvdum@0
   123
			{
indvdum@0
   124
				// Do we have any diff blocks yet?
indvdum@0
   125
				if (is_array($block))
indvdum@0
   126
				{
indvdum@0
   127
					// How many lines to keep as context from the copy block.
indvdum@0
   128
					$keep = ($i == sizeof($diffs) - 1) ? $ntrail : $nlead + $ntrail;
indvdum@0
   129
					if (sizeof($edit->orig) <= $keep)
indvdum@0
   130
					{
indvdum@0
   131
						// We have less lines in the block than we want for context => keep the whole block.
indvdum@0
   132
						$block[] = $edit;
indvdum@0
   133
					}
indvdum@0
   134
					else
indvdum@0
   135
					{
indvdum@0
   136
						if ($ntrail)
indvdum@0
   137
						{
indvdum@0
   138
							// Create a new block with as many lines as we need for the trailing context.
indvdum@0
   139
							$context = array_slice($edit->orig, 0, $ntrail);
indvdum@0
   140
							$block[] = new diff_op_copy($context);
indvdum@0
   141
						}
indvdum@0
   142
indvdum@0
   143
						$output .= $this->_block($x0, $ntrail + $xi - $x0, $y0, $ntrail + $yi - $y0, $block);
indvdum@0
   144
						$block = false;
indvdum@0
   145
					}
indvdum@0
   146
				}
indvdum@0
   147
				// Keep the copy block as the context for the next block.
indvdum@0
   148
				$context = $edit->orig;
indvdum@0
   149
			}
indvdum@0
   150
			else
indvdum@0
   151
			{
indvdum@0
   152
				// Don't we have any diff blocks yet?
indvdum@0
   153
				if (!is_array($block))
indvdum@0
   154
				{
indvdum@0
   155
					// Extract context lines from the preceding copy block.
indvdum@0
   156
					$context = array_slice($context, sizeof($context) - $nlead);
indvdum@0
   157
					$x0 = $xi - sizeof($context);
indvdum@0
   158
					$y0 = $yi - sizeof($context);
indvdum@0
   159
					$block = array();
indvdum@0
   160
indvdum@0
   161
					if ($context)
indvdum@0
   162
					{
indvdum@0
   163
						$block[] = new diff_op_copy($context);
indvdum@0
   164
					}
indvdum@0
   165
				}
indvdum@0
   166
				$block[] = $edit;
indvdum@0
   167
			}
indvdum@0
   168
indvdum@0
   169
			$xi += ($edit->orig) ? sizeof($edit->orig) : 0;
indvdum@0
   170
			$yi += ($edit->final) ? sizeof($edit->final) : 0;
indvdum@0
   171
		}
indvdum@0
   172
indvdum@0
   173
		if (is_array($block))
indvdum@0
   174
		{
indvdum@0
   175
			$output .= $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block);
indvdum@0
   176
		}
indvdum@0
   177
indvdum@0
   178
		return $output . $this->_end_diff();
indvdum@0
   179
	}
indvdum@0
   180
indvdum@0
   181
	function _block($xbeg, $xlen, $ybeg, $ylen, &$edits)
indvdum@0
   182
	{
indvdum@0
   183
		$output = $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
indvdum@0
   184
indvdum@0
   185
		foreach ($edits as $edit)
indvdum@0
   186
		{
indvdum@0
   187
			switch (get_class($edit))
indvdum@0
   188
			{
indvdum@0
   189
				case 'diff_op_copy':
indvdum@0
   190
					$output .= $this->_context($edit->orig);
indvdum@0
   191
				break;
indvdum@0
   192
indvdum@0
   193
				case 'diff_op_add':
indvdum@0
   194
					$output .= $this->_added($edit->final);
indvdum@0
   195
				break;
indvdum@0
   196
indvdum@0
   197
				case 'diff_op_delete':
indvdum@0
   198
					$output .= $this->_deleted($edit->orig);
indvdum@0
   199
				break;
indvdum@0
   200
indvdum@0
   201
				case 'diff_op_change':
indvdum@0
   202
					$output .= $this->_changed($edit->orig, $edit->final);
indvdum@0
   203
				break;
indvdum@0
   204
			}
indvdum@0
   205
		}
indvdum@0
   206
indvdum@0
   207
		return $output . $this->_end_block();
indvdum@0
   208
	}
indvdum@0
   209
indvdum@0
   210
	function _start_diff()
indvdum@0
   211
	{
indvdum@0
   212
		return '';
indvdum@0
   213
	}
indvdum@0
   214
indvdum@0
   215
	function _end_diff()
indvdum@0
   216
	{
indvdum@0
   217
		return '';
indvdum@0
   218
	}
indvdum@0
   219
indvdum@0
   220
	function _block_header($xbeg, $xlen, $ybeg, $ylen)
indvdum@0
   221
	{
indvdum@0
   222
		if ($xlen > 1)
indvdum@0
   223
		{
indvdum@0
   224
			$xbeg .= ',' . ($xbeg + $xlen - 1);
indvdum@0
   225
		}
indvdum@0
   226
indvdum@0
   227
		if ($ylen > 1)
indvdum@0
   228
		{
indvdum@0
   229
			$ybeg .= ',' . ($ybeg + $ylen - 1);
indvdum@0
   230
		}
indvdum@0
   231
indvdum@0
   232
		// this matches the GNU Diff behaviour
indvdum@0
   233
		if ($xlen && !$ylen)
indvdum@0
   234
		{
indvdum@0
   235
			$ybeg--;
indvdum@0
   236
		}
indvdum@0
   237
		else if (!$xlen)
indvdum@0
   238
		{
indvdum@0
   239
			$xbeg--;
indvdum@0
   240
		}
indvdum@0
   241
indvdum@0
   242
		return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
indvdum@0
   243
	}
indvdum@0
   244
indvdum@0
   245
	function _start_block($header)
indvdum@0
   246
	{
indvdum@0
   247
		return $header . "\n";
indvdum@0
   248
	}
indvdum@0
   249
indvdum@0
   250
	function _end_block()
indvdum@0
   251
	{
indvdum@0
   252
		return '';
indvdum@0
   253
	}
indvdum@0
   254
indvdum@0
   255
	function _lines($lines, $prefix = ' ')
indvdum@0
   256
	{
indvdum@0
   257
		return $prefix . implode("\n$prefix", $lines) . "\n";
indvdum@0
   258
	}
indvdum@0
   259
indvdum@0
   260
	function _context($lines)
indvdum@0
   261
	{
indvdum@0
   262
		return $this->_lines($lines, '  ');
indvdum@0
   263
	}
indvdum@0
   264
indvdum@0
   265
	function _added($lines)
indvdum@0
   266
	{
indvdum@0
   267
		return $this->_lines($lines, '> ');
indvdum@0
   268
	}
indvdum@0
   269
indvdum@0
   270
	function _deleted($lines)
indvdum@0
   271
	{
indvdum@0
   272
		return $this->_lines($lines, '< ');
indvdum@0
   273
	}
indvdum@0
   274
indvdum@0
   275
	function _changed($orig, $final)
indvdum@0
   276
	{
indvdum@0
   277
		return $this->_deleted($orig) . "---\n" . $this->_added($final);
indvdum@0
   278
	}
indvdum@0
   279
indvdum@0
   280
	/**
indvdum@0
   281
	* Our function to get the diff
indvdum@0
   282
	*/
indvdum@0
   283
	function get_diff_content($diff)
indvdum@0
   284
	{
indvdum@0
   285
		return $this->render($diff);
indvdum@0
   286
	}
indvdum@0
   287
}
indvdum@0
   288
indvdum@0
   289
/**
indvdum@0
   290
* Renders a unified diff
indvdum@0
   291
* @package diff
indvdum@0
   292
*/
indvdum@0
   293
class diff_renderer_unified extends diff_renderer
indvdum@0
   294
{
indvdum@0
   295
	var $_leading_context_lines = 4;
indvdum@0
   296
	var $_trailing_context_lines = 4;
indvdum@0
   297
indvdum@0
   298
	/**
indvdum@0
   299
	* Our function to get the diff
indvdum@0
   300
	*/
indvdum@0
   301
	function get_diff_content($diff)
indvdum@0
   302
	{
indvdum@0
   303
		return nl2br($this->render($diff));
indvdum@0
   304
	}
indvdum@0
   305
indvdum@0
   306
	function _block_header($xbeg, $xlen, $ybeg, $ylen)
indvdum@0
   307
	{
indvdum@0
   308
		if ($xlen != 1)
indvdum@0
   309
		{
indvdum@0
   310
			$xbeg .= ',' . $xlen;
indvdum@0
   311
		}
indvdum@0
   312
indvdum@0
   313
		if ($ylen != 1)
indvdum@0
   314
		{
indvdum@0
   315
			$ybeg .= ',' . $ylen;
indvdum@0
   316
		}
indvdum@0
   317
		return '<div class="diff"><big class="info">@@ -' . $xbeg . ' +' . $ybeg . ' @@</big></div>';
indvdum@0
   318
	}
indvdum@0
   319
indvdum@0
   320
	function _context($lines)
indvdum@0
   321
	{
indvdum@0
   322
		return '<pre class="diff context">' . htmlspecialchars($this->_lines($lines, ' ')) . '<br /></pre>';
indvdum@0
   323
	}
indvdum@0
   324
indvdum@0
   325
	function _added($lines)
indvdum@0
   326
	{
indvdum@0
   327
		return '<pre class="diff added">' . htmlspecialchars($this->_lines($lines, '+')) . '<br /></pre>';
indvdum@0
   328
	}
indvdum@0
   329
indvdum@0
   330
	function _deleted($lines)
indvdum@0
   331
	{
indvdum@0
   332
		return '<pre class="diff removed">' . htmlspecialchars($this->_lines($lines, '-')) . '<br /></pre>';
indvdum@0
   333
	}
indvdum@0
   334
indvdum@0
   335
	function _changed($orig, $final)
indvdum@0
   336
	{
indvdum@0
   337
		return $this->_deleted($orig) . $this->_added($final);
indvdum@0
   338
	}
indvdum@0
   339
indvdum@0
   340
	function _start_diff()
indvdum@0
   341
	{
indvdum@0
   342
		$start = '<div class="file">';
indvdum@0
   343
indvdum@0
   344
		return $start;
indvdum@0
   345
	}
indvdum@0
   346
indvdum@0
   347
	function _end_diff()
indvdum@0
   348
	{
indvdum@0
   349
		return '</div>';
indvdum@0
   350
	}
indvdum@0
   351
indvdum@0
   352
	function _end_block()
indvdum@0
   353
	{
indvdum@0
   354
		return '';
indvdum@0
   355
	}
indvdum@0
   356
}
indvdum@0
   357
indvdum@0
   358
/**
indvdum@0
   359
* "Inline" diff renderer.
indvdum@0
   360
*
indvdum@0
   361
* This class renders diffs in the Wiki-style "inline" format.
indvdum@0
   362
*
indvdum@0
   363
* @author  Ciprian Popovici
indvdum@0
   364
* @package diff
indvdum@0
   365
*/
indvdum@0
   366
class diff_renderer_inline extends diff_renderer
indvdum@0
   367
{
indvdum@0
   368
	var $_leading_context_lines = 10000;
indvdum@0
   369
	var $_trailing_context_lines = 10000;
indvdum@0
   370
indvdum@0
   371
	// Prefix and suffix for inserted text
indvdum@0
   372
	var $_ins_prefix = '<span class="ins">';
indvdum@0
   373
	var $_ins_suffix = '</span>';
indvdum@0
   374
indvdum@0
   375
	// Prefix and suffix for deleted text
indvdum@0
   376
	var $_del_prefix = '<span class="del">';
indvdum@0
   377
	var $_del_suffix = '</span>';
indvdum@0
   378
indvdum@0
   379
	var $_block_head = '';
indvdum@0
   380
indvdum@0
   381
	// What are we currently splitting on? Used to recurse to show word-level
indvdum@0
   382
	var $_split_level = 'lines';
indvdum@0
   383
indvdum@0
   384
	/**
indvdum@0
   385
	* Our function to get the diff
indvdum@0
   386
	*/
indvdum@0
   387
	function get_diff_content($diff)
indvdum@0
   388
	{
indvdum@0
   389
		return '<pre>' . nl2br($this->render($diff)) . '<br /></pre>';
indvdum@0
   390
	}
indvdum@0
   391
indvdum@0
   392
	function _start_diff()
indvdum@0
   393
	{
indvdum@0
   394
		return '';
indvdum@0
   395
	}
indvdum@0
   396
indvdum@0
   397
	function _end_diff()
indvdum@0
   398
	{
indvdum@0
   399
		return '';
indvdum@0
   400
	}
indvdum@0
   401
indvdum@0
   402
	function _block_header($xbeg, $xlen, $ybeg, $ylen)
indvdum@0
   403
	{
indvdum@0
   404
		return $this->_block_head;
indvdum@0
   405
	}
indvdum@0
   406
indvdum@0
   407
	function _start_block($header)
indvdum@0
   408
	{
indvdum@0
   409
		return $header;
indvdum@0
   410
	}
indvdum@0
   411
indvdum@0
   412
	function _lines($lines, $prefix = ' ', $encode = true)
indvdum@0
   413
	{
indvdum@0
   414
		if ($encode)
indvdum@0
   415
		{
indvdum@0
   416
			array_walk($lines, array(&$this, '_encode'));
indvdum@0
   417
		}
indvdum@0
   418
indvdum@0
   419
		if ($this->_split_level == 'words')
indvdum@0
   420
		{
indvdum@0
   421
			return implode('', $lines);
indvdum@0
   422
		}
indvdum@0
   423
		else
indvdum@0
   424
		{
indvdum@0
   425
			return implode("\n", $lines) . "\n";
indvdum@0
   426
		}
indvdum@0
   427
	}
indvdum@0
   428
indvdum@0
   429
	function _added($lines)
indvdum@0
   430
	{
indvdum@0
   431
		array_walk($lines, array(&$this, '_encode'));
indvdum@0
   432
		$lines[0] = $this->_ins_prefix . $lines[0];
indvdum@0
   433
		$lines[sizeof($lines) - 1] .= $this->_ins_suffix;
indvdum@0
   434
		return $this->_lines($lines, ' ', false);
indvdum@0
   435
	}
indvdum@0
   436
indvdum@0
   437
	function _deleted($lines, $words = false)
indvdum@0
   438
	{
indvdum@0
   439
		array_walk($lines, array(&$this, '_encode'));
indvdum@0
   440
		$lines[0] = $this->_del_prefix . $lines[0];
indvdum@0
   441
		$lines[sizeof($lines) - 1] .= $this->_del_suffix;
indvdum@0
   442
		return $this->_lines($lines, ' ', false);
indvdum@0
   443
	}
indvdum@0
   444
indvdum@0
   445
	function _changed($orig, $final)
indvdum@0
   446
	{
indvdum@0
   447
		// If we've already split on words, don't try to do so again - just display.
indvdum@0
   448
		if ($this->_split_level == 'words')
indvdum@0
   449
		{
indvdum@0
   450
			$prefix = '';
indvdum@0
   451
			while ($orig[0] !== false && $final[0] !== false && substr($orig[0], 0, 1) == ' ' && substr($final[0], 0, 1) == ' ')
indvdum@0
   452
			{
indvdum@0
   453
				$prefix .= substr($orig[0], 0, 1);
indvdum@0
   454
				$orig[0] = substr($orig[0], 1);
indvdum@0
   455
				$final[0] = substr($final[0], 1);
indvdum@0
   456
			}
indvdum@0
   457
indvdum@0
   458
			return $prefix . $this->_deleted($orig) . $this->_added($final);
indvdum@0
   459
		}
indvdum@0
   460
indvdum@0
   461
		$text1 = implode("\n", $orig);
indvdum@0
   462
		$text2 = implode("\n", $final);
indvdum@0
   463
indvdum@0
   464
		// Non-printing newline marker.
indvdum@0
   465
		$nl = "\0";
indvdum@0
   466
indvdum@0
   467
		// We want to split on word boundaries, but we need to preserve whitespace as well.
indvdum@0
   468
		// Therefore we split on words, but include all blocks of whitespace in the wordlist.
indvdum@0
   469
		$splitted_text_1 = $this->_split_on_words($text1, $nl);
indvdum@0
   470
		$splitted_text_2 = $this->_split_on_words($text2, $nl);
indvdum@0
   471
indvdum@0
   472
		$diff = new diff($splitted_text_1, $splitted_text_2);
indvdum@0
   473
		unset($splitted_text_1, $splitted_text_2);
indvdum@0
   474
indvdum@0
   475
		// Get the diff in inline format.
indvdum@0
   476
		$renderer = new diff_renderer_inline(array_merge($this->get_params(), array('split_level' => 'words')));
indvdum@0
   477
indvdum@0
   478
		// Run the diff and get the output.
indvdum@0
   479
		return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
indvdum@0
   480
	}
indvdum@0
   481
indvdum@0
   482
	function _split_on_words($string, $newline_escape = "\n")
indvdum@0
   483
	{
indvdum@0
   484
		// Ignore \0; otherwise the while loop will never finish.
indvdum@0
   485
		$string = str_replace("\0", '', $string);
indvdum@0
   486
indvdum@0
   487
		$words = array();
indvdum@0
   488
		$length = strlen($string);
indvdum@0
   489
		$pos = 0;
indvdum@0
   490
indvdum@0
   491
		$tab_there = true;
indvdum@0
   492
		while ($pos < $length)
indvdum@0
   493
		{
indvdum@0
   494
			// Check for tabs... do not include them
indvdum@0
   495
			if ($tab_there && substr($string, $pos, 1) === "\t")
indvdum@0
   496
			{
indvdum@0
   497
				$words[] = "\t";
indvdum@0
   498
				$pos++;
indvdum@0
   499
indvdum@0
   500
				continue;
indvdum@0
   501
			}
indvdum@0
   502
			else
indvdum@0
   503
			{
indvdum@0
   504
				$tab_there = false;
indvdum@0
   505
			}
indvdum@0
   506
indvdum@0
   507
			// Eat a word with any preceding whitespace.
indvdum@0
   508
			$spaces = strspn(substr($string, $pos), " \n");
indvdum@0
   509
			$nextpos = strcspn(substr($string, $pos + $spaces), " \n");
indvdum@0
   510
			$words[] = str_replace("\n", $newline_escape, substr($string, $pos, $spaces + $nextpos));
indvdum@0
   511
			$pos += $spaces + $nextpos;
indvdum@0
   512
		}
indvdum@0
   513
indvdum@0
   514
		return $words;
indvdum@0
   515
	}
indvdum@0
   516
indvdum@0
   517
	function _encode(&$string)
indvdum@0
   518
	{
indvdum@0
   519
		$string = htmlspecialchars($string);
indvdum@0
   520
	}
indvdum@0
   521
}
indvdum@0
   522
indvdum@0
   523
/**
indvdum@0
   524
* "raw" diff renderer.
indvdum@0
   525
* This class could be used to output a raw unified patch file
indvdum@0
   526
*
indvdum@0
   527
* @package diff
indvdum@0
   528
*/
indvdum@0
   529
class diff_renderer_raw extends diff_renderer
indvdum@0
   530
{
indvdum@0
   531
	var $_leading_context_lines = 4;
indvdum@0
   532
	var $_trailing_context_lines = 4;
indvdum@0
   533
indvdum@0
   534
	/**
indvdum@0
   535
	* Our function to get the diff
indvdum@0
   536
	*/
indvdum@0
   537
	function get_diff_content($diff)
indvdum@0
   538
	{
indvdum@0
   539
		return '<textarea style="height: 290px;" rows="15" cols="76" class="full">' . htmlspecialchars($this->render($diff)) . '</textarea>';
indvdum@0
   540
	}
indvdum@0
   541
indvdum@0
   542
	function _block_header($xbeg, $xlen, $ybeg, $ylen)
indvdum@0
   543
	{
indvdum@0
   544
		if ($xlen != 1)
indvdum@0
   545
		{
indvdum@0
   546
			$xbeg .= ',' . $xlen;
indvdum@0
   547
		}
indvdum@0
   548
indvdum@0
   549
		if ($ylen != 1)
indvdum@0
   550
		{
indvdum@0
   551
			$ybeg .= ',' . $ylen;
indvdum@0
   552
		}
indvdum@0
   553
		return '@@ -' . $xbeg . ' +' . $ybeg . ' @@';
indvdum@0
   554
	}
indvdum@0
   555
indvdum@0
   556
	function _context($lines)
indvdum@0
   557
	{
indvdum@0
   558
		return $this->_lines($lines, ' ');
indvdum@0
   559
	}
indvdum@0
   560
indvdum@0
   561
	function _added($lines)
indvdum@0
   562
	{
indvdum@0
   563
		return $this->_lines($lines, '+');
indvdum@0
   564
	}
indvdum@0
   565
indvdum@0
   566
	function _deleted($lines)
indvdum@0
   567
	{
indvdum@0
   568
		return $this->_lines($lines, '-');
indvdum@0
   569
	}
indvdum@0
   570
indvdum@0
   571
	function _changed($orig, $final)
indvdum@0
   572
	{
indvdum@0
   573
		return $this->_deleted($orig) . $this->_added($final);
indvdum@0
   574
	}
indvdum@0
   575
}
indvdum@0
   576
indvdum@0
   577
/**
indvdum@0
   578
* "chora (Horde)" diff renderer - similar style.
indvdum@0
   579
* This renderer class is a modified human_readable function from the Horde Framework.
indvdum@0
   580
*
indvdum@0
   581
* @package diff
indvdum@0
   582
*/
indvdum@0
   583
class diff_renderer_side_by_side extends diff_renderer
indvdum@0
   584
{
indvdum@0
   585
	var $_leading_context_lines = 3;
indvdum@0
   586
	var $_trailing_context_lines = 3;
indvdum@0
   587
indvdum@0
   588
	var $lines = array();
indvdum@0
   589
indvdum@0
   590
	// Hold the left and right columns of lines for change blocks.
indvdum@0
   591
	var $cols;
indvdum@0
   592
	var $state;
indvdum@0
   593
indvdum@0
   594
	var $data = false;
indvdum@0
   595
indvdum@0
   596
	/**
indvdum@0
   597
	* Our function to get the diff
indvdum@0
   598
	*/
indvdum@0
   599
	function get_diff_content($diff)
indvdum@0
   600
	{
indvdum@0
   601
		global $user;
indvdum@0
   602
indvdum@0
   603
		$output = '';
indvdum@0
   604
		$output .= '<table cellspacing="0" class="hrdiff">
indvdum@0
   605
<caption>
indvdum@0
   606
	<span class="unmodified">&nbsp;</span> ' . $user->lang['LINE_UNMODIFIED'] . '
indvdum@0
   607
	<span class="added">&nbsp;</span> ' . $user->lang['LINE_ADDED'] . '
indvdum@0
   608
	<span class="modified">&nbsp;</span> ' . $user->lang['LINE_MODIFIED'] . '
indvdum@0
   609
	<span class="removed">&nbsp;</span> ' . $user->lang['LINE_REMOVED'] . '
indvdum@0
   610
</caption>
indvdum@0
   611
<tbody>
indvdum@0
   612
';
indvdum@0
   613
indvdum@0
   614
		$this->render($diff);
indvdum@0
   615
indvdum@0
   616
		// Is the diff empty?
indvdum@0
   617
		if (!sizeof($this->lines))
indvdum@0
   618
		{
indvdum@0
   619
			$output .= '<tr><th colspan="2">' . $user->lang['NO_VISIBLE_CHANGES'] . '</th></tr>';
indvdum@0
   620
		}
indvdum@0
   621
		else
indvdum@0
   622
		{
indvdum@0
   623
			// Iterate through every header block of changes
indvdum@0
   624
			foreach ($this->lines as $header)
indvdum@0
   625
			{
indvdum@0
   626
				$output .= '<tr><th>' . $user->lang['LINE'] . ' ' . $header['oldline'] . '</th><th>' . $user->lang['LINE'] . ' ' . $header['newline'] . '</th></tr>';
indvdum@0
   627
indvdum@0
   628
				// Each header block consists of a number of changes (add, remove, change).
indvdum@0
   629
				$current_context = '';
indvdum@0
   630
indvdum@0
   631
				foreach ($header['contents'] as $change)
indvdum@0
   632
				{
indvdum@0
   633
					if (!empty($current_context) && $change['type'] != 'empty')
indvdum@0
   634
					{
indvdum@0
   635
						$line = $current_context;
indvdum@0
   636
						$current_context = '';
indvdum@0
   637
indvdum@0
   638
						$output .= '<tr class="unmodified"><td><pre>' . ((strlen($line)) ? $line : '&nbsp;') . '<br /></pre></td>
indvdum@0
   639
							<td><pre>' . ((strlen($line)) ? $line : '&nbsp;') . '<br /></pre></td></tr>';
indvdum@0
   640
					}
indvdum@0
   641
indvdum@0
   642
					switch ($change['type'])
indvdum@0
   643
					{
indvdum@0
   644
						case 'add':
indvdum@0
   645
							$line = '';
indvdum@0
   646
indvdum@0
   647
							foreach ($change['lines'] as $_line)
indvdum@0
   648
							{
indvdum@0
   649
								$line .= htmlspecialchars($_line) . '<br />';
indvdum@0
   650
							}
indvdum@0
   651
indvdum@0
   652
							$output .= '<tr><td class="added_empty">&nbsp;</td><td class="added"><pre>' . ((strlen($line)) ? $line : '&nbsp;') . '<br /></pre></td></tr>';
indvdum@0
   653
						break;
indvdum@0
   654
indvdum@0
   655
						case 'remove':
indvdum@0
   656
							$line = '';
indvdum@0
   657
indvdum@0
   658
							foreach ($change['lines'] as $_line)
indvdum@0
   659
							{
indvdum@0
   660
								$line .= htmlspecialchars($_line) . '<br />';
indvdum@0
   661
							}
indvdum@0
   662
indvdum@0
   663
							$output .= '<tr><td class="removed"><pre>' . ((strlen($line)) ? $line : '&nbsp;') . '<br /></pre></td><td class="removed_empty">&nbsp;</td></tr>';
indvdum@0
   664
						break;
indvdum@0
   665
indvdum@0
   666
						case 'empty':
indvdum@0
   667
							$current_context .= htmlspecialchars($change['line']) . '<br />';
indvdum@0
   668
						break;
indvdum@0
   669
indvdum@0
   670
						case 'change':
indvdum@0
   671
							// Pop the old/new stacks one by one, until both are empty.
indvdum@0
   672
							$oldsize = sizeof($change['old']);
indvdum@0
   673
							$newsize = sizeof($change['new']);
indvdum@0
   674
							$left = $right = '';
indvdum@0
   675
indvdum@0
   676
							for ($row = 0, $row_max = max($oldsize, $newsize); $row < $row_max; ++$row)
indvdum@0
   677
							{
indvdum@0
   678
								$left .= isset($change['old'][$row]) ? htmlspecialchars($change['old'][$row]) : '';
indvdum@0
   679
								$left .= '<br />';
indvdum@0
   680
								$right .= isset($change['new'][$row]) ? htmlspecialchars($change['new'][$row]) : '';
indvdum@0
   681
								$right .= '<br />';
indvdum@0
   682
							}
indvdum@0
   683
indvdum@0
   684
							$output .= '<tr>';
indvdum@0
   685
indvdum@0
   686
							if (!empty($left))
indvdum@0
   687
							{
indvdum@0
   688
								$output .= '<td class="modified"><pre>' . $left . '<br /></pre></td>';
indvdum@0
   689
							}
indvdum@0
   690
							else if ($row < $oldsize)
indvdum@0
   691
							{
indvdum@0
   692
								$output .= '<td class="modified">&nbsp;</td>';
indvdum@0
   693
							}
indvdum@0
   694
							else
indvdum@0
   695
							{
indvdum@0
   696
								$output .= '<td class="unmodified">&nbsp;</td>';
indvdum@0
   697
							}
indvdum@0
   698
indvdum@0
   699
							if (!empty($right))
indvdum@0
   700
							{
indvdum@0
   701
								$output .= '<td class="modified"><pre>' . $right . '<br /></pre></td>';
indvdum@0
   702
							}
indvdum@0
   703
							else if ($row < $newsize)
indvdum@0
   704
							{
indvdum@0
   705
								$output .= '<td class="modified">&nbsp;</td>';
indvdum@0
   706
							}
indvdum@0
   707
							else
indvdum@0
   708
							{
indvdum@0
   709
								$output .= '<td class="unmodified">&nbsp;</td>';
indvdum@0
   710
							}
indvdum@0
   711
indvdum@0
   712
							$output .= '</tr>';
indvdum@0
   713
						break;
indvdum@0
   714
					}
indvdum@0
   715
				}
indvdum@0
   716
indvdum@0
   717
				if (!empty($current_context))
indvdum@0
   718
				{
indvdum@0
   719
					$line = $current_context;
indvdum@0
   720
					$current_context = '';
indvdum@0
   721
indvdum@0
   722
					$output .= '<tr class="unmodified"><td><pre>' . ((strlen($line)) ? $line : '&nbsp;') . '<br /></pre></td>';
indvdum@0
   723
					$output .= '<td><pre>' . ((strlen($line)) ? $line : '&nbsp;') . '<br /></pre></td></tr>';
indvdum@0
   724
				}
indvdum@0
   725
			}
indvdum@0
   726
		}
indvdum@0
   727
indvdum@0
   728
		$output .= '</tbody></table>';
indvdum@0
   729
indvdum@0
   730
		return $output;
indvdum@0
   731
	}
indvdum@0
   732
indvdum@0
   733
	function _start_diff()
indvdum@0
   734
	{
indvdum@0
   735
		$this->lines = array();
indvdum@0
   736
indvdum@0
   737
		$this->data = false;
indvdum@0
   738
		$this->cols = array(array(), array());
indvdum@0
   739
		$this->state = 'empty';
indvdum@0
   740
indvdum@0
   741
		return '';
indvdum@0
   742
	}
indvdum@0
   743
indvdum@0
   744
	function _end_diff()
indvdum@0
   745
	{
indvdum@0
   746
		// Just flush any remaining entries in the columns stack.
indvdum@0
   747
		switch ($this->state)
indvdum@0
   748
		{
indvdum@0
   749
			case 'add':
indvdum@0
   750
				$this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]);
indvdum@0
   751
			break;
indvdum@0
   752
indvdum@0
   753
			case 'remove':
indvdum@0
   754
				// We have some removal lines pending in our stack, so flush them.
indvdum@0
   755
				$this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]);
indvdum@0
   756
			break;
indvdum@0
   757
indvdum@0
   758
			case 'change':
indvdum@0
   759
				// We have both remove and addition lines, so this is a change block.
indvdum@0
   760
				$this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]);
indvdum@0
   761
			break;
indvdum@0
   762
		}
indvdum@0
   763
indvdum@0
   764
		if ($this->data !== false)
indvdum@0
   765
		{
indvdum@0
   766
			$this->lines[] = $this->data;
indvdum@0
   767
		}
indvdum@0
   768
indvdum@0
   769
		return '';
indvdum@0
   770
	}
indvdum@0
   771
indvdum@0
   772
	function _block_header($xbeg, $xlen, $ybeg, $ylen)
indvdum@0
   773
	{
indvdum@0
   774
		// Push any previous header information to the return stack.
indvdum@0
   775
		if ($this->data !== false)
indvdum@0
   776
		{
indvdum@0
   777
			$this->lines[] = $this->data;
indvdum@0
   778
		}
indvdum@0
   779
indvdum@0
   780
		$this->data = array('type' => 'header', 'oldline' => $xbeg, 'newline' => $ybeg, 'contents' => array());
indvdum@0
   781
		$this->state = 'dump';
indvdum@0
   782
	}
indvdum@0
   783
indvdum@0
   784
	function _added($lines)
indvdum@0
   785
	{
indvdum@0
   786
		array_walk($lines, array(&$this, '_perform_add'));
indvdum@0
   787
	}
indvdum@0
   788
indvdum@0
   789
	function _perform_add($line)
indvdum@0
   790
	{
indvdum@0
   791
		if ($this->state == 'empty')
indvdum@0
   792
		{
indvdum@0
   793
			return '';
indvdum@0
   794
		}
indvdum@0
   795
indvdum@0
   796
		// This is just an addition line.
indvdum@0
   797
		if ($this->state == 'dump' || $this->state == 'add')
indvdum@0
   798
		{
indvdum@0
   799
			// Start adding to the addition stack.
indvdum@0
   800
			$this->cols[0][] = $line;
indvdum@0
   801
			$this->state = 'add';
indvdum@0
   802
		}
indvdum@0
   803
		else
indvdum@0
   804
		{
indvdum@0
   805
			// This is inside a change block, so start accumulating lines.
indvdum@0
   806
			$this->state = 'change';
indvdum@0
   807
			$this->cols[1][] = $line;
indvdum@0
   808
		}
indvdum@0
   809
	}
indvdum@0
   810
indvdum@0
   811
	function _deleted($lines)
indvdum@0
   812
	{
indvdum@0
   813
		array_walk($lines, array(&$this, '_perform_delete'));
indvdum@0
   814
	}
indvdum@0
   815
indvdum@0
   816
	function _perform_delete($line)
indvdum@0
   817
	{
indvdum@0
   818
		// This is a removal line.
indvdum@0
   819
		$this->state = 'remove';
indvdum@0
   820
		$this->cols[0][] = $line;
indvdum@0
   821
	}
indvdum@0
   822
indvdum@0
   823
	function _context($lines)
indvdum@0
   824
	{
indvdum@0
   825
		array_walk($lines, array(&$this, '_perform_context'));
indvdum@0
   826
	}
indvdum@0
   827
indvdum@0
   828
	function _perform_context($line)
indvdum@0
   829
	{
indvdum@0
   830
		// An empty block with no action.
indvdum@0
   831
		switch ($this->state)
indvdum@0
   832
		{
indvdum@0
   833
			case 'add':
indvdum@0
   834
				$this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]);
indvdum@0
   835
			break;
indvdum@0
   836
indvdum@0
   837
			case 'remove':
indvdum@0
   838
				// We have some removal lines pending in our stack, so flush them.
indvdum@0
   839
				$this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]);
indvdum@0
   840
			break;
indvdum@0
   841
indvdum@0
   842
			case 'change':
indvdum@0
   843
				// We have both remove and addition lines, so this is a change block.
indvdum@0
   844
				$this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]);
indvdum@0
   845
			break;
indvdum@0
   846
		}
indvdum@0
   847
indvdum@0
   848
		$this->cols = array(array(), array());
indvdum@0
   849
		$this->data['contents'][] = array('type' => 'empty', 'line' => $line);
indvdum@0
   850
		$this->state = 'dump';
indvdum@0
   851
	}
indvdum@0
   852
indvdum@0
   853
	function _changed($orig, $final)
indvdum@0
   854
	{
indvdum@0
   855
		return $this->_deleted($orig) . $this->_added($final);
indvdum@0
   856
	}
indvdum@0
   857
indvdum@0
   858
}
indvdum@0
   859
indvdum@0
   860
?>