r28805 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r28804‎ | r28805 | r28806 >
Date:19:53, 23 December 2007
Author:simetrical
Status:old
Tags:
Comment:
Fixes for r28797.
* Mark private methods private using a keyword.
* Reject arrays with count == 2: these will fail when you do array_slice( ... , 1 ).
* Treat xor consistent with the other operations: if there's only one parameter the result should just evaluate that, not always return false; and any number of parameters should be allowed.
* Fail fast on bad input: throw an exception if Autopromote encounters a condition it can't understand (after asking extensions).
* Code documentation! There were five lines of comments in the original commit.
* APCONDS_INGROUPS is not used, or for that matter defined.
* Editcount should use >=, not >, for consistency with past behavior and intuitiveness.
* "autopromoteUser" sounds like it's actually promoting the user somehow. Renamed the function to getAutopromoteGroups.
* Make sure we don't return the same group more than once, when we're returning a group. Probably not going to hurt, but may as well be clean.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/Autopromote.php (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/Defines.php (modified) (history)
  • /trunk/phase3/includes/User.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Defines.php
@@ -277,7 +277,8 @@
278278 # Flags for Parser::replaceLinkHolders
279279 define( 'RLH_FOR_UPDATE', 1 );
280280
281 -#Autopromote conditions
 281+# Autopromote conditions (must be here and not in Autopromote.php, so that
 282+# they're loaded for DefaultSettings.php before AutoLoader.php)
282283 define( 'APCOND_EDITCOUNT', 1 );
283284 define( 'APCOND_AGE', 2 );
284285 define( 'APCOND_EMAILCONFIRMED', 3 );
Index: trunk/phase3/includes/User.php
@@ -1652,10 +1652,10 @@
16531653 if( $this->mId ) {
16541654 $this->mEffectiveGroups[] = 'user';
16551655
1656 - $this->mEffectiveGroups = array_merge(
 1656+ $this->mEffectiveGroups = array_unique( array_merge(
16571657 $this->mEffectiveGroups,
1658 - Autopromote::autopromoteUser( $this )
1659 - );
 1658+ Autopromote::getAutopromoteGroups( $this )
 1659+ ) );
16601660
16611661 # Hook for additional groups
16621662 wfRunHooks( 'UserEffectiveGroups', array( &$this, &$this->mEffectiveGroups ) );
Index: trunk/phase3/includes/DefaultSettings.php
@@ -1189,7 +1189,18 @@
11901190 //$wgAutoConfirmCount = 50;
11911191
11921192 /**
1193 - * Automatically promote user flags to users that matchs some conditions
 1193+ * Automatically add a usergroup to any user who matches certain conditions.
 1194+ * The format is
 1195+ * array( '&' or '|' or '^', cond1, cond2, ... )
 1196+ * where cond1, cond2, ... are themselves conditions; *OR*
 1197+ * APCOND_EMAILCONFIRMED, *OR*
 1198+ * array( APCOND_EMAILCONFIRMED ), *OR*
 1199+ * array( APCOND_EDITCOUNT, number of edits ), *OR*
 1200+ * array( APCOND_AGE, seconds since registration ), *OR*
 1201+ * similar constructs defined by extensions.
 1202+ *
 1203+ * Note that EMAILCONFIRMED will always be true if $wgEmailAuthentication is
 1204+ * off!
11941205 */
11951206 $wgAutopromote = array(
11961207 'autoconfirmed' => array( '&',
Index: trunk/phase3/includes/Autopromote.php
@@ -4,21 +4,43 @@
55 * This class checks if user can get extra rights
66 * because of conditions specified in $wgAutopromote
77 */
8 -class Autopromote {
9 - public static function autopromoteUser( $user ) {
 8+class Autopromote {
 9+ /**
 10+ * Get the groups for the given user based on $wgAutopromote.
 11+ *
 12+ * @param User $user The user to get the groups for
 13+ * @return array Array of groups to promote to.
 14+ */
 15+ public static function getAutopromoteGroups( User $user ) {
1016 global $wgAutopromote;
1117 $promote = array();
1218 foreach( $wgAutopromote as $group => $cond ) {
1319 if( self::recCheckCondition( $cond, $user ) )
1420 $promote[] = $group;
1521 }
16 - return $promote;
 22+ return array_unique( $promote );
1723 }
1824
19 - //@private
20 - static function recCheckCondition( $cond, $user ) {
 25+ /**
 26+ * Recursively check a condition. Conditions are in the form
 27+ * array( '&' or '|' or '^', cond1, cond2, ... )
 28+ * where cond1, cond2, ... are themselves conditions; *OR*
 29+ * APCOND_EMAILCONFIRMED, *OR*
 30+ * array( APCOND_EMAILCONFIRMED ), *OR*
 31+ * array( APCOND_EDITCOUNT, number of edits ), *OR*
 32+ * array( APCOND_AGE, seconds since registration ), *OR*
 33+ * similar constructs defined by extensions.
 34+ * This function evaluates the former type recursively, and passes off to
 35+ * self::checkCondition for evaluation of the latter type.
 36+ *
 37+ * @param mixed $cond A condition, possibly containing other conditions
 38+ * @param User $user The user to check the conditions against
 39+ * @return bool Whether the condition is true
 40+ */
 41+ private static function recCheckCondition( $cond, User $user ) {
2142 $validOps = array( '&', '|', '^' );
22 - if( is_array( $cond ) && count( $cond ) > 0 && in_array( $cond[0], $validOps ) ) {
 43+ if( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], $validOps ) ) {
 44+ # Recursive condition
2345 if( $cond[0] == '&' ) {
2446 foreach( array_slice( $cond, 1 ) as $subcond )
2547 if( !self::recCheckCondition( $subcond, $user ) )
@@ -30,23 +52,41 @@
3153 return true;
3254 return false;
3355 } elseif( $cond[0] == '^' ) {
34 - if( count( $cond ) < 3 )
35 - return false;
36 - return self::recCheckCondition( $cond[1], $user )
37 - xor self::recCheckCondition( $cond[2], $user );
 56+ $res = null;
 57+ foreach( array_slice( $cond, 1 ) as $subcond ) {
 58+ if( is_null( $res ) )
 59+ $res = self::recCheckCondition( $subcond, $user );
 60+ else
 61+ $res = ($res xor self::recCheckCondition( $subcond, $user ));
 62+ }
 63+ return $res;
3864 }
3965 }
 66+ # If we got here, the array presumably does not contain other condi-
 67+ # tions; it's not recursive. Pass it off to self::checkCondition.
4068 if( !is_array( $cond ) )
4169 $cond = array( $cond );
4270 return self::checkCondition( $cond, $user );
4371 }
4472
45 - static function checkCondition( $cond, $user ) {
 73+ /**
 74+ * As recCheckCondition, but *not* recursive. The only valid conditions
 75+ * are those whose first element is APCOND_EMAILCONFIRMED/APCOND_EDITCOUNT/
 76+ * APCOND_AGE. Other types will throw an exception if no extension evalu-
 77+ * ates them.
 78+ *
 79+ * @param array $cond A condition, which must not contain other conditions
 80+ * @param User $user The user to check the condition against
 81+ * @return bool Whether the condition is true for the user
 82+ */
 83+ private static function checkCondition( $cond, User $user ) {
4684 if( count( $cond ) < 1 )
4785 return false;
4886 switch( $cond[0] ) {
4987 case APCOND_EMAILCONFIRMED:
5088 if( User::isValidEmailAddr( $user->getEmail() ) ) {
 89+ # FIXME: EMAILCONFIRMED is always true if
 90+ # wgEmailAuthentication if false, this is confusing.
5191 global $wgEmailAuthentication;
5292 if( $wgEmailAuthentication ) {
5393 return $user->getEmailAuthenticationTimestamp() ? true : false;
@@ -56,15 +96,17 @@
5797 }
5898 return false;
5999 case APCOND_EDITCOUNT:
60 - return $user->getEditCount() > $cond[1];
 100+ return $user->getEditCount() >= $cond[1];
61101 case APCOND_AGE:
62102 $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
63103 return $age >= $cond[1];
64 - case APCOND_INGROUPS:
65104 default:
66 - $result = false;
 105+ $result = null;
67106 wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), $user, &$result ) );
68 - return $result;
 107+ if( $result === null ) {
 108+ throw new MWException( "Unrecognized condition {$cond[0]} for autopromotion!" );
 109+ }
 110+ return $result ? true : false;
69111 }
70112 }
71 -}
\ No newline at end of file
 113+}
Index: trunk/phase3/RELEASE-NOTES
@@ -34,8 +34,7 @@
3535 any groups they are in.
3636 * New permission userrights-interwiki for changing user rights on foreign wikis.
3737 * $wgImplictGroups for groups that are hidden from Special:Listusers, etc.
38 -* $wgAutopromote: automatically promote user flags when user matches
39 - criterias
 38+* $wgAutopromote: automatically promote users who match specified criteria
4039
4140 === New features in 1.12 ===
4241 * (bug 10735) Add a warning for non-descriptive filenames at Special:Upload

Follow-up revisions

RevisionCommit summaryAuthorDate
r90819Made '^' (XOR) in recCheckCondition() act as a one-hot detector. Before r2880...aaron04:50, 26 June 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r28797Introduce new autopromotion systemvasilievvv11:38, 23 December 2007

Status & tagging log