4 * This class checks if user can get extra rights
5 * because of conditions specified in $wgAutopromote
9 * Get the groups for the given user based on $wgAutopromote.
11 * @param User $user The user to get the groups for
12 * @return array Array of groups to promote to.
14 public static function getAutopromoteGroups( User
$user ) {
15 global $wgAutopromote;
17 foreach( $wgAutopromote as $group => $cond ) {
18 if( self
::recCheckCondition( $cond, $user ) )
25 * Recursively check a condition. Conditions are in the form
26 * array( '&' or '|' or '^', cond1, cond2, ... )
27 * where cond1, cond2, ... are themselves conditions; *OR*
28 * APCOND_EMAILCONFIRMED, *OR*
29 * array( APCOND_EMAILCONFIRMED ), *OR*
30 * array( APCOND_EDITCOUNT, number of edits ), *OR*
31 * array( APCOND_AGE, seconds since registration ), *OR*
32 * similar constructs defined by extensions.
33 * This function evaluates the former type recursively, and passes off to
34 * self::checkCondition for evaluation of the latter type.
36 * @param mixed $cond A condition, possibly containing other conditions
37 * @param User $user The user to check the conditions against
38 * @return bool Whether the condition is true
40 private static function recCheckCondition( $cond, User
$user ) {
41 $validOps = array( '&', '|', '^' );
42 if( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
44 if( $cond[0] == '&' ) {
45 foreach( array_slice( $cond, 1 ) as $subcond )
46 if( !self
::recCheckCondition( $subcond, $user ) )
49 } elseif( $cond[0] == '|' ) {
50 foreach( array_slice( $cond, 1 ) as $subcond )
51 if( self
::recCheckCondition( $subcond, $user ) )
54 } elseif( $cond[0] == '^' ) {
56 foreach( array_slice( $cond, 1 ) as $subcond ) {
58 $res = self
::recCheckCondition( $subcond, $user );
60 $res = ($res xor self
::recCheckCondition( $subcond, $user ));
65 # If we got here, the array presumably does not contain other condi-
66 # tions; it's not recursive. Pass it off to self::checkCondition.
67 if( !is_array( $cond ) )
68 $cond = array( $cond );
69 return self
::checkCondition( $cond, $user );
73 * As recCheckCondition, but *not* recursive. The only valid conditions
74 * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
75 * APCOND_AGE. Other types will throw an exception if no extension evalu-
78 * @param array $cond A condition, which must not contain other conditions
79 * @param User $user The user to check the condition against
80 * @return bool Whether the condition is true for the user
82 private static function checkCondition( $cond, User
$user ) {
83 if( count( $cond ) < 1 )
86 case APCOND_EMAILCONFIRMED
:
87 if( User
::isValidEmailAddr( $user->getEmail() ) ) {
88 global $wgEmailAuthentication;
89 if( $wgEmailAuthentication ) {
90 return $user->getEmailAuthenticationTimestamp() ?
true : false;
96 case APCOND_EDITCOUNT
:
97 return $user->getEditCount() >= $cond[1];
99 $age = time() - wfTimestampOrNull( TS_UNIX
, $user->getRegistration() );
100 return $age >= $cond[1];
101 case APCOND_INGROUPS
:
102 $groups = array_slice( $cond, 1 );
103 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
106 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
107 if( $result === null ) {
108 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
110 return $result ?
true : false;