00001 <?php 00002 00007 class Autopromote { 00014 public static function getAutopromoteGroups( User $user ) { 00015 global $wgAutopromote; 00016 $promote = array(); 00017 foreach( $wgAutopromote as $group => $cond ) { 00018 if( self::recCheckCondition( $cond, $user ) ) 00019 $promote[] = $group; 00020 } 00021 00022 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) ); 00023 00024 return $promote; 00025 } 00026 00043 private static function recCheckCondition( $cond, User $user ) { 00044 $validOps = array( '&', '|', '^', '!' ); 00045 if( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) { 00046 # Recursive condition 00047 if( $cond[0] == '&' ) { 00048 foreach( array_slice( $cond, 1 ) as $subcond ) 00049 if( !self::recCheckCondition( $subcond, $user ) ) 00050 return false; 00051 return true; 00052 } elseif( $cond[0] == '|' ) { 00053 foreach( array_slice( $cond, 1 ) as $subcond ) 00054 if( self::recCheckCondition( $subcond, $user ) ) 00055 return true; 00056 return false; 00057 } elseif( $cond[0] == '^' ) { 00058 $res = null; 00059 foreach( array_slice( $cond, 1 ) as $subcond ) { 00060 if( is_null( $res ) ) 00061 $res = self::recCheckCondition( $subcond, $user ); 00062 else 00063 $res = ($res xor self::recCheckCondition( $subcond, $user )); 00064 } 00065 return $res; 00066 } elseif ( $cond[0] = '!' ) { 00067 foreach( array_slice( $cond, 1 ) as $subcond ) 00068 if( self::recCheckCondition( $subcond, $user ) ) 00069 return false; 00070 return true; 00071 } 00072 } 00073 # If we got here, the array presumably does not contain other condi- 00074 # tions; it's not recursive. Pass it off to self::checkCondition. 00075 if( !is_array( $cond ) ) 00076 $cond = array( $cond ); 00077 return self::checkCondition( $cond, $user ); 00078 } 00079 00090 private static function checkCondition( $cond, User $user ) { 00091 if( count( $cond ) < 1 ) 00092 return false; 00093 switch( $cond[0] ) { 00094 case APCOND_EMAILCONFIRMED: 00095 if( User::isValidEmailAddr( $user->getEmail() ) ) { 00096 global $wgEmailAuthentication; 00097 if( $wgEmailAuthentication ) { 00098 return (bool)$user->getEmailAuthenticationTimestamp(); 00099 } else { 00100 return true; 00101 } 00102 } 00103 return false; 00104 case APCOND_EDITCOUNT: 00105 return $user->getEditCount() >= $cond[1]; 00106 case APCOND_AGE: 00107 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() ); 00108 return $age >= $cond[1]; 00109 case APCOND_AGE_FROM_EDIT: 00110 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() ); 00111 return $age >= $cond[1]; 00112 case APCOND_INGROUPS: 00113 $groups = array_slice( $cond, 1 ); 00114 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups ); 00115 case APCOND_ISIP: 00116 return $cond[1] == wfGetIP(); 00117 case APCOND_IPINRANGE: 00118 return IP::isInRange( wfGetIP(), $cond[1] ); 00119 default: 00120 $result = null; 00121 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) ); 00122 if( $result === null ) { 00123 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" ); 00124 } 00125 return $result ? true : false; 00126 } 00127 } 00128 }