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-03-23 09:13:41 +00:00
|
|
|
( function ( mw, $, OO ) {
|
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
|
|
|
*
|
|
|
|
* @param {Object} data Data returned from the AJAX request
|
|
|
|
*/
|
|
|
|
function processSyntaxResult( data ) {
|
2018-03-30 06:55:03 +00:00
|
|
|
var position;
|
2012-10-26 00:23:17 +00:00
|
|
|
data = data.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
|
|
|
|
.focus()
|
|
|
|
.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 ) {
|
|
|
|
var msg = error === 'http' ? 'abusefilter-http-error' : 'unknown-error';
|
2012-10-26 00:23:17 +00:00
|
|
|
processSyntaxResultAlways(
|
2014-07-30 14:46:53 +00:00
|
|
|
mw.msg( msg, 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.
|
|
|
|
*
|
|
|
|
* @context 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() {
|
2018-03-30 06:55:03 +00:00
|
|
|
var filter = $plainTextBox.val(),
|
2018-02-07 21:08:04 +00:00
|
|
|
api = new mw.Api();
|
|
|
|
|
|
|
|
$( 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
|
|
|
|
|
|
|
api.post( {
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @context 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() {
|
2018-03-15 17:22:37 +00:00
|
|
|
var filterId = $.trim( $( '#mw-abusefilter-load-filter input' ).val() ),
|
2013-04-17 12:27:46 +00:00
|
|
|
api;
|
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
|
2013-04-17 12:27:46 +00:00
|
|
|
api = new mw.Api();
|
2012-10-26 00:23:17 +00:00
|
|
|
api.get( {
|
|
|
|
action: 'query',
|
|
|
|
list: 'abusefilters',
|
|
|
|
abfprop: 'pattern',
|
|
|
|
abfstartid: filterId,
|
|
|
|
abfendid: filterId,
|
|
|
|
abflimit: 1
|
|
|
|
} )
|
2018-02-07 21:08:04 +00:00
|
|
|
.always( function () {
|
|
|
|
$.removeSpinner( 'fetch-spinner' );
|
|
|
|
} )
|
|
|
|
.done( function ( data ) {
|
|
|
|
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-03-23 09:13:41 +00:00
|
|
|
$( '.mw-abusefilter-action-checkbox input' ).each( function () {
|
2011-09-30 00:48:00 +00:00
|
|
|
// mw-abusefilter-action-checkbox-{$action}
|
2018-03-23 09:13:41 +00:00
|
|
|
var action = this.parentNode.id.substr( 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 ) {
|
2016-12-26 10:34:16 +00:00
|
|
|
var api = new mw.Api(),
|
|
|
|
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
|
|
|
|
2014-10-04 14:42:46 +00:00
|
|
|
if ( !$element.is( ':visible' ) ) {
|
2018-03-23 09:13:41 +00:00
|
|
|
api.get( {
|
|
|
|
action: 'query',
|
|
|
|
meta: 'allmessages',
|
|
|
|
ammessages: message,
|
|
|
|
amargs: args.join( '|' )
|
|
|
|
} )
|
|
|
|
.done( function ( data ) {
|
|
|
|
api.parse( data.query.allmessages[ 0 ][ '*' ], {
|
|
|
|
disablelimitreport: '',
|
|
|
|
preview: '',
|
|
|
|
prop: 'text',
|
|
|
|
title: 'MediaWiki:' + message
|
|
|
|
} )
|
|
|
|
.done( function ( 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
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} else {
|
2014-10-04 14:42:46 +00:00
|
|
|
$element.hide();
|
|
|
|
previewButton.setFlags( { destructive: false, progressive: true } );
|
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',
|
|
|
|
url = mw.config.get( 'wgScript' ) +
|
|
|
|
'?title=MediaWiki:' + mw.util.wikiUrlencode( 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-23 09:13:41 +00:00
|
|
|
* Called if the filter group (#mw-abusefilter-edit-group-input select) is changed.
|
2013-04-02 19:44:01 +00:00
|
|
|
*
|
|
|
|
* @context 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
|
|
|
|
|
|
|
if ( !$( '#mw-abusefilter-action-warn-checkbox' ).is( ':checked' ) ) {
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$( '#mw-abusefilter-action-disallow-checkbox' ).is( ':checked' ) ) {
|
|
|
|
$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
|
|
|
*
|
|
|
|
* @context 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();
|
|
|
|
$( '#mw-abusefilter-load' ).click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 ) {
|
|
|
|
var disabled = config.disabled.length !== 0,
|
|
|
|
// mw-abusefilter-throttle-parameters, mw-abusefilter-tag-parameters
|
|
|
|
$container = $( '#mw-abusefilter-' + action + '-parameters' ),
|
|
|
|
// Character used to separate elements in the textarea.
|
|
|
|
separator = action === 'throttle' ? '\n' : ',',
|
|
|
|
selector, field, hiddenField;
|
|
|
|
|
|
|
|
selector =
|
|
|
|
new OO.ui.TagMultiselectWidget( {
|
|
|
|
inputPosition: 'outline',
|
|
|
|
allowArbitrary: true,
|
|
|
|
allowEditTags: true,
|
|
|
|
selected: config.values,
|
|
|
|
// abusefilter-edit-throttle-placeholder, abusefilter-edit-tag-placeholder
|
|
|
|
placeholder: OO.ui.msg( 'abusefilter-edit-' + action + '-placeholder' ),
|
|
|
|
disabled: disabled
|
|
|
|
} );
|
|
|
|
field =
|
|
|
|
new OO.ui.FieldLayout(
|
|
|
|
selector,
|
|
|
|
{
|
|
|
|
label: $( $.parseHTML( config.label ) ),
|
|
|
|
align: 'top'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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
|
2018-02-07 21:08:04 +00:00
|
|
|
$( document ).ready( 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' ),
|
|
|
|
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-03-23 09:13:41 +00:00
|
|
|
}
|
2018-03-30 06:55:03 +00:00
|
|
|
|
|
|
|
$plainTextBox = $( '#' + mw.config.get( 'abuseFilterBoxName' ) );
|
|
|
|
|
|
|
|
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.session.setOption( 'useWorker', false );
|
|
|
|
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() );
|
|
|
|
} );
|
|
|
|
|
|
|
|
$( '#mw-abusefilter-switcheditor' ).click( switchEditor );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2011-08-26 20:12:34 +00:00
|
|
|
// Hide the syntax ok message when the text changes
|
2018-09-03 14:03:24 +00:00
|
|
|
$plainTextBox.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();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2012-10-26 00:23:17 +00:00
|
|
|
$( '#mw-abusefilter-load' ).click( fetchFilter );
|
2013-07-17 13:22:11 +00:00
|
|
|
$( '#mw-abusefilter-load-filter' ).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
|
2014-10-04 14:42:46 +00:00
|
|
|
$( '#mw-abusefilter-warn-preview-button' ).click(
|
|
|
|
function () { previewMessage( 'warn' ); }
|
|
|
|
);
|
|
|
|
$( '#mw-abusefilter-disallow-preview-button' ).click(
|
|
|
|
function () { previewMessage( 'disallow' ); }
|
|
|
|
);
|
|
|
|
$( '#mw-abusefilter-warn-edit-button' ).click(
|
|
|
|
function () { editMessage( 'warn' ); }
|
|
|
|
);
|
|
|
|
$( '#mw-abusefilter-disallow-edit-button' ).click(
|
|
|
|
function () { editMessage( 'disallow' ); }
|
|
|
|
);
|
2018-03-23 09:13:41 +00:00
|
|
|
$( '.mw-abusefilter-action-checkbox input' ).click( hideDeselectedActions );
|
|
|
|
hideDeselectedActions();
|
|
|
|
|
|
|
|
$( '#wpFilterGlobal' ).change( toggleCustomMessages );
|
|
|
|
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
|
|
|
|
2018-03-23 09:13:41 +00:00
|
|
|
$( '#mw-abusefilter-edit-group-input select' ).change( onFilterGroupChange );
|
|
|
|
|
|
|
|
$( '#mw-abusefilter-export-link' ).click(
|
|
|
|
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
|
|
|
|
2012-10-26 00:23:17 +00:00
|
|
|
$( '#mw-abusefilter-syntaxcheck' ).click( doSyntaxCheck );
|
|
|
|
$( '#wpFilterBuilder' ).change( addText );
|
2011-08-26 20:12:34 +00:00
|
|
|
|
|
|
|
} );
|
2018-03-23 09:13:41 +00:00
|
|
|
}( mediaWiki, jQuery, OO ) );
|