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 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 ) ) {
25 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
31 * Get the groups for the given user based on the given criteria.
33 * Does not return groups the user already belongs to or has once belonged.
35 * @param $user User The user to get the groups for
36 * @param $event String key in $wgAutopromoteOnce (each one has groups/criteria)
38 * @return array Groups the user should be promoted to.
40 * @see $wgAutopromoteOnce
42 public static function getAutopromoteOnceGroups( User
$user, $event ) {
43 global $wgAutopromoteOnce;
47 if ( isset( $wgAutopromoteOnce[$event] ) && count( $wgAutopromoteOnce[$event] ) ) {
48 $currentGroups = $user->getGroups();
49 $formerGroups = $user->getFormerGroups();
50 foreach ( $wgAutopromoteOnce[$event] as $group => $cond ) {
51 // Do not check if the user's already a member
52 if ( in_array( $group, $currentGroups ) ) {
55 // Do not autopromote if the user has belonged to the group
56 if ( in_array( $group, $formerGroups ) ) {
59 // Finally - check the conditions
60 if ( self
::recCheckCondition( $cond, $user ) ) {
70 * Recursively check a condition. Conditions are in the form
71 * array( '&' or '|' or '^' or '!', cond1, cond2, ... )
72 * where cond1, cond2, ... are themselves conditions; *OR*
73 * APCOND_EMAILCONFIRMED, *OR*
74 * array( APCOND_EMAILCONFIRMED ), *OR*
75 * array( APCOND_EDITCOUNT, number of edits ), *OR*
76 * array( APCOND_AGE, seconds since registration ), *OR*
77 * similar constructs defined by extensions.
78 * This function evaluates the former type recursively, and passes off to
79 * self::checkCondition for evaluation of the latter type.
81 * @param $cond Mixed: a condition, possibly containing other conditions
82 * @param $user User The user to check the conditions against
83 * @return bool Whether the condition is true
85 private static function recCheckCondition( $cond, User
$user ) {
86 $validOps = array( '&', '|', '^', '!' );
88 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
90 if ( $cond[0] == '&' ) { // AND (all conds pass)
91 foreach ( array_slice( $cond, 1 ) as $subcond ) {
92 if ( !self
::recCheckCondition( $subcond, $user ) ) {
98 } elseif ( $cond[0] == '|' ) { // OR (at least one cond passes)
99 foreach ( array_slice( $cond, 1 ) as $subcond ) {
100 if ( self
::recCheckCondition( $subcond, $user ) ) {
106 } elseif ( $cond[0] == '^' ) { // XOR (exactly one cond passes)
107 if ( count( $cond ) > 3 ) {
108 wfWarn( 'recCheckCondition() given XOR ("^") condition on three or more conditions. Check your $wgAutopromote and $wgAutopromoteOnce settings.' );
110 return self
::recCheckCondition( $cond[1], $user )
111 xor self
::recCheckCondition( $cond[2], $user );
112 } elseif ( $cond[0] == '!' ) { // NOT (no conds pass)
113 foreach ( array_slice( $cond, 1 ) as $subcond ) {
114 if ( self
::recCheckCondition( $subcond, $user ) ) {
122 # If we got here, the array presumably does not contain other condi-
123 # tions; it's not recursive. Pass it off to self::checkCondition.
124 if ( !is_array( $cond ) ) {
125 $cond = array( $cond );
128 return self
::checkCondition( $cond, $user );
132 * As recCheckCondition, but *not* recursive. The only valid conditions
133 * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
134 * APCOND_AGE. Other types will throw an exception if no extension evalu-
137 * @param $cond Array: A condition, which must not contain other conditions
138 * @param $user User The user to check the condition against
139 * @return bool Whether the condition is true for the user
141 private static function checkCondition( $cond, User
$user ) {
142 global $wgEmailAuthentication;
143 if ( count( $cond ) < 1 ) {
148 case APCOND_EMAILCONFIRMED
:
149 if ( Sanitizer
::validateEmail( $user->getEmail() ) ) {
150 if ( $wgEmailAuthentication ) {
151 return (bool)$user->getEmailAuthenticationTimestamp();
157 case APCOND_EDITCOUNT
:
158 return $user->getEditCount() >= $cond[1];
160 $age = time() - wfTimestampOrNull( TS_UNIX
, $user->getRegistration() );
161 return $age >= $cond[1];
162 case APCOND_AGE_FROM_EDIT
:
163 $age = time() - wfTimestampOrNull( TS_UNIX
, $user->getFirstEditTimestamp() );
164 return $age >= $cond[1];
165 case APCOND_INGROUPS
:
166 $groups = array_slice( $cond, 1 );
167 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
169 return $cond[1] == $user->getRequest()->getIP();
170 case APCOND_IPINRANGE
:
171 return IP
::isInRange( $user->getRequest()->getIP(), $cond[1] );
173 return $user->isBlocked();
175 return in_array( 'bot', User
::getGroupPermissions( $user->getGroups() ) );
178 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
179 if ( $result === null ) {
180 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
183 return (bool)$result;