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