r24295 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24294‎ | r24295 | r24296 >
Date:01:00, 21 July 2007
Author:robchurch
Status:old
Tags:
Comment:
Support setting rights as well
Modified paths:
  • /trunk/extensions/AutomaticGroups/AutomaticGroups.php (modified) (history)
  • /trunk/extensions/AutomaticGroups/README (modified) (history)

Diff [purge]

Index: trunk/extensions/AutomaticGroups/AutomaticGroups.php
@@ -2,7 +2,8 @@
33
44 /**
55 * Extension provides convenient configuration of additional
6 - * effective groups based on a user's account age and edit count
 6+ * effective user rights and groups based on a user's account
 7+ * age and edit count
78 *
89 * @addtogroup Extensions
910 * @author Rob Church <robchur@gmail.com>
@@ -13,14 +14,15 @@
1415 'name' => 'Automatic Groups',
1516 'author' => 'Rob Church',
1617 'url' => 'http://www.mediawiki.org/wiki/Extension:Automatic_Groups',
17 - 'description' => 'Provides a convenient means to configure automatic group
 18+ 'description' => 'Convenient configuration of user rights and group
1819 membership based on user account age and edit count',
1920 );
2021
2122 /**
22 - * Register hook callback
 23+ * Register hook callbacks
2324 */
2425 $wgHooks['UserEffectiveGroups'][] = 'efAutomaticGroups';
 26+ $wgHooks['UserGetRights'][] = 'efAutomaticRights';
2527
2628 /**
2729 * Automatic group configuration
@@ -33,28 +35,65 @@
3436 $wgAutomaticGroups = array();
3537
3638 /**
37 - * Main execution function
 39+ * Automatic rights configuration; same format as $wgAutomaticGroups
 40+ */
 41+ $wgAutomaticRights = array();
 42+
 43+ /**
 44+ * Hook effective groups assignment
3845 *
39 - * @param User $user User to set groups for
40 - * @param array $groups User's explicit groups
 46+ * @param User $user User to add automatic groups to
 47+ * @param array $groups Group list to be modified
4148 * @return bool
4249 */
4350 function efAutomaticGroups( $user, &$groups ) {
4451 global $wgAutomaticGroups;
 52+ $groups = array_merge(
 53+ $groups,
 54+ efCalculateAutomaticRights( $user, $wgAutomaticGroups )
 55+ );
 56+ return true;
 57+ }
 58+
 59+ /**
 60+ * Hook effective rights assignment
 61+ *
 62+ * @param User $user User to add automatic groups to
 63+ * @param array $rights Rights list to be modified
 64+ * @return bool
 65+ */
 66+ function efAutomaticRights( $user, &$rights ) {
 67+ global $wgAutomaticRights;
 68+ $rights = array_merge(
 69+ $rights,
 70+ efCalculateAutomaticRights( $user, $wgAutomaticRights )
 71+ );
 72+ return true;
 73+ }
 74+
 75+ /**
 76+ * Calculate automatic rights or group membership
 77+ * using the supplied criteria
 78+ *
 79+ * @param User $user User to get rights/groups for
 80+ * @param array $criteria Criteria map
 81+ * @return array
 82+ */
 83+ function efCalculateAutomaticRights( $user, $criteria ) {
 84+ $attributes = array();
4585 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
46 - foreach( $wgAutomaticGroups as $group => $criteria ) {
47 - if( isset( $criteria['age'] ) && $age < $criteria['age'] )
 86+ foreach( $criteria as $attribute => $criterion ) {
 87+ if( isset( $criterion['age'] ) && $age < $criterion['age'] )
4888 continue;
49 - if( isset( $criteria['edits'] ) && $user->getEditCount() < $criteria['edits'] )
 89+ if( isset( $criterion['edits'] ) && $user->getEditCount() < $criterion['edits'] )
5090 continue;
51 - # User qualifies for this group
52 - $groups[] = $group;
 91+ # User qualifies for this attribute
 92+ $attributes[] = $attribute;
5393 }
54 - return true;
 94+ return $attributes;
5595 }
56 -
 96+
5797 } else {
5898 echo( "This file is an extension to MediaWiki and cannot be used standalone.\n" );
5999 exit( 1 );
60 -}
61 -
 100+}
\ No newline at end of file
Index: trunk/extensions/AutomaticGroups/README
@@ -5,9 +5,10 @@
66 == Overview ==
77
88 The Automatic Groups extension provides a convenient means to configure
9 -additional automatic groups for users matching specific criteria.
 9+additional automatic rights and user group membership for users matching
 10+specific criteria.
1011
11 -Groups can be added based on
 12+Rights and groups can be added based on
1213
1314 * account age
1415 * edit count
@@ -17,12 +18,13 @@
1819 == Configuration ==
1920
2021 Group criteria are set using the $wgAutomaticGroups global, which is an
21 -array mapping group names to an array of criteria.
 22+array mapping group names to an array of criteria. Rights criteria are
 23+set using $wgAutomaticRights, which has the same format.
2224
2325 The criteria are indexed by attribute, mapping to the minimum value
24 -required for that criterion. Multiple criteria are supported per group.
 26+required for that criterion.
2527
26 -Valid criteria are
 28+Valid criteria are:
2729
2830 * 'edits'
2931 Minimum edit count
@@ -36,7 +38,7 @@
3739
3840 $wgAutomaticGroups['uploaders'] = array( 'age' => 86400 * 4 );
3941
40 -To assign the group 'patrollers' to all accounts at least 4 days old
 42+To assign the 'delete' right to all accounts at least 4 days old
4143 with at least 250 edits, one might use
4244
43 - $wgAutomaticGroups['patrollers'] = array( 'age' => 86400 * 4, 'edits' => 250 );
\ No newline at end of file
 45+ $wgAutomaticRights['delete'] = array( 'age' => 86400 * 4, 'edits' => 250 );
\ No newline at end of file

Status & tagging log