* Move $var ? true : false check to boolval function
[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 /**
9 * Get the groups for the given user based on $wgAutopromote.
10 *
11 * @param User $user The user to get the groups for
12 * @return array Array of groups to promote to.
13 */
14 public static function getAutopromoteGroups( User $user ) {
15 global $wgAutopromote;
16 $promote = array();
17 foreach( $wgAutopromote as $group => $cond ) {
18 if( self::recCheckCondition( $cond, $user ) )
19 $promote[] = $group;
20 }
21 return $promote;
22 }
23
24 /**
25 * Recursively check a condition. Conditions are in the form
26 * array( '&' or '|' or '^', cond1, cond2, ... )
27 * where cond1, cond2, ... are themselves conditions; *OR*
28 * APCOND_EMAILCONFIRMED, *OR*
29 * array( APCOND_EMAILCONFIRMED ), *OR*
30 * array( APCOND_EDITCOUNT, number of edits ), *OR*
31 * array( APCOND_AGE, seconds since registration ), *OR*
32 * similar constructs defined by extensions.
33 * This function evaluates the former type recursively, and passes off to
34 * self::checkCondition for evaluation of the latter type.
35 *
36 * @param mixed $cond A condition, possibly containing other conditions
37 * @param User $user The user to check the conditions against
38 * @return bool Whether the condition is true
39 */
40 private static function recCheckCondition( $cond, User $user ) {
41 $validOps = array( '&', '|', '^' );
42 if( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
43 # Recursive condition
44 if( $cond[0] == '&' ) {
45 foreach( array_slice( $cond, 1 ) as $subcond )
46 if( !self::recCheckCondition( $subcond, $user ) )
47 return false;
48 return true;
49 } elseif( $cond[0] == '|' ) {
50 foreach( array_slice( $cond, 1 ) as $subcond )
51 if( self::recCheckCondition( $subcond, $user ) )
52 return true;
53 return false;
54 } elseif( $cond[0] == '^' ) {
55 $res = null;
56 foreach( array_slice( $cond, 1 ) as $subcond ) {
57 if( is_null( $res ) )
58 $res = self::recCheckCondition( $subcond, $user );
59 else
60 $res = ($res xor self::recCheckCondition( $subcond, $user ));
61 }
62 return $res;
63 }
64 }
65 # If we got here, the array presumably does not contain other condi-
66 # tions; it's not recursive. Pass it off to self::checkCondition.
67 if( !is_array( $cond ) )
68 $cond = array( $cond );
69 return self::checkCondition( $cond, $user );
70 }
71
72 /**
73 * As recCheckCondition, but *not* recursive. The only valid conditions
74 * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
75 * APCOND_AGE. Other types will throw an exception if no extension evalu-
76 * ates them.
77 *
78 * @param array $cond A condition, which must not contain other conditions
79 * @param User $user The user to check the condition against
80 * @return bool Whether the condition is true for the user
81 */
82 private static function checkCondition( $cond, User $user ) {
83 if( count( $cond ) < 1 )
84 return false;
85 switch( $cond[0] ) {
86 case APCOND_EMAILCONFIRMED:
87 if( User::isValidEmailAddr( $user->getEmail() ) ) {
88 global $wgEmailAuthentication;
89 if( $wgEmailAuthentication ) {
90 return boolval( $user->getEmailAuthenticationTimestamp() );
91 } else {
92 return true;
93 }
94 }
95 return false;
96 case APCOND_EDITCOUNT:
97 return $user->getEditCount() >= $cond[1];
98 case APCOND_AGE:
99 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
100 return $age >= $cond[1];
101 case APCOND_INGROUPS:
102 $groups = array_slice( $cond, 1 );
103 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
104 default:
105 $result = null;
106 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
107 if( $result === null ) {
108 throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
109 }
110 return $result ? true : false;
111 }
112 }
113 }