Merge "Remove Revision::getRevisionText from migrateArchiveText"
[lhc/web/wiklou.git] / includes / Autopromote.php
1 <?php
2 /**
3 * Automatic user rights promotion based on conditions specified
4 * in $wgAutopromote.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 use MediaWiki\MediaWikiServices;
25
26 /**
27 * This class checks if user can get extra rights
28 * because of conditions specified in $wgAutopromote
29 */
30 class Autopromote {
31 /**
32 * Get the groups for the given user based on $wgAutopromote.
33 *
34 * @param User $user The user to get the groups for
35 * @return array Array of groups to promote to.
36 */
37 public static function getAutopromoteGroups( User $user ) {
38 global $wgAutopromote;
39
40 $promote = [];
41
42 foreach ( $wgAutopromote as $group => $cond ) {
43 if ( self::recCheckCondition( $cond, $user ) ) {
44 $promote[] = $group;
45 }
46 }
47
48 Hooks::run( 'GetAutoPromoteGroups', [ $user, &$promote ] );
49
50 return $promote;
51 }
52
53 /**
54 * Get the groups for the given user based on the given criteria.
55 *
56 * Does not return groups the user already belongs to or has once belonged.
57 *
58 * @param User $user The user to get the groups for
59 * @param string $event Key in $wgAutopromoteOnce (each one has groups/criteria)
60 *
61 * @return array Groups the user should be promoted to.
62 *
63 * @see $wgAutopromoteOnce
64 */
65 public static function getAutopromoteOnceGroups( User $user, $event ) {
66 global $wgAutopromoteOnce;
67
68 $promote = [];
69
70 if ( isset( $wgAutopromoteOnce[$event] ) && count( $wgAutopromoteOnce[$event] ) ) {
71 $currentGroups = $user->getGroups();
72 $formerGroups = $user->getFormerGroups();
73 foreach ( $wgAutopromoteOnce[$event] as $group => $cond ) {
74 // Do not check if the user's already a member
75 if ( in_array( $group, $currentGroups ) ) {
76 continue;
77 }
78 // Do not autopromote if the user has belonged to the group
79 if ( in_array( $group, $formerGroups ) ) {
80 continue;
81 }
82 // Finally - check the conditions
83 if ( self::recCheckCondition( $cond, $user ) ) {
84 $promote[] = $group;
85 }
86 }
87 }
88
89 return $promote;
90 }
91
92 /**
93 * Recursively check a condition. Conditions are in the form
94 * [ '&' or '|' or '^' or '!', cond1, cond2, ... ]
95 * where cond1, cond2, ... are themselves conditions; *OR*
96 * APCOND_EMAILCONFIRMED, *OR*
97 * [ APCOND_EMAILCONFIRMED ], *OR*
98 * [ APCOND_EDITCOUNT, number of edits ], *OR*
99 * [ APCOND_AGE, seconds since registration ], *OR*
100 * similar constructs defined by extensions.
101 * This function evaluates the former type recursively, and passes off to
102 * self::checkCondition for evaluation of the latter type.
103 *
104 * @param mixed $cond A condition, possibly containing other conditions
105 * @param User $user The user to check the conditions against
106 * @return bool Whether the condition is true
107 */
108 private static function recCheckCondition( $cond, User $user ) {
109 $validOps = [ '&', '|', '^', '!' ];
110
111 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
112 # Recursive condition
113 if ( $cond[0] == '&' ) { // AND (all conds pass)
114 foreach ( array_slice( $cond, 1 ) as $subcond ) {
115 if ( !self::recCheckCondition( $subcond, $user ) ) {
116 return false;
117 }
118 }
119
120 return true;
121 } elseif ( $cond[0] == '|' ) { // OR (at least one cond passes)
122 foreach ( array_slice( $cond, 1 ) as $subcond ) {
123 if ( self::recCheckCondition( $subcond, $user ) ) {
124 return true;
125 }
126 }
127
128 return false;
129 } elseif ( $cond[0] == '^' ) { // XOR (exactly one cond passes)
130 if ( count( $cond ) > 3 ) {
131 wfWarn( 'recCheckCondition() given XOR ("^") condition on three or more conditions.' .
132 ' Check your $wgAutopromote and $wgAutopromoteOnce settings.' );
133 }
134 return self::recCheckCondition( $cond[1], $user )
135 xor self::recCheckCondition( $cond[2], $user );
136 } elseif ( $cond[0] == '!' ) { // NOT (no conds pass)
137 foreach ( array_slice( $cond, 1 ) as $subcond ) {
138 if ( self::recCheckCondition( $subcond, $user ) ) {
139 return false;
140 }
141 }
142
143 return true;
144 }
145 }
146 // If we got here, the array presumably does not contain other conditions;
147 // it's not recursive. Pass it off to self::checkCondition.
148 if ( !is_array( $cond ) ) {
149 $cond = [ $cond ];
150 }
151
152 return self::checkCondition( $cond, $user );
153 }
154
155 /**
156 * As recCheckCondition, but *not* recursive. The only valid conditions
157 * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
158 * APCOND_AGE. Other types will throw an exception if no extension evaluates them.
159 *
160 * @param array $cond A condition, which must not contain other conditions
161 * @param User $user The user to check the condition against
162 * @throws MWException
163 * @return bool Whether the condition is true for the user
164 */
165 private static function checkCondition( $cond, User $user ) {
166 global $wgEmailAuthentication;
167 if ( count( $cond ) < 1 ) {
168 return false;
169 }
170
171 switch ( $cond[0] ) {
172 case APCOND_EMAILCONFIRMED:
173 if ( Sanitizer::validateEmail( $user->getEmail() ) ) {
174 if ( $wgEmailAuthentication ) {
175 return (bool)$user->getEmailAuthenticationTimestamp();
176 } else {
177 return true;
178 }
179 }
180 return false;
181 case APCOND_EDITCOUNT:
182 $reqEditCount = $cond[1];
183
184 // T157718: Avoid edit count lookup if specified edit count is 0 or invalid
185 if ( $reqEditCount <= 0 ) {
186 return true;
187 }
188 return $user->getEditCount() >= $reqEditCount;
189 case APCOND_AGE:
190 $age = time() - (int)wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
191 return $age >= $cond[1];
192 case APCOND_AGE_FROM_EDIT:
193 $age = time() - (int)wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
194 return $age >= $cond[1];
195 case APCOND_INGROUPS:
196 $groups = array_slice( $cond, 1 );
197 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
198 case APCOND_ISIP:
199 return $cond[1] == $user->getRequest()->getIP();
200 case APCOND_IPINRANGE:
201 return IP::isInRange( $user->getRequest()->getIP(), $cond[1] );
202 case APCOND_BLOCKED:
203 return $user->getBlock() && $user->getBlock()->isSitewide();
204 case APCOND_ISBOT:
205 return in_array( 'bot', MediaWikiServices::getInstance()
206 ->getPermissionManager()
207 ->getGroupPermissions( $user->getGroups() ) );
208 default:
209 $result = null;
210 Hooks::run( 'AutopromoteCondition', [ $cond[0],
211 array_slice( $cond, 1 ), $user, &$result ] );
212 if ( $result === null ) {
213 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
214 }
215
216 return (bool)$result;
217 }
218 }
219 }