Merge changes Ic13414f0,I26085bfc
[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 /** @var int Number of leading context "lines" to preserve.
38 *
39 * This should be left at zero for this class, but subclasses
40 * may want to set this to other values.
41 */
42 protected $leadingContextLines = 0;
43
44 /** @var int Number of trailing context "lines" to preserve.
45 *
46 * This should be left at zero for this class, but subclasses
47 * may want to set this to other values.
48 */
49 protected $trailingContextLines = 0;
50
51 /**
52 * Format a diff.
53 *
54 * @param $diff Diff A Diff object.
55 * @return string The formatted output.
56 */
57 public function format( $diff ) {
58 wfProfileIn( __METHOD__ );
59
60 $xi = $yi = 1;
61 $block = false;
62 $context = array();
63
64 $nlead = $this->leadingContextLines;
65 $ntrail = $this->trailingContextLines;
66
67 $this->startDiff();
68
69 // Initialize $x0 and $y0 to prevent IDEs from getting confused.
70 $x0 = $y0 = 0;
71 foreach ( $diff->edits as $edit ) {
72 if ( $edit->type == 'copy' ) {
73 if ( is_array( $block ) ) {
74 if ( count( $edit->orig ) <= $nlead + $ntrail ) {
75 $block[] = $edit;
76 } else {
77 if ( $ntrail ) {
78 $context = array_slice( $edit->orig, 0, $ntrail );
79 $block[] = new DiffOpCopy( $context );
80 }
81 $this->block( $x0, $ntrail + $xi - $x0,
82 $y0, $ntrail + $yi - $y0,
83 $block );
84 $block = false;
85 }
86 }
87 $context = $edit->orig;
88 } else {
89 if ( !is_array( $block ) ) {
90 $context = array_slice( $context, count( $context ) - $nlead );
91 $x0 = $xi - count( $context );
92 $y0 = $yi - count( $context );
93 $block = array();
94 if ( $context ) {
95 $block[] = new DiffOpCopy( $context );
96 }
97 }
98 $block[] = $edit;
99 }
100
101 if ( $edit->orig ) {
102 $xi += count( $edit->orig );
103 }
104 if ( $edit->closing ) {
105 $yi += count( $edit->closing );
106 }
107 }
108
109 if ( is_array( $block ) ) {
110 $this->block( $x0, $xi - $x0,
111 $y0, $yi - $y0,
112 $block );
113 }
114
115 $end = $this->endDiff();
116 wfProfileOut( __METHOD__ );
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 $edits
127 * @throws MWException
128 */
129 protected function block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
130 wfProfileIn( __METHOD__ );
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 wfProfileOut( __METHOD__ );
147 }
148
149 protected function startDiff() {
150 ob_start();
151 }
152
153 /**
154 * @return string
155 */
156 protected function endDiff() {
157 $val = ob_get_contents();
158 ob_end_clean();
159
160 return $val;
161 }
162
163 /**
164 * @param $xbeg
165 * @param $xlen
166 * @param $ybeg
167 * @param $ylen
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 protected function startBlock( $header ) {
182 echo $header . "\n";
183 }
184
185 protected function endBlock() {
186 }
187
188 /**
189 * @param $lines
190 * @param $prefix string
191 */
192 protected function lines( $lines, $prefix = ' ' ) {
193 foreach ( $lines as $line ) {
194 echo "$prefix $line\n";
195 }
196 }
197
198 /**
199 * @param $lines
200 */
201 protected function context( $lines ) {
202 $this->lines( $lines );
203 }
204
205 /**
206 * @param $lines
207 */
208 protected function added( $lines ) {
209 $this->lines( $lines, '>' );
210 }
211
212 /**
213 * @param $lines
214 */
215 protected function deleted( $lines ) {
216 $this->lines( $lines, '<' );
217 }
218
219 /**
220 * @param $orig
221 * @param $closing
222 */
223 protected function changed( $orig, $closing ) {
224 $this->deleted( $orig );
225 echo "---\n";
226 $this->added( $closing );
227 }
228 }