MediaWiki  REL1_19
Autopromote.php
Go to the documentation of this file.
00001 <?php
00007 class Autopromote {
00014         public static function getAutopromoteGroups( User $user ) {
00015                 global $wgAutopromote;
00016 
00017                 $promote = array();
00018 
00019                 foreach ( $wgAutopromote as $group => $cond ) {
00020                         if ( self::recCheckCondition( $cond, $user ) ) {
00021                                 $promote[] = $group;
00022                         }
00023                 }
00024 
00025                 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
00026 
00027                 return $promote;
00028         }
00029 
00042         public static function getAutopromoteOnceGroups( User $user, $event ) {
00043                 global $wgAutopromoteOnce;
00044 
00045                 $promote = array();
00046 
00047                 if ( isset( $wgAutopromoteOnce[$event] ) && count( $wgAutopromoteOnce[$event] ) ) {
00048                         $currentGroups = $user->getGroups();
00049                         $formerGroups = $user->getFormerGroups();
00050                         foreach ( $wgAutopromoteOnce[$event] as $group => $cond ) {
00051                                 // Do not check if the user's already a member
00052                                 if ( in_array( $group, $currentGroups ) ) {
00053                                         continue;
00054                                 }
00055                                 // Do not autopromote if the user has belonged to the group
00056                                 if ( in_array( $group, $formerGroups ) ) {
00057                                         continue;
00058                                 }
00059                                 // Finally - check the conditions
00060                                 if ( self::recCheckCondition( $cond, $user ) ) {
00061                                         $promote[] = $group;
00062                                 }
00063                         }
00064                 }
00065 
00066                 return $promote;
00067         }
00068 
00085         private static function recCheckCondition( $cond, User $user ) {
00086                 $validOps = array( '&', '|', '^', '!' );
00087 
00088                 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
00089                         # Recursive condition
00090                         if ( $cond[0] == '&' ) { // AND (all conds pass)
00091                                 foreach ( array_slice( $cond, 1 ) as $subcond ) {
00092                                         if ( !self::recCheckCondition( $subcond, $user ) ) {
00093                                                 return false;
00094                                         }
00095                                 }
00096 
00097                                 return true;
00098                         } elseif ( $cond[0] == '|' ) { // OR (at least one cond passes)
00099                                 foreach ( array_slice( $cond, 1 ) as $subcond ) {
00100                                         if ( self::recCheckCondition( $subcond, $user ) ) {
00101                                                 return true;
00102                                         }
00103                                 }
00104 
00105                                 return false;
00106                         } elseif ( $cond[0] == '^' ) { // XOR (exactly one cond passes)
00107                                 if ( count( $cond ) > 3 ) {
00108                                         wfWarn( 'recCheckCondition() given XOR ("^") condition on three or more conditions. Check your $wgAutopromote and $wgAutopromoteOnce settings.' );
00109                                 }
00110                                 return self::recCheckCondition( $cond[1], $user )
00111                                         xor self::recCheckCondition( $cond[2], $user );
00112                         } elseif ( $cond[0] == '!' ) { // NOT (no conds pass)
00113                                 foreach ( array_slice( $cond, 1 ) as $subcond ) {
00114                                         if ( self::recCheckCondition( $subcond, $user ) ) {
00115                                                 return false;
00116                                         }
00117                                 }
00118 
00119                                 return true;
00120                         }
00121                 }
00122                 # If we got here, the array presumably does not contain other condi-
00123                 # tions; it's not recursive.  Pass it off to self::checkCondition.
00124                 if ( !is_array( $cond ) ) {
00125                         $cond = array( $cond );
00126                 }
00127 
00128                 return self::checkCondition( $cond, $user );
00129         }
00130 
00141         private static function checkCondition( $cond, User $user ) {
00142                 global $wgEmailAuthentication;
00143                 if ( count( $cond ) < 1 ) {
00144                         return false;
00145                 }
00146 
00147                 switch( $cond[0] ) {
00148                         case APCOND_EMAILCONFIRMED:
00149                                 if ( Sanitizer::validateEmail( $user->getEmail() ) ) {
00150                                         if ( $wgEmailAuthentication ) {
00151                                                 return (bool)$user->getEmailAuthenticationTimestamp();
00152                                         } else {
00153                                                 return true;
00154                                         }
00155                                 }
00156                                 return false;
00157                         case APCOND_EDITCOUNT:
00158                                 return $user->getEditCount() >= $cond[1];
00159                         case APCOND_AGE:
00160                                 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
00161                                 return $age >= $cond[1];
00162                         case APCOND_AGE_FROM_EDIT:
00163                                 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
00164                                 return $age >= $cond[1];
00165                         case APCOND_INGROUPS:
00166                                 $groups = array_slice( $cond, 1 );
00167                                 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
00168                         case APCOND_ISIP:
00169                                 return $cond[1] == $user->getRequest()->getIP();
00170                         case APCOND_IPINRANGE:
00171                                 return IP::isInRange( $user->getRequest()->getIP(), $cond[1] );
00172                         case APCOND_BLOCKED:
00173                                 return $user->isBlocked();
00174                         case APCOND_ISBOT:
00175                                 return in_array( 'bot', User::getGroupPermissions( $user->getGroups() ) );
00176                         default:
00177                                 $result = null;
00178                                 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
00179                                 if ( $result === null ) {
00180                                         throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
00181                                 }
00182 
00183                                 return (bool)$result;
00184                 }
00185         }
00186 }