33ca931fdb588175c77775566911688fc9cb17c9
[lhc/web/wiklou.git] / includes / diff / DiffFormatter.php
1 <?php
2 /**
3 * Base for diff rendering classes. Portions taken from phpwiki-1.3.3.
4 *
5 * Copyright © 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
6 * You may copy this code freely under the conditions of the GPL.
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 * @ingroup DifferenceEngine
25 */
26
27 /**
28 * Base class for diff formatters
29 *
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.
33 * @todo document
34 * @ingroup DifferenceEngine
35 */
36 abstract class DiffFormatter {
37
38 /** @var int Number of leading context "lines" to preserve.
39 *
40 * This should be left at zero for this class, but subclasses
41 * may want to set this to other values.
42 */
43 protected $leadingContextLines = 0;
44
45 /** @var int Number of trailing context "lines" to preserve.
46 *
47 * This should be left at zero for this class, but subclasses
48 * may want to set this to other values.
49 */
50 protected $trailingContextLines = 0;
51
52 /**
53 * Format a diff.
54 *
55 * @param Diff $diff
56 *
57 * @return string The formatted output.
58 */
59 public function format( $diff ) {
60
61 $xi = $yi = 1;
62 $block = false;
63 $context = array();
64
65 $nlead = $this->leadingContextLines;
66 $ntrail = $this->trailingContextLines;
67
68 $this->startDiff();
69
70 // Initialize $x0 and $y0 to prevent IDEs from getting confused.
71 $x0 = $y0 = 0;
72 foreach ( $diff->edits as $edit ) {
73 if ( $edit->type == 'copy' ) {
74 if ( is_array( $block ) ) {
75 if ( count( $edit->orig ) <= $nlead + $ntrail ) {
76 $block[] = $edit;
77 } else {
78 if ( $ntrail ) {
79 $context = array_slice( $edit->orig, 0, $ntrail );
80 $block[] = new DiffOpCopy( $context );
81 }
82 $this->block( $x0, $ntrail + $xi - $x0,
83 $y0, $ntrail + $yi - $y0,
84 $block );
85 $block = false;
86 }
87 }
88 $context = $edit->orig;
89 } else {
90 if ( !is_array( $block ) ) {
91 $context = array_slice( $context, count( $context ) - $nlead );
92 $x0 = $xi - count( $context );
93 $y0 = $yi - count( $context );
94 $block = array();
95 if ( $context ) {
96 $block[] = new DiffOpCopy( $context );
97 }
98 }
99 $block[] = $edit;
100 }
101
102 if ( $edit->orig ) {
103 $xi += count( $edit->orig );
104 }
105 if ( $edit->closing ) {
106 $yi += count( $edit->closing );
107 }
108 }
109
110 if ( is_array( $block ) ) {
111 $this->block( $x0, $xi - $x0,
112 $y0, $yi - $y0,
113 $block );
114 }
115
116 $end = $this->endDiff();
117
118 return $end;
119 }
120
121 /**
122 * @param int $xbeg
123 * @param int $xlen
124 * @param int $ybeg
125 * @param int $ylen
126 * @param array $edits
127 *
128 * @throws MWException If the edit type is not known.
129 */
130 protected function block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
131 $this->startBlock( $this->blockHeader( $xbeg, $xlen, $ybeg, $ylen ) );
132 foreach ( $edits as $edit ) {
133 if ( $edit->type == 'copy' ) {
134 $this->context( $edit->orig );
135 } elseif ( $edit->type == 'add' ) {
136 $this->added( $edit->closing );
137 } elseif ( $edit->type == 'delete' ) {
138 $this->deleted( $edit->orig );
139 } elseif ( $edit->type == 'change' ) {
140 $this->changed( $edit->orig, $edit->closing );
141 } else {
142 throw new MWException( "Unknown edit type: {$edit->type}" );
143 }
144 }
145 $this->endBlock();
146 }
147
148 protected function startDiff() {
149 ob_start();
150 }
151
152 /**
153 * @return string
154 */
155 protected function endDiff() {
156 $val = ob_get_contents();
157 ob_end_clean();
158
159 return $val;
160 }
161
162 /**
163 * @param int $xbeg
164 * @param int $xlen
165 * @param int $ybeg
166 * @param int $ylen
167 *
168 * @return string
169 */
170 protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
171 if ( $xlen > 1 ) {
172 $xbeg .= ',' . ( $xbeg + $xlen - 1 );
173 }
174 if ( $ylen > 1 ) {
175 $ybeg .= ',' . ( $ybeg + $ylen - 1 );
176 }
177
178 return $xbeg . ( $xlen ? ( $ylen ? 'c' : 'd' ) : 'a' ) . $ybeg;
179 }
180
181 /**
182 * Called at the start of a block of connected edits.
183 * This default implementation writes the header and a newline to the output buffer.
184 *
185 * @param string $header
186 */
187 protected function startBlock( $header ) {
188 echo $header . "\n";
189 }
190
191 /**
192 * Called at the end of a block of connected edits.
193 * This default implementation does nothing.
194 */
195 protected function endBlock() {
196 }
197
198 /**
199 * Writes all (optionally prefixed) lines to the output buffer, separated by newlines.
200 *
201 * @param string[] $lines
202 * @param string $prefix
203 */
204 protected function lines( $lines, $prefix = ' ' ) {
205 foreach ( $lines as $line ) {
206 echo "$prefix $line\n";
207 }
208 }
209
210 /**
211 * @param string[] $lines
212 */
213 protected function context( $lines ) {
214 $this->lines( $lines );
215 }
216
217 /**
218 * @param string[] $lines
219 */
220 protected function added( $lines ) {
221 $this->lines( $lines, '>' );
222 }
223
224 /**
225 * @param string[] $lines
226 */
227 protected function deleted( $lines ) {
228 $this->lines( $lines, '<' );
229 }
230
231 /**
232 * Writes the two sets of lines to the output buffer, separated by "---" and a newline.
233 *
234 * @param string[] $orig
235 * @param string[] $closing
236 */
237 protected function changed( $orig, $closing ) {
238 $this->deleted( $orig );
239 echo "---\n";
240 $this->added( $closing );
241 }
242
243 }