AbuseFilter: Make it possible to have differing default warning messages for differing filter groups.

Includes JS to sneakily change the default message if the user changes the filter group without having selected the "warn" action yet.

Change-Id: Ic753c3e018321dba3bf9f6d7bcee10a49c9faac8
This commit is contained in:
Andrew Garrett 2012-06-06 15:18:31 +10:00 committed by mlitn
parent 571ee59e61
commit 18bac6fed9
3 changed files with 53 additions and 15 deletions

View file

@ -173,3 +173,8 @@ $wgAbuseFilterCustomActionsHandlers = array();
// Valid "filter groups"  used for applying edit filters to certain types of actions
$wgAbuseFilterValidGroups = array('default');
// Default warning messages, per filter group
$wgAbuseFilterDefaultWarningMessage = array(
'default' => 'abusefilter-warning',
);

View file

@ -18,6 +18,9 @@ class AbuseFilterViewEdit extends AbuseFilterView {
$filter = $this->mFilter;
$history_id = $this->mHistoryID;
// Add default warning messages
$this->exposeWarningMessages();
if ( $filter == 'new' && !$user->isAllowed( 'abusefilter-modify' ) ) {
$out->addWikiMsg( 'abusefilter-edit-notallowed' );
return;
@ -307,7 +310,7 @@ class AbuseFilterViewEdit extends AbuseFilterView {
if ( count($wgAbuseFilterValidGroups) > 1 ) {
$groupSelector = new XmlSelect(
'wpFilterGroup',
'mw-abusefilter-edit-group',
'mw-abusefilter-edit-group-input',
'default'
);
@ -509,21 +512,20 @@ class AbuseFilterViewEdit extends AbuseFilterView {
foreach ( $wgAbuseFilterAvailableActions as $action ) {
$output .= $this->buildConsequenceSelector(
$action, $setActions[$action], @$actions[$action]['parameters'] );
$action, $setActions[$action], @$actions[$action]['parameters'], $row );
}
return $output;
}
/**
* Builds a selector for a single AbuseFilter action.
* @param $action String identifier for the action.
* Should be in $wgAbuseFilterAvailableActions
* @param $set Whether or not the action is set.
* @param $parameters If the action is set up, the parameters for it.
* @return string HTML text for the action editor, or NULL if the $action is invalid.
* @param $action The action to build an editor for
* @param $set Whether or not the action is activated
* @param $parameters Action parameters
* @param $row abuse_filter row object
* @return string
*/
function buildConsequenceSelector( $action, $set, $parameters ) {
function buildConsequenceSelector( $action, $set, $parameters, $row ) {
global $wgAbuseFilterAvailableActions;
if ( !in_array( $action, $wgAbuseFilterAvailableActions ) ) {
@ -592,6 +594,7 @@ class AbuseFilterViewEdit extends AbuseFilterView {
array( 'disabled' => '1', 'class' => 'mw-abusefilter-action-checkbox' ) );
return Xml::tags( 'p', null, $checkbox );
case 'warn':
global $wgAbuseFilterDefaultWarningMessage;
$output = '';
$checkbox = Xml::checkLabel(
wfMsg( 'abusefilter-edit-action-warn' ),
@ -600,7 +603,17 @@ class AbuseFilterViewEdit extends AbuseFilterView {
$set,
array( 'class' => 'mw-abusefilter-action-checkbox' ) + $cbReadOnlyAttrib );
$output .= Xml::tags( 'p', null, $checkbox );
$warnMsg = empty( $set ) ? 'abusefilter-warning' : $parameters[0];
if ( $set ) {
$warnMsg = $parameters[0];
} elseif (
$row &&
$row->af_group &&
isset($wgAbuseFilterDefaultWarningMessage[$row->af_group] )
) {
$warnMsg = $wgAbuseFilterDefaultWarningMessage[$row->af_group];
} else {
$warnMsg = 'abusefilter-warning';
}
$warnFields['abusefilter-edit-warn-message'] =
$this->getExistingSelector( $warnMsg );
@ -922,4 +935,9 @@ class AbuseFilterViewEdit extends AbuseFilterView {
return AbuseFilter::translateFromHistory( $row );
}
protected function exposeWarningMessages() {
global $wgOut, $wgAbuseFilterDefaultWarningMessage;
$wgOut->addJsConfigVars( 'wgAbuseFilterDefaultWarningMessage', $wgAbuseFilterDefaultWarningMessage );
}
}

View file

@ -1,7 +1,7 @@
/**
* AbuseFilter editing stuff
*/
new ( function( $, mw ) {
( function( $, mw ) {
/**
* Filter textarea
*
@ -160,9 +160,11 @@ new ( function( $, mw ) {
$.get( mw.config.get('wgScript'), {
title: 'MediaWiki:' + message,
action: 'render'
}, function( data ) {
$( '#mw-abusefilter-warn-preview' ).html( data )
} );
},
function( data ) {
$( '#mw-abusefilter-warn-preview' ).html( data );
}
);
};
/**
@ -198,9 +200,22 @@ new ( function( $, mw ) {
var $exportBox = $( '#mw-abusefilter-export' );
$( '#mw-abusefilter-export-link' ).toggle( function() {
$exportBox.show()
$exportBox.show();
}, function() {
$exportBox.hide();
} );
$('#mw-abusefilter-edit-group-input').change( function() {
var newVal = mw.config.get('wgAbuseFilterDefaultWarningMessage')[$(this).val()];
if ( !$('#mw-abusefilter-action-warn-checkbox').is(':checked') ) {
if ($('#mw-abusefilter-warn-message-existing').find("option[value='"+newVal+"']").length ) {
$('#mw-abusefilter-warn-message-existing').val(newVal);
$('#mw-abusefilter-warn-message-other').val('');
} else {
$('#mw-abusefilter-warn-message-existing').val('other');
$('#mw-abusefilter-warn-message-other').val(newVal);
}
}
});
} );
})( jQuery, mediaWiki );