* 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.
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;
}
*/
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.
*
*/
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;
}
*
* 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( '&',
),
);
+/**
+ * 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:
*
* 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
* @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;
}
}
$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();
}
$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' );