Introduce new autopromotion system
[lhc/web/wiklou.git] / includes / Autopromote.php
1 <?php
2
3 /**
4 * This class checks if user can get extra rights
5 * because of conditions specified in $wgAutopromote
6 */
7 class Autopromote {
8 public static function autopromoteUser( $user ) {
9 global $wgAutopromote;
10 $promote = array();
11 foreach( $wgAutopromote as $group => $cond ) {
12 if( self::recCheckCondition( $cond, $user ) )
13 $promote[] = $group;
14 }
15 return $promote;
16 }
17
18 //@private
19 static function recCheckCondition( $cond, $user ) {
20 $validOps = array( '&', '|', '^' );
21 if( is_array( $cond ) && count( $cond ) > 0 && in_array( $cond[0], $validOps ) ) {
22 if( $cond[0] == '&' ) {
23 foreach( array_slice( $cond, 1 ) as $subcond )
24 if( !self::recCheckCondition( $subcond, $user ) )
25 return false;
26 return true;
27 } elseif( $cond[0] == '|' ) {
28 foreach( array_slice( $cond, 1 ) as $subcond )
29 if( self::recCheckCondition( $subcond, $user ) )
30 return true;
31 return false;
32 } elseif( $cond[0] == '^' ) {
33 if( count( $cond ) < 3 )
34 return false;
35 return self::recCheckCondition( $cond[1], $user )
36 xor self::recCheckCondition( $cond[2], $user );
37 }
38 }
39 if( !is_array( $cond ) )
40 $cond = array( $cond );
41 return self::checkCondition( $cond, $user );
42 }
43
44 static function checkCondition( $cond, $user ) {
45 if( count( $cond ) < 1 )
46 return false;
47 switch( $cond[0] ) {
48 case APCOND_EMAILCONFIRMED:
49 if( User::isValidEmailAddr( $user->getEmail() ) ) {
50 global $wgEmailAuthentication;
51 if( $wgEmailAuthentication ) {
52 return boolval( $user->getEmailAuthenticationTimestamp() );
53 } else {
54 return true;
55 }
56 }
57 return false;
58 case APCOND_EDITCOUNT:
59 return $user->getEditCount() > $cond[1];
60 case APCOND_AGE:
61 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
62 return $age >= $cond[1];
63 case APCOND_INGROUPS:
64 default:
65 $result = false;
66 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), &$result ) );
67 return $result;
68 }
69 }
70 }