Follow-up r90749:
[lhc/web/wiklou.git] / includes / Autopromote.php
1 <?php
2 /**
3 * This class checks if user can get extra rights
4 * because of conditions specified in $wgAutopromote
5 */
6
7 class Autopromote {
8 /**
9 * Get the groups for the given user based on $wgAutopromote.
10 *
11 * @param $user User The user to get the groups for
12 * @return array Array of groups to promote to.
13 */
14 public static function getAutopromoteGroups( User $user ) {
15 global $wgAutopromote;
16
17 $promote = array();
18
19 foreach ( $wgAutopromote as $group => $cond ) {
20 if ( self::recCheckCondition( $cond, $user ) ) {
21 $promote[] = $group;
22 }
23 }
24
25 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
26
27 return $promote;
28 }
29
30 /**
31 * Get the groups for the given user based on the given criteria.
32 *
33 * Does not return groups the user already belongs to or has once belonged.
34 *
35 * @param $user The user to get the groups for
36 * @param $criteria array Groups and conditions the user must meet in order
37 * to be promoted to these groups. Array of the same format as
38 * \ref $wgAutopromote.
39 *
40 * @return array Groups the user should be promoted to.
41 */
42 public static function getAutopromoteOnceGroups( User $user, $criteria ) {
43 $promote = array();
44
45 $currentGroups = $user->getGroups();
46
47 foreach ( $criteria as $group => $cond ) {
48 // Do not check if the user's already a member
49 if ( in_array( $group, $currentGroups ) ) {
50 continue;
51 }
52 // Do not autopromote if the user has belonged to the group
53 $formerGroups = $user->getFormerGroups();
54 if ( in_array( $group, $formerGroups ) ) {
55 continue;
56 }
57 // Finally - check the conditions
58 if ( self::recCheckCondition( $cond, $user ) ) {
59 $promote[] = $group;
60 }
61 }
62
63 return $promote;
64 }
65
66 /**
67 * Recursively check a condition. Conditions are in the form
68 * array( '&' or '|' or '^', cond1, cond2, ... )
69 * where cond1, cond2, ... are themselves conditions; *OR*
70 * APCOND_EMAILCONFIRMED, *OR*
71 * array( APCOND_EMAILCONFIRMED ), *OR*
72 * array( APCOND_EDITCOUNT, number of edits ), *OR*
73 * array( APCOND_AGE, seconds since registration ), *OR*
74 * similar constructs defined by extensions.
75 * This function evaluates the former type recursively, and passes off to
76 * self::checkCondition for evaluation of the latter type.
77 *
78 * @param $cond Mixed: a condition, possibly containing other conditions
79 * @param $user User The user to check the conditions against
80 * @return bool Whether the condition is true
81 */
82 private static function recCheckCondition( $cond, User $user ) {
83 $validOps = array( '&', '|', '^', '!' );
84
85 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
86 # Recursive condition
87 if ( $cond[0] == '&' ) {
88 foreach ( array_slice( $cond, 1 ) as $subcond ) {
89 if ( !self::recCheckCondition( $subcond, $user ) ) {
90 return false;
91 }
92 }
93
94 return true;
95 } elseif ( $cond[0] == '|' ) {
96 foreach ( array_slice( $cond, 1 ) as $subcond ) {
97 if ( self::recCheckCondition( $subcond, $user ) ) {
98 return true;
99 }
100 }
101
102 return false;
103 } elseif ( $cond[0] == '^' ) {
104 $res = null;
105 foreach ( array_slice( $cond, 1 ) as $subcond ) {
106 if ( is_null( $res ) ) {
107 $res = self::recCheckCondition( $subcond, $user );
108 } else {
109 $res = ( $res xor self::recCheckCondition( $subcond, $user ) );
110 }
111 }
112
113 return $res;
114 } elseif ( $cond[0] == '!' ) {
115 foreach ( array_slice( $cond, 1 ) as $subcond ) {
116 if ( self::recCheckCondition( $subcond, $user ) ) {
117 return false;
118 }
119 }
120
121 return true;
122 }
123 }
124 # If we got here, the array presumably does not contain other condi-
125 # tions; it's not recursive. Pass it off to self::checkCondition.
126 if ( !is_array( $cond ) ) {
127 $cond = array( $cond );
128 }
129
130 return self::checkCondition( $cond, $user );
131 }
132
133 /**
134 * As recCheckCondition, but *not* recursive. The only valid conditions
135 * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
136 * APCOND_AGE. Other types will throw an exception if no extension evalu-
137 * ates them.
138 *
139 * @param $cond Array: A condition, which must not contain other conditions
140 * @param $user User The user to check the condition against
141 * @return bool Whether the condition is true for the user
142 */
143 private static function checkCondition( $cond, User $user ) {
144 global $wgEmailAuthentication;
145 if ( count( $cond ) < 1 ) {
146 return false;
147 }
148
149 switch( $cond[0] ) {
150 case APCOND_EMAILCONFIRMED:
151 if ( User::isValidEmailAddr( $user->getEmail() ) ) {
152 if ( $wgEmailAuthentication ) {
153 return (bool)$user->getEmailAuthenticationTimestamp();
154 } else {
155 return true;
156 }
157 }
158 return false;
159 case APCOND_EDITCOUNT:
160 return $user->getEditCount() >= $cond[1];
161 case APCOND_AGE:
162 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
163 return $age >= $cond[1];
164 case APCOND_AGE_FROM_EDIT:
165 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
166 return $age >= $cond[1];
167 case APCOND_INGROUPS:
168 $groups = array_slice( $cond, 1 );
169 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
170 case APCOND_ISIP:
171 return $cond[1] == wfGetIP();
172 case APCOND_IPINRANGE:
173 return IP::isInRange( wfGetIP(), $cond[1] );
174 case APCOND_BLOCKED:
175 return $user->isBlocked();
176 default:
177 $result = null;
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!" );
181 }
182
183 return (bool)$result;
184 }
185 }
186 }