Merge "Deprecate User::isBlocked()"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 29 Apr 2019 11:44:50 +0000 (11:44 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 29 Apr 2019 11:44:50 +0000 (11:44 +0000)
1  2 
RELEASE-NOTES-1.34
includes/specials/SpecialBlock.php
includes/specials/SpecialUserrights.php
includes/user/User.php

diff --combined RELEASE-NOTES-1.34
@@@ -46,7 -46,7 +46,7 @@@ For notes on 1.33.x and older releases
  
  ==== Changed external libraries ====
  * Updated Mustache from 1.0.0 to v3.0.1.
 -* Updated OOUI from v0.31.3 to v0.31.4.
 +* Updated OOUI from v0.31.3 to v0.31.5.
  * …
  
  ==== Removed external libraries ====
@@@ -67,7 -67,7 +67,7 @@@ MediaWiki supports over 350 languages. 
  Below only new and removed languages are listed, as well as changes to languages
  because of Phabricator reports.
  
 -* 
 +* (T152908) Added language support for N'Ko (nqo).
  
  === Breaking changes in 1.34 ===
  * Preferences class, deprecated in 1.31, has been removed.
  * User::randomPassword() method, deprecated in 1.27, have been removed.
  * MWNamespace::canTalk(), deprecated in 1.30, have been removed.
  * Parser class property $mUniqPrefix, deprecated in 1.26, has been removed.
 +* wfArrayFilter() and wfArrayFilterByKey(), deprecated in 1.32, have been
 +  removed.
 +* wfMakeUrlIndexes() function, deprecated in 1.33, have been removed.
  * …
  
  === Deprecations in 1.34 ===
  * The MWNamespace class is deprecated. Use MediaWikiServices::getNamespaceInfo.
  * ExtensionRegistry->load() is deprecated, as it breaks dependency checking.
    Instead, use ->queue().
+ * User::isBlocked() is deprecated since it does not tell you if the user is
+   blocked from editing a particular page. Use User::getBlock() or
+   PermissionManager::isBlockedFrom() or PermissionManager::userCan() instead.
  * …
  
  === Other changes in 1.34 ===
@@@ -21,9 -21,9 +21,9 @@@
   * @ingroup SpecialPage
   */
  
 -use MediaWiki\Block\BlockRestriction;
  use MediaWiki\Block\Restriction\PageRestriction;
  use MediaWiki\Block\Restriction\NamespaceRestriction;
 +use MediaWiki\MediaWikiServices;
  
  /**
   * A special page that allows users with 'block' right to block users from
@@@ -949,9 -949,8 +949,9 @@@ class SpecialBlock extends FormSpecialP
                                        $currentBlock->isSitewide( $block->isSitewide() );
  
                                        // Set the block id of the restrictions.
 +                                      $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore();
                                        $currentBlock->setRestrictions(
 -                                              BlockRestriction::setBlockId( $currentBlock->getId(), $restrictions )
 +                                              $blockRestrictionStore->setBlockId( $currentBlock->getId(), $restrictions )
                                        );
                                }
  
                } elseif ( is_string( $target ) ) {
                        $target = User::newFromName( $target );
                }
-               if ( $performer->isBlocked() ) {
+               if ( $performer->getBlock() ) {
                        if ( $target instanceof User && $target->getId() == $performer->getId() ) {
                                # User is trying to unblock themselves
+                               // @TODO Ensure that the block does not apply to the `unblockself`
+                               //       right.
                                if ( $performer->isAllowed( 'unblockself' ) ) {
                                        return true;
                                        # User blocked themselves and is now trying to reverse it
@@@ -61,23 -61,15 +61,23 @@@ class UserrightsPage extends SpecialPag
                $isself = $this->getUser()->equals( $targetUser );
  
                $available = $this->changeableGroups();
 -              if ( $targetUser->getId() == 0 ) {
 +              if ( $targetUser->getId() === 0 ) {
                        return false;
                }
  
 -              return !empty( $available['add'] )
 -                      || !empty( $available['remove'] )
 -                      || ( ( $isself || !$checkIfSelf ) &&
 -                              ( !empty( $available['add-self'] )
 -                                      || !empty( $available['remove-self'] ) ) );
 +              if ( $available['add'] || $available['remove'] ) {
 +                      // can change some rights for any user
 +                      return true;
 +              }
 +
 +              if ( ( $available['add-self'] || $available['remove-self'] )
 +                      && ( $isself || !$checkIfSelf )
 +              ) {
 +                      // can change some rights for self
 +                      return true;
 +              }
 +
 +              return false;
        }
  
        /**
                        * (e.g. they don't have the userrights permission), then don't
                        * allow them to change any user rights.
                        */
-                       if ( $user->isBlocked() && !$user->isAllowed( 'userrights' ) ) {
-                               throw new UserBlockedError( $user->getBlock() );
+                       if ( !$user->isAllowed( 'userrights' ) ) {
+                               // @TODO Should the user be blocked from changing user rights if they
+                               //       are partially blocked?
+                               $block = $user->getBlock();
+                               if ( $block ) {
+                                       throw new UserBlockedError( $user->getBlock() );
+                               }
                        }
  
                        $this->checkReadOnly();
diff --combined includes/user/User.php
@@@ -1372,7 -1372,7 +1372,7 @@@ class User implements IDBAccessObject, 
                $user = $session->getUser();
                if ( $user->isLoggedIn() ) {
                        $this->loadFromUserObject( $user );
-                       if ( $user->isBlocked() ) {
+                       if ( $user->getBlock() ) {
                                // If this user is autoblocked, set a cookie to track the Block. This has to be done on
                                // every session load, because an autoblocked editor might not edit again from the same
                                // IP address after being blocked.
  
                if ( $success ) {
                        $this->mTouched = $newTouched;
 -                      $this->clearSharedCache();
 +                      $this->clearSharedCache( 'changed' );
                } else {
                        // Clears on failure too since that is desired if the cache is stale
                        $this->clearSharedCache( 'refresh' );
        /**
         * Check if user is blocked
         *
+        * @deprecated since 1.34, use User::getBlock() or
+        *             PermissionManager::isBlockedFrom() or
+        *             PermissionManager::userCan() instead.
+        *
         * @param bool $fromReplica Whether to check the replica DB instead of
         *   the master. Hacked from false due to horrible probs on site.
         * @return bool True if blocked, false otherwise
         *
         * Called implicitly from invalidateCache() and saveSettings().
         *
 -       * @param string $mode Use 'refresh' to clear now; otherwise before DB commit
 +       * @param string $mode Use 'refresh' to clear now or 'changed' to clear before DB commit
         */
 -      public function clearSharedCache( $mode = 'changed' ) {
 +      public function clearSharedCache( $mode = 'refresh' ) {
                if ( !$this->getId() ) {
                        return;
                }
  
 +              $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
                $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
                $key = $this->getCacheKey( $cache );
 +
                if ( $mode === 'refresh' ) {
 -                      $cache->delete( $key, 1 );
 +                      $cache->delete( $key, 1 ); // low tombstone/"hold-off" TTL
                } else {
 -                      $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
 -                      if ( $lb->hasOrMadeRecentMasterChanges() ) {
 -                              $lb->getConnection( DB_MASTER )->onTransactionPreCommitOrIdle(
 -                                      function () use ( $cache, $key ) {
 -                                              $cache->delete( $key );
 -                                      },
 -                                      __METHOD__
 -                              );
 -                      } else {
 -                              $cache->delete( $key );
 -                      }
 +                      $lb->getConnection( DB_MASTER )->onTransactionPreCommitOrIdle(
 +                              function () use ( $cache, $key ) {
 +                                      $cache->delete( $key );
 +                              },
 +                              __METHOD__
 +                      );
                }
        }
  
         */
        public function invalidateCache() {
                $this->touch();
 -              $this->clearSharedCache();
 +              $this->clearSharedCache( 'changed' );
        }
  
        /**
                        // $user->isAllowed(). It is also checked in Title::checkUserBlock()
                        // to give a better error message in the common case.
                        $config = RequestContext::getMain()->getConfig();
+                       // @TODO Partial blocks should not prevent the user from logging in.
+                       //       see: https://phabricator.wikimedia.org/T208895
                        if (
                                $this->isLoggedIn() &&
                                $config->get( 'BlockDisablesLogin' ) &&
-                               $this->isBlocked()
+                               $this->getBlock()
                        ) {
                                $anon = new User;
                                $this->mRights = array_intersect( $this->mRights, $anon->getRights() );
                $this->saveOptions();
  
                Hooks::run( 'UserSaveSettings', [ $this ] );
 -              $this->clearSharedCache();
 +              $this->clearSharedCache( 'changed' );
                $this->getUserPage()->purgeSquid();
        }
  
         * @return bool A block was spread
         */
        public function spreadAnyEditBlock() {
-               if ( $this->isLoggedIn() && $this->isBlocked() ) {
+               if ( $this->isLoggedIn() && $this->getBlock() ) {
                        return $this->spreadBlock();
                }