| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Diff\TextDiffer; |
| 4 |
|
| 5 |
use DomainException; |
| 6 |
use InvalidArgumentException; |
| 7 |
use MediaWiki\Language\Language; |
| 8 |
use MediaWiki\Output\OutputPage; |
| 9 |
use MessageLocalizer; |
| 10 |
use UnexpectedValueException; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class ManifoldTextDiffer implements TextDiffer { |
| 19 |
|
| 20 |
private $localizer; |
| 21 |
|
| 22 |
private $contentLanguage; |
| 23 |
|
| 24 |
private $diffEngine; |
| 25 |
|
| 26 |
private $externalPath; |
| 27 |
|
| 28 |
private $differs; |
| 29 |
|
| 30 |
private $differsByFormat; |
| 31 |
|
| 32 |
private $wikidiff2Options; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function __construct( |
| 44 |
MessageLocalizer $localizer, |
| 45 |
?Language $contentLanguage, |
| 46 |
$diffEngine, |
| 47 |
$externalPath, |
| 48 |
$wikidiff2Options |
| 49 |
) { |
| 50 |
$this->localizer = $localizer; |
| 51 |
$this->contentLanguage = $contentLanguage; |
| 52 |
$this->diffEngine = $diffEngine; |
| 53 |
$this->externalPath = $externalPath; |
| 54 |
$this->wikidiff2Options = $wikidiff2Options; |
| 55 |
} |
| 56 |
|
| 57 |
public function getName(): string { |
| 58 |
return 'manifold'; |
| 59 |
} |
| 60 |
|
| 61 |
public function getFormats(): array { |
| 62 |
$differs = $this->getDiffersByFormat(); |
| 63 |
return array_keys( $differs ); |
| 64 |
} |
| 65 |
|
| 66 |
public function hasFormat( string $format ): bool { |
| 67 |
$differs = $this->getDiffersByFormat(); |
| 68 |
return isset( $differs[$format] ); |
| 69 |
} |
| 70 |
|
| 71 |
public function render( string $oldText, string $newText, string $format ): string { |
| 72 |
if ( !in_array( $format, $this->getFormats(), true ) ) { |
| 73 |
throw new InvalidArgumentException( |
| 74 |
'The requested format is not supported by this engine' ); |
| 75 |
} |
| 76 |
$results = $this->renderBatch( $oldText, $newText, [ $format ] ); |
| 77 |
return reset( $results ); |
| 78 |
} |
| 79 |
|
| 80 |
public function renderBatch( string $oldText, string $newText, array $formats ): array { |
| 81 |
$result = []; |
| 82 |
$differs = $this->splitBatchByDiffer( $formats ); |
| 83 |
|
| 84 |
foreach ( $differs as [ $differ, $formatBatch ] ) { |
| 85 |
$result += $differ->renderBatch( $oldText, $newText, $formatBatch ); |
| 86 |
} |
| 87 |
return $result; |
| 88 |
} |
| 89 |
|
| 90 |
public function getFormatContext( string $format ) { |
| 91 |
return $this->getDifferForFormat( $format )->getFormatContext( $format ); |
| 92 |
} |
| 93 |
|
| 94 |
public function addRowWrapper( string $format, string $diffText ): string { |
| 95 |
return $this->getDifferForFormat( $format )->addRowWrapper( $format, $diffText ); |
| 96 |
} |
| 97 |
|
| 98 |
public function addModules( OutputPage $out, string $format ): void { |
| 99 |
$this->getDifferForFormat( $format )->addModules( $out, $format ); |
| 100 |
} |
| 101 |
|
| 102 |
public function getCacheKeys( array $formats ): array { |
| 103 |
$keys = []; |
| 104 |
$engines = []; |
| 105 |
$differs = $this->splitBatchByDiffer( $formats ); |
| 106 |
|
| 107 |
foreach ( $differs as [ $differ, $formatBatch ] ) { |
| 108 |
$keys += $differ->getCacheKeys( $formatBatch ); |
| 109 |
$engines[] = $differ->getName() . '=' . implode( ',', $formatBatch ); |
| 110 |
} |
| 111 |
$keys['10-formats-and-engines'] = implode( ';', $engines ); |
| 112 |
return $keys; |
| 113 |
} |
| 114 |
|
| 115 |
public function localize( string $format, string $diff, array $options = [] ): string { |
| 116 |
return $this->getDifferForFormat( $format )->localize( $format, $diff, $options ); |
| 117 |
} |
| 118 |
|
| 119 |
public function getTablePrefixes( string $format ): array { |
| 120 |
return $this->getDifferForFormat( $format )->getTablePrefixes( $format ); |
| 121 |
} |
| 122 |
|
| 123 |
public function getPreferredFormatBatch( string $format ): array { |
| 124 |
return $this->getDifferForFormat( $format )->getPreferredFormatBatch( $format ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
private function getDiffersByFormat() { |
| 131 |
if ( $this->differsByFormat === null ) { |
| 132 |
$differs = []; |
| 133 |
foreach ( $this->getDiffers() as $differ ) { |
| 134 |
foreach ( $differ->getFormats() as $format ) { |
| 135 |
|
| 136 |
$differs[$format] ??= $differ; |
| 137 |
} |
| 138 |
} |
| 139 |
$this->differsByFormat = $differs; |
| 140 |
} |
| 141 |
return $this->differsByFormat; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
private function getDifferForFormat( $format ) { |
| 149 |
$differs = $this->getDiffersByFormat(); |
| 150 |
if ( !isset( $differs[$format] ) ) { |
| 151 |
throw new InvalidArgumentException( |
| 152 |
"Unknown format \"$format\"" |
| 153 |
); |
| 154 |
} |
| 155 |
return $differs[$format]; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function setEngine( string $name ) { |
| 164 |
$this->diffEngine = $name; |
| 165 |
$this->differs = null; |
| 166 |
$this->differsByFormat = null; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function getEngineForFormat( string $format ) { |
| 176 |
return $this->getDifferForFormat( $format )->getName(); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
private function getDiffers() { |
| 187 |
if ( $this->differs === null ) { |
| 188 |
$differs = []; |
| 189 |
if ( $this->diffEngine === null ) { |
| 190 |
$differNames = [ 'external', 'wikidiff2', 'php' ]; |
| 191 |
} else { |
| 192 |
$differNames = [ $this->diffEngine ]; |
| 193 |
} |
| 194 |
$failureReason = ''; |
| 195 |
foreach ( $differNames as $name ) { |
| 196 |
$differ = $this->maybeCreateDiffer( $name, $failureReason ); |
| 197 |
if ( $differ ) { |
| 198 |
$this->injectDeps( $differ ); |
| 199 |
$differs[] = $differ; |
| 200 |
} |
| 201 |
} |
| 202 |
if ( !$differs ) { |
| 203 |
throw new UnexpectedValueException( |
| 204 |
"Cannot use diff engine '{$this->diffEngine}': $failureReason" ); |
| 205 |
} |
| 206 |
|
| 207 |
$this->differs = $differs; |
| 208 |
} |
| 209 |
return $this->differs; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public function injectDeps( TextDiffer $differ ) { |
| 219 |
if ( $differ instanceof BaseTextDiffer ) { |
| 220 |
$differ->setLocalizer( $this->localizer ); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private function maybeCreateDiffer( $engine, &$failureReason ) { |
| 233 |
switch ( $engine ) { |
| 234 |
case 'external': |
| 235 |
if ( is_string( $this->externalPath ) ) { |
| 236 |
if ( is_executable( $this->externalPath ) ) { |
| 237 |
return new ExternalTextDiffer( |
| 238 |
$this->externalPath |
| 239 |
); |
| 240 |
} |
| 241 |
$failureReason = 'ExternalDiffEngine config points to a non-executable'; |
| 242 |
} elseif ( $this->externalPath ) { |
| 243 |
$failureReason = 'ExternalDiffEngine config is set to a non-string value'; |
| 244 |
} else { |
| 245 |
return null; |
| 246 |
} |
| 247 |
wfWarn( "$failureReason, ignoring" ); |
| 248 |
return null; |
| 249 |
|
| 250 |
case 'wikidiff2': |
| 251 |
if ( Wikidiff2TextDiffer::isInstalled() ) { |
| 252 |
return new Wikidiff2TextDiffer( |
| 253 |
$this->wikidiff2Options |
| 254 |
); |
| 255 |
} |
| 256 |
$failureReason = 'wikidiff2 is not available'; |
| 257 |
return null; |
| 258 |
|
| 259 |
case 'php': |
| 260 |
|
| 261 |
return new PhpTextDiffer( |
| 262 |
$this->contentLanguage |
| 263 |
); |
| 264 |
|
| 265 |
default: |
| 266 |
throw new DomainException( 'Invalid value for $wgDiffEngine: ' . $engine ); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
private function splitBatchByDiffer( $formats ) { |
| 280 |
$result = []; |
| 281 |
foreach ( $formats as $format ) { |
| 282 |
$differ = $this->getDifferForFormat( $format ); |
| 283 |
$name = $differ->getName(); |
| 284 |
if ( isset( $result[$name] ) ) { |
| 285 |
$result[$name][1][] = $format; |
| 286 |
} else { |
| 287 |
$result[$name] = [ $differ, [ $format ] ]; |
| 288 |
} |
| 289 |
} |
| 290 |
return array_values( $result ); |
| 291 |
} |
| 292 |
} |