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