MediaWiki  REL1_22
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.' .
00130                         ' Check your $wgAutopromote and $wgAutopromoteOnce settings.' );
00131                 }
00132                 return self::recCheckCondition( $cond[1], $user )
00133                     xor self::recCheckCondition( $cond[2], $user );
00134             } elseif ( $cond[0] == '!' ) { // NOT (no conds pass)
00135                 foreach ( array_slice( $cond, 1 ) as $subcond ) {
00136                     if ( self::recCheckCondition( $subcond, $user ) ) {
00137                         return false;
00138                     }
00139                 }
00140 
00141                 return true;
00142             }
00143         }
00144         // If we got here, the array presumably does not contain other conditions;
00145         // it's not recursive.  Pass it off to self::checkCondition.
00146         if ( !is_array( $cond ) ) {
00147             $cond = array( $cond );
00148         }
00149 
00150         return self::checkCondition( $cond, $user );
00151     }
00152 
00163     private static function checkCondition( $cond, User $user ) {
00164         global $wgEmailAuthentication;
00165         if ( count( $cond ) < 1 ) {
00166             return false;
00167         }
00168 
00169         switch ( $cond[0] ) {
00170             case APCOND_EMAILCONFIRMED:
00171                 if ( Sanitizer::validateEmail( $user->getEmail() ) ) {
00172                     if ( $wgEmailAuthentication ) {
00173                         return (bool)$user->getEmailAuthenticationTimestamp();
00174                     } else {
00175                         return true;
00176                     }
00177                 }
00178                 return false;
00179             case APCOND_EDITCOUNT:
00180                 return $user->getEditCount() >= $cond[1];
00181             case APCOND_AGE:
00182                 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
00183                 return $age >= $cond[1];
00184             case APCOND_AGE_FROM_EDIT:
00185                 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
00186                 return $age >= $cond[1];
00187             case APCOND_INGROUPS:
00188                 $groups = array_slice( $cond, 1 );
00189                 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
00190             case APCOND_ISIP:
00191                 return $cond[1] == $user->getRequest()->getIP();
00192             case APCOND_IPINRANGE:
00193                 return IP::isInRange( $user->getRequest()->getIP(), $cond[1] );
00194             case APCOND_BLOCKED:
00195                 return $user->isBlocked();
00196             case APCOND_ISBOT:
00197                 return in_array( 'bot', User::getGroupPermissions( $user->getGroups() ) );
00198             default:
00199                 $result = null;
00200                 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
00201                 if ( $result === null ) {
00202                     throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
00203                 }
00204 
00205                 return (bool)$result;
00206         }
00207     }
00208 }