objectcache: remove EventRelayer dependency from WANObjectCache
[lhc/web/wiklou.git] / includes / Block.php
index fb3caf6..573ce3d 100644 (file)
@@ -24,6 +24,8 @@ use Wikimedia\Rdbms\Database;
 use Wikimedia\Rdbms\IDatabase;
 use MediaWiki\Block\BlockRestriction;
 use MediaWiki\Block\Restriction\Restriction;
+use MediaWiki\Block\Restriction\NamespaceRestriction;
+use MediaWiki\Block\Restriction\PageRestriction;
 use MediaWiki\MediaWikiServices;
 
 class Block {
@@ -66,7 +68,10 @@ class Block {
        /** @var int Hack for foreign blocking (CentralAuth) */
        private $forcedTargetID;
 
-       /** @var int Block::TYPE_ constant. Can only be USER, IP or RANGE internally */
+       /**
+        * @var int Block::TYPE_ constant. After the block has been loaded
+        * from the database, this can only be USER, IP or RANGE.
+        */
        private $type;
 
        /** @var User */
@@ -186,7 +191,7 @@ class Block {
        }
 
        /**
-        * Load a blocked user from their block id.
+        * Load a block from the block id.
         *
         * @param int $id Block id to search for
         * @return Block|null
@@ -725,6 +730,7 @@ class Block {
                        'ipb_create_account'   => $this->prevents( 'createaccount' ),
                        'ipb_deleted'          => (int)$this->mHideName, // typecast required for SQLite
                        'ipb_allow_usertalk'   => !$this->prevents( 'editownusertalk' ),
+                       'ipb_sitewide'         => $this->isSitewide(),
                ] + CommentStore::getStore()->insert( $dbw, 'ipb_reason', $this->mReason )
                        + ActorMigration::newMigration()->getInsertValues( $dbw, 'ipb_by', $this->getBlocker() );
        }
@@ -1183,6 +1189,9 @@ class Block {
                        case 'read':
                                $res = false;
                                break;
+                       case 'purge':
+                               $res = false;
+                               break;
                }
                if ( !$res && $blockDisablesLogin ) {
                        // If a block would disable login, then it should
@@ -1307,7 +1316,7 @@ class Block {
         * @since 1.22
         */
        public static function getBlocksForIPList( array $ipChain, $isAnon, $fromMaster = false ) {
-               if ( !count( $ipChain ) ) {
+               if ( $ipChain === [] ) {
                        return [];
                }
 
@@ -1332,7 +1341,7 @@ class Block {
                        $conds[] = self::getRangeCond( IP::toHex( $ipaddr ) );
                }
 
-               if ( !count( $conds ) ) {
+               if ( $conds === [] ) {
                        return [];
                }
 
@@ -1388,7 +1397,7 @@ class Block {
         * @return Block|null The "best" block from the list
         */
        public static function chooseBlock( array $blocks, array $ipChain ) {
-               if ( !count( $blocks ) ) {
+               if ( $blocks === [] ) {
                        return null;
                } elseif ( count( $blocks ) == 1 ) {
                        return $blocks[0];
@@ -1541,7 +1550,9 @@ class Block {
        }
 
        /**
-        * Get the type of target for this particular block
+        * Get the type of target for this particular block. Autoblocks have whichever type
+        * corresponds to their target, so to detect if a block is an autoblock, we have to
+        * check the mAuto property instead.
         * @return int Block::TYPE_ constant, will never be TYPE_ID
         */
        public function getType() {
@@ -1827,4 +1838,77 @@ class Block {
 
                return false;
        }
+
+       /**
+        * Checks if a block applies to a particular namespace
+        *
+        * @since 1.33
+        *
+        * @param int $ns
+        * @return bool
+        */
+       public function appliesToNamespace( $ns ) {
+               if ( $this->isSitewide() ) {
+                       return true;
+               }
+
+               // Blocks do not apply to virtual namespaces.
+               if ( $ns < 0 ) {
+                       return false;
+               }
+
+               $restriction = $this->findRestriction( NamespaceRestriction::TYPE, $ns );
+
+               return (bool)$restriction;
+       }
+
+       /**
+        * Checks if a block applies to a particular page
+        *
+        * This check does not consider whether `$this->prevents( 'editownusertalk' )`
+        * returns false, as the identity of the user making the hypothetical edit
+        * isn't known here (particularly in the case of IP hardblocks, range
+        * blocks, and auto-blocks).
+        *
+        * @since 1.33
+        *
+        * @param int $pageId
+        * @return bool
+        */
+       public function appliesToPage( $pageId ) {
+               if ( $this->isSitewide() ) {
+                       return true;
+               }
+
+               // If the pageId is not over zero, the block cannot apply to it.
+               if ( $pageId <= 0 ) {
+                       return false;
+               }
+
+               $restriction = $this->findRestriction( PageRestriction::TYPE, $pageId );
+
+               return (bool)$restriction;
+       }
+
+       /**
+        * Find Restriction by type and value.
+        *
+        * @param string $type
+        * @param int $value
+        * @return Restriction|null
+        */
+       private function findRestriction( $type, $value ) {
+               $restrictions = $this->getRestrictions();
+               foreach ( $restrictions as $restriction ) {
+                       if ( $restriction->getType() !== $type ) {
+                               continue;
+                       }
+
+                       if ( $restriction->getValue() === $value ) {
+                               return $restriction;
+                       }
+               }
+
+               return null;
+       }
 }