3 * This class checks if user can get extra rights
4 * because of conditions specified in $wgAutopromote
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
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.
18 * Example configuration:
19 * \code $wgHooks['ArticleSaveComplete'][] = array (
20 * 'Autopromote::autopromoteOnceHook',
21 * array( 'somegroup' => array(APCOND_EDITCOUNT, 200) )
24 * The second array should be of the same format as \ref $wgAutopromote.
26 * This funciton simply runs User::autopromoteOnce() on $wgUser. You may
27 * run this method from your custom function if you wish.
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.
32 * @return Always true.
34 * @see User::autopromoteOnce()
37 public static function autopromoteOnceHook($criteria) {
39 $wgUser->autopromoteOnce($criteria);
44 * Get the groups for the given user based on $wgAutopromote.
46 * @param $user User The user to get the groups for
47 * @return array Array of groups to promote to.
49 public static function getAutopromoteGroups( User
$user ) {
50 global $wgAutopromote;
54 foreach ( $wgAutopromote as $group => $cond ) {
55 if ( self
::recCheckCondition( $cond, $user ) ) {
60 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
66 * Get the groups for the given user based on the given criteria.
68 * Does not return groups the user already belongs to or has once belonged.
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.
75 * @return array Groups the user should be promoted to.
77 public static function getAutopromoteOnceGroups( User
$user, $criteria ) {
80 //get the current groups
81 $currentGroups = $user->getGroups();
83 foreach( $criteria as $group => $cond ) {
84 //do not check if the user's already a member
85 if ( in_array($group, $currentGroups))
88 //do not autopromote if the user has belonged to the group
89 $formerGroups = $user->getFormerGroups();
90 if ( in_array($group, $formerGroups) )
93 //finally - check the conditions
94 if ( self
::recCheckCondition($cond, $user) )
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.
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
116 private static function recCheckCondition( $cond, User
$user ) {
117 $validOps = array( '&', '|', '^', '!' );
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 ) ) {
129 } elseif ( $cond[0] == '|' ) {
130 foreach ( array_slice( $cond, 1 ) as $subcond ) {
131 if ( self
::recCheckCondition( $subcond, $user ) ) {
137 } elseif ( $cond[0] == '^' ) {
139 foreach ( array_slice( $cond, 1 ) as $subcond ) {
140 if ( is_null( $res ) ) {
141 $res = self
::recCheckCondition( $subcond, $user );
143 $res = ( $res xor self
::recCheckCondition( $subcond, $user ) );
148 } elseif ( $cond[0] == '!' ) {
149 foreach ( array_slice( $cond, 1 ) as $subcond ) {
150 if ( self
::recCheckCondition( $subcond, $user ) ) {
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 );
164 return self
::checkCondition( $cond, $user );
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-
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
177 private static function checkCondition( $cond, User
$user ) {
178 global $wgEmailAuthentication;
179 if ( count( $cond ) < 1 ) {
184 case APCOND_EMAILCONFIRMED
:
185 if ( User
::isValidEmailAddr( $user->getEmail() ) ) {
186 if ( $wgEmailAuthentication ) {
187 return (bool)$user->getEmailAuthenticationTimestamp();
193 case APCOND_EDITCOUNT
:
194 return $user->getEditCount() >= $cond[1];
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 );
205 return $cond[1] == wfGetIP();
206 case APCOND_IPINRANGE
:
207 return IP
::isInRange( wfGetIP(), $cond[1] );
209 return $user->isBlocked();
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!" );
217 return (bool)$result;