MediaWiki fundraising/REL1_35
DiffFormatter.php
Go to the documentation of this file.
1<?php
36abstract class DiffFormatter {
37
43 protected $leadingContextLines = 0;
44
50 protected $trailingContextLines = 0;
51
53 private $result = '';
54
62 public function format( $diff ) {
63 $xi = $yi = 1;
64 $block = false;
65 $context = [];
66
69
70 $this->startDiff();
71
72 // Initialize $x0 and $y0 to prevent IDEs from getting confused.
73 $x0 = $y0 = 0;
74 foreach ( $diff->edits as $edit ) {
75 if ( $edit->type == 'copy' ) {
76 if ( is_array( $block ) ) {
77 if ( count( $edit->orig ) <= $nlead + $ntrail ) {
78 $block[] = $edit;
79 } else {
80 if ( $ntrail ) {
81 $context = array_slice( $edit->orig, 0, $ntrail );
82 $block[] = new DiffOpCopy( $context );
83 }
84 $this->block( $x0, $ntrail + $xi - $x0,
85 $y0, $ntrail + $yi - $y0,
86 $block );
87 $block = false;
88 }
89 }
90 $context = $edit->orig;
91 } else {
92 if ( !is_array( $block ) ) {
93 $context = array_slice( $context, count( $context ) - $nlead );
94 $x0 = $xi - count( $context );
95 $y0 = $yi - count( $context );
96 $block = [];
97 if ( $context ) {
98 $block[] = new DiffOpCopy( $context );
99 }
100 }
101 $block[] = $edit;
102 }
103
104 if ( $edit->orig ) {
105 $xi += count( $edit->orig );
106 }
107 if ( $edit->closing ) {
108 $yi += count( $edit->closing );
109 }
110 }
111
112 if ( is_array( $block ) ) {
113 $this->block( $x0, $xi - $x0,
114 $y0, $yi - $y0,
115 $block );
116 }
117
118 $end = $this->endDiff();
119
120 return $end;
121 }
122
132 protected function block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
133 $this->startBlock( $this->blockHeader( $xbeg, $xlen, $ybeg, $ylen ) );
134 foreach ( $edits as $edit ) {
135 if ( $edit->type == 'copy' ) {
136 $this->context( $edit->orig );
137 } elseif ( $edit->type == 'add' ) {
138 $this->added( $edit->closing );
139 } elseif ( $edit->type == 'delete' ) {
140 $this->deleted( $edit->orig );
141 } elseif ( $edit->type == 'change' ) {
142 $this->changed( $edit->orig, $edit->closing );
143 } else {
144 throw new MWException( "Unknown edit type: {$edit->type}" );
145 }
146 }
147 $this->endBlock();
148 }
149
150 protected function startDiff() {
151 $this->result = '';
152 }
153
159 protected function writeOutput( $text ) {
160 $this->result .= $text;
161 }
162
166 protected function endDiff() {
167 $val = $this->result;
168 $this->result = '';
169
170 return $val;
171 }
172
181 protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
182 if ( $xlen > 1 ) {
183 $xbeg .= ',' . ( $xbeg + $xlen - 1 );
184 }
185 if ( $ylen > 1 ) {
186 $ybeg .= ',' . ( $ybeg + $ylen - 1 );
187 }
188
189 return $xbeg . ( $xlen ? ( $ylen ? 'c' : 'd' ) : 'a' ) . $ybeg;
190 }
191
198 protected function startBlock( $header ) {
199 $this->writeOutput( $header . "\n" );
200 }
201
206 protected function endBlock() {
207 }
208
215 protected function lines( $lines, $prefix = ' ' ) {
216 foreach ( $lines as $line ) {
217 $this->writeOutput( "$prefix $line\n" );
218 }
219 }
220
224 protected function context( $lines ) {
225 $this->lines( $lines );
226 }
227
231 protected function added( $lines ) {
232 $this->lines( $lines, '>' );
233 }
234
238 protected function deleted( $lines ) {
239 $this->lines( $lines, '<' );
240 }
241
248 protected function changed( $orig, $closing ) {
249 $this->deleted( $orig );
250 $this->writeOutput( "---\n" );
251 $this->added( $closing );
252 }
253
254}
Base class for diff formatters.
block( $xbeg, $xlen, $ybeg, $ylen, &$edits)
int $trailingContextLines
Number of trailing context "lines" to preserve.
writeOutput( $text)
Writes a string to the output buffer.
blockHeader( $xbeg, $xlen, $ybeg, $ylen)
endBlock()
Called at the end of a block of connected edits.
lines( $lines, $prefix=' ')
Writes all (optionally prefixed) lines to the output buffer, separated by newlines.
format( $diff)
Format a diff.
string $result
The output buffer; holds the output while it is built.
int $leadingContextLines
Number of leading context "lines" to preserve.
startBlock( $header)
Called at the start of a block of connected edits.
changed( $orig, $closing)
Writes the two sets of lines to the output buffer, separated by "---" and a newline.
Extends DiffOp.
MediaWiki exception.
$line
Definition mcc.php:119
if(!file_exists( $CREDITS)) $lines
$header