http://www.mediawiki.org/wiki/Special:Code/MediaWiki/90755
Revision: 90755
Author: aaron
Date: 2011-06-25 04:58:48 +0000 (Sat, 25 Jun 2011)
Log Message:
-----------
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.
Modified Paths:
--------------
trunk/phase3/includes/Article.php
trunk/phase3/includes/Autopromote.php
trunk/phase3/includes/DefaultSettings.php
trunk/phase3/includes/User.php
trunk/phase3/includes/Wiki.php
Modified: trunk/phase3/includes/Article.php
===================================================================
--- trunk/phase3/includes/Article.php 2011-06-25 04:45:52 UTC (rev 90754)
+++ trunk/phase3/includes/Article.php 2011-06-25 04:58:48 UTC (rev 90755)
@@ -2347,6 +2347,9 @@
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;
}
Modified: trunk/phase3/includes/Autopromote.php
===================================================================
--- trunk/phase3/includes/Autopromote.php 2011-06-25 04:45:52 UTC (rev
90754)
+++ trunk/phase3/includes/Autopromote.php 2011-06-25 04:58:48 UTC (rev
90755)
@@ -6,41 +6,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.
*
* @param $user User The user to get the groups for
@@ -76,24 +41,25 @@
*/
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;
}
Modified: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php 2011-06-25 04:45:52 UTC (rev
90754)
+++ trunk/phase3/includes/DefaultSettings.php 2011-06-25 04:58:48 UTC (rev
90755)
@@ -3513,12 +3513,6 @@
*
* 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( '&',
@@ -3528,6 +3522,25 @@
);
/**
+ * 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:
*
Modified: trunk/phase3/includes/User.php
===================================================================
--- trunk/phase3/includes/User.php 2011-06-25 04:45:52 UTC (rev 90754)
+++ trunk/phase3/includes/User.php 2011-06-25 04:58:48 UTC (rev 90755)
@@ -1107,28 +1107,30 @@
*
* 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 @@
* @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;
}
}
Modified: trunk/phase3/includes/Wiki.php
===================================================================
--- trunk/phase3/includes/Wiki.php 2011-06-25 04:45:52 UTC (rev 90754)
+++ trunk/phase3/includes/Wiki.php 2011-06-25 04:58:48 UTC (rev 90755)
@@ -113,6 +113,9 @@
$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 @@
$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' );
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs