Added one-time promote support via Autopromote::autopromoteOnceHook function. This...
[lhc/web/wiklou.git] / includes / Autopromote.php
1 <?php
2 /**
3 * This class checks if user can get extra rights
4 * because of conditions specified in $wgAutopromote
5 */
6
7 class Autopromote {
8 /**
9 * A function which may be assigned to a hook in order to check
10 * autopromotion of the current user (\ref $wgUser) to the specified
11 * group.
12 *
13 * Contrary to autopromotion by \ref $wgAutopromote, the group will be
14 * possible to remove manually via Special:UserRights. In such case it
15 * will not be re-added autmoatically. The user will also not lose the
16 * group if they no longer meet the criteria.
17 *
18 * Example configuration:
19 * \code $wgHooks['ArticleSaveComplete'][] = array (
20 * 'Autopromote::autopromoteOnceHook',
21 * array( 'somegroup' => array(APCOND_EDITCOUNT, 200) )
22 * ); \endcode
23 *
24 * The second array should be of the same format as \ref $wgAutopromote.
25 *
26 * This funciton simply runs User::autopromoteOnce() on $wgUser. You may
27 * run this method from your custom function if you wish.
28 *
29 * @param $criteria array Groups and conditions which must be met in order to
30 * aquire these groups. Array of the same format as \ref $wgAutopromote.
31 *
32 * @return Always true.
33 *
34 * @see User::autopromoteOnce()
35 * @see $wgAutopromote
36 */
37 public static function autopromoteOnceHook($criteria) {
38 global $wgUser;
39 $wgUser->autopromoteOnce($criteria);
40 return true;
41 }
42
43 /**
44 * Get the groups for the given user based on $wgAutopromote.
45 *
46 * @param $user User The user to get the groups for
47 * @return array Array of groups to promote to.
48 */
49 public static function getAutopromoteGroups( User $user ) {
50 global $wgAutopromote;
51
52 $promote = array();
53
54 foreach ( $wgAutopromote as $group => $cond ) {
55 if ( self::recCheckCondition( $cond, $user ) ) {
56 $promote[] = $group;
57 }
58 }
59
60 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
61
62 return $promote;
63 }
64
65 /**
66 * Get the groups for the given user based on the given criteria.
67 *
68 * Does not return groups the user already belongs to or has once belonged.
69 *
70 * @param $user The user to get the groups for
71 * @param $criteria array Groups and conditions the user must meet in order
72 * to be promoted to these groups. Array of the same format as
73 * \ref $wgAutopromote.
74 *
75 * @return array Groups the user should be promoted to.
76 */
77 public static function getAutopromoteOnceGroups( User $user, $criteria ) {
78 $promote = array();
79
80 //get the current groups
81 $currentGroups = $user->getGroups();
82
83 foreach( $criteria as $group => $cond ) {
84 //do not check if the user's already a member
85 if ( in_array($group, $currentGroups))
86 continue;
87
88 //do not autopromote if the user has belonged to the group
89 $formerGroups = $user->getFormerGroups();
90 if ( in_array($group, $formerGroups) )
91 continue;
92
93 //finally - check the conditions
94 if ( self::recCheckCondition($cond, $user) )
95 $promote[] = $group;
96 }
97 return $promote;
98 }
99
100 /**
101 * Recursively check a condition. Conditions are in the form
102 * array( '&' or '|' or '^', cond1, cond2, ... )
103 * where cond1, cond2, ... are themselves conditions; *OR*
104 * APCOND_EMAILCONFIRMED, *OR*
105 * array( APCOND_EMAILCONFIRMED ), *OR*
106 * array( APCOND_EDITCOUNT, number of edits ), *OR*
107 * array( APCOND_AGE, seconds since registration ), *OR*
108 * similar constructs defined by extensions.
109 * This function evaluates the former type recursively, and passes off to
110 * self::checkCondition for evaluation of the latter type.
111 *
112 * @param $cond Mixed: a condition, possibly containing other conditions
113 * @param $user User The user to check the conditions against
114 * @return bool Whether the condition is true
115 */
116 private static function recCheckCondition( $cond, User $user ) {
117 $validOps = array( '&', '|', '^', '!' );
118
119 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
120 # Recursive condition
121 if ( $cond[0] == '&' ) {
122 foreach ( array_slice( $cond, 1 ) as $subcond ) {
123 if ( !self::recCheckCondition( $subcond, $user ) ) {
124 return false;
125 }
126 }
127
128 return true;
129 } elseif ( $cond[0] == '|' ) {
130 foreach ( array_slice( $cond, 1 ) as $subcond ) {
131 if ( self::recCheckCondition( $subcond, $user ) ) {
132 return true;
133 }
134 }
135
136 return false;
137 } elseif ( $cond[0] == '^' ) {
138 $res = null;
139 foreach ( array_slice( $cond, 1 ) as $subcond ) {
140 if ( is_null( $res ) ) {
141 $res = self::recCheckCondition( $subcond, $user );
142 } else {
143 $res = ( $res xor self::recCheckCondition( $subcond, $user ) );
144 }
145 }
146
147 return $res;
148 } elseif ( $cond[0] == '!' ) {
149 foreach ( array_slice( $cond, 1 ) as $subcond ) {
150 if ( self::recCheckCondition( $subcond, $user ) ) {
151 return false;
152 }
153 }
154
155 return true;
156 }
157 }
158 # If we got here, the array presumably does not contain other condi-
159 # tions; it's not recursive. Pass it off to self::checkCondition.
160 if ( !is_array( $cond ) ) {
161 $cond = array( $cond );
162 }
163
164 return self::checkCondition( $cond, $user );
165 }
166
167 /**
168 * As recCheckCondition, but *not* recursive. The only valid conditions
169 * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
170 * APCOND_AGE. Other types will throw an exception if no extension evalu-
171 * ates them.
172 *
173 * @param $cond Array: A condition, which must not contain other conditions
174 * @param $user User The user to check the condition against
175 * @return bool Whether the condition is true for the user
176 */
177 private static function checkCondition( $cond, User $user ) {
178 global $wgEmailAuthentication;
179 if ( count( $cond ) < 1 ) {
180 return false;
181 }
182
183 switch( $cond[0] ) {
184 case APCOND_EMAILCONFIRMED:
185 if ( User::isValidEmailAddr( $user->getEmail() ) ) {
186 if ( $wgEmailAuthentication ) {
187 return (bool)$user->getEmailAuthenticationTimestamp();
188 } else {
189 return true;
190 }
191 }
192 return false;
193 case APCOND_EDITCOUNT:
194 return $user->getEditCount() >= $cond[1];
195 case APCOND_AGE:
196 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
197 return $age >= $cond[1];
198 case APCOND_AGE_FROM_EDIT:
199 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
200 return $age >= $cond[1];
201 case APCOND_INGROUPS:
202 $groups = array_slice( $cond, 1 );
203 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
204 case APCOND_ISIP:
205 return $cond[1] == wfGetIP();
206 case APCOND_IPINRANGE:
207 return IP::isInRange( wfGetIP(), $cond[1] );
208 case APCOND_BLOCKED:
209 return $user->isBlocked();
210 default:
211 $result = null;
212 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
213 if ( $result === null ) {
214 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
215 }
216
217 return (bool)$result;
218 }
219 }
220 }