MediaWiki  REL1_20
Autopromote.php
Go to the documentation of this file.
00001 <?php
00028 class Autopromote {
00035         public static function getAutopromoteGroups( User $user ) {
00036                 global $wgAutopromote;
00037 
00038                 $promote = array();
00039 
00040                 foreach ( $wgAutopromote as $group => $cond ) {
00041                         if ( self::recCheckCondition( $cond, $user ) ) {
00042                                 $promote[] = $group;
00043                         }
00044                 }
00045 
00046                 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
00047 
00048                 return $promote;
00049         }
00050 
00063         public static function getAutopromoteOnceGroups( User $user, $event ) {
00064                 global $wgAutopromoteOnce;
00065 
00066                 $promote = array();
00067 
00068                 if ( isset( $wgAutopromoteOnce[$event] ) && count( $wgAutopromoteOnce[$event] ) ) {
00069                         $currentGroups = $user->getGroups();
00070                         $formerGroups = $user->getFormerGroups();
00071                         foreach ( $wgAutopromoteOnce[$event] as $group => $cond ) {
00072                                 // Do not check if the user's already a member
00073                                 if ( in_array( $group, $currentGroups ) ) {
00074                                         continue;
00075                                 }
00076                                 // Do not autopromote if the user has belonged to the group
00077                                 if ( in_array( $group, $formerGroups ) ) {
00078                                         continue;
00079                                 }
00080                                 // Finally - check the conditions
00081                                 if ( self::recCheckCondition( $cond, $user ) ) {
00082                                         $promote[] = $group;
00083                                 }
00084                         }
00085                 }
00086 
00087                 return $promote;
00088         }
00089 
00106         private static function recCheckCondition( $cond, User $user ) {
00107                 $validOps = array( '&', '|', '^', '!' );
00108 
00109                 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
00110                         # Recursive condition
00111                         if ( $cond[0] == '&' ) { // AND (all conds pass)
00112                                 foreach ( array_slice( $cond, 1 ) as $subcond ) {
00113                                         if ( !self::recCheckCondition( $subcond, $user ) ) {
00114                                                 return false;
00115                                         }
00116                                 }
00117 
00118                                 return true;
00119                         } elseif ( $cond[0] == '|' ) { // OR (at least one cond passes)
00120                                 foreach ( array_slice( $cond, 1 ) as $subcond ) {
00121                                         if ( self::recCheckCondition( $subcond, $user ) ) {
00122                                                 return true;
00123                                         }
00124                                 }
00125 
00126                                 return false;
00127                         } elseif ( $cond[0] == '^' ) { // XOR (exactly one cond passes)
00128                                 if ( count( $cond ) > 3 ) {
00129                                         wfWarn( 'recCheckCondition() given XOR ("^") condition on three or more conditions. Check your $wgAutopromote and $wgAutopromoteOnce settings.' );
00130                                 }
00131                                 return self::recCheckCondition( $cond[1], $user )
00132                                         xor self::recCheckCondition( $cond[2], $user );
00133                         } elseif ( $cond[0] == '!' ) { // NOT (no conds pass)
00134                                 foreach ( array_slice( $cond, 1 ) as $subcond ) {
00135                                         if ( self::recCheckCondition( $subcond, $user ) ) {
00136                                                 return false;
00137                                         }
00138                                 }
00139 
00140                                 return true;
00141                         }
00142                 }
00143                 # If we got here, the array presumably does not contain other condi-
00144                 # tions; it's not recursive.  Pass it off to self::checkCondition.
00145                 if ( !is_array( $cond ) ) {
00146                         $cond = array( $cond );
00147                 }
00148 
00149                 return self::checkCondition( $cond, $user );
00150         }
00151 
00162         private static function checkCondition( $cond, User $user ) {
00163                 global $wgEmailAuthentication;
00164                 if ( count( $cond ) < 1 ) {
00165                         return false;
00166                 }
00167 
00168                 switch( $cond[0] ) {
00169                         case APCOND_EMAILCONFIRMED:
00170                                 if ( Sanitizer::validateEmail( $user->getEmail() ) ) {
00171                                         if ( $wgEmailAuthentication ) {
00172                                                 return (bool)$user->getEmailAuthenticationTimestamp();
00173                                         } else {
00174                                                 return true;
00175                                         }
00176                                 }
00177                                 return false;
00178                         case APCOND_EDITCOUNT:
00179                                 return $user->getEditCount() >= $cond[1];
00180                         case APCOND_AGE:
00181                                 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
00182                                 return $age >= $cond[1];
00183                         case APCOND_AGE_FROM_EDIT:
00184                                 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
00185                                 return $age >= $cond[1];
00186                         case APCOND_INGROUPS:
00187                                 $groups = array_slice( $cond, 1 );
00188                                 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
00189                         case APCOND_ISIP:
00190                                 return $cond[1] == $user->getRequest()->getIP();
00191                         case APCOND_IPINRANGE:
00192                                 return IP::isInRange( $user->getRequest()->getIP(), $cond[1] );
00193                         case APCOND_BLOCKED:
00194                                 return $user->isBlocked();
00195                         case APCOND_ISBOT:
00196                                 return in_array( 'bot', User::getGroupPermissions( $user->getGroups() ) );
00197                         default:
00198                                 $result = null;
00199                                 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
00200                                 if ( $result === null ) {
00201                                         throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
00202                                 }
00203 
00204                                 return (bool)$result;
00205                 }
00206         }
00207 }