3 * This class checks if user can get extra rights
4 * 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;
19 foreach ( $wgAutopromote as $group => $cond ) {
20 if ( self
::recCheckCondition( $cond, $user ) )
24 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
30 * Recursively check a condition. Conditions are in the form
31 * array( '&' or '|' or '^', cond1, cond2, ... )
32 * where cond1, cond2, ... are themselves conditions; *OR*
33 * APCOND_EMAILCONFIRMED, *OR*
34 * array( APCOND_EMAILCONFIRMED ), *OR*
35 * array( APCOND_EDITCOUNT, number of edits ), *OR*
36 * array( APCOND_AGE, seconds since registration ), *OR*
37 * similar constructs defined by extensions.
38 * This function evaluates the former type recursively, and passes off to
39 * self::checkCondition for evaluation of the latter type.
41 * @param $cond Mixed: a condition, possibly containing other conditions
42 * @param $user The user to check the conditions against
43 * @return bool Whether the condition is true
45 private static function recCheckCondition( $cond, User
$user ) {
46 $validOps = array( '&', '|', '^', '!' );
48 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
50 if ( $cond[0] == '&' ) {
51 foreach ( array_slice( $cond, 1 ) as $subcond ) {
52 if ( !self
::recCheckCondition( $subcond, $user ) ) {
58 } elseif ( $cond[0] == '|' ) {
59 foreach ( array_slice( $cond, 1 ) as $subcond ) {
60 if ( self
::recCheckCondition( $subcond, $user ) ) {
66 } elseif ( $cond[0] == '^' ) {
68 foreach ( array_slice( $cond, 1 ) as $subcond ) {
69 if ( is_null( $res ) ) {
70 $res = self
::recCheckCondition( $subcond, $user );
72 $res = ( $res xor self
::recCheckCondition( $subcond, $user ) );
77 } elseif ( $cond[0] = '!' ) {
78 foreach ( array_slice( $cond, 1 ) as $subcond ) {
79 if ( self
::recCheckCondition( $subcond, $user ) ) {
87 # If we got here, the array presumably does not contain other condi-
88 # tions; it's not recursive. Pass it off to self::checkCondition.
89 if ( !is_array( $cond ) ) {
90 $cond = array( $cond );
93 return self
::checkCondition( $cond, $user );
97 * As recCheckCondition, but *not* recursive. The only valid conditions
98 * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
99 * APCOND_AGE. Other types will throw an exception if no extension evalu-
102 * @param $cond Array: A condition, which must not contain other conditions
103 * @param $user The user to check the condition against
104 * @return bool Whether the condition is true for the user
106 private static function checkCondition( $cond, User
$user ) {
107 if ( count( $cond ) < 1 ) {
112 case APCOND_EMAILCONFIRMED
:
113 if ( User
::isValidEmailAddr( $user->getEmail() ) ) {
114 global $wgEmailAuthentication;
115 if ( $wgEmailAuthentication ) {
116 return (bool)$user->getEmailAuthenticationTimestamp();
122 case APCOND_EDITCOUNT
:
123 return $user->getEditCount() >= $cond[1];
125 $age = time() - wfTimestampOrNull( TS_UNIX
, $user->getRegistration() );
126 return $age >= $cond[1];
127 case APCOND_AGE_FROM_EDIT
:
128 $age = time() - wfTimestampOrNull( TS_UNIX
, $user->getFirstEditTimestamp() );
129 return $age >= $cond[1];
130 case APCOND_INGROUPS
:
131 $groups = array_slice( $cond, 1 );
132 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
134 return $cond[1] == wfGetIP();
135 case APCOND_IPINRANGE
:
136 return IP
::isInRange( wfGetIP(), $cond[1] );
138 return $user->isBlocked();
141 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
142 if ( $result === null ) {
143 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
146 return $result ?
true : false;