Index: trunk/extensions/AutomaticGroups/AutomaticGroups.php |
— | — | @@ -2,7 +2,8 @@ |
3 | 3 | |
4 | 4 | /** |
5 | 5 | * 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 |
7 | 8 | * |
8 | 9 | * @addtogroup Extensions |
9 | 10 | * @author Rob Church <robchur@gmail.com> |
— | — | @@ -13,14 +14,15 @@ |
14 | 15 | 'name' => 'Automatic Groups', |
15 | 16 | 'author' => 'Rob Church', |
16 | 17 | '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 |
18 | 19 | membership based on user account age and edit count', |
19 | 20 | ); |
20 | 21 | |
21 | 22 | /** |
22 | | - * Register hook callback |
| 23 | + * Register hook callbacks |
23 | 24 | */ |
24 | 25 | $wgHooks['UserEffectiveGroups'][] = 'efAutomaticGroups'; |
| 26 | + $wgHooks['UserGetRights'][] = 'efAutomaticRights'; |
25 | 27 | |
26 | 28 | /** |
27 | 29 | * Automatic group configuration |
— | — | @@ -33,28 +35,65 @@ |
34 | 36 | $wgAutomaticGroups = array(); |
35 | 37 | |
36 | 38 | /** |
37 | | - * Main execution function |
| 39 | + * Automatic rights configuration; same format as $wgAutomaticGroups |
| 40 | + */ |
| 41 | + $wgAutomaticRights = array(); |
| 42 | + |
| 43 | + /** |
| 44 | + * Hook effective groups assignment |
38 | 45 | * |
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 |
41 | 48 | * @return bool |
42 | 49 | */ |
43 | 50 | function efAutomaticGroups( $user, &$groups ) { |
44 | 51 | 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(); |
45 | 85 | $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'] ) |
48 | 88 | continue; |
49 | | - if( isset( $criteria['edits'] ) && $user->getEditCount() < $criteria['edits'] ) |
| 89 | + if( isset( $criterion['edits'] ) && $user->getEditCount() < $criterion['edits'] ) |
50 | 90 | continue; |
51 | | - # User qualifies for this group |
52 | | - $groups[] = $group; |
| 91 | + # User qualifies for this attribute |
| 92 | + $attributes[] = $attribute; |
53 | 93 | } |
54 | | - return true; |
| 94 | + return $attributes; |
55 | 95 | } |
56 | | - |
| 96 | + |
57 | 97 | } else { |
58 | 98 | echo( "This file is an extension to MediaWiki and cannot be used standalone.\n" ); |
59 | 99 | exit( 1 ); |
60 | | -} |
61 | | - |
| 100 | +} |
\ No newline at end of file |
Index: trunk/extensions/AutomaticGroups/README |
— | — | @@ -5,9 +5,10 @@ |
6 | 6 | == Overview == |
7 | 7 | |
8 | 8 | 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. |
10 | 11 | |
11 | | -Groups can be added based on |
| 12 | +Rights and groups can be added based on |
12 | 13 | |
13 | 14 | * account age |
14 | 15 | * edit count |
— | — | @@ -17,12 +18,13 @@ |
18 | 19 | == Configuration == |
19 | 20 | |
20 | 21 | 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. |
22 | 24 | |
23 | 25 | 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. |
25 | 27 | |
26 | | -Valid criteria are |
| 28 | +Valid criteria are: |
27 | 29 | |
28 | 30 | * 'edits' |
29 | 31 | Minimum edit count |
— | — | @@ -36,7 +38,7 @@ |
37 | 39 | |
38 | 40 | $wgAutomaticGroups['uploaders'] = array( 'age' => 86400 * 4 ); |
39 | 41 | |
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 |
41 | 43 | with at least 250 edits, one might use |
42 | 44 | |
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 |