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 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 ) )
22 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
28 * Recursively check a condition. Conditions are in the form
29 * array( '&' or '|' or '^', cond1, cond2, ... )
30 * where cond1, cond2, ... are themselves conditions; *OR*
31 * APCOND_EMAILCONFIRMED, *OR*
32 * array( APCOND_EMAILCONFIRMED ), *OR*
33 * array( APCOND_EDITCOUNT, number of edits ), *OR*
34 * array( APCOND_AGE, seconds since registration ), *OR*
35 * similar constructs defined by extensions.
36 * This function evaluates the former type recursively, and passes off to
37 * self::checkCondition for evaluation of the latter type.
39 * @param $cond Mixed: a condition, possibly containing other conditions
40 * @param $user The user to check the conditions against
41 * @return bool Whether the condition is true
43 private static function recCheckCondition( $cond, User
$user ) {
44 $validOps = array( '&', '|', '^', '!' );
45 if( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
47 if( $cond[0] == '&' ) {
48 foreach( array_slice( $cond, 1 ) as $subcond )
49 if( !self
::recCheckCondition( $subcond, $user ) )
52 } elseif( $cond[0] == '|' ) {
53 foreach( array_slice( $cond, 1 ) as $subcond )
54 if( self
::recCheckCondition( $subcond, $user ) )
57 } elseif( $cond[0] == '^' ) {
59 foreach( array_slice( $cond, 1 ) as $subcond ) {
61 $res = self
::recCheckCondition( $subcond, $user );
63 $res = ($res xor self
::recCheckCondition( $subcond, $user ));
66 } elseif ( $cond[0] = '!' ) {
67 foreach( array_slice( $cond, 1 ) as $subcond )
68 if( self
::recCheckCondition( $subcond, $user ) )
73 # If we got here, the array presumably does not contain other condi-
74 # tions; it's not recursive. Pass it off to self::checkCondition.
75 if( !is_array( $cond ) )
76 $cond = array( $cond );
77 return self
::checkCondition( $cond, $user );
81 * As recCheckCondition, but *not* recursive. The only valid conditions
82 * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
83 * APCOND_AGE. Other types will throw an exception if no extension evalu-
86 * @param $cond Array: A condition, which must not contain other conditions
87 * @param $user The user to check the condition against
88 * @return bool Whether the condition is true for the user
90 private static function checkCondition( $cond, User
$user ) {
91 if( count( $cond ) < 1 )
94 case APCOND_EMAILCONFIRMED
:
95 if( User
::isValidEmailAddr( $user->getEmail() ) ) {
96 global $wgEmailAuthentication;
97 if( $wgEmailAuthentication ) {
98 return (bool)$user->getEmailAuthenticationTimestamp();
104 case APCOND_EDITCOUNT
:
105 return $user->getEditCount() >= $cond[1];
107 $age = time() - wfTimestampOrNull( TS_UNIX
, $user->getRegistration() );
108 return $age >= $cond[1];
109 case APCOND_INGROUPS
:
110 $groups = array_slice( $cond, 1 );
111 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
114 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
115 if( $result === null ) {
116 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
118 return $result ?
true : false;