| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
use UnexpectedValueException; |
| 28 |
use Wikimedia\Diff\Diff; |
| 29 |
use Wikimedia\Diff\UnifiedDiffFormatter; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class DiffParser { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected $prefixLength = 1; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protected $left; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected $leftPos; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected $right; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected $rightPos; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected $changeSet; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function getChangeSet( $leftText, $rightText ) { |
| 86 |
$left = trim( $leftText ); |
| 87 |
$right = trim( $rightText ); |
| 88 |
|
| 89 |
if ( $left === '' ) { |
| 90 |
|
| 91 |
return $this->getChangeSetFromEmptyLeft( $right ); |
| 92 |
} |
| 93 |
|
| 94 |
$diffs = new Diff( explode( "\n", $left ), explode( "\n", $right ) ); |
| 95 |
$format = new UnifiedDiffFormatter(); |
| 96 |
$diff = $format->format( $diffs ); |
| 97 |
|
| 98 |
return $this->parse( $diff, $left, $right ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
private function getChangeSetFromEmptyLeft( $right ) { |
| 108 |
$rightLines = explode( "\n", $right ); |
| 109 |
|
| 110 |
return [ |
| 111 |
'_info' => [ |
| 112 |
'lhs-length' => 1, |
| 113 |
'rhs-length' => count( $rightLines ), |
| 114 |
'lhs' => [ '' ], |
| 115 |
'rhs' => $rightLines |
| 116 |
], |
| 117 |
[ |
| 118 |
'right-pos' => 1, |
| 119 |
'left-pos' => 1, |
| 120 |
'action' => 'add', |
| 121 |
'content' => $right, |
| 122 |
] |
| 123 |
]; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
protected static function usingInternalDiff() { |
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
protected function parse( $diff, $left, $right ) { |
| 147 |
$this->left = explode( "\n", $left ); |
| 148 |
$this->right = explode( "\n", $right ); |
| 149 |
$diff = explode( "\n", $diff ); |
| 150 |
|
| 151 |
$this->leftPos = 0; |
| 152 |
$this->rightPos = 0; |
| 153 |
$this->changeSet = [ |
| 154 |
'_info' => [ |
| 155 |
'lhs-length' => count( $this->left ), |
| 156 |
'rhs-length' => count( $this->right ), |
| 157 |
'lhs' => $this->left, |
| 158 |
'rhs' => $this->right, |
| 159 |
], |
| 160 |
]; |
| 161 |
|
| 162 |
$change = null; |
| 163 |
foreach ( $diff as $line ) { |
| 164 |
$change = $this->parseLine( $line, $change ); |
| 165 |
} |
| 166 |
if ( $change === null ) { |
| 167 |
return $this->changeSet; |
| 168 |
} |
| 169 |
|
| 170 |
return array_merge( $this->changeSet, $change->getChangeSet() ); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
protected function parseLine( $line, ?DiffGroup $change = null ) { |
| 182 |
if ( $line ) { |
| 183 |
$op = $line[0]; |
| 184 |
if ( strlen( $line ) > $this->prefixLength ) { |
| 185 |
$line = substr( $line, $this->prefixLength ); |
| 186 |
} else { |
| 187 |
$line = ''; |
| 188 |
} |
| 189 |
} else { |
| 190 |
$op = ' '; |
| 191 |
} |
| 192 |
|
| 193 |
switch ( $op ) { |
| 194 |
case '@': |
| 195 |
|
| 196 |
if ( $change !== null ) { |
| 197 |
$this->changeSet = array_merge( $this->changeSet, $change->getChangeSet() ); |
| 198 |
$change = null; |
| 199 |
} |
| 200 |
|
| 201 |
[ , $left, $right ] = explode( ' ', $line, 3 ); |
| 202 |
[ $this->leftPos ] = explode( ',', substr( $left, 1 ), 2 ); |
| 203 |
[ $this->rightPos ] = explode( ',', substr( $right, 1 ), 2 ); |
| 204 |
$this->leftPos = (int)$this->leftPos; |
| 205 |
$this->rightPos = (int)$this->rightPos; |
| 206 |
|
| 207 |
|
| 208 |
$this->leftPos--; |
| 209 |
$this->rightPos--; |
| 210 |
break; |
| 211 |
|
| 212 |
case ' ': |
| 213 |
|
| 214 |
if ( $change !== null ) { |
| 215 |
$this->changeSet = array_merge( $this->changeSet, $change->getChangeSet() ); |
| 216 |
$change = null; |
| 217 |
} |
| 218 |
$this->leftPos++; |
| 219 |
$this->rightPos++; |
| 220 |
break; |
| 221 |
|
| 222 |
case '-': |
| 223 |
|
| 224 |
if ( $this->left[$this->leftPos] !== $line ) { |
| 225 |
throw new UnexpectedValueException( 'Positional error: left' ); |
| 226 |
} |
| 227 |
if ( $change === null ) { |
| 228 |
|
| 229 |
$change = new DiffGroup( $this->leftPos, $this->rightPos ); |
| 230 |
} |
| 231 |
$change->subtract( $line ); |
| 232 |
$this->leftPos++; |
| 233 |
break; |
| 234 |
|
| 235 |
case '+': |
| 236 |
|
| 237 |
if ( $this->right[$this->rightPos] !== $line ) { |
| 238 |
throw new UnexpectedValueException( 'Positional error: right' ); |
| 239 |
} |
| 240 |
if ( $change === null ) { |
| 241 |
|
| 242 |
$change = new DiffGroup( $this->leftPos, $this->rightPos ); |
| 243 |
} |
| 244 |
$change->add( $line ); |
| 245 |
$this->rightPos++; |
| 246 |
break; |
| 247 |
|
| 248 |
default: |
| 249 |
throw new UnexpectedValueException( 'Unknown Diff Operation: ' . $op ); |
| 250 |
} |
| 251 |
|
| 252 |
return $change; |
| 253 |
} |
| 254 |
} |