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