Introduce new autopromotion system
authorVictor Vasiliev <vasilievvv@users.mediawiki.org>
Sun, 23 Dec 2007 11:38:24 +0000 (11:38 +0000)
committerVictor Vasiliev <vasilievvv@users.mediawiki.org>
Sun, 23 Dec 2007 11:38:24 +0000 (11:38 +0000)
RELEASE-NOTES
includes/AutoLoader.php
includes/Autopromote.php [new file with mode: 0644]
includes/DefaultSettings.php
includes/Defines.php
includes/User.php

index 93367e6..e3bf47a 100644 (file)
@@ -33,6 +33,9 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
      remove the groups specified in $wgAddGroups and $wgRemoveGroups for
      any groups they are in.
 * New permission userrights-interwiki for changing user rights on foreign wikis.
+* $wgImplictGroups for groups that are hidden from Special:Listusers, etc.
+* $wgAutopromote: automatically promote user flags when user matches
+  criterias
 
 === New features in 1.12 ===
 * (bug 10735) Add a warning for non-descriptive filenames at Special:Upload
index 2a01444..4f92f65 100644 (file)
@@ -17,6 +17,7 @@ function __autoload($className) {
                'AlphabeticPager' => 'includes/Pager.php',
                'Article' => 'includes/Article.php',
                'AuthPlugin' => 'includes/AuthPlugin.php',
+               'Autopromote' => 'includes/Autopromote.php',
                'BagOStuff' => 'includes/BagOStuff.php',
                'HashBagOStuff' => 'includes/BagOStuff.php',
                'SqlBagOStuff' => 'includes/BagOStuff.php',
diff --git a/includes/Autopromote.php b/includes/Autopromote.php
new file mode 100644 (file)
index 0000000..97ea48d
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+
+/**
+ * This class checks if user can get extra rights
+ * because of conditions specified in $wgAutopromote
+ */
+class Autopromote {    
+       public static function autopromoteUser( $user ) {
+               global $wgAutopromote;
+               $promote = array();
+               foreach( $wgAutopromote as $group => $cond ) {
+                       if( self::recCheckCondition( $cond, $user ) )
+                               $promote[] = $group;
+               }
+               return $promote;
+       }
+
+       //@private
+       static function recCheckCondition( $cond, $user ) {
+               $validOps = array( '&', '|', '^' );
+               if( is_array( $cond ) && count( $cond ) > 0 && in_array( $cond[0], $validOps ) ) {
+                       if( $cond[0] == '&' ) {
+                               foreach( array_slice( $cond, 1 ) as $subcond )
+                                       if( !self::recCheckCondition( $subcond, $user ) )
+                                               return false;
+                               return true;
+                       } elseif( $cond[0] == '|' ) {
+                               foreach( array_slice( $cond, 1 ) as $subcond ) 
+                                       if( self::recCheckCondition( $subcond, $user ) )
+                                               return true;
+                               return false;
+                       } elseif( $cond[0] == '^' ) {
+                               if( count( $cond ) < 3 )
+                                       return false;
+                               return self::recCheckCondition( $cond[1], $user )
+                                       xor self::recCheckCondition( $cond[2], $user );
+                       }
+               }
+               if( !is_array( $cond ) )
+                       $cond = array( $cond );
+               return self::checkCondition( $cond, $user );
+       }
+
+       static function checkCondition( $cond, $user ) {
+               if( count( $cond ) < 1 )
+                       return false;
+               switch( $cond[0] ) {
+                       case APCOND_EMAILCONFIRMED:
+                               if( User::isValidEmailAddr( $user->getEmail() ) ) {
+                                       global $wgEmailAuthentication;
+                                       if( $wgEmailAuthentication ) {
+                                               return boolval( $user->getEmailAuthenticationTimestamp() );
+                                       } else {
+                                               return true;
+                                       }
+                               }
+                               return false;
+                       case APCOND_EDITCOUNT:
+                               return $user->getEditCount() > $cond[1];
+                       case APCOND_AGE:
+                               $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
+                               return $age >= $cond[1];
+                       case APCOND_INGROUPS:
+                       default:
+                               $result = false;
+                               wfRunHooks( 'AutopromoteCondition', array( $cond[0], array_slice( $cond, 1 ), &$result ) );
+                               return $result;
+               }
+       }
+}
\ No newline at end of file
index f066a11..fde53de 100644 (file)
@@ -1127,6 +1127,12 @@ $wgGroupPermissions['bureaucrat']['userrights'] = true;
  */
 # $wgGroupPermissions['developer']['siteadmin'] = true;
 
+
+/**
+ * Implicit groups, aren't shown on Special:Listusers or somewhere else
+ */
+$wgImplicitGroups = array( '*', 'user', 'autoconfirmed', 'emailconfirmed' );
+
 /**
  * Set of available actions that can be restricted via action=protect
  * You probably shouldn't change this.
@@ -1182,6 +1188,17 @@ $wgAutoConfirmAge = 0;
 $wgAutoConfirmCount = 0;
 //$wgAutoConfirmCount = 50;
 
+/**
+ * Automatically promote user flags to users that matchs some conditions
+ */
+$wgAutopromote = array(
+       'autoconfirmed' => array( '&',
+               array( APCOND_EDITCOUNT, &$wgAutoConfirmCount ),
+               array( APCOND_AGE, &$wgAutoConfirmAge ),
+       ),
+       'emailconfirmed' => APCOND_EMAILCONFIRMED,
+);
+
 /**
  * These settings can be used to give finer control over who can assign which
  * groups at Special:Userrights.  Example configuration:
index 2ee527d..d76b347 100644 (file)
@@ -277,3 +277,7 @@ define( 'SFH_OBJECT_ARGS', 2 );
 # Flags for Parser::replaceLinkHolders
 define( 'RLH_FOR_UPDATE', 1 );
 
+#Autopromote conditions
+define( 'APCOND_EDITCOUNT', 1 );
+define( 'APCOND_AGE', 2 );
+define( 'APCOND_EMAILCONFIRMED', 3 );
index 0d2865c..10048a6 100644 (file)
@@ -1651,23 +1651,12 @@ class User {
                        $this->mEffectiveGroups[] = '*';
                        if( $this->mId ) {
                                $this->mEffectiveGroups[] = 'user';
-                               
-                               global $wgAutoConfirmAge, $wgAutoConfirmCount;
 
-                               $accountAge = time() - wfTimestampOrNull( TS_UNIX, $this->mRegistration );
-                               if( $accountAge >= $wgAutoConfirmAge && $this->getEditCount() >= $wgAutoConfirmCount ) {
-                                       $this->mEffectiveGroups[] = 'autoconfirmed';
-                               }
-                               # Implicit group for users whose email addresses are confirmed
-                               global $wgEmailAuthentication;
-                               if( self::isValidEmailAddr( $this->mEmail ) ) {
-                                       if( $wgEmailAuthentication ) {
-                                               if( $this->mEmailAuthenticated )
-                                                       $this->mEffectiveGroups[] = 'emailconfirmed';
-                                       } else {
-                                               $this->mEffectiveGroups[] = 'emailconfirmed';
-                                       }
-                               }
+                               $this->mEffectiveGroups = array_merge(
+                                       $this->mEffectiveGroups,
+                                       Autopromote::autopromoteUser( $this )
+                               );
+
                                # Hook for additional groups
                                wfRunHooks( 'UserEffectiveGroups', array( &$this, &$this->mEffectiveGroups ) );
                        }
@@ -2595,11 +2584,9 @@ class User {
         * @return array
         */
        public static function getImplicitGroups() {
-               static $groups = null;
-               if( !is_array( $groups ) ) {
-                       $groups = array( '*', 'user', 'autoconfirmed', 'emailconfirmed' );
-                       wfRunHooks( 'UserGetImplicitGroups', array( &$groups ) );
-               }
+               global $wgImplicitGroups;
+               $groups = $wgImplicitGroups;
+               wfRunHooks( 'UserGetImplicitGroups', array( &$groups ) );       #deprecated, use $wgImplictGroups instead
                return $groups;
        }