Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 71 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| DiffFormatter | |
0.00% |
0 / 70 |
|
0.00% |
0 / 13 |
1122 | |
0.00% |
0 / 1 |
| format | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
132 | |||
| block | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
| startDiff | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| writeOutput | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| endDiff | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| blockHeader | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| startBlock | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| endBlock | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| lines | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| context | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| added | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| deleted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| changed | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | * @license GPL-2.0-or-later |
| 9 | * @file |
| 10 | * @ingroup DifferenceEngine |
| 11 | */ |
| 12 | |
| 13 | namespace Wikimedia\Diff; |
| 14 | |
| 15 | use UnexpectedValueException; |
| 16 | |
| 17 | /** |
| 18 | * Base class for diff formatters |
| 19 | * |
| 20 | * This class formats the diff in classic diff format. |
| 21 | * It is intended that this class be customized via inheritance, |
| 22 | * to obtain fancier outputs. |
| 23 | * @todo document |
| 24 | * @ingroup DifferenceEngine |
| 25 | */ |
| 26 | abstract class DiffFormatter { |
| 27 | |
| 28 | /** @var int Number of leading context "lines" to preserve. |
| 29 | * |
| 30 | * This should be left at zero for this class, but subclasses |
| 31 | * may want to set this to other values. |
| 32 | */ |
| 33 | protected $leadingContextLines = 0; |
| 34 | |
| 35 | /** @var int Number of trailing context "lines" to preserve. |
| 36 | * |
| 37 | * This should be left at zero for this class, but subclasses |
| 38 | * may want to set this to other values. |
| 39 | */ |
| 40 | protected $trailingContextLines = 0; |
| 41 | |
| 42 | /** @var string The output buffer; holds the output while it is built. */ |
| 43 | private $result = ''; |
| 44 | |
| 45 | /** |
| 46 | * Format a diff. |
| 47 | * |
| 48 | * @param Diff $diff |
| 49 | * |
| 50 | * @return string The formatted output. |
| 51 | */ |
| 52 | public function format( $diff ) { |
| 53 | $xi = $yi = 1; |
| 54 | $block = false; |
| 55 | $context = []; |
| 56 | |
| 57 | $nlead = $this->leadingContextLines; |
| 58 | $ntrail = $this->trailingContextLines; |
| 59 | |
| 60 | $this->startDiff(); |
| 61 | |
| 62 | // Initialize $x0 and $y0 to prevent IDEs from getting confused. |
| 63 | $x0 = $y0 = 0; |
| 64 | foreach ( $diff->edits as $edit ) { |
| 65 | if ( $edit->type == 'copy' ) { |
| 66 | if ( is_array( $block ) ) { |
| 67 | if ( count( $edit->orig ) <= $nlead + $ntrail ) { |
| 68 | $block[] = $edit; |
| 69 | } else { |
| 70 | if ( $ntrail ) { |
| 71 | $context = array_slice( $edit->orig, 0, $ntrail ); |
| 72 | $block[] = new DiffOpCopy( $context ); |
| 73 | } |
| 74 | $this->block( $x0, $ntrail + $xi - $x0, |
| 75 | $y0, $ntrail + $yi - $y0, |
| 76 | $block ); |
| 77 | $block = false; |
| 78 | } |
| 79 | } |
| 80 | $context = $edit->orig; |
| 81 | } else { |
| 82 | if ( !is_array( $block ) ) { |
| 83 | $context = array_slice( $context, count( $context ) - $nlead ); |
| 84 | $x0 = $xi - count( $context ); |
| 85 | $y0 = $yi - count( $context ); |
| 86 | $block = []; |
| 87 | if ( $context ) { |
| 88 | $block[] = new DiffOpCopy( $context ); |
| 89 | } |
| 90 | } |
| 91 | $block[] = $edit; |
| 92 | } |
| 93 | |
| 94 | if ( $edit->orig ) { |
| 95 | $xi += count( $edit->orig ); |
| 96 | } |
| 97 | if ( $edit->closing ) { |
| 98 | $yi += count( $edit->closing ); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if ( is_array( $block ) ) { |
| 103 | $this->block( $x0, $xi - $x0, |
| 104 | $y0, $yi - $y0, |
| 105 | $block ); |
| 106 | } |
| 107 | |
| 108 | $end = $this->endDiff(); |
| 109 | |
| 110 | return $end; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @param int $xbeg |
| 115 | * @param int $xlen |
| 116 | * @param int $ybeg |
| 117 | * @param int $ylen |
| 118 | * @param array &$edits |
| 119 | */ |
| 120 | protected function block( $xbeg, $xlen, $ybeg, $ylen, &$edits ) { |
| 121 | $this->startBlock( $this->blockHeader( $xbeg, $xlen, $ybeg, $ylen ) ); |
| 122 | foreach ( $edits as $edit ) { |
| 123 | if ( $edit->type == 'copy' ) { |
| 124 | $this->context( $edit->orig ); |
| 125 | } elseif ( $edit->type == 'add' ) { |
| 126 | $this->added( $edit->closing ); |
| 127 | } elseif ( $edit->type == 'delete' ) { |
| 128 | $this->deleted( $edit->orig ); |
| 129 | } elseif ( $edit->type == 'change' ) { |
| 130 | $this->changed( $edit->orig, $edit->closing ); |
| 131 | } else { |
| 132 | throw new UnexpectedValueException( "Unknown edit type: {$edit->type}" ); |
| 133 | } |
| 134 | } |
| 135 | $this->endBlock(); |
| 136 | } |
| 137 | |
| 138 | protected function startDiff() { |
| 139 | $this->result = ''; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Writes a string to the output buffer. |
| 144 | * |
| 145 | * @param string $text |
| 146 | */ |
| 147 | protected function writeOutput( $text ) { |
| 148 | $this->result .= $text; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @return string |
| 153 | */ |
| 154 | protected function endDiff() { |
| 155 | $val = $this->result; |
| 156 | $this->result = ''; |
| 157 | |
| 158 | return $val; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @param int $xbeg |
| 163 | * @param int $xlen |
| 164 | * @param int $ybeg |
| 165 | * @param int $ylen |
| 166 | * |
| 167 | * @return string |
| 168 | */ |
| 169 | protected function blockHeader( $xbeg, $xlen, $ybeg, $ylen ) { |
| 170 | if ( $xlen > 1 ) { |
| 171 | $xbeg .= ',' . ( $xbeg + $xlen - 1 ); |
| 172 | } |
| 173 | if ( $ylen > 1 ) { |
| 174 | $ybeg .= ',' . ( $ybeg + $ylen - 1 ); |
| 175 | } |
| 176 | |
| 177 | return $xbeg . ( $xlen ? ( $ylen ? 'c' : 'd' ) : 'a' ) . $ybeg; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Called at the start of a block of connected edits. |
| 182 | * This default implementation writes the header and a newline to the output buffer. |
| 183 | * |
| 184 | * @param string $header |
| 185 | */ |
| 186 | protected function startBlock( $header ) { |
| 187 | $this->writeOutput( $header . "\n" ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Called at the end of a block of connected edits. |
| 192 | * This default implementation does nothing. |
| 193 | */ |
| 194 | protected function endBlock() { |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Writes all (optionally prefixed) lines to the output buffer, separated by newlines. |
| 199 | * |
| 200 | * @param string[] $lines |
| 201 | * @param string $prefix |
| 202 | */ |
| 203 | protected function lines( $lines, $prefix = ' ' ) { |
| 204 | foreach ( $lines as $line ) { |
| 205 | $this->writeOutput( "$prefix $line\n" ); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * @param string[] $lines |
| 211 | */ |
| 212 | protected function context( $lines ) { |
| 213 | $this->lines( $lines ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @param string[] $lines |
| 218 | */ |
| 219 | protected function added( $lines ) { |
| 220 | $this->lines( $lines, '>' ); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * @param string[] $lines |
| 225 | */ |
| 226 | protected function deleted( $lines ) { |
| 227 | $this->lines( $lines, '<' ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Writes the two sets of lines to the output buffer, separated by "---" and a newline. |
| 232 | * |
| 233 | * @param string[] $orig |
| 234 | * @param string[] $closing |
| 235 | */ |
| 236 | protected function changed( $orig, $closing ) { |
| 237 | $this->deleted( $orig ); |
| 238 | $this->writeOutput( "---\n" ); |
| 239 | $this->added( $closing ); |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | |
| 244 | /** @deprecated class alias since 1.41 */ |
| 245 | class_alias( DiffFormatter::class, 'DiffFormatter' ); |