2012-10-26 00:23:17 +00:00
|
|
|
/**
|
|
|
|
* AbuseFilter editing JavaScript
|
|
|
|
*
|
|
|
|
* @author John Du Hart
|
|
|
|
* @author Marius Hoch <hoo@online.de>
|
|
|
|
*/
|
2018-03-30 06:55:03 +00:00
|
|
|
/* global ace */
|
2012-10-26 00:23:17 +00:00
|
|
|
|
2018-11-23 15:44:58 +00:00
|
|
|
( function () {
|
2012-10-26 00:23:17 +00:00
|
|
|
'use strict';
|
2012-09-02 11:07:02 +00:00
|
|
|
|
2018-10-03 12:02:00 +00:00
|
|
|
// @var {jQuery} Filter editor for JS and jQuery handling
|
2018-03-30 06:55:03 +00:00
|
|
|
var $filterBox,
|
|
|
|
// Filter editor for Ace specific functions
|
|
|
|
filterEditor,
|
2018-10-03 12:02:00 +00:00
|
|
|
// @var {jQuery} Hidden textarea for submitting form
|
2018-03-30 06:55:03 +00:00
|
|
|
$plainTextBox,
|
2018-10-03 12:02:00 +00:00
|
|
|
// @var {boolean} To determine what editor to use
|
2018-03-23 09:13:41 +00:00
|
|
|
useAce = false,
|
|
|
|
// Infused OOUI elements
|
2014-10-04 14:42:46 +00:00
|
|
|
toggleWarnPreviewButton, warnMessageExisting, warnMessageOther,
|
|
|
|
toggleDisallowPreviewButton, disallowMessageExisting, disallowMessageOther;
|
2012-10-26 00:23:17 +00:00
|
|
|
|
|
|
|
/**
|
2014-10-04 14:42:46 +00:00
|
|
|
* Returns the currently selected warning or disallow message.
|
2012-10-26 00:23:17 +00:00
|
|
|
*
|
2014-10-04 14:42:46 +00:00
|
|
|
* @param {string} action The action to get the message for
|
2018-02-07 21:08:04 +00:00
|
|
|
* @return {string} current warning message
|
2012-10-26 00:23:17 +00:00
|
|
|
*/
|
2014-10-04 14:42:46 +00:00
|
|
|
function getCurrentMessage( action ) {
|
|
|
|
var existing = action === 'warn' ? warnMessageExisting : disallowMessageExisting,
|
|
|
|
other = action === 'warn' ? warnMessageOther : disallowMessageOther,
|
|
|
|
message = existing.getValue();
|
2012-10-26 00:23:17 +00:00
|
|
|
|
|
|
|
if ( message === 'other' ) {
|
2014-10-04 14:42:46 +00:00
|
|
|
message = other.getValue();
|
2012-10-26 00:23:17 +00:00
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2012-10-26 00:23:17 +00:00
|
|
|
return message;
|
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2012-10-26 00:23:17 +00:00
|
|
|
/**
|
|
|
|
* Things always needed after syntax checks
|
|
|
|
*
|
2018-10-03 12:02:00 +00:00
|
|
|
* @param {string} resultText The message to show, telling if the syntax is valid
|
2012-10-26 00:23:17 +00:00
|
|
|
* @param {string} className Class to add
|
2018-10-03 12:02:00 +00:00
|
|
|
* @param {boolean} syntaxOk Is the syntax ok?
|
2012-10-26 00:23:17 +00:00
|
|
|
*/
|
|
|
|
function processSyntaxResultAlways( resultText, className, syntaxOk ) {
|
2011-08-26 20:12:34 +00:00
|
|
|
$.removeSpinner( 'abusefilter-syntaxcheck' );
|
2017-08-09 16:54:42 +00:00
|
|
|
$( '#mw-abusefilter-syntaxcheck' ).prop( 'disabled', false );
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2012-10-26 00:23:17 +00:00
|
|
|
$( '#mw-abusefilter-syntaxresult' )
|
2011-08-26 20:12:34 +00:00
|
|
|
.show()
|
2017-07-14 17:47:20 +00:00
|
|
|
.attr( 'class', className )
|
2012-10-26 00:23:17 +00:00
|
|
|
.text( resultText )
|
|
|
|
.data( 'syntaxOk', syntaxOk );
|
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2018-03-30 06:55:03 +00:00
|
|
|
/**
|
|
|
|
* Switch between Ace Editor and classic textarea
|
|
|
|
*/
|
|
|
|
function switchEditor() {
|
|
|
|
if ( useAce ) {
|
|
|
|
useAce = false;
|
|
|
|
$filterBox.hide();
|
|
|
|
$plainTextBox.show();
|
|
|
|
} else {
|
|
|
|
useAce = true;
|
|
|
|
filterEditor.session.setValue( $plainTextBox.val() );
|
|
|
|
$filterBox.show();
|
|
|
|
$plainTextBox.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 00:23:17 +00:00
|
|
|
/**
|
2014-10-04 14:42:46 +00:00
|
|
|
* Takes the data retrieved in doSyntaxCheck and processes it.
|
2012-10-26 00:23:17 +00:00
|
|
|
*
|
2018-10-03 15:55:06 +00:00
|
|
|
* @param {Object} response Data returned from the AJAX request
|
2012-10-26 00:23:17 +00:00
|
|
|
*/
|
2018-10-03 15:55:06 +00:00
|
|
|
function processSyntaxResult( response ) {
|
|
|
|
var position,
|
|
|
|
data = response.abusefilterchecksyntax;
|
2011-09-30 00:48:00 +00:00
|
|
|
|
2012-09-02 11:07:02 +00:00
|
|
|
if ( data.status === 'ok' ) {
|
2011-08-26 20:12:34 +00:00
|
|
|
// Successful
|
2012-10-26 00:23:17 +00:00
|
|
|
processSyntaxResultAlways(
|
|
|
|
mw.msg( 'abusefilter-edit-syntaxok' ),
|
|
|
|
'mw-abusefilter-syntaxresult-ok',
|
|
|
|
true
|
|
|
|
);
|
2011-08-26 20:12:34 +00:00
|
|
|
} else {
|
2012-10-26 00:23:17 +00:00
|
|
|
// Set a custom error message as we're aware of the actual problem
|
|
|
|
processSyntaxResultAlways(
|
|
|
|
mw.message( 'abusefilter-edit-syntaxerr', data.message ).toString(),
|
|
|
|
'mw-abusefilter-syntaxresult-error',
|
|
|
|
false
|
|
|
|
);
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2018-03-30 06:55:03 +00:00
|
|
|
if ( useAce ) {
|
|
|
|
filterEditor.focus();
|
2018-04-12 10:03:52 +00:00
|
|
|
// Convert index (used in textareas) in position {row, column} for ace
|
|
|
|
position = filterEditor.session.getDocument().indexToPosition( data.character );
|
2018-03-30 06:55:03 +00:00
|
|
|
filterEditor.navigateTo( position.row, position.column );
|
|
|
|
filterEditor.scrollToRow( position.row );
|
|
|
|
} else {
|
|
|
|
$plainTextBox
|
2019-02-18 17:36:42 +00:00
|
|
|
.trigger( 'focus' )
|
2018-03-30 06:55:03 +00:00
|
|
|
.textSelection( 'setSelection', { start: data.character } );
|
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
}
|
2012-10-26 00:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-04 14:42:46 +00:00
|
|
|
* Acts on errors after doSyntaxCheck.
|
2014-07-30 14:46:53 +00:00
|
|
|
*
|
|
|
|
* @param {string} error Error code returned from the AJAX request
|
|
|
|
* @param {Object} details Details about the error
|
2012-10-26 00:23:17 +00:00
|
|
|
*/
|
2014-07-30 14:46:53 +00:00
|
|
|
function processSyntaxResultFailure( error, details ) {
|
2012-10-26 00:23:17 +00:00
|
|
|
processSyntaxResultAlways(
|
2020-02-22 15:59:28 +00:00
|
|
|
mw.msg(
|
|
|
|
error === 'http' ? 'abusefilter-http-error' : 'unknown-error',
|
|
|
|
details && details.exception
|
|
|
|
),
|
2012-10-26 00:23:17 +00:00
|
|
|
'mw-abusefilter-syntaxresult-error',
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2018-02-07 21:08:04 +00:00
|
|
|
/**
|
|
|
|
* Sends the current filter text to be checked for syntax issues.
|
|
|
|
*
|
2020-06-06 15:16:41 +00:00
|
|
|
* @this HTMLElement
|
2018-10-03 12:02:00 +00:00
|
|
|
* @param {jQuery.Event} e The event fired when the function is called
|
2018-02-07 21:08:04 +00:00
|
|
|
*/
|
|
|
|
function doSyntaxCheck() {
|
2020-06-10 23:52:37 +00:00
|
|
|
var filter = $plainTextBox.val();
|
2018-02-07 21:08:04 +00:00
|
|
|
|
|
|
|
$( this )
|
|
|
|
.prop( 'disabled', true )
|
2018-03-16 08:42:42 +00:00
|
|
|
.injectSpinner( { id: 'abusefilter-syntaxcheck', size: 'large' } );
|
2018-02-07 21:08:04 +00:00
|
|
|
|
2020-06-10 23:52:37 +00:00
|
|
|
new mw.Api().post( {
|
2018-02-07 21:08:04 +00:00
|
|
|
action: 'abusefilterchecksyntax',
|
|
|
|
filter: filter
|
|
|
|
} )
|
|
|
|
.done( processSyntaxResult )
|
|
|
|
.fail( processSyntaxResultFailure );
|
|
|
|
}
|
|
|
|
|
2012-10-26 00:23:17 +00:00
|
|
|
/**
|
2014-10-04 14:42:46 +00:00
|
|
|
* Adds text to the filter textarea.
|
2012-10-26 00:23:17 +00:00
|
|
|
* Fired by a change event from the #wpFilterBuilder dropdown
|
|
|
|
*/
|
|
|
|
function addText() {
|
2011-08-26 20:12:34 +00:00
|
|
|
var $filterBuilder = $( '#wpFilterBuilder' );
|
2012-09-02 11:07:02 +00:00
|
|
|
|
|
|
|
if ( $filterBuilder.prop( 'selectedIndex' ) === 0 ) {
|
2011-08-26 20:12:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-30 06:55:03 +00:00
|
|
|
if ( useAce ) {
|
|
|
|
filterEditor.insert( $filterBuilder.val() + ' ' );
|
|
|
|
$plainTextBox.val( filterEditor.getSession().getValue() );
|
2018-08-22 08:57:15 +00:00
|
|
|
filterEditor.focus();
|
2018-03-30 06:55:03 +00:00
|
|
|
} else {
|
|
|
|
$plainTextBox.textSelection(
|
|
|
|
'encapsulateSelection', { pre: $filterBuilder.val() + ' ' }
|
|
|
|
);
|
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
$filterBuilder.prop( 'selectedIndex', 0 );
|
2012-10-26 00:23:17 +00:00
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2012-10-26 00:23:17 +00:00
|
|
|
/**
|
2013-04-02 19:44:01 +00:00
|
|
|
* Fetches a filter from the API and inserts it into the filter box.
|
|
|
|
*
|
2020-06-06 15:16:41 +00:00
|
|
|
* @this HTMLElement
|
2018-10-03 12:02:00 +00:00
|
|
|
* @param {jQuery.Event} e The event fired when the function is called
|
2012-10-26 00:23:17 +00:00
|
|
|
*/
|
|
|
|
function fetchFilter() {
|
2020-06-10 23:52:37 +00:00
|
|
|
var filterId = $( '#mw-abusefilter-load-filter input' ).val().trim();
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2012-09-02 11:07:02 +00:00
|
|
|
if ( filterId === '' ) {
|
2011-08-26 20:12:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-16 08:42:42 +00:00
|
|
|
$( this ).injectSpinner( { id: 'fetch-spinner', size: 'large' } );
|
2012-10-26 00:23:17 +00:00
|
|
|
|
|
|
|
// We just ignore errors or unexisting filters over here
|
2020-06-10 23:52:37 +00:00
|
|
|
new mw.Api().get( {
|
2012-10-26 00:23:17 +00:00
|
|
|
action: 'query',
|
|
|
|
list: 'abusefilters',
|
|
|
|
abfprop: 'pattern',
|
|
|
|
abfstartid: filterId,
|
|
|
|
abfendid: filterId,
|
|
|
|
abflimit: 1
|
|
|
|
} )
|
2018-11-19 14:39:43 +00:00
|
|
|
.always( function removeSpinner() {
|
2018-02-07 21:08:04 +00:00
|
|
|
$.removeSpinner( 'fetch-spinner' );
|
|
|
|
} )
|
2018-11-19 14:39:43 +00:00
|
|
|
.done( function insertFilter( data ) {
|
2018-02-07 21:08:04 +00:00
|
|
|
if ( data.query.abusefilters[ 0 ] !== undefined ) {
|
2018-03-30 06:55:03 +00:00
|
|
|
if ( useAce ) {
|
|
|
|
filterEditor.setValue( data.query.abusefilters[ 0 ].pattern );
|
|
|
|
}
|
|
|
|
$plainTextBox.val( data.query.abusefilters[ 0 ].pattern );
|
2018-02-07 21:08:04 +00:00
|
|
|
}
|
|
|
|
} );
|
2012-10-26 00:23:17 +00:00
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2012-10-26 00:23:17 +00:00
|
|
|
/**
|
2014-10-04 14:42:46 +00:00
|
|
|
* Cycles through all action checkboxes and hides parameter divs.
|
2012-10-26 00:23:17 +00:00
|
|
|
* that don't have checked boxes
|
|
|
|
*/
|
|
|
|
function hideDeselectedActions() {
|
2018-11-19 14:39:43 +00:00
|
|
|
$( '.mw-abusefilter-action-checkbox input' ).each( function showHideParams() {
|
2011-09-30 00:48:00 +00:00
|
|
|
// mw-abusefilter-action-checkbox-{$action}
|
2021-12-21 21:43:52 +00:00
|
|
|
var action = this.parentNode.id.slice( 31 ),
|
2012-09-02 11:07:02 +00:00
|
|
|
$params = $( '#mw-abusefilter-' + action + '-parameters' );
|
2011-08-26 20:12:34 +00:00
|
|
|
|
|
|
|
if ( $params.length ) {
|
|
|
|
if ( this.checked ) {
|
|
|
|
$params.show();
|
|
|
|
} else {
|
|
|
|
$params.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
2012-10-26 00:23:17 +00:00
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2012-10-26 00:23:17 +00:00
|
|
|
/**
|
2014-10-04 14:42:46 +00:00
|
|
|
* Fetches the selected warning message for previewing.
|
|
|
|
*
|
|
|
|
* @param {string} action The action the message refers to
|
2016-12-26 10:34:16 +00:00
|
|
|
*/
|
2014-10-04 14:42:46 +00:00
|
|
|
function previewMessage( action ) {
|
2020-06-10 23:52:37 +00:00
|
|
|
var api,
|
2016-12-26 10:34:16 +00:00
|
|
|
args = [
|
2018-03-10 12:18:49 +00:00
|
|
|
'<nowiki>' + $( 'input[name=wpFilterDescription]' ).val() + '</nowiki>',
|
2016-12-26 10:34:16 +00:00
|
|
|
$( '#mw-abusefilter-edit-id' ).children().last().text()
|
|
|
|
],
|
2014-10-04 14:42:46 +00:00
|
|
|
message = getCurrentMessage( action ),
|
|
|
|
// mw-abusefilter-warn-preview, mw-abusefilter-disallow-preview
|
|
|
|
$element = $( '#mw-abusefilter-' + action + '-preview' ),
|
|
|
|
previewButton = action === 'warn' ? toggleWarnPreviewButton : toggleDisallowPreviewButton;
|
2018-03-23 09:13:41 +00:00
|
|
|
|
2019-07-31 13:52:09 +00:00
|
|
|
if ( $element.css( 'display' ) !== 'none' ) {
|
2018-10-03 15:55:06 +00:00
|
|
|
$element.hide();
|
|
|
|
previewButton.setFlags( { destructive: false, progressive: true } );
|
|
|
|
} else {
|
2020-06-10 23:52:37 +00:00
|
|
|
api = new mw.Api();
|
2018-03-23 09:13:41 +00:00
|
|
|
api.get( {
|
|
|
|
action: 'query',
|
|
|
|
meta: 'allmessages',
|
|
|
|
ammessages: message,
|
|
|
|
amargs: args.join( '|' )
|
|
|
|
} )
|
2018-11-19 14:39:43 +00:00
|
|
|
.done( function parseMessage( data ) {
|
2018-03-23 09:13:41 +00:00
|
|
|
api.parse( data.query.allmessages[ 0 ][ '*' ], {
|
|
|
|
disablelimitreport: '',
|
|
|
|
preview: '',
|
|
|
|
prop: 'text',
|
|
|
|
title: 'MediaWiki:' + message
|
|
|
|
} )
|
2018-11-19 14:39:43 +00:00
|
|
|
.done( function showMessage( html ) {
|
2014-10-04 14:42:46 +00:00
|
|
|
$element.show().html( html );
|
|
|
|
previewButton.setFlags(
|
2018-09-03 08:49:24 +00:00
|
|
|
{ destructive: true, progressive: false }
|
|
|
|
);
|
2018-03-23 09:13:41 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
2012-10-26 00:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-04 14:42:46 +00:00
|
|
|
* Redirects the browser to the message for editing.
|
|
|
|
*
|
|
|
|
* @param {string} action The action for which the message is used
|
2012-10-26 00:23:17 +00:00
|
|
|
*/
|
2014-10-04 14:42:46 +00:00
|
|
|
function editMessage( action ) {
|
|
|
|
var message = getCurrentMessage( action ),
|
2018-09-23 11:06:39 +00:00
|
|
|
defaultMsg = action === 'warn' ? 'warning' : 'disallowed',
|
2019-07-03 06:42:49 +00:00
|
|
|
url = mw.util.getUrl( 'MediaWiki:' + message, {
|
|
|
|
action: 'edit',
|
|
|
|
preload: 'MediaWiki:abusefilter-' + defaultMsg
|
|
|
|
} );
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2018-09-23 11:06:39 +00:00
|
|
|
window.open( url, '_blank' );
|
2012-10-26 00:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-31 22:35:34 +00:00
|
|
|
* Called if the filter group (#mw-abusefilter-edit-group-input select) is changed. Allows
|
|
|
|
* using different default messages for 'warn' and 'disallow', based on the group.
|
2013-04-02 19:44:01 +00:00
|
|
|
*
|
2020-06-06 15:16:41 +00:00
|
|
|
* @this HTMLELement
|
2018-10-03 12:02:00 +00:00
|
|
|
* @param {jQuery.Event} e The event fired when the function is called
|
2012-10-26 00:23:17 +00:00
|
|
|
*/
|
|
|
|
function onFilterGroupChange() {
|
2014-10-04 14:42:46 +00:00
|
|
|
var $afWarnMessageExisting, $afDisallowMessageExisting, newVal;
|
2012-10-26 00:23:17 +00:00
|
|
|
|
2021-01-31 22:35:34 +00:00
|
|
|
if (
|
|
|
|
!$( '#mw-abusefilter-action-checkbox-warn input' ).is( ':checked' ) &&
|
|
|
|
$( this ).val() in mw.config.get( 'wgAbuseFilterDefaultWarningMessage' )
|
|
|
|
) {
|
2018-03-23 09:13:41 +00:00
|
|
|
$afWarnMessageExisting = $( '#mw-abusefilter-warn-message-existing select' );
|
2018-02-07 21:08:04 +00:00
|
|
|
newVal = mw.config.get( 'wgAbuseFilterDefaultWarningMessage' )[ $( this ).val() ];
|
2012-10-26 00:23:17 +00:00
|
|
|
|
2013-04-17 12:27:46 +00:00
|
|
|
if ( $afWarnMessageExisting.find( 'option[value=\'' + newVal + '\']' ).length ) {
|
2012-10-26 00:23:17 +00:00
|
|
|
$afWarnMessageExisting.val( newVal );
|
2014-10-04 14:42:46 +00:00
|
|
|
warnMessageOther.setValue( '' );
|
2012-10-26 00:23:17 +00:00
|
|
|
} else {
|
|
|
|
$afWarnMessageExisting.val( 'other' );
|
2014-10-04 14:42:46 +00:00
|
|
|
warnMessageOther.setValue( newVal );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 22:35:34 +00:00
|
|
|
if (
|
|
|
|
!$( '#mw-abusefilter-action-checkbox-disallow input' ).is( ':checked' ) &&
|
|
|
|
$( this ).val() in mw.config.get( 'wgAbuseFilterDefaultDisallowMessage' )
|
|
|
|
) {
|
2014-10-04 14:42:46 +00:00
|
|
|
$afDisallowMessageExisting = $( '#mw-abusefilter-disallow-message-existing select' );
|
|
|
|
newVal = mw.config.get( 'wgAbuseFilterDefaultDisallowMessage' )[ $( this ).val() ];
|
|
|
|
|
|
|
|
if ( $afDisallowMessageExisting.find( 'option[value=\'' + newVal + '\']' ).length ) {
|
|
|
|
$afDisallowMessageExisting.val( newVal );
|
|
|
|
disallowMessageOther.setValue( '' );
|
|
|
|
} else {
|
|
|
|
$afDisallowMessageExisting.val( 'other' );
|
|
|
|
disallowMessageOther.setValue( newVal );
|
2012-10-26 00:23:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2013-06-26 21:57:20 +00:00
|
|
|
/**
|
2014-10-04 14:42:46 +00:00
|
|
|
* Remove the options for warning and disallow messages if the filter is set to global.
|
2013-06-26 21:57:20 +00:00
|
|
|
*/
|
|
|
|
function toggleCustomMessages() {
|
|
|
|
// Use the table over here as hideDeselectedActions might alter the visibility of the div
|
2014-10-04 14:42:46 +00:00
|
|
|
var $warnOptions = $( '#mw-abusefilter-warn-parameters > table' ),
|
|
|
|
$disallowOptions = $( '#mw-abusefilter-disallow-parameters > table' );
|
2013-06-26 21:57:20 +00:00
|
|
|
|
|
|
|
if ( $( '#wpFilterGlobal' ).is( ':checked' ) ) {
|
|
|
|
// It's a global filter, so use the default message and hide the option from the user
|
2014-10-04 14:42:46 +00:00
|
|
|
warnMessageExisting.setValue( 'abusefilter-warning' );
|
|
|
|
disallowMessageExisting.setValue( 'abusefilter-disallowed' );
|
2013-06-26 21:57:20 +00:00
|
|
|
|
|
|
|
$warnOptions.hide();
|
2014-10-04 14:42:46 +00:00
|
|
|
$disallowOptions.hide();
|
2013-06-26 21:57:20 +00:00
|
|
|
} else {
|
|
|
|
$warnOptions.show();
|
2014-10-04 14:42:46 +00:00
|
|
|
$disallowOptions.show();
|
2013-06-26 21:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 13:22:11 +00:00
|
|
|
/**
|
2014-10-04 14:42:46 +00:00
|
|
|
* Called if the user presses a key in the load filter field.
|
2013-07-17 13:22:11 +00:00
|
|
|
*
|
2020-06-06 15:16:41 +00:00
|
|
|
* @this HTMLELement
|
2018-10-03 12:02:00 +00:00
|
|
|
* @param {jQuery.Event} e The event fired when the function is called
|
2013-07-17 13:22:11 +00:00
|
|
|
*/
|
|
|
|
function onFilterKeypress( e ) {
|
|
|
|
if ( e.type === 'keypress' && e.which === 13 ) {
|
|
|
|
e.preventDefault();
|
2019-02-18 17:36:42 +00:00
|
|
|
$( '#mw-abusefilter-load' ).trigger( 'click' );
|
2013-07-17 13:22:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 12:06:46 +00:00
|
|
|
/**
|
|
|
|
* Warn if the user changed anything and tries to leave the window
|
|
|
|
*/
|
|
|
|
function setWarnOnLeave() {
|
|
|
|
var warnOnLeave,
|
|
|
|
$form = $( '#mw-abusefilter-editing-form' ),
|
|
|
|
origValues = $form.serialize();
|
|
|
|
|
|
|
|
warnOnLeave = mw.confirmCloseWindow( {
|
|
|
|
test: function () {
|
|
|
|
return $form.serialize() !== origValues;
|
|
|
|
},
|
|
|
|
message: mw.msg( 'abusefilter-edit-warn-leave' )
|
|
|
|
} );
|
|
|
|
|
2019-02-18 17:36:42 +00:00
|
|
|
$form.on( 'submit', function () {
|
2018-12-04 12:06:46 +00:00
|
|
|
warnOnLeave.release();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2018-09-09 10:14:31 +00:00
|
|
|
/**
|
|
|
|
* Builds a TagMultiselectWidget, to be used both for throttle groups and change tags
|
|
|
|
*
|
|
|
|
* @param {string} action Either 'throttle' or 'tag', will be used to build element IDs
|
|
|
|
* @param {Array} config The array with configuration passed from PHP code
|
|
|
|
*/
|
|
|
|
function buildSelector( action, config ) {
|
2019-02-27 10:52:59 +00:00
|
|
|
// mw-abusefilter-throttle-parameters, mw-abusefilter-tag-parameters
|
|
|
|
var $container = $( '#mw-abusefilter-' + action + '-parameters' ),
|
2018-09-09 10:14:31 +00:00
|
|
|
// Character used to separate elements in the textarea.
|
|
|
|
separator = action === 'throttle' ? '\n' : ',',
|
2019-01-24 11:58:41 +00:00
|
|
|
selector, field, fieldOpts, hiddenField;
|
2018-09-09 10:14:31 +00:00
|
|
|
|
2020-06-10 23:52:37 +00:00
|
|
|
selector = new OO.ui.TagMultiselectWidget( {
|
|
|
|
inputPosition: 'outline',
|
|
|
|
allowArbitrary: true,
|
|
|
|
allowEditTags: true,
|
2022-10-07 15:09:55 +00:00
|
|
|
// eslint-disable-next-line es-x/no-array-prototype-values
|
2020-06-10 23:52:37 +00:00
|
|
|
selected: config.values,
|
|
|
|
// The following messages are used here:
|
|
|
|
// * abusefilter-edit-throttle-placeholder
|
|
|
|
// * abusefilter-edit-tag-placeholder
|
|
|
|
placeholder: OO.ui.msg( 'abusefilter-edit-' + action + '-placeholder' ),
|
|
|
|
disabled: config.disabled
|
|
|
|
} );
|
2019-01-24 11:58:41 +00:00
|
|
|
|
|
|
|
fieldOpts = {
|
|
|
|
label: $( $.parseHTML( config.label ) ),
|
|
|
|
align: 'top'
|
|
|
|
};
|
|
|
|
if ( action === 'throttle' ) {
|
2019-03-18 09:51:53 +00:00
|
|
|
fieldOpts.help = new OO.ui.HtmlSnippet( config.help );
|
2019-01-24 11:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
field = new OO.ui.FieldLayout( selector, fieldOpts );
|
2018-09-09 10:14:31 +00:00
|
|
|
|
|
|
|
// mw-abusefilter-hidden-throttle-field, mw-abusefilter-hidden-tag-field
|
|
|
|
hiddenField = OO.ui.infuse( $( '#mw-abusefilter-hidden-' + action + '-field' ) );
|
|
|
|
selector.on( 'change', function () {
|
|
|
|
hiddenField.setValue( selector.getValue().join( separator ) );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// mw-abusefilter-hidden-throttle, mw-abusefilter-hidden-tag
|
|
|
|
$( '#mw-abusefilter-hidden-' + action ).hide();
|
|
|
|
$container.append( field.$element );
|
|
|
|
}
|
|
|
|
|
2012-09-02 11:07:02 +00:00
|
|
|
// On ready initialization
|
2019-02-18 17:36:42 +00:00
|
|
|
$( function () {
|
2018-03-30 06:55:03 +00:00
|
|
|
var basePath, readOnly,
|
2018-03-23 09:13:41 +00:00
|
|
|
$exportBox = $( '#mw-abusefilter-export' ),
|
|
|
|
isFilterEditor = mw.config.get( 'isFilterEditor' ),
|
|
|
|
tagConfig = mw.config.get( 'tagConfig' ),
|
2018-09-09 10:14:31 +00:00
|
|
|
throttleConfig = mw.config.get( 'throttleConfig' ),
|
2020-09-24 22:56:59 +00:00
|
|
|
$switchEditorBtn = $( '#mw-abusefilter-switcheditor' ),
|
2018-09-09 10:14:31 +00:00
|
|
|
cbEnabled, cbDeleted;
|
2018-03-23 09:13:41 +00:00
|
|
|
|
|
|
|
if ( isFilterEditor ) {
|
|
|
|
// Configure the actual editing interface
|
|
|
|
if ( tagConfig ) {
|
|
|
|
// Build the tag selector
|
2018-09-09 10:14:31 +00:00
|
|
|
buildSelector( 'tag', tagConfig );
|
|
|
|
}
|
|
|
|
if ( throttleConfig ) {
|
|
|
|
// Build the throttle groups selector
|
|
|
|
buildSelector( 'throttle', throttleConfig );
|
2018-03-23 09:13:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-04 14:42:46 +00:00
|
|
|
toggleWarnPreviewButton = OO.ui.infuse( $( '#mw-abusefilter-warn-preview-button' ) );
|
|
|
|
warnMessageExisting = OO.ui.infuse( $( '#mw-abusefilter-warn-message-existing' ) );
|
|
|
|
warnMessageOther = OO.ui.infuse( $( '#mw-abusefilter-warn-message-other' ) );
|
|
|
|
toggleDisallowPreviewButton = OO.ui.infuse( $( '#mw-abusefilter-disallow-preview-button' ) );
|
|
|
|
disallowMessageExisting = OO.ui.infuse( $( '#mw-abusefilter-disallow-message-existing' ) );
|
|
|
|
disallowMessageOther = OO.ui.infuse( $( '#mw-abusefilter-disallow-message-other' ) );
|
2018-12-04 12:06:46 +00:00
|
|
|
setWarnOnLeave();
|
2018-03-23 09:13:41 +00:00
|
|
|
}
|
2018-03-30 06:55:03 +00:00
|
|
|
|
2019-08-17 16:53:13 +00:00
|
|
|
$plainTextBox = $( '#wpFilterRules' );
|
2018-03-30 06:55:03 +00:00
|
|
|
|
|
|
|
if ( $( '#wpAceFilterEditor' ).length ) {
|
|
|
|
// CodeEditor is installed.
|
|
|
|
mw.loader.using( [ 'ext.abuseFilter.ace' ] ).then( function () {
|
|
|
|
$filterBox = $( '#wpAceFilterEditor' );
|
|
|
|
|
|
|
|
filterEditor = ace.edit( 'wpAceFilterEditor' );
|
|
|
|
filterEditor.session.setMode( 'ace/mode/abusefilter' );
|
|
|
|
|
|
|
|
// Ace setup from codeEditor extension
|
|
|
|
basePath = mw.config.get( 'wgExtensionAssetsPath', '' );
|
|
|
|
if ( basePath.slice( 0, 2 ) === '//' ) {
|
2018-09-03 08:49:24 +00:00
|
|
|
// ACE uses web workers, which have importScripts, which don't like
|
|
|
|
// relative links. This is a problem only when the assets are on another
|
|
|
|
// server, so this rewrite should suffice.
|
2018-03-30 06:55:03 +00:00
|
|
|
basePath = window.location.protocol + basePath;
|
|
|
|
}
|
|
|
|
ace.config.set( 'basePath', basePath + '/CodeEditor/modules/ace' );
|
|
|
|
|
|
|
|
// Settings for Ace editor box
|
|
|
|
readOnly = mw.config.get( 'aceConfig' ).aceReadOnly;
|
|
|
|
|
|
|
|
filterEditor.setTheme( 'ace/theme/textmate' );
|
|
|
|
filterEditor.setReadOnly( readOnly );
|
|
|
|
filterEditor.$blockScrolling = Infinity;
|
|
|
|
|
2018-04-16 15:01:35 +00:00
|
|
|
// Display Ace editor
|
|
|
|
switchEditor();
|
2018-03-30 06:55:03 +00:00
|
|
|
|
|
|
|
// Hide the syntax ok message when the text changes and sync dummy box
|
2018-09-03 14:03:24 +00:00
|
|
|
filterEditor.on( 'change', function () {
|
2018-03-30 06:55:03 +00:00
|
|
|
var $el = $( '#mw-abusefilter-syntaxresult' );
|
|
|
|
|
|
|
|
if ( $el.data( 'syntaxOk' ) ) {
|
|
|
|
$el.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
$plainTextBox.val( filterEditor.getSession().getValue() );
|
|
|
|
} );
|
|
|
|
|
2020-09-24 22:56:59 +00:00
|
|
|
$switchEditorBtn.on( 'click', switchEditor );
|
|
|
|
$switchEditorBtn.show();
|
2018-03-30 06:55:03 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2011-08-26 20:12:34 +00:00
|
|
|
// Hide the syntax ok message when the text changes
|
2019-02-18 17:36:42 +00:00
|
|
|
$plainTextBox.on( 'change', function () {
|
2011-08-26 20:12:34 +00:00
|
|
|
var $el = $( '#mw-abusefilter-syntaxresult' );
|
2012-09-02 11:07:02 +00:00
|
|
|
|
2011-08-26 20:12:34 +00:00
|
|
|
if ( $el.data( 'syntaxOk' ) ) {
|
|
|
|
$el.hide();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2019-02-18 17:36:42 +00:00
|
|
|
$( '#mw-abusefilter-load' ).on( 'click', fetchFilter );
|
|
|
|
$( '#mw-abusefilter-load-filter' ).on( 'keypress', onFilterKeypress );
|
2011-08-26 20:12:34 +00:00
|
|
|
|
2018-03-23 09:13:41 +00:00
|
|
|
if ( isFilterEditor ) {
|
|
|
|
// Add logic for flags and consequences
|
2019-02-18 17:36:42 +00:00
|
|
|
$( '#mw-abusefilter-warn-preview-button' ).on( 'click',
|
2014-10-04 14:42:46 +00:00
|
|
|
function () { previewMessage( 'warn' ); }
|
|
|
|
);
|
2019-02-18 17:36:42 +00:00
|
|
|
$( '#mw-abusefilter-disallow-preview-button' ).on( 'click',
|
2014-10-04 14:42:46 +00:00
|
|
|
function () { previewMessage( 'disallow' ); }
|
|
|
|
);
|
2019-02-18 17:36:42 +00:00
|
|
|
$( '#mw-abusefilter-warn-edit-button' ).on( 'click',
|
2014-10-04 14:42:46 +00:00
|
|
|
function () { editMessage( 'warn' ); }
|
|
|
|
);
|
2019-02-18 17:36:42 +00:00
|
|
|
$( '#mw-abusefilter-disallow-edit-button' ).on( 'click',
|
2014-10-04 14:42:46 +00:00
|
|
|
function () { editMessage( 'disallow' ); }
|
|
|
|
);
|
2019-02-18 17:36:42 +00:00
|
|
|
$( '.mw-abusefilter-action-checkbox input' ).on( 'click', hideDeselectedActions );
|
2018-03-23 09:13:41 +00:00
|
|
|
hideDeselectedActions();
|
|
|
|
|
2019-02-18 17:36:42 +00:00
|
|
|
$( '#wpFilterGlobal' ).on( 'change', toggleCustomMessages );
|
2018-03-23 09:13:41 +00:00
|
|
|
toggleCustomMessages();
|
|
|
|
|
|
|
|
cbEnabled = OO.ui.infuse( $( '#wpFilterEnabled' ) );
|
|
|
|
cbDeleted = OO.ui.infuse( $( '#wpFilterDeleted' ) );
|
|
|
|
OO.ui.infuse( $( '#wpFilterDeletedLabel' ) );
|
|
|
|
cbEnabled.on( 'change',
|
|
|
|
function () {
|
|
|
|
cbDeleted.setDisabled( cbEnabled.isSelected() );
|
|
|
|
if ( cbEnabled.isSelected() ) {
|
|
|
|
cbDeleted.setSelected( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2013-06-26 21:57:20 +00:00
|
|
|
|
2018-03-23 09:13:41 +00:00
|
|
|
cbDeleted.on( 'change',
|
|
|
|
function () {
|
|
|
|
if ( cbDeleted.isSelected() ) {
|
|
|
|
cbEnabled.setSelected( false );
|
|
|
|
}
|
2018-05-02 16:59:55 +00:00
|
|
|
}
|
2018-03-23 09:13:41 +00:00
|
|
|
);
|
2018-05-02 16:59:55 +00:00
|
|
|
|
2019-02-18 17:36:42 +00:00
|
|
|
$( '#mw-abusefilter-edit-group-input select' ).on( 'change', onFilterGroupChange );
|
2018-03-23 09:13:41 +00:00
|
|
|
|
2019-02-18 17:36:42 +00:00
|
|
|
$( '#mw-abusefilter-export-link' ).on( 'click',
|
2018-03-23 09:13:41 +00:00
|
|
|
function ( e ) {
|
|
|
|
e.preventDefault();
|
|
|
|
$exportBox.toggle();
|
2018-05-02 16:59:55 +00:00
|
|
|
}
|
2018-03-23 09:13:41 +00:00
|
|
|
);
|
|
|
|
}
|
2018-05-02 16:59:55 +00:00
|
|
|
|
2019-02-18 17:36:42 +00:00
|
|
|
$( '#mw-abusefilter-syntaxcheck' ).on( 'click', doSyntaxCheck );
|
|
|
|
$( '#wpFilterBuilder' ).on( 'change', addText );
|
2011-08-26 20:12:34 +00:00
|
|
|
} );
|
2018-11-23 15:44:58 +00:00
|
|
|
}() );
|