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
|
<?php
namespace MediaWiki\Diff\TextDiffer;
use MediaWiki\Html\Html;
use TextSlotDiffRenderer;
/**
* @since 1.41
*/
class Wikidiff2TextDiffer extends BaseTextDiffer {
/** @var string */
private $version;
/** @var bool */
private $haveMoveSupport;
/** @var bool */
private $haveMultiFormatSupport;
/** @var bool */
private $haveCutoffParameter;
/** @var bool */
private $useMultiFormat;
/** @var array */
private $defaultOptions;
/** @var array[] */
private $formatOptions;
/** @var string */
private $optionsHash;
private const OPT_NAMES = [
'numContextLines',
'changeThreshold',
'movedLineThreshold',
'maxMovedLines',
'maxWordLevelDiffComplexity',
'maxSplitSize',
'initialSplitThreshold',
'finalSplitThreshold',
];
/**
* Fake wikidiff2 extension version for PHPUnit testing
* @var string|null
*/
public static $fakeVersionForTesting = null;
/**
* Determine whether the extension is installed (or mocked for testing)
*
* @return bool
*/
public static function isInstalled() {
return self::$fakeVersionForTesting !== null
|| function_exists( 'wikidiff2_do_diff' );
}
/**
* @param array $options
*/
public function __construct( $options ) {
$this->version = self::$fakeVersionForTesting ?? phpversion( 'wikidiff2' );
$this->haveMoveSupport = version_compare( $this->version, '1.5.0', '>=' );
$this->haveMultiFormatSupport = version_compare( $this->version, '1.14.0', '>=' );
$this->haveCutoffParameter = $this->haveMoveSupport
&& version_compare( $this->version, '1.8.0', '<' );
$this->useMultiFormat = $this->haveMultiFormatSupport && !empty( $options['useMultiFormat'] );
$validOpts = array_fill_keys( self::OPT_NAMES, true );
$this->defaultOptions = array_intersect_key( $options, $validOpts );
$this->formatOptions = [];
foreach ( $options['formatOptions'] ?? [] as $format => $formatOptions ) {
$this->formatOptions[$format] = array_intersect_key( $formatOptions, $validOpts );
}
}
public function getName(): string {
return 'wikidiff2';
}
public function getFormatContext( string $format ) {
return $format === 'inline' ? self::CONTEXT_PLAIN : self::CONTEXT_ROW;
}
public function getCacheKeys( array $formats ): array {
return [
'20-wikidiff2-version' => $this->version,
'21-wikidiff2-options' => $this->getOptionsHash(),
];
}
/**
* Get a hash of the cache-varying constructor options
*
* @return string
*/
private function getOptionsHash() {
if ( $this->optionsHash === null ) {
$json = json_encode(
[
$this->useMultiFormat,
$this->defaultOptions,
$this->formatOptions,
],
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
);
$this->optionsHash = substr( md5( $json ), 0, 8 );
}
return $this->optionsHash;
}
public function doRenderBatch( string $oldText, string $newText, array $formats ): array {
if ( $this->useMultiFormat ) {
if ( !$this->formatOptions ) {
/** @var array $result */
$result = wikidiff2_multi_format_diff(
$oldText,
$newText,
[ 'formats' => $formats ] + $this->defaultOptions
);
} else {
$result = [];
foreach ( $formats as $format ) {
$result[$format] = wikidiff2_multi_format_diff(
$oldText,
$newText,
[ 'formats' => $formats ]
+ ( $this->formatOptions[$format] ?? [] )
+ $this->defaultOptions
);
}
}
} else {
$result = [];
foreach ( $formats as $format ) {
switch ( $format ) {
case 'table':
$result['table'] = $this->doTableFormat( $oldText, $newText );
break;
case 'inline':
$result['inline'] = $this->doInlineFormat( $oldText, $newText );
break;
}
}
}
return $result;
}
/**
* Do a table format diff
*
* @param string $old
* @param string $new
* @return string
*/
private function doTableFormat( $old, $new ) {
if ( $this->haveCutoffParameter ) {
return wikidiff2_do_diff(
$old,
$new,
2,
0
);
} else {
// Don't pass the 4th parameter introduced in version 1.5.0 and removed in version 1.8.0
return wikidiff2_do_diff(
$old,
$new,
2
);
}
}
/**
* Do an inline format diff
*
* @param string $oldText
* @param string $newText
* @return string
*/
private function doInlineFormat( $oldText, $newText ) {
return wikidiff2_inline_diff( $oldText, $newText, 2 );
}
public function getFormats(): array {
return [ 'table', 'inline' ];
}
public function getTablePrefixes( string $format ): array {
$localizer = $this->getLocalizer();
$ins = Html::element( 'span',
[ 'class' => 'mw-diff-inline-legend-ins' ],
$localizer->msg( 'diff-inline-tooltip-ins' )->plain()
);
$del = Html::element( 'span',
[ 'class' => 'mw-diff-inline-legend-del' ],
$localizer->msg( 'diff-inline-tooltip-del' )->plain()
);
$hideDiffClass = $format === 'inline' ? '' : 'oo-ui-element-hidden';
$legend = Html::rawElement( 'div',
[ 'class' => 'mw-diff-inline-legend ' . $hideDiffClass ], "$ins $del"
);
return [ TextSlotDiffRenderer::INLINE_LEGEND_KEY => $legend ];
}
public function localize( string $format, string $diff, array $options = [] ): string {
$diff = $this->localizeLineNumbers( $diff,
$options['reducedLineNumbers'] ?? false
);
if ( $this->haveMoveSupport ) {
$diff = $this->addLocalizedTitleTooltips( $format, $diff );
}
return $diff;
}
/**
* Add title attributes for tooltips on various diff elements
*
* @param string $format
* @param string $text
* @return string
*/
private function addLocalizedTitleTooltips( $format, $text ) {
// Moved paragraph indicators.
$localizer = $this->getLocalizer();
$replacements = [
'class="mw-diff-movedpara-right"' =>
'class="mw-diff-movedpara-right" title="' .
$localizer->msg( 'diff-paragraph-moved-toold' )->escaped() . '"',
'class="mw-diff-movedpara-left"' =>
'class="mw-diff-movedpara-left" title="' .
$localizer->msg( 'diff-paragraph-moved-tonew' )->escaped() . '"',
];
// For inline diffs, add tooltips to `<ins>` and `<del>`.
if ( $format == 'inline' ) {
$replacements['<ins>'] = Html::openElement( 'ins',
[ 'title' => $localizer->msg( 'diff-inline-tooltip-ins' )->plain() ] );
$replacements['<del>'] = Html::openElement( 'del',
[ 'title' => $localizer->msg( 'diff-inline-tooltip-del' )->plain() ] );
}
return strtr( $text, $replacements );
}
public function getPreferredFormatBatch( string $format ): array {
if ( $this->formatOptions ) {
return [ $format ];
} else {
return [ 'table', 'inline' ];
}
}
}
|