3 * Base for diff rendering classes. Portions taken from phpwiki-1.3.3.
5 * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
6 * You may copy this code freely under the conditions of the GPL.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup DifferenceEngine
28 * Base class for diff formatters
30 * This class formats the diff in classic diff format.
31 * It is intended that this class be customized via inheritance,
32 * to obtain fancier outputs.
34 * @ingroup DifferenceEngine
36 abstract class DiffFormatter
{
38 * Number of leading context "lines" to preserve.
40 * This should be left at zero for this class, but subclasses
41 * may want to set this to other values.
43 protected $leadingContextLines = 0;
46 * Number of trailing context "lines" to preserve.
48 * This should be left at zero for this class, but subclasses
49 * may want to set this to other values.
51 protected $trailingContextLines = 0;
56 * @param $diff Diff A Diff object.
57 * @return string The formatted output.
59 public function format( $diff ) {
60 wfProfileIn( __METHOD__
);
66 $nlead = $this->leadingContextLines
;
67 $ntrail = $this->trailingContextLines
;
71 foreach ( $diff->edits
as $edit ) {
72 if ( $edit->type
== 'copy' ) {
73 if ( is_array( $block ) ) {
74 if ( count( $edit->orig
) <= $nlead +
$ntrail ) {
78 $context = array_slice( $edit->orig
, 0, $ntrail );
79 $block[] = new DiffOp_Copy( $context );
81 $this->block( $x0, $ntrail +
$xi - $x0,
82 $y0, $ntrail +
$yi - $y0,
87 $context = $edit->orig
;
89 if ( !is_array( $block ) ) {
90 $context = array_slice( $context, count( $context ) - $nlead );
91 $x0 = $xi - count( $context );
92 $y0 = $yi - count( $context );
95 $block[] = new DiffOp_Copy( $context );
102 $xi +
= count( $edit->orig
);
104 if ( $edit->closing
) {
105 $yi +
= count( $edit->closing
);
109 if ( is_array( $block ) ) {
110 $this->block( $x0, $xi - $x0,
115 $end = $this->endDiff();
116 wfProfileOut( __METHOD__
);
127 protected function block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
128 wfProfileIn( __METHOD__
);
129 $this->startBlock( $this->blockHeader( $xbeg, $xlen, $ybeg, $ylen ) );
130 foreach ( $edits as $edit ) {
131 if ( $edit->type
== 'copy' ) {
132 $this->context( $edit->orig
);
133 } elseif ( $edit->type
== 'add' ) {
134 $this->added( $edit->closing
);
135 } elseif ( $edit->type
== 'delete' ) {
136 $this->deleted( $edit->orig
);
137 } elseif ( $edit->type
== 'change' ) {
138 $this->changed( $edit->orig
, $edit->closing
);
140 throw new MWException( "Unknown edit type: {$edit->type}" );
144 wfProfileOut( __METHOD__
);
147 protected function startDiff() {
154 protected function endDiff() {
155 $val = ob_get_contents();
167 protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
169 $xbeg .= ',' . ( $xbeg +
$xlen - 1 );
172 $ybeg .= ',' . ( $ybeg +
$ylen - 1 );
175 return $xbeg . ( $xlen ?
( $ylen ?
'c' : 'd' ) : 'a' ) . $ybeg;
178 protected function startBlock( $header ) {
182 protected function endBlock() {
187 * @param $prefix string
189 protected function lines( $lines, $prefix = ' ' ) {
190 foreach ( $lines as $line ) {
191 echo "$prefix $line\n";
198 protected function context( $lines ) {
199 $this->lines( $lines );
205 protected function added( $lines ) {
206 $this->lines( $lines, '>' );
212 protected function deleted( $lines ) {
213 $this->lines( $lines, '<' );
220 protected function changed( $orig, $closing ) {
221 $this->deleted( $orig );
223 $this->added( $closing );