3 * Automatic user rights promotion based on conditions specified
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.
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.
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
24 use MediaWiki\MediaWikiServices
;
27 * This class checks if user can get extra rights
28 * because of conditions specified in $wgAutopromote
32 * Get the groups for the given user based on $wgAutopromote.
34 * @param User $user The user to get the groups for
35 * @return array Array of groups to promote to.
37 public static function getAutopromoteGroups( User
$user ) {
38 global $wgAutopromote;
42 foreach ( $wgAutopromote as $group => $cond ) {
43 if ( self
::recCheckCondition( $cond, $user ) ) {
48 Hooks
::run( 'GetAutoPromoteGroups', [ $user, &$promote ] );
54 * Get the groups for the given user based on the given criteria.
56 * Does not return groups the user already belongs to or has once belonged.
58 * @param User $user The user to get the groups for
59 * @param string $event Key in $wgAutopromoteOnce (each one has groups/criteria)
61 * @return array Groups the user should be promoted to.
63 * @see $wgAutopromoteOnce
65 public static function getAutopromoteOnceGroups( User
$user, $event ) {
66 global $wgAutopromoteOnce;
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 ) ) {
78 // Do not autopromote if the user has belonged to the group
79 if ( in_array( $group, $formerGroups ) ) {
82 // Finally - check the conditions
83 if ( self
::recCheckCondition( $cond, $user ) ) {
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.
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
108 private static function recCheckCondition( $cond, User
$user ) {
109 $validOps = [ '&', '|', '^', '!' ];
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 ) ) {
121 } elseif ( $cond[0] == '|' ) { // OR (at least one cond passes)
122 foreach ( array_slice( $cond, 1 ) as $subcond ) {
123 if ( self
::recCheckCondition( $subcond, $user ) ) {
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.' );
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 ) ) {
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 ) ) {
152 return self
::checkCondition( $cond, $user );
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.
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
165 private static function checkCondition( $cond, User
$user ) {
166 global $wgEmailAuthentication;
167 if ( count( $cond ) < 1 ) {
171 switch ( $cond[0] ) {
172 case APCOND_EMAILCONFIRMED
:
173 if ( Sanitizer
::validateEmail( $user->getEmail() ) ) {
174 if ( $wgEmailAuthentication ) {
175 return (bool)$user->getEmailAuthenticationTimestamp();
181 case APCOND_EDITCOUNT
:
182 $reqEditCount = $cond[1];
184 // T157718: Avoid edit count lookup if specified edit count is 0 or invalid
185 if ( $reqEditCount <= 0 ) {
188 return $user->getEditCount() >= $reqEditCount;
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 );
199 return $cond[1] == $user->getRequest()->getIP();
200 case APCOND_IPINRANGE
:
201 return IP
::isInRange( $user->getRequest()->getIP(), $cond[1] );
203 return $user->getBlock() && $user->getBlock()->isSitewide();
205 return in_array( 'bot', MediaWikiServices
::getInstance()
206 ->getPermissionManager()
207 ->getGroupPermissions( $user->getGroups() ) );
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!" );
216 return (bool)$result;