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