Follow-up r90749: pushed down accessing of $wgAutopromoteOnce to getAutopromoteOnceGr...
[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 $event String 'onEdit' or 'onView' (each one has groups/criteria)
37 *
38 * @return array Groups the user should be promoted to.
39 */
40 public static function getAutopromoteOnceGroups( User $user, $event ) {
41 global $wgAutopromoteOnce;
42
43 $promote = array();
44
45 if ( isset( $wgAutopromoteOnce[$event] ) && count( $wgAutopromoteOnce[$event] ) ) {
46 $currentGroups = $user->getGroups();
47 foreach ( $wgAutopromoteOnce[$event] 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
64 return $promote;
65 }
66
67 /**
68 * Recursively check a condition. Conditions are in the form
69 * array( '&' or '|' or '^', cond1, cond2, ... )
70 * where cond1, cond2, ... are themselves conditions; *OR*
71 * APCOND_EMAILCONFIRMED, *OR*
72 * array( APCOND_EMAILCONFIRMED ), *OR*
73 * array( APCOND_EDITCOUNT, number of edits ), *OR*
74 * array( APCOND_AGE, seconds since registration ), *OR*
75 * similar constructs defined by extensions.
76 * This function evaluates the former type recursively, and passes off to
77 * self::checkCondition for evaluation of the latter type.
78 *
79 * @param $cond Mixed: a condition, possibly containing other conditions
80 * @param $user User The user to check the conditions against
81 * @return bool Whether the condition is true
82 */
83 private static function recCheckCondition( $cond, User $user ) {
84 $validOps = array( '&', '|', '^', '!' );
85
86 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
87 # Recursive condition
88 if ( $cond[0] == '&' ) {
89 foreach ( array_slice( $cond, 1 ) as $subcond ) {
90 if ( !self::recCheckCondition( $subcond, $user ) ) {
91 return false;
92 }
93 }
94
95 return true;
96 } elseif ( $cond[0] == '|' ) {
97 foreach ( array_slice( $cond, 1 ) as $subcond ) {
98 if ( self::recCheckCondition( $subcond, $user ) ) {
99 return true;
100 }
101 }
102
103 return false;
104 } elseif ( $cond[0] == '^' ) {
105 $res = null;
106 foreach ( array_slice( $cond, 1 ) as $subcond ) {
107 if ( is_null( $res ) ) {
108 $res = self::recCheckCondition( $subcond, $user );
109 } else {
110 $res = ( $res xor self::recCheckCondition( $subcond, $user ) );
111 }
112 }
113
114 return $res;
115 } elseif ( $cond[0] == '!' ) {
116 foreach ( array_slice( $cond, 1 ) as $subcond ) {
117 if ( self::recCheckCondition( $subcond, $user ) ) {
118 return false;
119 }
120 }
121
122 return true;
123 }
124 }
125 # If we got here, the array presumably does not contain other condi-
126 # tions; it's not recursive. Pass it off to self::checkCondition.
127 if ( !is_array( $cond ) ) {
128 $cond = array( $cond );
129 }
130
131 return self::checkCondition( $cond, $user );
132 }
133
134 /**
135 * As recCheckCondition, but *not* recursive. The only valid conditions
136 * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
137 * APCOND_AGE. Other types will throw an exception if no extension evalu-
138 * ates them.
139 *
140 * @param $cond Array: A condition, which must not contain other conditions
141 * @param $user User The user to check the condition against
142 * @return bool Whether the condition is true for the user
143 */
144 private static function checkCondition( $cond, User $user ) {
145 global $wgEmailAuthentication;
146 if ( count( $cond ) < 1 ) {
147 return false;
148 }
149
150 switch( $cond[0] ) {
151 case APCOND_EMAILCONFIRMED:
152 if ( User::isValidEmailAddr( $user->getEmail() ) ) {
153 if ( $wgEmailAuthentication ) {
154 return (bool)$user->getEmailAuthenticationTimestamp();
155 } else {
156 return true;
157 }
158 }
159 return false;
160 case APCOND_EDITCOUNT:
161 return $user->getEditCount() >= $cond[1];
162 case APCOND_AGE:
163 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
164 return $age >= $cond[1];
165 case APCOND_AGE_FROM_EDIT:
166 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
167 return $age >= $cond[1];
168 case APCOND_INGROUPS:
169 $groups = array_slice( $cond, 1 );
170 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
171 case APCOND_ISIP:
172 return $cond[1] == wfGetIP();
173 case APCOND_IPINRANGE:
174 return IP::isInRange( wfGetIP(), $cond[1] );
175 case APCOND_BLOCKED:
176 return $user->isBlocked();
177 default:
178 $result = null;
179 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
180 if ( $result === null ) {
181 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
182 }
183
184 return (bool)$result;
185 }
186 }
187 }