aboutsummaryrefslogtreecommitdiffstats
path: root/includes/diff/TextDiffer/TextDiffer.php
blob: 8046001b13449148c3ebc4665fd968d3a5fb0c99 (plain) (blame)
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
<?php

namespace MediaWiki\Diff\TextDiffer;

use MediaWiki\Output\OutputPage;

/**
 * An interface for parts of a diff page view which represent changes to text
 *
 * @since 1.41
 */
interface TextDiffer {
	/**
	 * The HTML returned is an ordinary block
	 */
	public const CONTEXT_PLAIN = 'plain';

	/**
	 * The HTML returned is zero or more table rows
	 */
	public const CONTEXT_ROW = 'row';

	/**
	 * The return value is plain text and should be wrapped in a <pre>
	 */
	public const CONTEXT_PRE = 'pre';

	/**
	 * Get a stable unique name to identify this differ in a cache key.
	 */
	public function getName(): string;

	/**
	 * Get the supported format names
	 *
	 * @return string[]
	 */
	public function getFormats(): array;

	/**
	 * Determine whether we support the specified format
	 *
	 * @param string $format
	 * @return bool
	 */
	public function hasFormat( string $format ): bool;

	/**
	 * Get the context for a given format. Returns one of the CONTEXT_* constants.
	 *
	 * @param string $format
	 * @return string
	 */
	public function getFormatContext( string $format );

	/**
	 * Make the context consistent by adding a colspan=4 wrapper around plain
	 * HTML output.
	 *
	 * @param string $format
	 * @param string $diffText
	 * @return string
	 */
	public function addRowWrapper( string $format, string $diffText ): string;

	/**
	 * Generate a diff comparing $oldText with $newText.
	 * The result must be passed through localize() before being sent to the user.
	 *
	 * @param string $oldText
	 * @param string $newText
	 * @param string $format
	 * @return string
	 */
	public function render( string $oldText, string $newText, string $format ): string;

	/**
	 * Render a diff in multiple formats.
	 * The results must be passed through localize() before being sent to the user.
	 *
	 * @param string $oldText
	 * @param string $newText
	 * @param string[] $formats
	 * @return array An array with the format in the key and the diff in the value.
	 */
	public function renderBatch( string $oldText, string $newText, array $formats ): array;

	/**
	 * Modify the OutputPage, adding any headers required by the specified format.
	 *
	 * @param OutputPage $out
	 * @param string $format
	 * @return void
	 */
	public function addModules( OutputPage $out, string $format ): void;

	/**
	 * Get additional cache keys required by the specified formats.
	 *
	 * The result should have unique string keys, so that cache keys can be
	 * deduplicated.
	 *
	 * @param string[] $formats
	 * @return array<string,string>
	 */
	public function getCacheKeys( array $formats ): array;

	/**
	 * Expand messages in the diff text using the current MessageLocalizer.
	 *
	 * Perform any other necessary post-cache transformations.
	 *
	 * @param string $format
	 * @param string $diff
	 * @param array $options An associative array of options, may contain:
	 *   - reducedLineNumbers: If true, remove "line 1" but allow other line numbers
	 * @return string
	 */
	public function localize( string $format, string $diff, array $options = [] ): string;

	/**
	 * Get table prefixes for the specified format. These are HTML fragments
	 * placed above all slot diffs. The key should be a string, used for sorting
	 * and deduplication.
	 *
	 * @param string $format
	 * @return array
	 */
	public function getTablePrefixes( string $format ): array;

	/**
	 * Given a format, get a list of formats which can be generated at the same
	 * time with minimal additional CPU cost.
	 *
	 * @param string $format
	 * @return string[]
	 */
	public function getPreferredFormatBatch( string $format ): array;
}