r90755 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90754‎ | r90755 | r90756 >
Date:04:58, 25 June 2011
Author:aaron
Status:ok
Tags:
Comment:
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 (modified) (history)
  • /trunk/phase3/includes/Autopromote.php (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/User.php (modified) (history)
  • /trunk/phase3/includes/Wiki.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/User.php
@@ -1107,28 +1107,30 @@
11081108 *
11091109 * Contrary to autopromotion by \ref $wgAutopromote, the group will be
11101110 * possible to remove manually via Special:UserRights. In such case it
1111 - * will not be re-added autmoatically. The user will also not lose the
 1111+ * will not be re-added automatically. The user will also not lose the
11121112 * group if they no longer meet the criteria.
11131113 *
1114 - * @param $criteria array Groups and conditions the user must meet in order
1115 - * to be promoted to these groups. Array of the same format as
1116 - * \ref $wgAutopromote.
 1114+ * @param $event String 'onEdit' or 'onView' (each one has groups/criteria)
11171115 *
11181116 * @return array Array of groups the user has been promoted to.
11191117 *
11201118 * @see $wgAutopromote
1121 - * @see Autopromote::autopromoteOnceHook()
11221119 */
1123 - public function autopromoteOnce( $criteria ) {
1124 - if ($this->getId()) {
1125 - $toPromote = Autopromote::getAutopromoteOnceGroups($this, $criteria);
1126 - foreach($toPromote as $group)
1127 - $this->addGroup($group);
1128 - return $toPromote;
 1120+ public function addAutopromoteOnceGroups( $event ) {
 1121+ global $wgAutopromoteOnce;
 1122+ if ( isset( $wgAutopromoteOnce[$event] ) ) {
 1123+ $criteria = $wgAutopromoteOnce[$event]; // group/requirement pairs
 1124+ if ( count( $criteria ) && $this->getId() ) {
 1125+ $toPromote = Autopromote::getAutopromoteOnceGroups( $this, $criteria );
 1126+ foreach ( $toPromote as $group ) {
 1127+ $this->addGroup( $group );
 1128+ }
 1129+ return $toPromote;
 1130+ }
11291131 }
11301132 return array();
11311133 }
1132 -
 1134+
11331135 /**
11341136 * Clear various cached data stored in this object.
11351137 * @param $reloadFrom String Reload user and user_groups table data from a
@@ -2278,14 +2280,14 @@
22792281 * @return array Names of the groups the user has belonged to.
22802282 */
22812283 function getFormerGroups() {
2282 - if(is_null($this->mFormerGroups)) {
 2284+ if( is_null( $this->mFormerGroups ) ) {
22832285 $dbr = wfGetDB( DB_MASTER );
22842286 $res = $dbr->select( 'user_former_groups',
22852287 array( 'ufg_group' ),
22862288 array( 'ufg_user' => $this->mId ),
22872289 __METHOD__ );
22882290 $this->mFormerGroups = array();
2289 - while( $row = $dbr->fetchObject( $res ) ) {
 2291+ foreach( $res as $row ) {
22902292 $this->mFormerGroups[] = $row->ufg_group;
22912293 }
22922294 }
Index: trunk/phase3/includes/Article.php
@@ -2347,6 +2347,9 @@
23482348 wfRunHooks( 'ArticleSaveComplete', array( &$this, &$user, $text, $summary,
23492349 $flags & EDIT_MINOR, null, null, &$flags, $revision, &$status, $baseRevId ) );
23502350
 2351+ # Promote user to any groups they meet the criteria for
 2352+ $user->addAutopromoteOnceGroups( 'onEdit' );
 2353+
23512354 wfProfileOut( __METHOD__ );
23522355 return $status;
23532356 }
Index: trunk/phase3/includes/Wiki.php
@@ -113,6 +113,9 @@
114114 $output = $this->context->getOutput();
115115 $user = $this->context->getUser();
116116
 117+ # Promote user to any groups they meet the criteria for
 118+ $user->addAutopromoteOnceGroups( 'onView' );
 119+
117120 if ( $request->getVal( 'printable' ) === 'yes' ) {
118121 $output->setPrintable();
119122 }
@@ -125,7 +128,7 @@
126129 $request,
127130 $this
128131 ) );
129 -
 132+
130133 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
131134 if ( $title instanceof BadTitle ) {
132135 throw new ErrorPageError( 'badtitle', 'badtitletext' );
Index: trunk/phase3/includes/DefaultSettings.php
@@ -3513,12 +3513,6 @@
35143514 *
35153515 * If $wgEmailAuthentication is off, APCOND_EMAILCONFIRMED will be true for any
35163516 * user who has provided an e-mail address.
3517 - *
3518 - * If the groups should be removable, consider using
3519 - * Autopromote::autopromoteOnceHook() instead.
3520 - *
3521 - * @see Autopromote::autopromoteOnceHook()
3522 - * @see User::autopromoteOnce()
35233517 */
35243518 $wgAutopromote = array(
35253519 'autoconfirmed' => array( '&',
@@ -3528,6 +3522,25 @@
35293523 );
35303524
35313525 /**
 3526+ * Automatically add a usergroup to any user who matches certain conditions.
 3527+ * Does not add the user to the group again if it has been removed.
 3528+ * Also, does not remove the group if the user no longer meets the criteria.
 3529+ *
 3530+ * The format is
 3531+ * array( event => criteria, ... )
 3532+ * where event is
 3533+ * 'onEdit' (when user edits) or 'onView' (when user views the wiki)
 3534+ * and criteria has the same format as $wgAutopromote
 3535+ *
 3536+ * @see $wgAutopromote
 3537+ * @since 1.18
 3538+ */
 3539+$wgAutopromoteOnce = array(
 3540+ 'onEdit' => array(),
 3541+ 'onView' => array()
 3542+);
 3543+
 3544+/**
35323545 * $wgAddGroups and $wgRemoveGroups can be used to give finer control over who
35333546 * can assign which groups at Special:Userrights. Example configuration:
35343547 *
Index: trunk/phase3/includes/Autopromote.php
@@ -6,41 +6,6 @@
77
88 class Autopromote {
99 /**
10 - * A function which may be assigned to a hook in order to check
11 - * autopromotion of the current user (\ref $wgUser) to the specified
12 - * group.
13 - *
14 - * Contrary to autopromotion by \ref $wgAutopromote, the group will be
15 - * possible to remove manually via Special:UserRights. In such case it
16 - * will not be re-added autmoatically. The user will also not lose the
17 - * group if they no longer meet the criteria.
18 - *
19 - * Example configuration:
20 - * \code $wgHooks['ArticleSaveComplete'][] = array (
21 - * 'Autopromote::autopromoteOnceHook',
22 - * array( 'somegroup' => array(APCOND_EDITCOUNT, 200) )
23 - * ); \endcode
24 - *
25 - * The second array should be of the same format as \ref $wgAutopromote.
26 - *
27 - * This funciton simply runs User::autopromoteOnce() on $wgUser. You may
28 - * run this method from your custom function if you wish.
29 - *
30 - * @param $criteria array Groups and conditions which must be met in order to
31 - * aquire these groups. Array of the same format as \ref $wgAutopromote.
32 - *
33 - * @return Always true.
34 - *
35 - * @see User::autopromoteOnce()
36 - * @see $wgAutopromote
37 - */
38 - public static function autopromoteOnceHook($criteria) {
39 - global $wgUser;
40 - $wgUser->autopromoteOnce($criteria);
41 - return true;
42 - }
43 -
44 - /**
4510 * Get the groups for the given user based on $wgAutopromote.
4611 *
4712 * @param $user User The user to get the groups for
@@ -76,24 +41,25 @@
7742 */
7843 public static function getAutopromoteOnceGroups( User $user, $criteria ) {
7944 $promote = array();
80 -
81 - //get the current groups
 45+
8246 $currentGroups = $user->getGroups();
83 -
84 - foreach( $criteria as $group => $cond ) {
85 - //do not check if the user's already a member
86 - if ( in_array($group, $currentGroups))
 47+
 48+ foreach ( $criteria as $group => $cond ) {
 49+ // Do not check if the user's already a member
 50+ if ( in_array( $group, $currentGroups ) ) {
8751 continue;
88 -
89 - //do not autopromote if the user has belonged to the group
 52+ }
 53+ // Do not autopromote if the user has belonged to the group
9054 $formerGroups = $user->getFormerGroups();
91 - if ( in_array($group, $formerGroups) )
 55+ if ( in_array( $group, $formerGroups ) ) {
9256 continue;
93 -
94 - //finally - check the conditions
95 - if ( self::recCheckCondition($cond, $user) )
96 - $promote[] = $group;
 57+ }
 58+ // Finally - check the conditions
 59+ if ( self::recCheckCondition( $cond, $user ) ) {
 60+ $promote[] = $group;
 61+ }
9762 }
 63+
9864 return $promote;
9965 }
10066

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r90749Added one-time promote support via Autopromote::autopromoteOnceHook function....aaron02:52, 25 June 2011

Status & tagging log