Edit File by line
/home/barbar84/public_h.../wp-inclu.../Text/Diff
File: Renderer.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* A class to render Diffs in different formats.
[2] Fix | Delete
*
[3] Fix | Delete
* This class renders the diff in classic diff format. It is intended that
[4] Fix | Delete
* this class be customized via inheritance, to obtain fancier outputs.
[5] Fix | Delete
*
[6] Fix | Delete
* Copyright 2004-2010 The Horde Project (http://www.horde.org/)
[7] Fix | Delete
*
[8] Fix | Delete
* See the enclosed file COPYING for license information (LGPL). If you did
[9] Fix | Delete
* not receive this file, see http://opensource.org/licenses/lgpl-license.php.
[10] Fix | Delete
*
[11] Fix | Delete
* @package Text_Diff
[12] Fix | Delete
*/
[13] Fix | Delete
class Text_Diff_Renderer {
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Number of leading context "lines" to preserve.
[17] Fix | Delete
*
[18] Fix | Delete
* This should be left at zero for this class, but subclasses may want to
[19] Fix | Delete
* set this to other values.
[20] Fix | Delete
*/
[21] Fix | Delete
var $_leading_context_lines = 0;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Number of trailing context "lines" to preserve.
[25] Fix | Delete
*
[26] Fix | Delete
* This should be left at zero for this class, but subclasses may want to
[27] Fix | Delete
* set this to other values.
[28] Fix | Delete
*/
[29] Fix | Delete
var $_trailing_context_lines = 0;
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Constructor.
[33] Fix | Delete
*/
[34] Fix | Delete
function __construct( $params = array() )
[35] Fix | Delete
{
[36] Fix | Delete
foreach ($params as $param => $value) {
[37] Fix | Delete
$v = '_' . $param;
[38] Fix | Delete
if (isset($this->$v)) {
[39] Fix | Delete
$this->$v = $value;
[40] Fix | Delete
}
[41] Fix | Delete
}
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* PHP4 constructor.
[46] Fix | Delete
*/
[47] Fix | Delete
public function Text_Diff_Renderer( $params = array() ) {
[48] Fix | Delete
self::__construct( $params );
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* Get any renderer parameters.
[53] Fix | Delete
*
[54] Fix | Delete
* @return array All parameters of this renderer object.
[55] Fix | Delete
*/
[56] Fix | Delete
function getParams()
[57] Fix | Delete
{
[58] Fix | Delete
$params = array();
[59] Fix | Delete
foreach (get_object_vars($this) as $k => $v) {
[60] Fix | Delete
if ($k[0] == '_') {
[61] Fix | Delete
$params[substr($k, 1)] = $v;
[62] Fix | Delete
}
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
return $params;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Renders a diff.
[70] Fix | Delete
*
[71] Fix | Delete
* @param Text_Diff $diff A Text_Diff object.
[72] Fix | Delete
*
[73] Fix | Delete
* @return string The formatted output.
[74] Fix | Delete
*/
[75] Fix | Delete
function render($diff)
[76] Fix | Delete
{
[77] Fix | Delete
$xi = $yi = 1;
[78] Fix | Delete
$block = false;
[79] Fix | Delete
$context = array();
[80] Fix | Delete
[81] Fix | Delete
$nlead = $this->_leading_context_lines;
[82] Fix | Delete
$ntrail = $this->_trailing_context_lines;
[83] Fix | Delete
[84] Fix | Delete
$output = $this->_startDiff();
[85] Fix | Delete
[86] Fix | Delete
$diffs = $diff->getDiff();
[87] Fix | Delete
foreach ($diffs as $i => $edit) {
[88] Fix | Delete
/* If these are unchanged (copied) lines, and we want to keep
[89] Fix | Delete
* leading or trailing context lines, extract them from the copy
[90] Fix | Delete
* block. */
[91] Fix | Delete
if (is_a($edit, 'Text_Diff_Op_copy')) {
[92] Fix | Delete
/* Do we have any diff blocks yet? */
[93] Fix | Delete
if (is_array($block)) {
[94] Fix | Delete
/* How many lines to keep as context from the copy
[95] Fix | Delete
* block. */
[96] Fix | Delete
$keep = $i == count($diffs) - 1 ? $ntrail : $nlead + $ntrail;
[97] Fix | Delete
if (count($edit->orig) <= $keep) {
[98] Fix | Delete
/* We have less lines in the block than we want for
[99] Fix | Delete
* context => keep the whole block. */
[100] Fix | Delete
$block[] = $edit;
[101] Fix | Delete
} else {
[102] Fix | Delete
if ($ntrail) {
[103] Fix | Delete
/* Create a new block with as many lines as we need
[104] Fix | Delete
* for the trailing context. */
[105] Fix | Delete
$context = array_slice($edit->orig, 0, $ntrail);
[106] Fix | Delete
$block[] = new Text_Diff_Op_copy($context);
[107] Fix | Delete
}
[108] Fix | Delete
/* @todo */
[109] Fix | Delete
$output .= $this->_block($x0, $ntrail + $xi - $x0,
[110] Fix | Delete
$y0, $ntrail + $yi - $y0,
[111] Fix | Delete
$block);
[112] Fix | Delete
$block = false;
[113] Fix | Delete
}
[114] Fix | Delete
}
[115] Fix | Delete
/* Keep the copy block as the context for the next block. */
[116] Fix | Delete
$context = $edit->orig;
[117] Fix | Delete
} else {
[118] Fix | Delete
/* Don't we have any diff blocks yet? */
[119] Fix | Delete
if (!is_array($block)) {
[120] Fix | Delete
/* Extract context lines from the preceding copy block. */
[121] Fix | Delete
$context = array_slice($context, count($context) - $nlead);
[122] Fix | Delete
$x0 = $xi - count($context);
[123] Fix | Delete
$y0 = $yi - count($context);
[124] Fix | Delete
$block = array();
[125] Fix | Delete
if ($context) {
[126] Fix | Delete
$block[] = new Text_Diff_Op_copy($context);
[127] Fix | Delete
}
[128] Fix | Delete
}
[129] Fix | Delete
$block[] = $edit;
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
if ($edit->orig) {
[133] Fix | Delete
$xi += count($edit->orig);
[134] Fix | Delete
}
[135] Fix | Delete
if ($edit->final) {
[136] Fix | Delete
$yi += count($edit->final);
[137] Fix | Delete
}
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
if (is_array($block)) {
[141] Fix | Delete
$output .= $this->_block($x0, $xi - $x0,
[142] Fix | Delete
$y0, $yi - $y0,
[143] Fix | Delete
$block);
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
return $output . $this->_endDiff();
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
function _block($xbeg, $xlen, $ybeg, $ylen, &$edits)
[150] Fix | Delete
{
[151] Fix | Delete
$output = $this->_startBlock($this->_blockHeader($xbeg, $xlen, $ybeg, $ylen));
[152] Fix | Delete
[153] Fix | Delete
foreach ($edits as $edit) {
[154] Fix | Delete
switch (strtolower(get_class($edit))) {
[155] Fix | Delete
case 'text_diff_op_copy':
[156] Fix | Delete
$output .= $this->_context($edit->orig);
[157] Fix | Delete
break;
[158] Fix | Delete
[159] Fix | Delete
case 'text_diff_op_add':
[160] Fix | Delete
$output .= $this->_added($edit->final);
[161] Fix | Delete
break;
[162] Fix | Delete
[163] Fix | Delete
case 'text_diff_op_delete':
[164] Fix | Delete
$output .= $this->_deleted($edit->orig);
[165] Fix | Delete
break;
[166] Fix | Delete
[167] Fix | Delete
case 'text_diff_op_change':
[168] Fix | Delete
$output .= $this->_changed($edit->orig, $edit->final);
[169] Fix | Delete
break;
[170] Fix | Delete
}
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
return $output . $this->_endBlock();
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
function _startDiff()
[177] Fix | Delete
{
[178] Fix | Delete
return '';
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
function _endDiff()
[182] Fix | Delete
{
[183] Fix | Delete
return '';
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
[187] Fix | Delete
{
[188] Fix | Delete
if ($xlen > 1) {
[189] Fix | Delete
$xbeg .= ',' . ($xbeg + $xlen - 1);
[190] Fix | Delete
}
[191] Fix | Delete
if ($ylen > 1) {
[192] Fix | Delete
$ybeg .= ',' . ($ybeg + $ylen - 1);
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
// this matches the GNU Diff behaviour
[196] Fix | Delete
if ($xlen && !$ylen) {
[197] Fix | Delete
$ybeg--;
[198] Fix | Delete
} elseif (!$xlen) {
[199] Fix | Delete
$xbeg--;
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
function _startBlock($header)
[206] Fix | Delete
{
[207] Fix | Delete
return $header . "\n";
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
function _endBlock()
[211] Fix | Delete
{
[212] Fix | Delete
return '';
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
function _lines($lines, $prefix = ' ')
[216] Fix | Delete
{
[217] Fix | Delete
return $prefix . implode("\n$prefix", $lines) . "\n";
[218] Fix | Delete
}
[219] Fix | Delete
[220] Fix | Delete
function _context($lines)
[221] Fix | Delete
{
[222] Fix | Delete
return $this->_lines($lines, ' ');
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
function _added($lines)
[226] Fix | Delete
{
[227] Fix | Delete
return $this->_lines($lines, '> ');
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
function _deleted($lines)
[231] Fix | Delete
{
[232] Fix | Delete
return $this->_lines($lines, '< ');
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
function _changed($orig, $final)
[236] Fix | Delete
{
[237] Fix | Delete
return $this->_deleted($orig) . "---\n" . $this->_added($final);
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function