summaryrefslogtreecommitdiff
blob: 0670ef450a503be85e51db3924580c77bc0da19d (plain)
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
<?php
/**
 * MediaWiki Extension: Echo
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * This program is distributed WITHOUT ANY WARRANTY.
 */

/**
 * @file
 * @ingroup Extensions
 * @author Erik Bernhardson
 */

/**
 * Calculates the individual sets of differences between two pieces of text
 * as individual groupings of add, subtract, and change actions. Internally
 * uses 0-indexing for positions.  All results from the class are 1 indexed
 * to stay consistent with the original diff output and the previous diff
 * parsing code.
 */
class EchoDiffParser {

	/**
	 * @var int $prefixLength The number of characters the diff prefixes a line with
	 */
	protected $prefixLength = 1;

	/**
	 * @var string[] $left The text of the left side of the diff operation
	 */
	protected $left;

	/**
	 * @var int $leftPos The current position within the left text
	 */
	protected $leftPos;

	/**
	 * @var string[] $right The text of the right side of the diff operation
	 */
	protected $right;

	/**
	 * @var int $rightPos The current position within the right text
	 */
	protected $rightPos;

	/**
	 * @var array[] $changeSet Set of add, subtract, or change operations within the diff
	 */
	protected $changeSet;

	/**
	 * Get the set of add, subtract, and change operations required to transform leftText into rightText
	 *
	 * @param string $leftText The left, or old, revision of the text
	 * @param string $rightText The right, or new, revision of the text
	 * @return array[] Array of arrays containing changes to individual groups of lines within the text
	 * Each change consists of:
	 * An 'action', one of:
	 * - add
	 * - subtract
	 * - change
	 * 'content' that was added or removed, or in the case
	 *     of a change, 'old_content' and 'new_content'
	 * 'left_pos' and 'right_pos' (in 1-indexed lines) of the change.
	 */
	public function getChangeSet( $leftText, $rightText ) {
		$left = trim( $leftText );
		$right = trim( $rightText );

		if ( $left === '' ) {
			// fixes T155998
			return $this->getChangeSetFromEmptyLeft( $right );
		}

		$diffs = new Diff( explode( "\n", $left ), explode( "\n", $right ) );
		$format = new UnifiedDiffFormatter();
		$diff = $format->format( $diffs );

		return $this->parse( $diff, $left, $right );
	}

	/**
	 * If we add content to an empty page the changeSet can be composed straightaway
	 *
	 * @param string $right
	 * @return array[] See {@see getChangeSet}
	 */
	private function getChangeSetFromEmptyLeft( $right ) {
		$rightLines = explode( "\n", $right );

		return [
			'_info' => [
				'lhs-length' => 1,
				'rhs-length' => count( $rightLines ),
				'lhs' => [ '' ],
				'rhs' => $rightLines
			],
			[
				'right-pos' => 1,
				'left-pos' => 1,
				'action' => 'add',
				'content' => $right,
			]
		];
	}

	/**
	 * Duplicates the check from the global wfDiff function to determine
	 * if we are using internal or external diff utilities
	 *
	 * @deprecated since 1.29, the internal diff parser is always used
	 * @return bool
	 */
	protected static function usingInternalDiff() {
		return true;
	}

	/**
	 * Parse the unified diff output into an array of changes to individual groups of the text
	 *
	 * @param string $diff The unified diff output
	 * @param string $left The left side of the diff used for sanity checks
	 * @param string $right The right side of the diff used for sanity checks
	 *
	 * @return array[]
	 */
	protected function parse( $diff, $left, $right ) {
		$this->left = explode( "\n", $left );
		$this->right = explode( "\n", $right );
		$diff = explode( "\n", $diff );

		$this->leftPos = 0;
		$this->rightPos = 0;
		$this->changeSet = [
			'_info' => [
				'lhs-length' => count( $this->left ),
				'rhs-length' => count( $this->right ),
				'lhs' => $this->left,
				'rhs' => $this->right,
			],
		];

		$change = null;
		foreach ( $diff as $line ) {
			$change = $this->parseLine( $line, $change );
		}
		if ( $change === null ) {
			return $this->changeSet;
		} else {
			return array_merge( $this->changeSet, $change->getChangeSet() );
		}
	}

	/**
	 * Parse the next line of the unified diff output
	 *
	 * @param string $line The next line of the unified diff
	 * @param EchoDiffGroup|null $change Changes the immediately previous lines
	 *
	 * @throws MWException
	 * @return EchoDiffGroup Changes to this line and any changed lines immediately previous
	 */
	protected function parseLine( $line, EchoDiffGroup $change = null ) {
		if ( $line ) {
			$op = $line[0];
			if ( strlen( $line ) > $this->prefixLength ) {
				$line = substr( $line, $this->prefixLength );
			} else {
				$line = '';
			}
		} else {
			$op = ' ';
		}

		switch ( $op ) {
			case '@': // metadata
				if ( $change !== null ) {
					$this->changeSet = array_merge( $this->changeSet, $change->getChangeSet() );
					$change = null;
				}
				// @@ -start,numLines +start,numLines @@
				list( , $left, $right ) = explode( ' ', $line );
				list( $this->leftPos ) = explode( ',', substr( $left, 1 ) );
				list( $this->rightPos ) = explode( ',', substr( $right, 1 ) );

				// -1 because diff is 1 indexed and we are 0 indexed
				$this->leftPos--;
				$this->rightPos--;
				break;

			case ' ': // No changes
				if ( $change !== null ) {
					$this->changeSet = array_merge( $this->changeSet, $change->getChangeSet() );
					$change = null;
				}
				$this->leftPos++;
				$this->rightPos++;
				break;

			case '-': // subtract
				if ( $this->left[$this->leftPos] !== $line ) {
					throw new MWException( 'Positional error: left' );
				}
				if ( $change === null ) {
					$change = new EchoDiffGroup( $this->leftPos, $this->rightPos );
				}
				$change->subtract( $line );
				$this->leftPos++;
				break;

			case '+': // add
				if ( $this->right[$this->rightPos] !== $line ) {
					throw new MWException( 'Positional error: right' );
				}
				if ( $change === null ) {
					$change = new EchoDiffGroup( $this->leftPos, $this->rightPos );
				}
				$change->add( $line );
				$this->rightPos++;
				break;

			default:
				throw new MWException( 'Unknown Diff Operation: ' . $op );
		}

		return $change;
	}
}