]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/Autopromote.php
MediaWiki 1.15.4-scripts
[autoinstalls/mediawiki.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 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                 
22                 wfRunHooks( 'GetAutoPromoteGroups', array( $user, &$promote ) );
23                 
24                 return $promote;
25         }
26
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.
38          *
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
42          */
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 ));
64                                 }
65                                 return $res;
66                         } elseif ( $cond[0] = '!' ) {
67                                 foreach( array_slice( $cond, 1 ) as $subcond )
68                                         if( self::recCheckCondition( $subcond, $user ) )
69                                                 return false;
70                                 return true;
71                         }
72                 }
73                 # If we got here, the array presumably does not contain other condi-
74                 # tions; it's not recursive.  Pass it off to self::checkCondition.
75                 if( !is_array( $cond ) )
76                         $cond = array( $cond );
77                 return self::checkCondition( $cond, $user );
78         }
79
80         /**
81          * As recCheckCondition, but *not* recursive.  The only valid conditions
82          * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
83          * APCOND_AGE.  Other types will throw an exception if no extension evalu-
84          * ates them.
85          *
86          * @param $cond Array: A condition, which must not contain other conditions
87          * @param $user The user to check the condition against
88          * @return bool Whether the condition is true for the user
89          */
90         private static function checkCondition( $cond, User $user ) {
91                 if( count( $cond ) < 1 )
92                         return false;
93                 switch( $cond[0] ) {
94                         case APCOND_EMAILCONFIRMED:
95                                 if( User::isValidEmailAddr( $user->getEmail() ) ) {
96                                         global $wgEmailAuthentication;
97                                         if( $wgEmailAuthentication ) {
98                                                 return (bool)$user->getEmailAuthenticationTimestamp();
99                                         } else {
100                                                 return true;
101                                         }
102                                 }
103                                 return false;
104                         case APCOND_EDITCOUNT:
105                                 return $user->getEditCount() >= $cond[1];
106                         case APCOND_AGE:
107                                 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
108                                 return $age >= $cond[1];
109                         case APCOND_AGE_FROM_EDIT:
110                                 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getFirstEditTimestamp() );
111                                 return $age >= $cond[1];
112                         case APCOND_INGROUPS:
113                                 $groups = array_slice( $cond, 1 );
114                                 return count( array_intersect( $groups, $user->getGroups() ) ) == count( $groups );
115                         case APCOND_ISIP:
116                                 return $cond[1] == wfGetIP();
117                         case APCOND_IPINRANGE:
118                                 return IP::isInRange( wfGetIP(), $cond[1] );
119                         default:
120                                 $result = null;
121                                 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
122                                 if( $result === null ) {
123                                         throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
124                                 }
125                                 return $result ? true : false;
126                 }
127         }
128 }