diff --git a/includes/specials/pagers/BlockListPager.php b/includes/specials/pagers/BlockListPager.php index 41f8bf23716..cd782133f56 100644 --- a/includes/specials/pagers/BlockListPager.php +++ b/includes/specials/pagers/BlockListPager.php @@ -72,6 +72,9 @@ class BlockListPager extends TablePager { /** @var string[] */ private $formattedComments = []; + /** @var string[] Cache of messages to avoid them being recreated for every row of the pager. */ + private $messages = []; + /** * @param IContextSource $context * @param BlockActionInfo $blockActionInfo @@ -141,13 +144,12 @@ class BlockListPager extends TablePager { * @param string $name * @param string|null $value * @return string - * @suppress PhanTypeArraySuspicious */ public function formatValue( $name, $value ) { - static $msg = null; - if ( $msg === null ) { + if ( $this->messages === [] ) { $keys = [ 'anononlyblock', + 'blanknamespace', 'createaccountblock', 'noautoblockblock', 'emailblock', @@ -157,13 +159,13 @@ class BlockListPager extends TablePager { 'blocklist-editing', 'blocklist-editing-sitewide', 'blocklist-hidden-param', + 'blocklist-hidden-placeholder', ]; foreach ( $keys as $key ) { - $msg[$key] = $this->msg( $key )->text(); + $this->messages[$key] = $this->msg( $key )->text(); } } - '@phan-var string[] $msg'; /** @var stdClass $row */ $row = $this->mCurrentRow; @@ -200,7 +202,7 @@ class BlockListPager extends TablePager { if ( $row->bt_auto ) { $links[] = $linkRenderer->makeKnownLink( $this->specialPageFactory->getTitleForAlias( 'Unblock' ), - $msg['unblocklink'], + $this->messages['unblocklink'], [], [ 'wpTarget' => "#{$row->bl_id}" ] ); @@ -208,11 +210,11 @@ class BlockListPager extends TablePager { $target = $row->bt_address ?? $row->bt_user_text; $links[] = $linkRenderer->makeKnownLink( $this->specialPageFactory->getTitleForAlias( "Unblock/$target" ), - $msg['unblocklink'] + $this->messages['unblocklink'] ); $links[] = $linkRenderer->makeKnownLink( $this->specialPageFactory->getTitleForAlias( "Block/$target" ), - $msg['change-blocklink'] + $this->messages['change-blocklink'] ); } $formatted .= ' ' . Html::rawElement( @@ -248,35 +250,35 @@ class BlockListPager extends TablePager { $properties = []; if ( $row->bl_deleted ) { - $properties[] = htmlspecialchars( $msg['blocklist-hidden-param' ] ); + $properties[] = htmlspecialchars( $this->messages['blocklist-hidden-param' ] ); } if ( $row->bl_sitewide ) { - $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] ); + $properties[] = htmlspecialchars( $this->messages['blocklist-editing-sitewide'] ); } if ( !$row->bl_sitewide && $this->restrictions ) { $list = $this->getRestrictionListHTML( $row ); if ( $list ) { - $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list; + $properties[] = htmlspecialchars( $this->messages['blocklist-editing'] ) . $list; } } if ( $row->bl_anon_only ) { - $properties[] = htmlspecialchars( $msg['anononlyblock'] ); + $properties[] = htmlspecialchars( $this->messages['anononlyblock'] ); } if ( $row->bl_create_account ) { - $properties[] = htmlspecialchars( $msg['createaccountblock'] ); + $properties[] = htmlspecialchars( $this->messages['createaccountblock'] ); } if ( $row->bt_user && !$row->bl_enable_autoblock ) { - $properties[] = htmlspecialchars( $msg['noautoblockblock'] ); + $properties[] = htmlspecialchars( $this->messages['noautoblockblock'] ); } if ( $row->bl_block_email ) { - $properties[] = htmlspecialchars( $msg['emailblock'] ); + $properties[] = htmlspecialchars( $this->messages['emailblock'] ); } if ( !$row->bl_allow_usertalk ) { - $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] ); + $properties[] = htmlspecialchars( $this->messages['blocklist-nousertalk'] ); } $formatted = Html::rawElement( @@ -321,7 +323,7 @@ class BlockListPager extends TablePager { return Html::element( 'span', [ 'class' => 'mw-blocklist-hidden' ], - $this->msg( 'blocklist-hidden-placeholder' )->text() + $this->messages['blocklist-hidden-placeholder'] ); } elseif ( $target instanceof UserIdentity ) { $userId = $target->getId(); @@ -369,7 +371,7 @@ class BlockListPager extends TablePager { break; case NamespaceRestriction::TYPE: $text = $restriction->getValue() === NS_MAIN - ? $this->msg( 'blanknamespace' )->text() + ? $this->messages['blanknamespace'] : $this->getLanguage()->getFormattedNsText( $restriction->getValue() ); @@ -396,6 +398,10 @@ class BlockListPager extends TablePager { $items[$restriction->getType()][] = Html::rawElement( 'li', [], + // The following messages may be used here: + // * ipb-action-create + // * ipb-action-move + // * ipb-action-upload $this->msg( 'ipb-action-' . $this->blockActionInfo->getActionFromId( $restriction->getValue() ) )->escaped() ); @@ -413,6 +419,11 @@ class BlockListPager extends TablePager { $sets[] = Html::rawElement( 'li', [], + // The following messages may be used here: + // * blocklist-editing-sitewide + // * blocklist-editing-page + // * blocklist-editing-ns + // * blocklist-editing-action $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement( 'ul', [], diff --git a/tests/phpunit/includes/api/query/ApiQueryBlocksTest.php b/tests/phpunit/includes/api/query/ApiQueryBlocksTest.php index 8ae55fec947..92803c3229e 100644 --- a/tests/phpunit/includes/api/query/ApiQueryBlocksTest.php +++ b/tests/phpunit/includes/api/query/ApiQueryBlocksTest.php @@ -88,15 +88,16 @@ class ApiQueryBlocksTest extends ApiTestCase { $badActor = $this->getTestUser()->getUser(); $sysop = $this->getTestSysop()->getUser(); - $parentBlock = $this->getServiceContainer()->getDatabaseBlockStore() - ->insertBlockWithParams( [ - 'targetUser' => $badActor, - 'by' => $sysop, - 'hideName' => true, - 'enableAutoblock' => true, - ] ); + $store = $this->getServiceContainer()->getDatabaseBlockStore(); + $parentBlock = new DatabaseBlock( [ + 'hideName' => true, + 'enableAutoblock' => true, + ] ); + $parentBlock->setTarget( $badActor ); + $parentBlock->setBlocker( $sysop ); + $store->insertBlock( $parentBlock ); - $autoblock = $this->getServiceContainer()->getDatabaseBlockStore() + $autoblock = $store ->doAutoblock( $parentBlock, '1.2.3.4' ); [ $data ] = $this->doApiRequest( [ diff --git a/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php b/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php index 7ad98cb1d9f..00d07281dc6 100644 --- a/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php +++ b/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php @@ -15,6 +15,7 @@ use MediaWiki\Linker\LinkRenderer; use MediaWiki\MainConfigNames; use MediaWiki\Pager\BlockListPager; use MediaWiki\Permissions\SimpleAuthority; +use MediaWiki\Permissions\UltimateAuthority; use MediaWiki\Request\FauxRequest; use MediaWiki\SpecialPage\SpecialPageFactory; use MediaWiki\Utils\MWTimestamp; @@ -379,17 +380,19 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase { RequestContext::getMain()->setLanguage( 'qqx' ); // Create autoblock $addr = '127.0.0.1'; - $this->getServiceContainer()->getDatabaseBlockStore() - ->insertBlockWithParams( [ - 'address' => $addr, - 'auto' => true, - 'by' => $sysop - ] ); + $block = new DatabaseBlock( [ + 'address' => $addr, + 'auto' => true, + ] ); + $block->setBlocker( $sysop ); + $blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); + $status = $blockStore->insertBlock( $block ); + $this->assertNotFalse( $status ); // Run the pager over all blocks (there should only be one) $pager = $this->getBlockListPager(); $body = $pager->getBody(); // Check that we managed to generate a remove link - $this->assertStringContainsString( '(remove-blocklink)', $body ); + $this->assertStringContainsString( '(unblocklink)', $body ); // Check that we didn't leak the IP address into it $this->assertStringNotContainsString( $addr, $body ); } @@ -412,15 +415,18 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase { public function testBlockLinkSuppression() { $user = $this->getTestUser()->getUserIdentity(); $store = $this->getServiceContainer()->getDatabaseBlockStore(); - $store->insertBlockWithParams( [ - 'targetUser' => $user, - 'by' => $this->getTestSysop()->getUser(), - ] ); - $store->insertBlockWithParams( [ - 'targetUser' => $user, - 'by' => $this->getTestSysop()->getUser(), + $block = new DatabaseBlock(); + $block->setTarget( $user ); + $block->setBlocker( $this->getTestSysop()->getUser() ); + $status = $store->insertBlock( $block ); + $this->assertNotFalse( $status ); + $block = new DatabaseBlock( [ 'hideName' => true ] ); + $block->setTarget( $user ); + $block->setBlocker( $this->getTestSysop()->getUser() ); + $status = $store->insertBlock( $block, null ); + $this->assertNotFalse( $status ); RequestContext::getMain()->setAuthority( new SimpleAuthority( @@ -441,12 +447,14 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase { public function testAutoblockSuppression() { $user = $this->getTestUser()->getUserIdentity(); $store = $this->getServiceContainer()->getDatabaseBlockStore(); - $block = $store->insertBlockWithParams( [ - 'targetUser' => $user, - 'by' => $this->getTestSysop()->getUser(), + $block = new DatabaseBlock( [ 'hideName' => true, 'enableAutoblock' => true, ] ); + $block->setTarget( $user ); + $block->setBlocker( $this->getTestSysop()->getUser() ); + $status = $store->insertBlock( $block ); + $this->assertNotFalse( $status ); $store->doAutoblock( $block, '127.0.0.42' ); $pager = $this->getBlockListPager();