MediaWiki  1.23.0
DiffFormatter.php
Go to the documentation of this file.
1 <?php
36 abstract class DiffFormatter {
37 
43  protected $leadingContextLines = 0;
44 
50  protected $trailingContextLines = 0;
51 
59  public function format( $diff ) {
60  wfProfileIn( __METHOD__ );
61 
62  $xi = $yi = 1;
63  $block = false;
64  $context = array();
65 
68 
69  $this->startDiff();
70 
71  // Initialize $x0 and $y0 to prevent IDEs from getting confused.
72  $x0 = $y0 = 0;
73  foreach ( $diff->edits as $edit ) {
74  if ( $edit->type == 'copy' ) {
75  if ( is_array( $block ) ) {
76  if ( count( $edit->orig ) <= $nlead + $ntrail ) {
77  $block[] = $edit;
78  } else {
79  if ( $ntrail ) {
80  $context = array_slice( $edit->orig, 0, $ntrail );
81  $block[] = new DiffOpCopy( $context );
82  }
83  $this->block( $x0, $ntrail + $xi - $x0,
84  $y0, $ntrail + $yi - $y0,
85  $block );
86  $block = false;
87  }
88  }
89  $context = $edit->orig;
90  } else {
91  if ( !is_array( $block ) ) {
92  $context = array_slice( $context, count( $context ) - $nlead );
93  $x0 = $xi - count( $context );
94  $y0 = $yi - count( $context );
95  $block = array();
96  if ( $context ) {
97  $block[] = new DiffOpCopy( $context );
98  }
99  }
100  $block[] = $edit;
101  }
102 
103  if ( $edit->orig ) {
104  $xi += count( $edit->orig );
105  }
106  if ( $edit->closing ) {
107  $yi += count( $edit->closing );
108  }
109  }
110 
111  if ( is_array( $block ) ) {
112  $this->block( $x0, $xi - $x0,
113  $y0, $yi - $y0,
114  $block );
115  }
116 
117  $end = $this->endDiff();
118  wfProfileOut( __METHOD__ );
119 
120  return $end;
121  }
122 
132  protected function block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) {
133  wfProfileIn( __METHOD__ );
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 );
144  } else {
145  throw new MWException( "Unknown edit type: {$edit->type}" );
146  }
147  }
148  $this->endBlock();
149  wfProfileOut( __METHOD__ );
150  }
151 
152  protected function startDiff() {
153  ob_start();
154  }
155 
159  protected function endDiff() {
160  $val = ob_get_contents();
161  ob_end_clean();
162 
163  return $val;
164  }
165 
174  protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) {
175  if ( $xlen > 1 ) {
176  $xbeg .= ',' . ( $xbeg + $xlen - 1 );
177  }
178  if ( $ylen > 1 ) {
179  $ybeg .= ',' . ( $ybeg + $ylen - 1 );
180  }
181 
182  return $xbeg . ( $xlen ? ( $ylen ? 'c' : 'd' ) : 'a' ) . $ybeg;
183  }
184 
191  protected function startBlock( $header ) {
192  echo $header . "\n";
193  }
194 
199  protected function endBlock() {
200  }
201 
208  protected function lines( $lines, $prefix = ' ' ) {
209  foreach ( $lines as $line ) {
210  echo "$prefix $line\n";
211  }
212  }
213 
217  protected function context( $lines ) {
218  $this->lines( $lines );
219  }
220 
224  protected function added( $lines ) {
225  $this->lines( $lines, '>' );
226  }
227 
231  protected function deleted( $lines ) {
232  $this->lines( $lines, '<' );
233  }
234 
241  protected function changed( $orig, $closing ) {
242  $this->deleted( $orig );
243  echo "---\n";
244  $this->added( $closing );
245  }
246 
247 }
DiffFormatter\changed
changed( $orig, $closing)
Writes the two sets of lines to the output buffer, separated by "---" and a newline.
Definition: DiffFormatter.php:239
DiffFormatter
Base class for diff formatters.
Definition: DiffFormatter.php:36
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
DiffFormatter\added
added( $lines)
Definition: DiffFormatter.php:222
DiffFormatter\context
context( $lines)
Definition: DiffFormatter.php:215
DiffFormatter\$leadingContextLines
int $leadingContextLines
Number of leading context "lines" to preserve.
Definition: DiffFormatter.php:42
DiffOpCopy
Definition: DairikiDiff.php:97
DiffFormatter\block
block( $xbeg, $xlen, $ybeg, $ylen, &$edits)
Definition: DiffFormatter.php:130
MWException
MediaWiki exception.
Definition: MWException.php:26
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
$lines
$lines
Definition: router.php:65
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
DiffFormatter\startBlock
startBlock( $header)
Called at the start of a block of connected edits.
Definition: DiffFormatter.php:189
DiffFormatter\$trailingContextLines
int $trailingContextLines
Number of trailing context "lines" to preserve.
Definition: DiffFormatter.php:48
$line
$line
Definition: cdb.php:57
DiffFormatter\deleted
deleted( $lines)
Definition: DiffFormatter.php:229
DiffFormatter\startDiff
startDiff()
Definition: DiffFormatter.php:150
DiffFormatter\blockHeader
blockHeader( $xbeg, $xlen, $ybeg, $ylen)
Definition: DiffFormatter.php:172
DiffFormatter\lines
lines( $lines, $prefix=' ')
Writes all (optionally prefixed) lines to the output buffer, separated by newlines.
Definition: DiffFormatter.php:206
DiffFormatter\format
format( $diff)
Format a diff.
Definition: DiffFormatter.php:57
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
DiffFormatter\endBlock
endBlock()
Called at the end of a block of connected edits.
Definition: DiffFormatter.php:197
DiffFormatter\endDiff
endDiff()
Definition: DiffFormatter.php:157