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 {
'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() );
}
* prohibited from editing any page on the site (other than their own talk
* page).
*
+ * @since 1.33
* @param null|bool $x
* @return bool
*/
case 'read':
$res = false;
break;
+ case 'purge':
+ $res = false;
+ break;
}
if ( !$res && $blockDisablesLogin ) {
// If a block would disable login, then it should
* @since 1.22
*/
public static function getBlocksForIPList( array $ipChain, $isAnon, $fromMaster = false ) {
- if ( !count( $ipChain ) ) {
+ if ( $ipChain === [] ) {
return [];
}
$conds[] = self::getRangeCond( IP::toHex( $ipaddr ) );
}
- if ( !count( $conds ) ) {
+ if ( $conds === [] ) {
return [];
}
* @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];
/**
* Get block information used in different block error messages
*
+ * @since 1.33
* @param IContextSource $context
* @return array
*/
* Getting the restrictions will perform a database query if the restrictions
* are not already loaded.
*
+ * @since 1.33
* @return Restriction[]
*/
public function getRestrictions() {
/**
* Set Restrictions.
*
+ * @since 1.33
* @param Restriction[] $restrictions
- *
* @return self
*/
public function setRestrictions( array $restrictions ) {
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;
+ }
}