Debian Codegraph @

extensions/AbuseFilter/modules/ext.abuseFilter.tools.js

raw symbols SCIP
1 /**
2 * JavaScript for AbuseFilter tools
3 *
4 * @author John Du Hart
5 * @author Marius Hoch <[email protected]>
6 */
7
8 ( function () {
9 'use strict';
10
11 /**
12 * Submits the expression to be evaluated.
13 *
14 * @this HTMLElement
15 * @param {jQuery.Event} e The event fired when the function is called
16 */
17 function doExprSubmit( e ) {
18 var expr = $( '#wpFilterRules' ).val();
19
20 $( this ).injectSpinner( { id: 'abusefilter-expr', size: 'large' } );
21
22 new mw.Api().post( {
23 action: 'abusefilterevalexpression',
24 expression: expr,
25 prettyprint: 1
26 } )
27 .fail( function showError( error, details ) {
28 // TODO This might use api.getErrorMessage()
29 var msg;
30 if ( error === 'http' ) {
31 msg = 'abusefilter-http-error';
32 } else if ( error === 'abusefilter-tools-syntax-error' ) {
33 msg = 'abusefilter-tools-syntax-error';
34 } else {
35 msg = 'unknown-error';
36 }
37 $.removeSpinner( 'abusefilter-expr' );
38 $( '#mw-abusefilter-expr-result' )
39 // Message keys are listed above
40 // eslint-disable-next-line mediawiki/msg-doc
41 .text( mw.msg( msg, details.exception ) )
42 .addClass( 'mw-abusefilter-tools-error' )
43 .show();
44 } )
45 .done( function showResult( data ) {
46 $.removeSpinner( 'abusefilter-expr' );
47
48 $( '#mw-abusefilter-expr-result' )
49 .text( data.abusefilterevalexpression.result )
50 .removeClass( 'mw-abusefilter-tools-error' )
51 .show();
52 } );
53 e.preventDefault();
54 }
55
56 /**
57 * Processes the result of the unblocking autopromotions for a user
58 *
59 * @param {Object} data The response of the API request
60 */
61 function processReautoconfirm( data ) {
62 mw.notify(
63 mw.message(
64 'abusefilter-reautoconfirm-done',
65 data.abusefilterunblockautopromote.user
66 ).toString()
67 );
68
69 $.removeSpinner( 'abusefilter-reautoconfirm' );
70 }
71
72 /**
73 * Processes the result of the unblocking autopromotions for a user in case of an error
74 *
75 * @param {string} errorCode Identifier of the error
76 * @param {Object} data The response of the API request
77 */
78 function processReautoconfirmFailure( errorCode, data ) {
79 var msg;
80
81 switch ( errorCode ) {
82 case 'permissiondenied':
83 msg = mw.msg( 'abusefilter-reautoconfirm-notallowed' );
84 break;
85 case 'http':
86 msg = mw.msg( 'abusefilter-http-error', data && data.exception );
87 break;
88 case 'notsuspended':
89 msg = data.error.info;
90 break;
91 default:
92 msg = mw.msg( 'unknown-error' );
93 break;
94 }
95 mw.notify( msg );
96
97 $.removeSpinner( 'abusefilter-reautoconfirm' );
98 }
99
100 /**
101 * Submits a call to reautoconfirm a user.
102 *
103 * @this HTMLElement
104 * @param {jQuery.Event} e The event fired when the function is called
105 * @return {boolean} False to prevent form submission
106 */
107 function doReautoSubmit() {
108 var nameField = OO.ui.infuse( $( '#reautoconfirm-user' ) ),
109 name = nameField.getValue();
110
111 if ( name === '' ) {
112 return false;
113 }
114
115 $( this ).injectSpinner( { id: 'abusefilter-reautoconfirm', size: 'large' } );
116
117 new mw.Api().post( {
118 action: 'abusefilterunblockautopromote',
119 user: name,
120 token: mw.user.tokens.get( 'csrfToken' )
121 } )
122 .done( processReautoconfirm )
123 .fail( processReautoconfirmFailure );
124 return false;
125 }
126
127 $( function initialize() {
128 $( '#mw-abusefilter-submitexpr' ).on( 'click', doExprSubmit );
129 $( '#mw-abusefilter-reautoconfirmsubmit' ).on( 'click', doReautoSubmit );
130 } );
131 }() );