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 /** @var int 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;
45 /** @var int Number of trailing context "lines" to preserve.
47 * This should be left at zero for this class, but subclasses
48 * may want to set this to other values.
50 protected $trailingContextLines = 0;
57 * @return string The formatted output.
59 public function format( $diff ) {
60 wfProfileIn( __METHOD__
);
66 $nlead = $this->leadingContextLines
;
67 $ntrail = $this->trailingContextLines
;
71 // Initialize $x0 and $y0 to prevent IDEs from getting confused.
73 foreach ( $diff->edits
as $edit ) {
74 if ( $edit->type
== 'copy' ) {
75 if ( is_array( $block ) ) {
76 if ( count( $edit->orig
) <= $nlead +
$ntrail ) {
80 $context = array_slice( $edit->orig
, 0, $ntrail );
81 $block[] = new DiffOpCopy( $context );
83 $this->block( $x0, $ntrail +
$xi - $x0,
84 $y0, $ntrail +
$yi - $y0,
89 $context = $edit->orig
;
91 if ( !is_array( $block ) ) {
92 $context = array_slice( $context, count( $context ) - $nlead );
93 $x0 = $xi - count( $context );
94 $y0 = $yi - count( $context );
97 $block[] = new DiffOpCopy( $context );
104 $xi +
= count( $edit->orig
);
106 if ( $edit->closing
) {
107 $yi +
= count( $edit->closing
);
111 if ( is_array( $block ) ) {
112 $this->block( $x0, $xi - $x0,
117 $end = $this->endDiff();
118 wfProfileOut( __METHOD__
);
128 * @param array $edits
130 * @throws MWException If the edit type is not known.
132 protected function block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
133 wfProfileIn( __METHOD__
);
134 $this->startBlock( $this->blockHeader( $xbeg, $xlen, $ybeg, $ylen ) );
135 foreach ( $edits as $edit ) {
136 if ( $edit->type
== 'copy' ) {
137 $this->context( $edit->orig
);
138 } elseif ( $edit->type
== 'add' ) {
139 $this->added( $edit->closing
);
140 } elseif ( $edit->type
== 'delete' ) {
141 $this->deleted( $edit->orig
);
142 } elseif ( $edit->type
== 'change' ) {
143 $this->changed( $edit->orig
, $edit->closing
);
145 throw new MWException( "Unknown edit type: {$edit->type}" );
149 wfProfileOut( __METHOD__
);
152 protected function startDiff() {
159 protected function endDiff() {
160 $val = ob_get_contents();
174 protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
176 $xbeg .= ',' . ( $xbeg +
$xlen - 1 );
179 $ybeg .= ',' . ( $ybeg +
$ylen - 1 );
182 return $xbeg . ( $xlen ?
( $ylen ?
'c' : 'd' ) : 'a' ) . $ybeg;
186 * Called at the start of a block of connected edits.
187 * This default implementation writes the header and a newline to the output buffer.
189 * @param string $header
191 protected function startBlock( $header ) {
196 * Called at the end of a block of connected edits.
197 * This default implementation does nothing.
199 protected function endBlock() {
203 * Writes all (optionally prefixed) lines to the output buffer, separated by newlines.
205 * @param string[] $lines
206 * @param string $prefix
208 protected function lines( $lines, $prefix = ' ' ) {
209 foreach ( $lines as $line ) {
210 echo "$prefix $line\n";
215 * @param string[] $lines
217 protected function context( $lines ) {
218 $this->lines( $lines );
222 * @param string[] $lines
224 protected function added( $lines ) {
225 $this->lines( $lines, '>' );
229 * @param string[] $lines
231 protected function deleted( $lines ) {
232 $this->lines( $lines, '<' );
236 * Writes the two sets of lines to the output buffer, separated by "---" and a newline.
238 * @param string[] $orig
239 * @param string[] $closing
241 protected function changed( $orig, $closing ) {
242 $this->deleted( $orig );
244 $this->added( $closing );