X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FBlock.php;h=0999ad20634fe93880f252e6972ddd1c3da1dd89;hb=fc23633035eebb24b7199cb15683e25100184e2b;hp=2ca56a5a901969e74f44706666ca35c726ea064a;hpb=24ebca2251f94a20acee53e583d65aa7fac2c15d;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/Block.php b/includes/Block.php index 2ca56a5a90..0999ad2063 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -44,40 +44,40 @@ class Block { public $mParentBlockId; /** @var int */ - protected $mId; + private $mId; /** @var bool */ - protected $mFromMaster; + private $mFromMaster; /** @var bool */ - protected $mBlockEmail; + private $mBlockEmail; /** @var bool */ - protected $mDisableUsertalk; + private $mDisableUsertalk; /** @var bool */ - protected $mCreateAccount; + private $mCreateAccount; /** @var User|string */ - protected $target; + private $target; /** @var int Hack for foreign blocking (CentralAuth) */ - protected $forcedTargetID; + private $forcedTargetID; /** @var int Block::TYPE_ constant. Can only be USER, IP or RANGE internally */ - protected $type; + private $type; /** @var User */ - protected $blocker; + private $blocker; /** @var bool */ - protected $isHardblock; + private $isHardblock; /** @var bool */ - protected $isAutoblocking; + private $isAutoblocking; /** @var string|null */ - protected $systemBlockType; + private $systemBlockType; # TYPE constants const TYPE_USER = 1; @@ -183,11 +183,14 @@ class Block { */ public static function newFromID( $id ) { $dbr = wfGetDB( DB_REPLICA ); + $blockQuery = self::getQueryInfo(); $res = $dbr->selectRow( - 'ipblocks', - self::selectFields(), + $blockQuery['tables'], + $blockQuery['fields'], [ 'ipb_id' => $id ], - __METHOD__ + __METHOD__, + [], + $blockQuery['joins'] ); if ( $res ) { return self::newFromRow( $res ); @@ -199,15 +202,16 @@ class Block { /** * Return the list of ipblocks fields that should be selected to create * a new block. + * @deprecated since 1.31, use self::getQueryInfo() instead. * @return array */ public static function selectFields() { + wfDeprecated( __METHOD__, '1.31' ); return [ 'ipb_id', 'ipb_address', 'ipb_by', 'ipb_by_text', - 'ipb_reason', 'ipb_timestamp', 'ipb_auto', 'ipb_anon_only', @@ -218,6 +222,39 @@ class Block { 'ipb_block_email', 'ipb_allow_usertalk', 'ipb_parent_block_id', + ] + CommentStore::newKey( 'ipb_reason' )->getFields(); + } + + /** + * Return the tables, fields, and join conditions to be selected to create + * a new block object. + * @since 1.31 + * @return array With three keys: + * - tables: (string[]) to include in the `$table` to `IDatabase->select()` + * - fields: (string[]) to include in the `$vars` to `IDatabase->select()` + * - joins: (array) to include in the `$join_conds` to `IDatabase->select()` + */ + public static function getQueryInfo() { + $commentQuery = CommentStore::newKey( 'ipb_reason' )->getJoin(); + return [ + 'tables' => [ 'ipblocks' ] + $commentQuery['tables'], + 'fields' => [ + 'ipb_id', + 'ipb_address', + 'ipb_by', + 'ipb_by_text', + 'ipb_timestamp', + 'ipb_auto', + 'ipb_anon_only', + 'ipb_create_account', + 'ipb_enable_autoblock', + 'ipb_expiry', + 'ipb_deleted', + 'ipb_block_email', + 'ipb_allow_usertalk', + 'ipb_parent_block_id', + ] + $commentQuery['fields'], + 'joins' => $commentQuery['joins'], ]; } @@ -294,7 +331,10 @@ class Block { } } - $res = $db->select( 'ipblocks', self::selectFields(), $conds, __METHOD__ ); + $blockQuery = self::getQueryInfo(); + $res = $db->select( + $blockQuery['tables'], $blockQuery['fields'], $conds, __METHOD__, [], $blockQuery['joins'] + ); # This result could contain a block on the user, a block on the IP, and a russian-doll # set of rangeblocks. We want to choose the most specific one, so keep a leader board. @@ -411,7 +451,6 @@ class Block { $this->setBlocker( $row->ipb_by_text ); } - $this->mReason = $row->ipb_reason; $this->mTimestamp = wfTimestamp( TS_MW, $row->ipb_timestamp ); $this->mAuto = $row->ipb_auto; $this->mHideName = $row->ipb_deleted; @@ -419,7 +458,11 @@ class Block { $this->mParentBlockId = $row->ipb_parent_block_id; // I wish I didn't have to do this - $this->mExpiry = wfGetDB( DB_REPLICA )->decodeExpiry( $row->ipb_expiry ); + $db = wfGetDB( DB_REPLICA ); + $this->mExpiry = $db->decodeExpiry( $row->ipb_expiry ); + $this->mReason = CommentStore::newKey( 'ipb_reason' ) + // Legacy because $row may have come from self::selectFields() + ->getCommentLegacy( $db, $row )->text; $this->isHardblock( !$row->ipb_anon_only ); $this->isAutoblocking( $row->ipb_enable_autoblock ); @@ -488,8 +531,7 @@ class Block { self::purgeExpired(); } - $row = $this->getDatabaseArray(); - $row['ipb_id'] = $dbw->nextSequenceValue( "ipblocks_ipb_id_seq" ); + $row = $this->getDatabaseArray( $dbw ); $dbw->insert( 'ipblocks', $row, __METHOD__, [ 'IGNORE' ] ); $affected = $dbw->affectedRows(); @@ -558,7 +600,7 @@ class Block { // update corresponding autoblock(s) (T50813) $dbw->update( 'ipblocks', - $this->getAutoblockUpdateArray(), + $this->getAutoblockUpdateArray( $dbw ), [ 'ipb_parent_block_id' => $this->getId() ], __METHOD__ ); @@ -583,14 +625,11 @@ class Block { /** * Get an array suitable for passing to $dbw->insert() or $dbw->update() - * @param IDatabase $db + * @param IDatabase $dbw * @return array */ - protected function getDatabaseArray( $db = null ) { - if ( !$db ) { - $db = wfGetDB( DB_REPLICA ); - } - $expiry = $db->encodeExpiry( $this->mExpiry ); + protected function getDatabaseArray( IDatabase $dbw ) { + $expiry = $dbw->encodeExpiry( $this->mExpiry ); if ( $this->forcedTargetID ) { $uid = $this->forcedTargetID; @@ -603,8 +642,7 @@ class Block { 'ipb_user' => $uid, 'ipb_by' => $this->getBy(), 'ipb_by_text' => $this->getByName(), - 'ipb_reason' => $this->mReason, - 'ipb_timestamp' => $db->timestamp( $this->mTimestamp ), + 'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp ), 'ipb_auto' => $this->mAuto, 'ipb_anon_only' => !$this->isHardblock(), 'ipb_create_account' => $this->prevents( 'createaccount' ), @@ -616,23 +654,23 @@ class Block { 'ipb_block_email' => $this->prevents( 'sendemail' ), 'ipb_allow_usertalk' => !$this->prevents( 'editownusertalk' ), 'ipb_parent_block_id' => $this->mParentBlockId - ]; + ] + CommentStore::newKey( 'ipb_reason' )->insert( $dbw, $this->mReason ); return $a; } /** + * @param IDatabase $dbw * @return array */ - protected function getAutoblockUpdateArray() { + protected function getAutoblockUpdateArray( IDatabase $dbw ) { return [ 'ipb_by' => $this->getBy(), 'ipb_by_text' => $this->getByName(), - 'ipb_reason' => $this->mReason, 'ipb_create_account' => $this->prevents( 'createaccount' ), 'ipb_deleted' => (int)$this->mHideName, // typecast required for SQLite 'ipb_allow_usertalk' => !$this->prevents( 'editownusertalk' ), - ]; + ] + CommentStore::newKey( 'ipb_reason' )->insert( $dbw, $this->mReason ); } /** @@ -710,7 +748,7 @@ class Block { // than getting the msg raw and explode()'ing it. $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); $lines = $cache->getWithSetCallback( - $cache->makeKey( 'ipb', 'autoblock', 'whitelist' ), + $cache->makeKey( 'ip-autoblock', 'whitelist' ), $cache::TTL_DAY, function ( $curValue, &$ttl, array &$setOpts ) { $setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) ); @@ -1188,14 +1226,14 @@ class Block { if ( !$isAnon ) { $conds = [ $conds, 'ipb_anon_only' => 0 ]; } - $selectFields = array_merge( - [ 'ipb_range_start', 'ipb_range_end' ], - self::selectFields() - ); - $rows = $db->select( 'ipblocks', - $selectFields, + $blockQuery = self::getQueryInfo(); + $rows = $db->select( + $blockQuery['tables'], + array_merge( [ 'ipb_range_start', 'ipb_range_end' ], $blockQuery['fields'] ), $conds, - __METHOD__ + __METHOD__, + [], + $blockQuery['joins'] ); $blocks = []; @@ -1330,7 +1368,7 @@ class Block { * which in turn gives User::getName(). * * @param string|int|User|null $target - * @return array( User|String|null, Block::TYPE_ constant|null ) + * @return array [ User|String|null, Block::TYPE_ constant|null ] */ public static function parseTarget( $target ) { # We may have been through this before @@ -1354,7 +1392,7 @@ class Block { self::TYPE_IP ]; - } elseif ( IP::isValidBlock( $target ) ) { + } elseif ( IP::isValidRange( $target ) ) { # Can't create a User from an IP range return [ IP::sanitizeRange( $target ), self::TYPE_RANGE ]; } @@ -1397,7 +1435,7 @@ class Block { * Get the target and target type for this particular Block. Note that for autoblocks, * this returns the unredacted name; frontend functions need to call $block->getRedactedName() * in this situation. - * @return array( User|String, Block::TYPE_ constant ) + * @return array [ User|String, Block::TYPE_ constant ] * @todo FIXME: This should be an integral part of the Block member variables */ public function getTargetAndType() { @@ -1441,9 +1479,19 @@ class Block { /** * Set the user who implemented (or will implement) this block - * @param User|string $user Local User object or username string for foreign users + * @param User|string $user Local User object or username string */ public function setBlocker( $user ) { + if ( is_string( $user ) ) { + $user = User::newFromName( $user, false ); + } + + if ( $user->isAnon() && User::isUsableName( $user->getName() ) ) { + throw new InvalidArgumentException( + 'Blocker must be a local user or a name that cannot be a local user' + ); + } + $this->blocker = $user; } @@ -1501,7 +1549,7 @@ class Block { return $id; } $hmac = MWCryptHash::hmac( $id, $secretKey, false ); - $cookieValue = $id . '!' . $hmac; + $cookieValue = $id . '!' . $hmac; return $cookieValue; } @@ -1513,7 +1561,7 @@ class Block { * * @param string $cookieValue The string in which to find the ID. * - * @return integer|null The block ID, or null if the HMAC is present and invalid. + * @return int|null The block ID, or null if the HMAC is present and invalid. */ public static function getIdFromCookieValue( $cookieValue ) { // Extract the ID prefix from the cookie value (may be the whole value, if no bang found).