Debian Codegraph @

includes/diff/TextDiffer/ManifoldTextDiffer.php

raw symbols SCIP
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 * A TextDiffer which acts as a container for other TextDiffers, and dispatches
14 * requests to them.
15 *
16 * @since 1.41
17 */
18 class ManifoldTextDiffer implements TextDiffer {
19 /** @var MessageLocalizer */
20 private $localizer;
21 /** @var Language|null */
22 private $contentLanguage;
23 /** @var string|null */
24 private $diffEngine;
25 /** @var string|false|null */
26 private $externalPath;
27 /** @var TextDiffer[]|null Differs in order of priority, from highest to lowest */
28 private $differs;
29 /** @var TextDiffer[]|null The differ to use for each format */
30 private $differsByFormat;
31 /** @var array */
32 private $wikidiff2Options;
33
34 /**
35 * @internal For use by DifferenceEngine, ContentHandler
36 *
37 * @param MessageLocalizer $localizer
38 * @param Language|null $contentLanguage
39 * @param string|null $diffEngine The DiffEngine config variable
40 * @param string|false|null $externalPath The ExternalDiffEngine config variable
41 * @param array $wikidiff2Options The Wikidiff2Options config variable
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 /** @var TextDiffer $differ */
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 /** @var TextDiffer $differ */
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 * @return TextDiffer[]
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 // getDiffers() is in order of priority -- don't overwrite
136 $differs[$format] ??= $differ;
137 }
138 }
139 $this->differsByFormat = $differs;
140 }
141 return $this->differsByFormat;
142 }
143
144 /**
145 * @param string $format
146 * @return TextDiffer
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 * Disable text differs apart from the one with the given name.
160 *
161 * @param string $name
162 */
163 public function setEngine( string $name ) {
164 $this->diffEngine = $name;
165 $this->differs = null;
166 $this->differsByFormat = null;
167 }
168
169 /**
170 * Get the text differ name which will be used for the specified format
171 *
172 * @param string $format
173 * @return string|null
174 */
175 public function getEngineForFormat( string $format ) {
176 return $this->getDifferForFormat( $format )->getName();
177 }
178
179 /**
180 * Get differs in a numerically indexed array. When a format is requested,
181 * the first TextDiffer in this array which can handle the format will be
182 * used.
183 *
184 * @return TextDiffer[]
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 // TODO: add a hook here, allowing extensions to add differs
207 $this->differs = $differs;
208 }
209 return $this->differs;
210 }
211
212 /**
213 * Initialize an object which may be a subclass of BaseTextDiffer, passing
214 * down injected dependencies.
215 *
216 * @param TextDiffer $differ
217 */
218 public function injectDeps( TextDiffer $differ ) {
219 if ( $differ instanceof BaseTextDiffer ) {
220 $differ->setLocalizer( $this->localizer );
221 }
222 }
223
224 /**
225 * Create a TextDiffer by engine name. If it can't be created due to a
226 * configuration or platform issue, return null and set $failureReason.
227 *
228 * @param string $engine
229 * @param string &$failureReason Out param which will be set to the failure reason
230 * @return TextDiffer|null
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 // Always available.
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 * Given an array of formats, break it down by the TextDiffer object which
272 * will handle each format. Each element of the result array is a list in
273 * which the first element is the TextDiffer object, and the second element
274 * is the list of formats which the TextDiffer will handle.
275 *
276 * @param array $formats
277 * @return array|array{0:TextDiffer,1:string[]}
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 }