summaryrefslogtreecommitdiff
blob: 11699a50c0d3848b02fe8fd13f129b533f99f046 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/**
 * @phan-file-suppress PhanTypeArraySuspiciousNullable Some confusion with class members
 */
class AbuseFilterViewDiff extends AbuseFilterView {
	/**
	 * @var (string|array)[]|null The old version of the filter
	 */
	public $mOldVersion = null;
	/**
	 * @var (string|array)[]|null The new version of the filter
	 */
	public $mNewVersion = null;
	/**
	 * @var int|null The history ID of the next version, if any
	 */
	public $mNextHistoryId = null;
	/**
	 * @var int|null The ID of the filter
	 */
	public $mFilter = null;

	/**
	 * Shows the page
	 */
	public function show() {
		$show = $this->loadData();
		$out = $this->getOutput();
		$out->enableOOUI();
		$out->addModuleStyles( [ 'oojs-ui.styles.icons-movement' ] );

		$links = [];
		if ( $this->mFilter ) {
			$links['abusefilter-history-backedit'] =
				$this->getTitle( $this->mFilter )->getFullURL();
			$links['abusefilter-diff-backhistory'] =
				$this->getTitle( 'history/' . $this->mFilter )->getFullURL();
		}

		foreach ( $links as $msg => $href ) {
			$links[$msg] =
				new OOUI\ButtonWidget( [
					'label' => $this->msg( $msg )->text(),
					'href' => $href
				] );
		}

		$backlinks =
			new OOUI\HorizontalLayout( [
				'items' => $links
			] );
		$out->addHTML( $backlinks );

		if ( $show ) {
			$out->addHTML( $this->formatDiff() );
			// Next and previous change links
			$buttons = [];
			if ( AbuseFilter::getFirstFilterChange( $this->mFilter ) !=
				$this->mOldVersion['meta']['history_id']
			) {
				// Create a "previous change" link if this isn't the first change of the given filter
				$href = $this->getTitle(
					'history/' . $this->mFilter . '/diff/prev/' . $this->mOldVersion['meta']['history_id']
				)->getFullURL();
				$buttons[] = new OOUI\ButtonWidget( [
					'label' => $this->msg( 'abusefilter-diff-prev' )->text(),
					'href' => $href,
					'icon' => 'previous'
				] );
			}

			if ( $this->mNextHistoryId !== null ) {
				// Create a "next change" link if this isn't the last change of the given filter
				$href = $this->getTitle(
					'history/' . $this->mFilter . '/diff/prev/' . $this->mNextHistoryId
				)->getFullURL();
				$buttons[] = new OOUI\ButtonWidget( [
					'label' => $this->msg( 'abusefilter-diff-next' )->text(),
					'href' => $href,
					'icon' => 'next'
				] );
			}

			if ( count( $buttons ) > 0 ) {
				$buttons = new OOUI\HorizontalLayout( [
					'items' => $buttons,
					'classes' => [ 'mw-abusefilter-history-buttons' ]
				] );
				$out->addHTML( $buttons );
			}
		}
	}

	/**
	 * @return bool
	 */
	public function loadData() {
		$oldSpec = $this->mParams[3];
		$newSpec = $this->mParams[4];
		$this->mFilter = $this->mParams[1];

		if ( !is_numeric( $this->mFilter ) ) {
			$this->getOutput()->addWikiMsg( 'abusefilter-diff-invalid' );
			return false;
		}

		$this->mOldVersion = $this->loadSpec( $oldSpec, $newSpec );
		$this->mNewVersion = $this->loadSpec( $newSpec, $oldSpec );

		if ( $this->mOldVersion === null || $this->mNewVersion === null ) {
			$this->getOutput()->addWikiMsg( 'abusefilter-diff-invalid' );
			return false;
		}

		if ( !AbuseFilter::canViewPrivate( $this->getUser() ) &&
			(
				in_array( 'hidden', explode( ',', $this->mOldVersion['info']['flags'] ) ) ||
				in_array( 'hidden', explode( ',', $this->mNewVersion['info']['flags'] ) )
			)
		) {
			$this->getOutput()->addWikiMsg( 'abusefilter-history-error-hidden' );
			return false;
		}

		$this->mNextHistoryId = $this->getNextHistoryId(
			$this->mNewVersion['meta']['history_id']
		);

		return true;
	}

	/**
	 * Get the history ID of the next change
	 *
	 * @param int $historyId History id to find next change of
	 * @return int|null Id of the next change or null if there isn't one
	 */
	public function getNextHistoryId( $historyId ) {
		$dbr = wfGetDB( DB_REPLICA );
		$row = $dbr->selectRow(
			'abuse_filter_history',
			'afh_id',
			[
				'afh_filter' => $this->mFilter,
				'afh_id > ' . $dbr->addQuotes( $historyId ),
			],
			__METHOD__,
			[ 'ORDER BY' => 'afh_timestamp ASC' ]
		);
		if ( $row ) {
			return $row->afh_id;
		}
		return null;
	}

	/**
	 * @param string $spec
	 * @param string $otherSpec
	 * @return (string|array)[]|null
	 */
	public function loadSpec( $spec, $otherSpec ) {
		static $dependentSpecs = [ 'prev', 'next' ];
		static $cache = [];

		if ( isset( $cache[$spec] ) ) {
			return $cache[$spec];
		}

		$dbr = wfGetDB( DB_REPLICA );
		// All but afh_filter, afh_deleted and afh_changed_fields
		$selectFields = [
			'afh_id',
			'afh_user',
			'afh_user_text',
			'afh_timestamp',
			'afh_pattern',
			'afh_comments',
			'afh_flags',
			'afh_public_comments',
			'afh_actions',
			'afh_group',
		];
		$row = null;
		if ( is_numeric( $spec ) ) {
			$row = $dbr->selectRow(
				'abuse_filter_history',
				$selectFields,
				[ 'afh_id' => $spec, 'afh_filter' => $this->mFilter ],
				__METHOD__
			);
		} elseif ( $spec === 'cur' ) {
			$row = $dbr->selectRow(
				'abuse_filter_history',
				$selectFields,
				[ 'afh_filter' => $this->mFilter ],
				__METHOD__,
				[ 'ORDER BY' => 'afh_timestamp desc' ]
			);
		} elseif ( ( $spec === 'prev' || $spec === 'next' ) &&
			!in_array( $otherSpec, $dependentSpecs )
		) {
			// cached
			$other = $this->loadSpec( $otherSpec, $spec );

			$comparison = $spec === 'prev' ? '<' : '>';
			$order = $spec === 'prev' ? 'DESC' : 'ASC';
			$row = $dbr->selectRow(
				'abuse_filter_history',
				$selectFields,
				[
					'afh_filter' => $this->mFilter,
					"afh_id $comparison" . $dbr->addQuotes( $other['meta']['history_id'] ),
				],
				__METHOD__,
				[ 'ORDER BY' => "afh_timestamp $order" ]
			);

			if ( $other && !$row ) {
				$t = $this->getTitle(
					'history/' . $this->mFilter . '/item/' . $other['meta']['history_id'] );
				$this->getOutput()->redirect( $t->getFullURL() );
				return null;
			}
		}

		if ( !$row ) {
			return null;
		}

		$data = $this->loadFromHistoryRow( $row );
		$cache[$spec] = $data;
		return $data;
	}

	/**
	 * @param stdClass $row
	 * @return (string|array)[]
	 */
	public function loadFromHistoryRow( $row ) {
		return [
			'meta' => [
				'history_id' => $row->afh_id,
				'modified_by' => $row->afh_user,
				'modified_by_text' => $row->afh_user_text,
				'modified' => $row->afh_timestamp,
			],
			'info' => [
				'description' => $row->afh_public_comments,
				'flags' => $row->afh_flags,
				'notes' => $row->afh_comments,
				'group' => $row->afh_group,
			],
			'pattern' => $row->afh_pattern,
			'actions' => unserialize( $row->afh_actions ),
		];
	}

	/**
	 * @param string $timestamp
	 * @param int $history_id
	 * @return string
	 */
	public function formatVersionLink( $timestamp, $history_id ) {
		$filter = $this->mFilter;
		$text = $this->getLanguage()->timeanddate( $timestamp, true );
		$title = $this->getTitle( "history/$filter/item/$history_id" );

		$link = $this->linkRenderer->makeLink( $title, $text );

		return $link;
	}

	/**
	 * @return string
	 */
	public function formatDiff() {
		$oldVersion = $this->mOldVersion;
		$newVersion = $this->mNewVersion;

		// headings
		$oldLink = $this->formatVersionLink(
			$oldVersion['meta']['modified'],
			$oldVersion['meta']['history_id']
		);
		$newLink = $this->formatVersionLink(
			$newVersion['meta']['modified'],
			$newVersion['meta']['history_id']
		);

		$oldUserLink = Linker::userLink(
			$oldVersion['meta']['modified_by'],
			$oldVersion['meta']['modified_by_text']
		);
		$newUserLink = Linker::userLink(
			$newVersion['meta']['modified_by'],
			$newVersion['meta']['modified_by_text']
		);

		$headings = '';
		$headings .= Xml::tags( 'th', null,
			$this->msg( 'abusefilter-diff-item' )->parse() );
		$headings .= Xml::tags( 'th', null,
			$this->msg( 'abusefilter-diff-version' )
				->rawParams( $oldLink, $oldUserLink )
				->params( $newVersion['meta']['modified_by_text'] )
				->parse()
		);
		$headings .= Xml::tags( 'th', null,
			$this->msg( 'abusefilter-diff-version' )
				->rawParams( $newLink, $newUserLink )
				->params( $newVersion['meta']['modified_by_text'] )
				->parse()
		);

		$headings = Xml::tags( 'tr', null, $headings );

		$body = '';
		// Basic info
		$infoHeader = $this->getHeaderRow( 'abusefilter-diff-info' );
		$info = '';
		$info .= $this->getDiffRow(
			'abusefilter-edit-description',
			$oldVersion['info']['description'],
			$newVersion['info']['description']
		);
		if (
			count( $this->getConfig()->get( 'AbuseFilterValidGroups' ) ) > 1 ||
			$oldVersion['info']['group'] !== $newVersion['info']['group']
		) {
			$info .= $this->getDiffRow(
				'abusefilter-edit-group',
				AbuseFilter::nameGroup( $oldVersion['info']['group'] ),
				AbuseFilter::nameGroup( $newVersion['info']['group'] )
			);
		}
		$info .= $this->getDiffRow(
			'abusefilter-edit-flags',
			AbuseFilter::formatFlags( $oldVersion['info']['flags'], $this->getLanguage() ),
			AbuseFilter::formatFlags( $newVersion['info']['flags'], $this->getLanguage() )
		);

		$info .= $this->getDiffRow(
			'abusefilter-edit-notes',
			$oldVersion['info']['notes'],
			$newVersion['info']['notes']
		);

		if ( $info !== '' ) {
			$body .= $infoHeader . $info;
		}

		$patternHeader = $this->getHeaderRow( 'abusefilter-diff-pattern' );
		$pattern = '';
		$pattern .= $this->getDiffRow(
			'abusefilter-edit-rules',
			$oldVersion['pattern'],
			$newVersion['pattern']
		);

		if ( $pattern !== '' ) {
			$body .= $patternHeader . $pattern;
		}

		$actionsHeader = $this->getHeaderRow( 'abusefilter-edit-consequences' );
		$actions = '';

		$oldActions = $this->stringifyActions( $oldVersion['actions'] );
		$newActions = $this->stringifyActions( $newVersion['actions'] );

		$actions .= $this->getDiffRow(
			'abusefilter-edit-consequences',
			$oldActions,
			$newActions
		);

		if ( $actions !== '' ) {
			$body .= $actionsHeader . $actions;
		}

		$html = "<table class='wikitable'>
			<thead>$headings</thead>
			<tbody>$body</tbody>
		</table>";

		$html = Xml::tags( 'h2', null, $this->msg( 'abusefilter-diff-title' )->parse() ) . $html;

		return $html;
	}

	/**
	 * @param array $actions
	 * @return array
	 */
	public function stringifyActions( $actions ) {
		$lines = [];

		ksort( $actions );
		foreach ( $actions as $action => $parameters ) {
			$lines[] = AbuseFilter::formatAction( $action, $parameters, $this->getLanguage() );
		}

		if ( !count( $lines ) ) {
			$lines[] = '';
		}

		return $lines;
	}

	/**
	 * @param string $msg
	 * @return string
	 */
	public function getHeaderRow( $msg ) {
		$html = $this->msg( $msg )->parse();
		$html = Xml::tags( 'th', [ 'colspan' => 3 ], $html );
		$html = Xml::tags( 'tr', [ 'class' => 'mw-abusefilter-diff-header' ], $html );

		return $html;
	}

	/**
	 * @param string $msg
	 * @param array|string $old
	 * @param array|string $new
	 * @return string
	 */
	public function getDiffRow( $msg, $old, $new ) {
		if ( !is_array( $old ) ) {
			$old = explode( "\n", preg_replace( "/\\\r\\\n?/", "\n", $old ) );
		}
		if ( !is_array( $new ) ) {
			$new = explode( "\n", preg_replace( "/\\\r\\\n?/", "\n", $new ) );
		}

		if ( $old === $new ) {
			return '';
		}

		$diffEngine = new DifferenceEngine( $this->getContext() );

		$diffEngine->showDiffStyle();

		$diff = new Diff( $old, $new );
		$formatter = new TableDiffFormatterFullContext();
		$formattedDiff = $diffEngine->addHeader( $formatter->format( $diff ), '', '' );

		return Xml::tags( 'tr', null,
			Xml::tags( 'th', null, $this->msg( $msg )->parse() ) .
			Xml::tags( 'td', [ 'colspan' => 2 ], $formattedDiff )
		) . "\n";
	}
}