Debian Codegraph @

includes/diff/TextDiffer/Wikidiff2TextDiffer.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Diff\TextDiffer;
4
5 use MediaWiki\Html\Html;
6 use TextSlotDiffRenderer;
7
8 /**
9 * @since 1.41
10 */
11 class Wikidiff2TextDiffer extends BaseTextDiffer {
12 /** @var string */
13 private $version;
14 /** @var bool */
15 private $haveMoveSupport;
16 /** @var bool */
17 private $haveMultiFormatSupport;
18 /** @var bool */
19 private $haveCutoffParameter;
20 /** @var bool */
21 private $useMultiFormat;
22 /** @var array */
23 private $defaultOptions;
24 /** @var array[] */
25 private $formatOptions;
26 /** @var string */
27 private $optionsHash;
28
29 private const OPT_NAMES = [
30 'numContextLines',
31 'changeThreshold',
32 'movedLineThreshold',
33 'maxMovedLines',
34 'maxWordLevelDiffComplexity',
35 'maxSplitSize',
36 'initialSplitThreshold',
37 'finalSplitThreshold',
38 ];
39
40 /**
41 * Fake wikidiff2 extension version for PHPUnit testing
42 * @var string|null
43 */
44 public static $fakeVersionForTesting = null;
45
46 /**
47 * Determine whether the extension is installed (or mocked for testing)
48 *
49 * @return bool
50 */
51 public static function isInstalled() {
52 return self::$fakeVersionForTesting !== null
53 || function_exists( 'wikidiff2_do_diff' );
54 }
55
56 /**
57 * @param array $options
58 */
59 public function __construct( $options ) {
60 $this->version = self::$fakeVersionForTesting ?? phpversion( 'wikidiff2' );
61 $this->haveMoveSupport = version_compare( $this->version, '1.5.0', '>=' );
62 $this->haveMultiFormatSupport = version_compare( $this->version, '1.14.0', '>=' );
63 $this->haveCutoffParameter = $this->haveMoveSupport
64 && version_compare( $this->version, '1.8.0', '<' );
65
66 $this->useMultiFormat = $this->haveMultiFormatSupport && !empty( $options['useMultiFormat'] );
67 $validOpts = array_fill_keys( self::OPT_NAMES, true );
68 $this->defaultOptions = array_intersect_key( $options, $validOpts );
69 $this->formatOptions = [];
70 foreach ( $options['formatOptions'] ?? [] as $format => $formatOptions ) {
71 $this->formatOptions[$format] = array_intersect_key( $formatOptions, $validOpts );
72 }
73 }
74
75 public function getName(): string {
76 return 'wikidiff2';
77 }
78
79 public function getFormatContext( string $format ) {
80 return $format === 'inline' ? self::CONTEXT_PLAIN : self::CONTEXT_ROW;
81 }
82
83 public function getCacheKeys( array $formats ): array {
84 return [
85 '20-wikidiff2-version' => $this->version,
86 '21-wikidiff2-options' => $this->getOptionsHash(),
87 ];
88 }
89
90 /**
91 * Get a hash of the cache-varying constructor options
92 *
93 * @return string
94 */
95 private function getOptionsHash() {
96 if ( $this->optionsHash === null ) {
97 $json = json_encode(
98 [
99 $this->useMultiFormat,
100 $this->defaultOptions,
101 $this->formatOptions,
102 ],
103 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
104 );
105 $this->optionsHash = substr( md5( $json ), 0, 8 );
106 }
107 return $this->optionsHash;
108 }
109
110 public function doRenderBatch( string $oldText, string $newText, array $formats ): array {
111 if ( $this->useMultiFormat ) {
112 if ( !$this->formatOptions ) {
113 /** @var array $result */
114 $result = wikidiff2_multi_format_diff(
115 $oldText,
116 $newText,
117 [ 'formats' => $formats ] + $this->defaultOptions
118 );
119 } else {
120 $result = [];
121 foreach ( $formats as $format ) {
122 $result[$format] = wikidiff2_multi_format_diff(
123 $oldText,
124 $newText,
125 [ 'formats' => $formats ]
126 + ( $this->formatOptions[$format] ?? [] )
127 + $this->defaultOptions
128 );
129 }
130 }
131 } else {
132 $result = [];
133 foreach ( $formats as $format ) {
134 switch ( $format ) {
135 case 'table':
136 $result['table'] = $this->doTableFormat( $oldText, $newText );
137 break;
138
139 case 'inline':
140 $result['inline'] = $this->doInlineFormat( $oldText, $newText );
141 break;
142 }
143 }
144 }
145 return $result;
146 }
147
148 /**
149 * Do a table format diff
150 *
151 * @param string $old
152 * @param string $new
153 * @return string
154 * @suppress PhanParamTooManyInternal,UnusedSuppression Spurious Phan warning with wikidiff <1.5.0 or >=1.8.0
155 */
156 private function doTableFormat( $old, $new ) {
157 if ( $this->haveCutoffParameter ) {
158 return wikidiff2_do_diff(
159 $old,
160 $new,
161 2,
162 0
163 );
164 } else {
165 // Don't pass the 4th parameter introduced in version 1.5.0 and removed in version 1.8.0
166 return wikidiff2_do_diff(
167 $old,
168 $new,
169 2
170 );
171 }
172 }
173
174 /**
175 * Do an inline format diff
176 *
177 * @param string $oldText
178 * @param string $newText
179 * @return string
180 */
181 private function doInlineFormat( $oldText, $newText ) {
182 return wikidiff2_inline_diff( $oldText, $newText, 2 );
183 }
184
185 public function getFormats(): array {
186 return [ 'table', 'inline' ];
187 }
188
189 public function getTablePrefixes( string $format ): array {
190 $localizer = $this->getLocalizer();
191 $ins = Html::element( 'span',
192 [ 'class' => 'mw-diff-inline-legend-ins' ],
193 $localizer->msg( 'diff-inline-tooltip-ins' )->plain()
194 );
195 $del = Html::element( 'span',
196 [ 'class' => 'mw-diff-inline-legend-del' ],
197 $localizer->msg( 'diff-inline-tooltip-del' )->plain()
198 );
199 $hideDiffClass = $format === 'inline' ? '' : 'oo-ui-element-hidden';
200 $legend = Html::rawElement( 'div',
201 [ 'class' => 'mw-diff-inline-legend ' . $hideDiffClass ], "$del $ins"
202 );
203 return [ TextSlotDiffRenderer::INLINE_LEGEND_KEY => $legend ];
204 }
205
206 public function localize( string $format, string $diff, array $options = [] ): string {
207 $diff = $this->localizeLineNumbers( $diff,
208 $options['reducedLineNumbers'] ?? false
209 );
210 if ( $this->haveMoveSupport ) {
211 $diff = $this->addLocalizedTitleTooltips( $format, $diff );
212 }
213 return $diff;
214 }
215
216 /**
217 * Add title attributes for tooltips on various diff elements
218 *
219 * @param string $format
220 * @param string $text
221 * @return string
222 */
223 private function addLocalizedTitleTooltips( $format, $text ) {
224 // Moved paragraph indicators.
225 $localizer = $this->getLocalizer();
226 $replacements = [
227 'class="mw-diff-movedpara-right"' =>
228 'class="mw-diff-movedpara-right" title="' .
229 $localizer->msg( 'diff-paragraph-moved-toold' )->escaped() . '"',
230 'class="mw-diff-movedpara-left"' =>
231 'class="mw-diff-movedpara-left" title="' .
232 $localizer->msg( 'diff-paragraph-moved-tonew' )->escaped() . '"',
233 ];
234 // For inline diffs, add tooltips to `<ins>` and `<del>`.
235 if ( $format == 'inline' ) {
236 $replacements['<ins>'] = Html::openElement( 'ins',
237 [ 'title' => $localizer->msg( 'diff-inline-tooltip-ins' )->plain() ] );
238 $replacements['<del>'] = Html::openElement( 'del',
239 [ 'title' => $localizer->msg( 'diff-inline-tooltip-del' )->plain() ] );
240 }
241 return strtr( $text, $replacements );
242 }
243
244 public function getPreferredFormatBatch( string $format ): array {
245 if ( $this->formatOptions ) {
246 return [ $format ];
247 } else {
248 return [ 'table', 'inline' ];
249 }
250 }
251 }