Update formatting for includes/diff
[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 foreach ( $diff->edits as $edit ) {
70 if ( $edit->type == 'copy' ) {
71 if ( is_array( $block ) ) {
72 if ( count( $edit->orig ) <= $nlead + $ntrail ) {
73 $block[] = $edit;
74 } else {
75 if ( $ntrail ) {
76 $context = array_slice( $edit->orig, 0, $ntrail );
77 $block[] = new DiffOp_Copy( $context );
78 }
79 $this->block( $x0, $ntrail + $xi - $x0,
80 $y0, $ntrail + $yi - $y0,
81 $block );
82 $block = false;
83 }
84 }
85 $context = $edit->orig;
86 } else {
87 if ( !is_array( $block ) ) {
88 $context = array_slice( $context, count( $context ) - $nlead );
89 $x0 = $xi - count( $context );
90 $y0 = $yi - count( $context );
91 $block = array();
92 if ( $context ) {
93 $block[] = new DiffOp_Copy( $context );
94 }
95 }
96 $block[] = $edit;
97 }
98
99 if ( $edit->orig ) {
100 $xi += count( $edit->orig );
101 }
102 if ( $edit->closing ) {
103 $yi += count( $edit->closing );
104 }
105 }
106
107 if ( is_array( $block ) ) {
108 $this->block( $x0, $xi - $x0,
109 $y0, $yi - $y0,
110 $block );
111 }
112
113 $end = $this->endDiff();
114 wfProfileOut( __METHOD__ );
115
116 return $end;
117 }
118
119 /**
120 * @param $xbeg
121 * @param $xlen
122 * @param $ybeg
123 * @param $ylen
124 * @param $edits
125 */
126 protected function block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
127 wfProfileIn( __METHOD__ );
128 $this->startBlock( $this->blockHeader( $xbeg, $xlen, $ybeg, $ylen ) );
129 foreach ( $edits as $edit ) {
130 if ( $edit->type == 'copy' ) {
131 $this->context( $edit->orig );
132 } elseif ( $edit->type == 'add' ) {
133 $this->added( $edit->closing );
134 } elseif ( $edit->type == 'delete' ) {
135 $this->deleted( $edit->orig );
136 } elseif ( $edit->type == 'change' ) {
137 $this->changed( $edit->orig, $edit->closing );
138 } else {
139 throw new MWException( "Unknown edit type: {$edit->type}" );
140 }
141 }
142 $this->endBlock();
143 wfProfileOut( __METHOD__ );
144 }
145
146 protected function startDiff() {
147 ob_start();
148 }
149
150 /**
151 * @return string
152 */
153 protected function endDiff() {
154 $val = ob_get_contents();
155 ob_end_clean();
156
157 return $val;
158 }
159
160 /**
161 * @param $xbeg
162 * @param $xlen
163 * @param $ybeg
164 * @param $ylen
165 * @return string
166 */
167 protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
168 if ( $xlen > 1 ) {
169 $xbeg .= ',' . ( $xbeg + $xlen - 1 );
170 }
171 if ( $ylen > 1 ) {
172 $ybeg .= ',' . ( $ybeg + $ylen - 1 );
173 }
174
175 return $xbeg . ( $xlen ? ( $ylen ? 'c' : 'd' ) : 'a' ) . $ybeg;
176 }
177
178 protected function startBlock( $header ) {
179 echo $header . "\n";
180 }
181
182 protected function endBlock() {
183 }
184
185 /**
186 * @param $lines
187 * @param $prefix string
188 */
189 protected function lines( $lines, $prefix = ' ' ) {
190 foreach ( $lines as $line ) {
191 echo "$prefix $line\n";
192 }
193 }
194
195 /**
196 * @param $lines
197 */
198 protected function context( $lines ) {
199 $this->lines( $lines );
200 }
201
202 /**
203 * @param $lines
204 */
205 protected function added( $lines ) {
206 $this->lines( $lines, '>' );
207 }
208
209 /**
210 * @param $lines
211 */
212 protected function deleted( $lines ) {
213 $this->lines( $lines, '<' );
214 }
215
216 /**
217 * @param $orig
218 * @param $closing
219 */
220 protected function changed( $orig, $closing ) {
221 $this->deleted( $orig );
222 echo "---\n";
223 $this->added( $closing );
224 }
225 }