From: Aaron Schulz Date: Sat, 25 Jun 2011 04:58:48 +0000 (+0000) Subject: Follow-up r90749: X-Git-Tag: 1.31.0-rc.0~29292 X-Git-Url: http://git.cyclocoop.org/url?a=commitdiff_plain;h=a44e63967d4f36c7ffd086e977b6484b5370670b;p=lhc%2Fweb%2Fwiklou.git Follow-up r90749: * 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. --- diff --git a/includes/Article.php b/includes/Article.php index ffd6a16f7f..2f87022710 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -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; } diff --git a/includes/Autopromote.php b/includes/Autopromote.php index d9c1f5fba1..f06be2995a 100644 --- a/includes/Autopromote.php +++ b/includes/Autopromote.php @@ -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; } diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index ad7013b85a..158bb5940e 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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: diff --git a/includes/User.php b/includes/User.php index c2742a235c..cffe90b3e7 100644 --- a/includes/User.php +++ b/includes/User.php @@ -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; } } diff --git a/includes/Wiki.php b/includes/Wiki.php index ca42eca408..1ab94c23b6 100644 --- a/includes/Wiki.php +++ b/includes/Wiki.php @@ -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' );