MediaWiki REL1_30
Autopromote.php
Go to the documentation of this file.
1<?php
35 public static function getAutopromoteGroups( User $user ) {
36 global $wgAutopromote;
37
38 $promote = [];
39
40 foreach ( $wgAutopromote as $group => $cond ) {
41 if ( self::recCheckCondition( $cond, $user ) ) {
42 $promote[] = $group;
43 }
44 }
45
46 Hooks::run( 'GetAutoPromoteGroups', [ $user, &$promote ] );
47
48 return $promote;
49 }
50
63 public static function getAutopromoteOnceGroups( User $user, $event ) {
64 global $wgAutopromoteOnce;
65
66 $promote = [];
67
68 if ( isset( $wgAutopromoteOnce[$event] ) && count( $wgAutopromoteOnce[$event] ) ) {
69 $currentGroups = $user->getGroups();
70 $formerGroups = $user->getFormerGroups();
71 foreach ( $wgAutopromoteOnce[$event] as $group => $cond ) {
72 // Do not check if the user's already a member
73 if ( in_array( $group, $currentGroups ) ) {
74 continue;
75 }
76 // Do not autopromote if the user has belonged to the group
77 if ( in_array( $group, $formerGroups ) ) {
78 continue;
79 }
80 // Finally - check the conditions
81 if ( self::recCheckCondition( $cond, $user ) ) {
82 $promote[] = $group;
83 }
84 }
85 }
86
87 return $promote;
88 }
89
106 private static function recCheckCondition( $cond, User $user ) {
107 $validOps = [ '&', '|', '^', '!' ];
108
109 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
110 # Recursive condition
111 if ( $cond[0] == '&' ) { // AND (all conds pass)
112 foreach ( array_slice( $cond, 1 ) as $subcond ) {
113 if ( !self::recCheckCondition( $subcond, $user ) ) {
114 return false;
115 }
116 }
117
118 return true;
119 } elseif ( $cond[0] == '|' ) { // OR (at least one cond passes)
120 foreach ( array_slice( $cond, 1 ) as $subcond ) {
121 if ( self::recCheckCondition( $subcond, $user ) ) {
122 return true;
123 }
124 }
125
126 return false;
127 } elseif ( $cond[0] == '^' ) { // XOR (exactly one cond passes)
128 if ( count( $cond ) > 3 ) {
129 wfWarn( 'recCheckCondition() given XOR ("^") condition on three or more conditions.' .
130 ' Check your $wgAutopromote and $wgAutopromoteOnce settings.' );
131 }
132 return self::recCheckCondition( $cond[1], $user )
133 xor self::recCheckCondition( $cond[2], $user );
134 } elseif ( $cond[0] == '!' ) { // NOT (no conds pass)
135 foreach ( array_slice( $cond, 1 ) as $subcond ) {
136 if ( self::recCheckCondition( $subcond, $user ) ) {
137 return false;
138 }
139 }
140
141 return true;
142 }
143 }
144 // If we got here, the array presumably does not contain other conditions;
145 // it's not recursive. Pass it off to self::checkCondition.
146 if ( !is_array( $cond ) ) {
147 $cond = [ $cond ];
148 }
149
150 return self::checkCondition( $cond, $user );
151 }
152
163 private static function checkCondition( $cond, User $user ) {
165 if ( count( $cond ) < 1 ) {
166 return false;
167 }
168
169 switch ( $cond[0] ) {
171 if ( Sanitizer::validateEmail( $user->getEmail() ) ) {
173 return (bool)$user->getEmailAuthenticationTimestamp();
174 } else {
175 return true;
176 }
177 }
178 return false;
179 case APCOND_EDITCOUNT:
180 $reqEditCount = $cond[1];
181
182 // T157718: Avoid edit count lookup if specified edit count is 0 or invalid
183 if ( $reqEditCount <= 0 ) {
184 return true;
185 }
186 return $user->getEditCount() >= $reqEditCount;
187 case APCOND_AGE:
188 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
189 return $age >= $cond[1];
191 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
192 return $age >= $cond[1];
193 case APCOND_INGROUPS:
194 $groups = array_slice( $cond, 1 );
195 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
196 case APCOND_ISIP:
197 return $cond[1] == $user->getRequest()->getIP();
198 case APCOND_IPINRANGE:
199 return IP::isInRange( $user->getRequest()->getIP(), $cond[1] );
200 case APCOND_BLOCKED:
201 return $user->isBlocked();
202 case APCOND_ISBOT:
203 return in_array( 'bot', User::getGroupPermissions( $user->getGroups() ) );
204 default:
205 $result = null;
206 Hooks::run( 'AutopromoteCondition', [ $cond[0],
207 array_slice( $cond, 1 ), $user, &$result ] );
208 if ( $result === null ) {
209 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
210 }
211
212 return (bool)$result;
213 }
214 }
215}
$wgAutopromote
Array containing the conditions of automatic promotion of a user to specific groups.
$wgEmailAuthentication
Require email authentication before sending mail to an email address.
$wgAutopromoteOnce
Automatically add a usergroup to any user who matches certain conditions.
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
This class checks if user can get extra rights because of conditions specified in $wgAutopromote.
static recCheckCondition( $cond, User $user)
Recursively check a condition.
static getAutopromoteOnceGroups(User $user, $event)
Get the groups for the given user based on the given criteria.
static checkCondition( $cond, User $user)
As recCheckCondition, but not recursive.
static getAutopromoteGroups(User $user)
Get the groups for the given user based on $wgAutopromote.
MediaWiki exception.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
namespace being checked & $result
Definition hooks.txt:2293
const APCOND_BLOCKED
Definition Defines.php:213
const APCOND_AGE
Definition Defines.php:207
const APCOND_EMAILCONFIRMED
Definition Defines.php:208
const APCOND_IPINRANGE
Definition Defines.php:211
const APCOND_INGROUPS
Definition Defines.php:209
const APCOND_ISBOT
Definition Defines.php:214
const APCOND_AGE_FROM_EDIT
Definition Defines.php:212
const APCOND_ISIP
Definition Defines.php:210
const APCOND_EDITCOUNT
Definition Defines.php:206