Follow-up r90749:
authorAaron Schulz <aaron@users.mediawiki.org>
Sat, 25 Jun 2011 04:58:48 +0000 (04:58 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Sat, 25 Jun 2011 04:58:48 +0000 (04:58 +0000)
* Removed clunky autopromoteOnceHook function and added $wgAutopromoteOnce. It currently supports edit or view based triggering. This makes configuration much simpler.
* Added short-circuit to addAutopromoteOnceGroups() by checking if $criteria is empty
* Spacing tweaks and typo fixes.

includes/Article.php
includes/Autopromote.php
includes/DefaultSettings.php
includes/User.php
includes/Wiki.php

index ffd6a16..2f87022 100644 (file)
@@ -2347,6 +2347,9 @@ class Article {
                wfRunHooks( 'ArticleSaveComplete', array( &$this, &$user, $text, $summary,
                        $flags & EDIT_MINOR, null, null, &$flags, $revision, &$status, $baseRevId ) );
 
+               # Promote user to any groups they meet the criteria for
+               $user->addAutopromoteOnceGroups( 'onEdit' );
+
                wfProfileOut( __METHOD__ );
                return $status;
        }
index d9c1f5f..f06be29 100644 (file)
@@ -5,41 +5,6 @@
  */
 
 class Autopromote {
-       /**
-        * A function which may be assigned to a hook in order to check 
-        *   autopromotion of the current user (\ref $wgUser) to the specified 
-        *   group.
-        *    
-        * Contrary to autopromotion by \ref $wgAutopromote, the group will be 
-        *   possible to remove manually via Special:UserRights. In such case it
-        *   will not be re-added autmoatically. The user will also not lose the
-        *   group if they no longer meet the criteria.
-        *   
-        * Example configuration: 
-        * \code $wgHooks['ArticleSaveComplete'][] = array (
-        *     'Autopromote::autopromoteOnceHook', 
-        *     array( 'somegroup' => array(APCOND_EDITCOUNT, 200) )
-        * ); \endcode
-        * 
-        * The second array should be of the same format as \ref $wgAutopromote.
-        * 
-        * This funciton simply runs User::autopromoteOnce() on $wgUser. You may
-        *   run this method from your custom function if you wish.  
-        * 
-        * @param $criteria array Groups and conditions which must be met in order to
-        *   aquire these groups. Array of the same format as \ref $wgAutopromote.
-        *               
-        * @return Always true.
-        * 
-        * @see User::autopromoteOnce()
-        * @see $wgAutopromote
-        */
-       public static function autopromoteOnceHook($criteria) {
-               global $wgUser; 
-               $wgUser->autopromoteOnce($criteria); 
-               return true; 
-       }
-       
        /**
         * Get the groups for the given user based on $wgAutopromote.
         *
@@ -76,24 +41,25 @@ class Autopromote {
         */
        public static function getAutopromoteOnceGroups( User $user, $criteria ) {
                $promote = array();
-               
-               //get the current groups 
+
                $currentGroups = $user->getGroups();
-               
-               foreach( $criteria as $group => $cond ) {
-                       //do not check if the user's already a member
-                       if ( in_array($group, $currentGroups))
+
+               foreach ( $criteria as $group => $cond ) {
+                       // Do not check if the user's already a member
+                       if ( in_array( $group, $currentGroups ) ) {
                                continue;
-               
-                       //do not autopromote if the user has belonged to the group
+                       }
+                       // Do not autopromote if the user has belonged to the group
                        $formerGroups = $user->getFormerGroups();
-                       if ( in_array($group, $formerGroups) )
+                       if ( in_array( $group, $formerGroups ) ) {
                                continue;
-                               
-                       //finally - check the conditions 
-                       if ( self::recCheckCondition($cond, $user) )
-                               $promote[] = $group; 
+                       }
+                       // Finally - check the conditions
+                       if ( self::recCheckCondition( $cond, $user ) ) {
+                               $promote[] = $group;
+                       }
                }
+
                return $promote;
        }
 
index ad7013b..158bb59 100644 (file)
@@ -3513,12 +3513,6 @@ $wgAutoConfirmCount = 0;
  *
  * If $wgEmailAuthentication is off, APCOND_EMAILCONFIRMED will be true for any
  * user who has provided an e-mail address.
- * 
- * If the groups should be removable, consider using 
- *   Autopromote::autopromoteOnceHook() instead.
- * 
- * @see Autopromote::autopromoteOnceHook()
- * @see User::autopromoteOnce()
  */
 $wgAutopromote = array(
        'autoconfirmed' => array( '&',
@@ -3527,6 +3521,25 @@ $wgAutopromote = array(
        ),
 );
 
+/**
+ * Automatically add a usergroup to any user who matches certain conditions.
+ * Does not add the user to the group again if it has been removed.
+ * Also, does not remove the group if the user no longer meets the criteria.
+ *
+ * The format is
+ *     array( event => criteria, ... )
+ * where event is
+ *     'onEdit' (when user edits) or 'onView' (when user views the wiki)
+ * and criteria has the same format as $wgAutopromote
+ *
+ * @see $wgAutopromote
+ * @since 1.18
+ */
+$wgAutopromoteOnce = array(
+       'onEdit' => array(),
+       'onView' => array()
+);
+
 /**
  * $wgAddGroups and $wgRemoveGroups can be used to give finer control over who
  * can assign which groups at Special:Userrights.  Example configuration:
index c2742a2..cffe90b 100644 (file)
@@ -1107,28 +1107,30 @@ class User {
         * 
         * Contrary to autopromotion by \ref $wgAutopromote, the group will be 
         *   possible to remove manually via Special:UserRights. In such case it
-        *   will not be re-added autmoatically. The user will also not lose the
+        *   will not be re-added automatically. The user will also not lose the
         *   group if they no longer meet the criteria.
         *   
-        * @param $criteria array Groups and conditions the user must meet in order
-        *   to be promoted to these groups. Array of the same format as 
-        *   \ref $wgAutopromote. 
+        * @param $event String 'onEdit' or 'onView' (each one has groups/criteria)
         *   
         * @return array Array of groups the user has been promoted to.  
         * 
         * @see $wgAutopromote
-        * @see Autopromote::autopromoteOnceHook()
         */
-       public function autopromoteOnce( $criteria ) {          
-               if ($this->getId())     {
-                       $toPromote = Autopromote::getAutopromoteOnceGroups($this, $criteria);
-                       foreach($toPromote as $group)
-                               $this->addGroup($group);
-                       return $toPromote;
+       public function addAutopromoteOnceGroups( $event ) {
+               global $wgAutopromoteOnce;
+               if ( isset( $wgAutopromoteOnce[$event] ) ) {
+                       $criteria = $wgAutopromoteOnce[$event]; // group/requirement pairs
+                       if ( count( $criteria ) && $this->getId() ) {
+                               $toPromote = Autopromote::getAutopromoteOnceGroups( $this, $criteria );
+                               foreach ( $toPromote as $group ) {
+                                       $this->addGroup( $group );
+                               }
+                               return $toPromote;
+                       }
                }
                return array(); 
        }
-       
+
        /**
         * Clear various cached data stored in this object.
         * @param $reloadFrom String Reload user and user_groups table data from a
@@ -2278,14 +2280,14 @@ class User {
         * @return array Names of the groups the user has belonged to. 
         */
        function getFormerGroups() {
-               if(is_null($this->mFormerGroups)) {
+               if( is_null( $this->mFormerGroups ) ) {
                        $dbr = wfGetDB( DB_MASTER );
                        $res = $dbr->select( 'user_former_groups',
                                array( 'ufg_group' ),
                                array( 'ufg_user' => $this->mId ),
                                __METHOD__ );
                        $this->mFormerGroups = array();
-                       while( $row = $dbr->fetchObject( $res ) ) {
+                       foreach( $res as $row ) {
                                $this->mFormerGroups[] = $row->ufg_group;
                        }
                }       
index ca42eca..1ab94c2 100644 (file)
@@ -113,6 +113,9 @@ class MediaWiki {
                $output = $this->context->getOutput();
                $user = $this->context->getUser();
 
+               # Promote user to any groups they meet the criteria for
+               $user->addAutopromoteOnceGroups( 'onView' );
+
                if ( $request->getVal( 'printable' ) === 'yes' ) {
                        $output->setPrintable();
                }
@@ -125,7 +128,7 @@ class MediaWiki {
                        $request,
                        $this
                ) );
-
+               
                // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
                if ( $title instanceof BadTitle ) {
                        throw new ErrorPageError( 'badtitle', 'badtitletext' );