r102138 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102137‎ | r102138 | r102139 >
Date:01:15, 6 November 2011
Author:cryptocoryne
Status:resolved (Comments)
Tags:
Comment:
Allow to define custom actions and their callback functions
Modified paths:
  • /trunk/extensions/AbuseFilter/AbuseFilter.class.php (modified) (history)
  • /trunk/extensions/AbuseFilter/AbuseFilter.php (modified) (history)

Diff [purge]

Index: trunk/extensions/AbuseFilter/AbuseFilter.php
@@ -166,3 +166,5 @@
167167
168168 // Block duration
169169 $wgAbuseFilterBlockDuration = 'indefinite';
 170+
 171+$wgAbuseFilterCustomActionsHandlers = false;
\ No newline at end of file
Index: trunk/extensions/AbuseFilter/AbuseFilter.class.php
@@ -935,6 +935,8 @@
936936 public static function takeConsequenceAction( $action, $parameters, $title,
937937 $vars, $rule_desc )
938938 {
 939+ global $wgAbuseFilterCustomActionsHandlers;
 940+
939941 $display = '';
940942 switch ( $action ) {
941943 case 'disallow':
@@ -1073,6 +1075,18 @@
10741076 AbuseFilter::$tagsToSet[$actionID] = $parameters;
10751077 break;
10761078 default:
 1079+ if( is_array( $wgAbuseFilterCustomActionsHandlers ) &&
 1080+ in_array( $action, array_keys( $wgAbuseFilterCustomActionsHandlers ) ) )
 1081+ {
 1082+ $custom_function = $wgAbuseFilterCustomActionsHandlers[$action];
 1083+ if( is_callable( $custom_function ) ) {
 1084+ $ok = call_user_func( $custom_function, $action, $parameters, $title, $vars, $rule_desc );
 1085+ }
 1086+ if( $ok ) {
 1087+ $display .= wfMsgExt( 'abusefilter-' . $action, 'parseinline', array() ) . "<br />\n";
 1088+ }
 1089+ break;
 1090+ }
10771091 wfDebugLog( 'AbuseFilter', "Unrecognised action $action" );
10781092 }
10791093

Follow-up revisions

RevisionCommit summaryAuthorDate
r102197Followup r102138 -- fixes according to Werdna's commentcryptocoryne21:05, 6 November 2011

Comments

#Comment by Werdna (talk | contribs)   06:41, 6 November 2011

Hey there! This is an awesome improvement, I'm so glad somebody's helping me make AbuseFilter more extensible! I know the code isn't that easy to work with.

I know this is one of your first features, so I'll just give you a bit of feedback to help you get it all polished up.

+$wgAbuseFilterCustomActionsHandlers = false;

Why not just initialise to an empty array? The way you've done it, extensions have to have logic to check if it's been initialised yet. If you just set it to an empty array, this gets side-stepped.

+				if( is_array( $wgAbuseFilterCustomActionsHandlers ) &&
+					in_array( $action, array_keys( $wgAbuseFilterCustomActionsHandlers ) ) )
+				{

You can achieve the same result with array_key_exists( $action, $wgAbuseFilterCustomActionsHandlers)

+					if( is_callable( $custom_function ) ) {
+						$ok = call_user_func( $custom_function, $action, $parameters, $title, $vars, $rule_desc );
+					}

$ok won't be set if the function isn't callable. You should set it to false for the case that the function isn't callable (you might also want to consider throwing some sort of error in this case to improve debugging). This avoids a PHP error.

+				}
 				wfDebugLog( 'AbuseFilter', "Unrecognised action $action" );

You might want to make that debug logging appear in an 'else' clause, since the action isn't unrecognised if it's handled by your code.

One more recommendation: instead of using a specific message for the action, you might want to consider letting these custom functions return their own error message. So the function that gets put in $wgAbuseFilterCustomActionsHandlers can return whatever needs to be added to $display

Let me know how you go, and please do tell me if you need any help at all working with AbuseFilter.

#Comment by Nikerabbit (talk | contribs)   10:14, 6 November 2011

Is there any reason to use array_key_exists over isset here?

#Comment by Cryptocoryne (talk | contribs)   21:05, 6 November 2011

Fixed in r102197.

Status & tagging log