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