Merge "Update GenderCache to use NamespaceInfo"
[lhc/web/wiklou.git] / includes / Block.php
index b17ec86..0d13f7d 100644 (file)
@@ -22,7 +22,7 @@
 
 use Wikimedia\Rdbms\Database;
 use Wikimedia\Rdbms\IDatabase;
-use MediaWiki\Block\BlockRestriction;
+use MediaWiki\Block\BlockRestrictionStore;
 use MediaWiki\Block\Restriction\Restriction;
 use MediaWiki\Block\Restriction\NamespaceRestriction;
 use MediaWiki\Block\Restriction\PageRestriction;
@@ -100,7 +100,7 @@ class Block {
        const TYPE_ID = 5;
 
        /**
-        * Create a new block with specified parameters on a user, IP or IP range.
+        * Create a new block with specified option parameters on a user, IP or IP range.
         *
         * @param array $options Parameters of the block:
         *     address string|User  Target user name, User object, IP address or IP range
@@ -125,10 +125,9 @@ class Block {
         *                          actions, except those specifically allowed by
         *                          other block flags
         *
-        * @since 1.26 accepts $options array instead of individual parameters; order
-        * of parameters above reflects the original order
+        * @since 1.26 $options array
         */
-       function __construct( $options = [] ) {
+       public function __construct( array $options = [] ) {
                $defaults = [
                        'address'         => '',
                        'user'            => null,
@@ -305,7 +304,9 @@ class Block {
                        && $this->isSitewide() == $block->isSitewide()
                        // Block::getRestrictions() may perform a database query, so keep it at
                        // the end.
-                       && BlockRestriction::equals( $this->getRestrictions(), $block->getRestrictions() )
+                       && $this->getBlockRestrictionStore()->equals(
+                               $this->getRestrictions(), $block->getRestrictions()
+                       )
                );
        }
 
@@ -390,8 +391,7 @@ class Block {
                                $start = Wikimedia\base_convert( $block->getRangeStart(), 16, 10 );
                                $size = log( $end - $start + 1, 2 );
 
-                               # This has the nice property that a /32 block is ranked equally with a
-                               # single-IP block, which is exactly what it is...
+                               # Rank a range block covering a single IP equally with a single-IP block
                                $score = self::TYPE_RANGE - 1 + ( $size / 128 );
 
                        } else {
@@ -523,10 +523,10 @@ class Block {
 
                $dbw = wfGetDB( DB_MASTER );
 
-               BlockRestriction::deleteByParentBlockId( $this->getId() );
+               $this->getBlockRestrictionStore()->deleteByParentBlockId( $this->getId() );
                $dbw->delete( 'ipblocks', [ 'ipb_parent_block_id' => $this->getId() ], __METHOD__ );
 
-               BlockRestriction::deleteByBlockId( $this->getId() );
+               $this->getBlockRestrictionStore()->deleteByBlockId( $this->getId() );
                $dbw->delete( 'ipblocks', [ 'ipb_id' => $this->getId() ], __METHOD__ );
 
                return $dbw->affectedRows() > 0;
@@ -565,7 +565,7 @@ class Block {
                if ( $affected ) {
                        $this->setId( $dbw->insertId() );
                        if ( $this->restrictions ) {
-                               BlockRestriction::insert( $this->restrictions );
+                               $this->getBlockRestrictionStore()->insert( $this->restrictions );
                        }
                }
 
@@ -585,12 +585,12 @@ class Block {
                        );
                        if ( $ids ) {
                                $dbw->delete( 'ipblocks', [ 'ipb_id' => $ids ], __METHOD__ );
-                               BlockRestriction::deleteByBlockId( $ids );
+                               $this->getBlockRestrictionStore()->deleteByBlockId( $ids );
                                $dbw->insert( 'ipblocks', $row, __METHOD__, [ 'IGNORE' ] );
                                $affected = $dbw->affectedRows();
                                $this->setId( $dbw->insertId() );
                                if ( $this->restrictions ) {
-                                       BlockRestriction::insert( $this->restrictions );
+                                       $this->getBlockRestrictionStore()->insert( $this->restrictions );
                                }
                        }
                }
@@ -634,9 +634,9 @@ class Block {
                if ( $this->restrictions !== null ) {
                        // An empty array should remove all of the restrictions.
                        if ( empty( $this->restrictions ) ) {
-                               $success = BlockRestriction::deleteByBlockId( $this->getId() );
+                               $success = $this->getBlockRestrictionStore()->deleteByBlockId( $this->getId() );
                        } else {
-                               $success = BlockRestriction::update( $this->restrictions );
+                               $success = $this->getBlockRestrictionStore()->update( $this->restrictions );
                        }
                        // Update the result. The first false is the result, otherwise, true.
                        $result = $result && $success;
@@ -653,11 +653,11 @@ class Block {
 
                        // Only update the restrictions if they have been modified.
                        if ( $this->restrictions !== null ) {
-                               BlockRestriction::updateByParentBlockId( $this->getId(), $this->restrictions );
+                               $this->getBlockRestrictionStore()->updateByParentBlockId( $this->getId(), $this->restrictions );
                        }
                } else {
                        // autoblock no longer required, delete corresponding autoblock(s)
-                       BlockRestriction::deleteByParentBlockId( $this->getId() );
+                       $this->getBlockRestrictionStore()->deleteByParentBlockId( $this->getId() );
                        $dbw->delete(
                                'ipblocks',
                                [ 'ipb_parent_block_id' => $this->getId() ],
@@ -1069,7 +1069,9 @@ class Block {
                $this->mId = (int)$blockId;
 
                if ( is_array( $this->restrictions ) ) {
-                       $this->restrictions = BlockRestriction::setBlockId( $blockId, $this->restrictions );
+                       $this->restrictions = $this->getBlockRestrictionStore()->setBlockId(
+                               $blockId, $this->restrictions
+                       );
                }
 
                return $this;
@@ -1297,7 +1299,7 @@ class Block {
                                // their own talk page unless a restriction exists on the
                                // page or User_talk: namespace
                                wfSetVar( $this->allowUsertalk, $x === null ? null : !$x );
-                               $res = !$this->isUserTalkEditAllowed();
+                               $res = !$this->isUsertalkEditAllowed();
 
                                // edit own user talk can be disabled by config
                                if ( !$blockAllowsUTEdit ) {
@@ -1367,7 +1369,9 @@ class Block {
                                        $fname
                                );
                                if ( $ids ) {
-                                       BlockRestriction::deleteByBlockId( $ids );
+                                       $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore();
+                                       $blockRestrictionStore->deleteByBlockId( $ids );
+
                                        $dbw->delete( 'ipblocks', [ 'ipb_id' => $ids ], $fname );
                                }
                        }
@@ -1941,7 +1945,7 @@ class Block {
                        if ( !$this->mId ) {
                                return [];
                        }
-                       $this->restrictions = BlockRestriction::loadByBlockId( $this->mId );
+                       $this->restrictions = $this->getBlockRestrictionStore()->loadByBlockId( $this->mId );
                }
 
                return $this->restrictions;
@@ -2126,17 +2130,17 @@ class Block {
         * Check if the block should be tracked with a cookie.
         *
         * @since 1.33
-        * @param bool $isIpUser The user is logged out
+        * @param bool $isAnon The user is logged out
         * @return bool The block should be tracked with a cookie
         */
-       public function shouldTrackWithCookie( $isIpUser ) {
+       public function shouldTrackWithCookie( $isAnon ) {
                $config = RequestContext::getMain()->getConfig();
                switch ( $this->getType() ) {
                        case self::TYPE_IP:
                        case self::TYPE_RANGE:
-                               return $isIpUser && $config->get( 'CookieSetOnIpBlock' );
+                               return $isAnon && $config->get( 'CookieSetOnIpBlock' );
                        case self::TYPE_USER:
-                               return !$isIpUser && $config->get( 'CookieSetOnAutoblock' ) && $this->isAutoblocking();
+                               return !$isAnon && $config->get( 'CookieSetOnAutoblock' ) && $this->isAutoblocking();
                        default:
                                return false;
                }
@@ -2146,7 +2150,7 @@ class Block {
         * Check if the block prevents a user from resetting their password
         *
         * @since 1.33
-        * @return bool|null The block blocks password reset
+        * @return bool The block blocks password reset
         */
        public function appliesToPasswordReset() {
                switch ( $this->getSystemBlockType() ) {
@@ -2159,8 +2163,16 @@ class Block {
                        case 'wgSoftBlockRanges':
                                return false;
                        default:
-                               return false;
+                               return true;
                }
        }
 
+       /**
+        * Get a BlockRestrictionStore instance
+        *
+        * @return BlockRestrictionStore
+        */
+       private function getBlockRestrictionStore() : BlockRestrictionStore {
+               return MediaWikiServices::getInstance()->getBlockRestrictionStore();
+       }
 }