summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/AbuseFilter/modules/ext.abuseFilter.examine.js
blob: e51becf66b32475fa1a3053978956182d106f5a6 (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
/**
 * Check a filter against a change
 *
 * @author John Du Hart
 * @author Marius Hoch <hoo@online.de>
 */

( function ( mw, $ ) {
	'use strict';

	// Syntax result div
	// @type {jQuery}
	var $syntaxResult;

	/**
	 * Processes the results of the filter test
	 *
	 * @param {Object} data
	 */
	function examinerTestProcess( data ) {
		var msg, exClass;
		$.removeSpinner( 'filter-check' );

		if ( data.abusefiltercheckmatch.result ) {
			exClass = 'mw-abusefilter-examine-match';
			msg = 'abusefilter-examine-match';
		} else {
			exClass = 'mw-abusefilter-examine-nomatch';
			msg = 'abusefilter-examine-nomatch';
		}
		$syntaxResult
			.attr( 'class', exClass )
			.text( mw.msg( msg ) )
			.show();
	}

	/**
	 * Processes the results of the filter test in case of an error
	 *
	 * @param {string} error Error code returned from the AJAX request
	 * @param {Object} details Details about the error
	 */
	function examinerTestProcessFailure( error, details ) {
		var msg;
		$.removeSpinner( 'filter-check' );

		if ( error === 'badsyntax' ) {
			$syntaxResult.attr(
				'class', 'mw-abusefilter-syntaxresult-error'
			);
			msg = 'abusefilter-examine-syntaxerror';
		} else if ( error === 'nosuchrcid' || error === 'nosuchlogid' ) {
			msg = 'abusefilter-examine-notfound';
		} else if ( error === 'permissiondenied' ) {
			// The 'abusefilter-modify' right is needed to use this API
			msg = 'abusefilter-mustbeeditor';
		} else if ( error === 'http' ) {
			msg = 'abusefilter-http-error';
		} else {
			msg = 'unknown-error';
		}

		$syntaxResult
			.text( mw.msg( msg, details && details.exception ) )
			.show();
	}

	/**
	 * Tests the filter against an rc event or abuse log entry.
	 *
	 * @context HTMLElement
	 * @param {jQuery.Event} e
	 */
	function examinerTestFilter() {
		var filter = $( '#wpTestFilter' ).val(),
			examine = mw.config.get( 'abuseFilterExamine' ),
			params = {
				action: 'abusefiltercheckmatch',
				filter: filter
			},
			api = new mw.Api();

		$( this ).injectSpinner( { id: 'filter-check', size: 'large' } );

		if ( examine.type === 'rc' ) {
			params.rcid = examine.id;
		} else {
			params.logid = examine.id;
		}

		// Use post due to the rather large amount of data
		api.post( params )
			.done( examinerTestProcess )
			.fail( examinerTestProcessFailure );
	}

	$( document ).ready( function () {
		$syntaxResult = $( '#mw-abusefilter-syntaxresult' );
		$( '#mw-abusefilter-examine-test' ).click( examinerTestFilter );
	} );
}( mediaWiki, jQuery ) );