From d4fda6454262e70131ba1b9391e7d5213d8b3cdb Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 18 Jul 2011 20:44:51 +0000 Subject: [PATCH 1/1] Refactored new getAutomaticGroups() function out of getEffectiveGroups() --- includes/User.php | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/includes/User.php b/includes/User.php index 52320f242c..5685d9d67f 100644 --- a/includes/User.php +++ b/includes/User.php @@ -199,7 +199,7 @@ class User { * Lazy-initialized variables, invalidated with clearInstanceCache */ var $mNewtalk, $mDatePreference, $mBlockedby, $mHash, $mRights, - $mBlockreason, $mEffectiveGroups, $mFormerGroups, $mBlockedGlobally, + $mBlockreason, $mEffectiveGroups, $mImplicitGroups, $mFormerGroups, $mBlockedGlobally, $mLocked, $mHideName, $mOptions; /** @@ -1180,6 +1180,7 @@ class User { $this->mHash = false; $this->mRights = null; $this->mEffectiveGroups = null; + $this->mImplicitGroups = null; $this->mOptions = null; if ( $reloadFrom ) { @@ -2296,22 +2297,44 @@ class User { public function getEffectiveGroups( $recache = false ) { if ( $recache || is_null( $this->mEffectiveGroups ) ) { wfProfileIn( __METHOD__ ); - $this->mEffectiveGroups = $this->getGroups(); - $this->mEffectiveGroups[] = '*'; - if( $this->getId() ) { - $this->mEffectiveGroups[] = 'user'; + $this->mEffectiveGroups = array_unique( array_merge( + $this->getGroups(), // explicit groups + $this->getAutomaticGroups( $recache ) // implicit groups + ) ); + # Hook for additional groups + wfRunHooks( 'UserEffectiveGroups', array( &$this, &$this->mEffectiveGroups ) ); + wfProfileOut( __METHOD__ ); + } + return $this->mEffectiveGroups; + } + + /** + * Get the list of implicit group memberships this user has. + * This includes 'user' if logged in, '*' for all accounts, + * and autopromoted groups + * @param $recache Bool Whether to avoid the cache + * @return Array of String internal group names + */ + public function getAutomaticGroups( $recache = false ) { + if ( $recache || is_null( $this->mImplicitGroups ) ) { + wfProfileIn( __METHOD__ ); + $this->mImplicitGroups = array( '*' ); + if ( $this->getId() ) { + $this->mImplicitGroups[] = 'user'; - $this->mEffectiveGroups = array_unique( array_merge( - $this->mEffectiveGroups, + $this->mImplicitGroups = array_unique( array_merge( + $this->mImplicitGroups, Autopromote::getAutopromoteGroups( $this ) ) ); - - # Hook for additional groups - wfRunHooks( 'UserEffectiveGroups', array( &$this, &$this->mEffectiveGroups ) ); + } + if ( $recache ) { + # Assure data consistency with rights/groups, + # as getEffectiveGroups() depends on this function + $this->mEffectiveGroups = null; } wfProfileOut( __METHOD__ ); } - return $this->mEffectiveGroups; + return $this->mImplicitGroups; } /** -- 2.20.1