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;
52 /** @var string The output buffer; holds the output while it is built. */
60 * @return string The formatted output.
62 public function format( $diff ) {
68 $nlead = $this->leadingContextLines
;
69 $ntrail = $this->trailingContextLines
;
73 // Initialize $x0 and $y0 to prevent IDEs from getting confused.
75 foreach ( $diff->edits
as $edit ) {
76 if ( $edit->type
== 'copy' ) {
77 if ( is_array( $block ) ) {
78 if ( count( $edit->orig
) <= $nlead +
$ntrail ) {
82 $context = array_slice( $edit->orig
, 0, $ntrail );
83 $block[] = new DiffOpCopy( $context );
85 $this->block( $x0, $ntrail +
$xi - $x0,
86 $y0, $ntrail +
$yi - $y0,
91 $context = $edit->orig
;
93 if ( !is_array( $block ) ) {
94 $context = array_slice( $context, count( $context ) - $nlead );
95 $x0 = $xi - count( $context );
96 $y0 = $yi - count( $context );
99 $block[] = new DiffOpCopy( $context );
106 $xi +
= count( $edit->orig
);
108 if ( $edit->closing
) {
109 $yi +
= count( $edit->closing
);
113 if ( is_array( $block ) ) {
114 $this->block( $x0, $xi - $x0,
119 $end = $this->endDiff();
129 * @param array $edits
131 * @throws MWException If the edit type is not known.
133 protected function block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
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}" );
151 protected function startDiff() {
156 * Writes a string to the output buffer.
158 * @param string $text
160 protected function writeOutput( $text ) {
161 $this->result
.= $text;
167 protected function endDiff() {
168 $val = $this->result
;
182 protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
184 $xbeg .= ',' . ( $xbeg +
$xlen - 1 );
187 $ybeg .= ',' . ( $ybeg +
$ylen - 1 );
190 return $xbeg . ( $xlen ?
( $ylen ?
'c' : 'd' ) : 'a' ) . $ybeg;
194 * Called at the start of a block of connected edits.
195 * This default implementation writes the header and a newline to the output buffer.
197 * @param string $header
199 protected function startBlock( $header ) {
200 $this->writeOutput( $header . "\n" );
204 * Called at the end of a block of connected edits.
205 * This default implementation does nothing.
207 protected function endBlock() {
211 * Writes all (optionally prefixed) lines to the output buffer, separated by newlines.
213 * @param string[] $lines
214 * @param string $prefix
216 protected function lines( $lines, $prefix = ' ' ) {
217 foreach ( $lines as $line ) {
218 $this->writeOutput( "$prefix $line\n" );
223 * @param string[] $lines
225 protected function context( $lines ) {
226 $this->lines( $lines );
230 * @param string[] $lines
232 protected function added( $lines ) {
233 $this->lines( $lines, '>' );
237 * @param string[] $lines
239 protected function deleted( $lines ) {
240 $this->lines( $lines, '<' );
244 * Writes the two sets of lines to the output buffer, separated by "---" and a newline.
246 * @param string[] $orig
247 * @param string[] $closing
249 protected function changed( $orig, $closing ) {
250 $this->deleted( $orig );
251 $this->writeOutput( "---\n" );
252 $this->added( $closing );