diff --git a/includes/specialpage/SpecialPageFactory.php b/includes/specialpage/SpecialPageFactory.php index f001cdbf34b..68b72565813 100644 --- a/includes/specialpage/SpecialPageFactory.php +++ b/includes/specialpage/SpecialPageFactory.php @@ -129,11 +129,28 @@ class SpecialPageFactory { ] ], 'Unblock' => \SpecialUnblock::class, - 'BlockList' => \SpecialBlockList::class, + 'BlockList' => [ + 'class' => \SpecialBlockList::class, + 'services' => [ + 'PermissionManager', + 'LinkBatchFactory', + 'BlockRestrictionStore', + 'DBLoadBalancer', + 'SpecialPageFactory', + 'ActorMigration', + 'CommentStore', + ], + ], 'AutoblockList' => [ 'class' => \SpecialAutoblockList::class, 'services' => [ 'PermissionManager', + 'LinkBatchFactory', + 'BlockRestrictionStore', + 'DBLoadBalancer', + 'SpecialPageFactory', + 'ActorMigration', + 'CommentStore', ], ], 'ChangePassword' => \SpecialChangePassword::class, diff --git a/includes/specials/SpecialAutoblockList.php b/includes/specials/SpecialAutoblockList.php index 6e2adecb9d7..bed42ca1f25 100644 --- a/includes/specials/SpecialAutoblockList.php +++ b/includes/specials/SpecialAutoblockList.php @@ -21,8 +21,11 @@ * @ingroup SpecialPage */ -use MediaWiki\MediaWikiServices; +use MediaWiki\Block\BlockRestrictionStore; +use MediaWiki\Cache\LinkBatchFactory; use MediaWiki\Permissions\PermissionManager; +use MediaWiki\SpecialPage\SpecialPageFactory; +use Wikimedia\Rdbms\ILoadBalancer; /** * A special page that lists autoblocks @@ -35,13 +38,51 @@ class SpecialAutoblockList extends SpecialPage { /** @var PermissionManager */ private $permManager; + /** @var LinkBatchFactory */ + private $linkBatchFactory; + + /** @var BlockRestrictionStore */ + private $blockRestrictionStore; + + /** @var ILoadBalancer */ + private $loadBalancer; + + /** @var SpecialPageFactory */ + private $specialPageFactory; + + /** @var ActorMigration */ + private $actorMigration; + + /** @var CommentStore */ + private $commentStore; + /** * @param PermissionManager $permManager + * @param LinkBatchFactory $linkBatchFactory + * @param BlockRestrictionStore $blockRestrictionStore + * @param ILoadBalancer $loadBalancer + * @param SpecialPageFactory $specialPageFactory + * @param ActorMigration $actorMigration + * @param CommentStore $commentStore */ - public function __construct( PermissionManager $permManager ) { + public function __construct( + PermissionManager $permManager, + LinkBatchFactory $linkBatchFactory, + BlockRestrictionStore $blockRestrictionStore, + ILoadBalancer $loadBalancer, + SpecialPageFactory $specialPageFactory, + ActorMigration $actorMigration, + CommentStore $commentStore + ) { parent::__construct( 'AutoblockList' ); $this->permManager = $permManager; + $this->linkBatchFactory = $linkBatchFactory; + $this->blockRestrictionStore = $blockRestrictionStore; + $this->loadBalancer = $loadBalancer; + $this->specialPageFactory = $specialPageFactory; + $this->actorMigration = $actorMigration; + $this->commentStore = $commentStore; } /** @@ -99,7 +140,13 @@ class SpecialAutoblockList extends SpecialPage { return new BlockListPager( $this, $conds, - MediaWikiServices::getInstance()->getLinkBatchFactory() + $this->linkBatchFactory, + $this->blockRestrictionStore, + $this->permManager, + $this->loadBalancer, + $this->specialPageFactory, + $this->actorMigration, + $this->commentStore ); } diff --git a/includes/specials/SpecialBlockList.php b/includes/specials/SpecialBlockList.php index 5506d54e441..4847cbe475c 100644 --- a/includes/specials/SpecialBlockList.php +++ b/includes/specials/SpecialBlockList.php @@ -21,10 +21,14 @@ * @ingroup SpecialPage */ +use MediaWiki\Block\BlockRestrictionStore; use MediaWiki\Block\DatabaseBlock; -use MediaWiki\MediaWikiServices; +use MediaWiki\Cache\LinkBatchFactory; +use MediaWiki\Permissions\PermissionManager; +use MediaWiki\SpecialPage\SpecialPageFactory; use Wikimedia\IPUtils; use Wikimedia\Rdbms\IDatabase; +use Wikimedia\Rdbms\ILoadBalancer; /** * A special page that lists existing blocks @@ -38,8 +42,45 @@ class SpecialBlockList extends SpecialPage { protected $blockType; - public function __construct() { + /** @var PermissionManager */ + private $permManager; + + /** @var LinkBatchFactory */ + private $linkBatchFactory; + + /** @var BlockRestrictionStore */ + private $blockRestrictionStore; + + /** @var ILoadBalancer */ + private $loadBalancer; + + /** @var SpecialPageFactory */ + private $specialPageFactory; + + /** @var ActorMigration */ + private $actorMigration; + + /** @var CommentStore */ + private $commentStore; + + public function __construct( + PermissionManager $permManager, + LinkBatchFactory $linkBatchFactory, + BlockRestrictionStore $blockRestrictionStore, + ILoadBalancer $loadBalancer, + SpecialPageFactory $specialPageFactory, + ActorMigration $actorMigration, + CommentStore $commentStore + ) { parent::__construct( 'BlockList' ); + + $this->permManager = $permManager; + $this->linkBatchFactory = $linkBatchFactory; + $this->blockRestrictionStore = $blockRestrictionStore; + $this->loadBalancer = $loadBalancer; + $this->specialPageFactory = $specialPageFactory; + $this->actorMigration = $actorMigration; + $this->commentStore = $commentStore; } /** @@ -64,7 +105,7 @@ class SpecialBlockList extends SpecialPage { if ( $action == 'unblock' || $action == 'submit' && $request->wasPosted() ) { # B/C @since 1.18: Unblock interface is now at Special:Unblock - $title = SpecialPage::getTitleFor( 'Unblock', $this->target ); + $title = $this->specialPageFactory->getTitleForAlias( 'Unblock/' . $this->target ); $out->redirect( $title->getFullURL() ); return; @@ -138,10 +179,7 @@ class SpecialBlockList extends SpecialPage { $conds = []; $db = $this->getDB(); # Is the user allowed to see hidden blocks? - if ( !MediaWikiServices::getInstance() - ->getPermissionManager() - ->userHasRight( $this->getUser(), 'hideuser' ) - ) { + if ( !$this->permManager->userHasRight( $this->getUser(), 'hideuser' ) ) { $conds['ipb_deleted'] = 0; } @@ -205,7 +243,13 @@ class SpecialBlockList extends SpecialPage { return new BlockListPager( $this, $conds, - MediaWikiServices::getInstance()->getLinkBatchFactory() + $this->linkBatchFactory, + $this->blockRestrictionStore, + $this->permManager, + $this->loadBalancer, + $this->specialPageFactory, + $this->actorMigration, + $this->commentStore ); } @@ -266,6 +310,6 @@ class SpecialBlockList extends SpecialPage { * @return IDatabase */ protected function getDB() { - return wfGetDB( DB_REPLICA ); + return $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA ); } } diff --git a/includes/specials/pagers/BlockListPager.php b/includes/specials/pagers/BlockListPager.php index 3cb6ec95256..a948d35afc0 100644 --- a/includes/specials/pagers/BlockListPager.php +++ b/includes/specials/pagers/BlockListPager.php @@ -19,14 +19,17 @@ * @ingroup Pager */ +use MediaWiki\Block\BlockRestrictionStore; use MediaWiki\Block\DatabaseBlock; use MediaWiki\Block\Restriction\NamespaceRestriction; use MediaWiki\Block\Restriction\PageRestriction; use MediaWiki\Block\Restriction\Restriction; use MediaWiki\Cache\LinkBatchFactory; -use MediaWiki\MediaWikiServices; +use MediaWiki\Permissions\PermissionManager; +use MediaWiki\SpecialPage\SpecialPageFactory; use MediaWiki\User\UserIdentity; use Wikimedia\IPUtils; +use Wikimedia\Rdbms\ILoadBalancer; use Wikimedia\Rdbms\IResultWrapper; /** @@ -46,20 +49,53 @@ class BlockListPager extends TablePager { /** @var LinkBatchFactory */ private $linkBatchFactory; + /** @var BlockRestrictionStore */ + private $blockRestrictionStore; + + /** @var PermissionManager */ + private $permissionManager; + + /** @var SpecialPageFactory */ + private $specialPageFactory; + + /** @var ActorMigration */ + private $actorMigration; + + /** @var CommentStore */ + private $commentStore; + /** * @param SpecialPage $page * @param array $conds - * @param LinkBatchFactory|null $linkBatchFactory + * @param LinkBatchFactory $linkBatchFactory + * @param BlockRestrictionStore $blockRestrictionStore + * @param PermissionManager $permissionManager + * @param ILoadBalancer $loadBalancer + * @param SpecialPageFactory $specialPageFactory + * @param ActorMigration $actorMigration + * @param CommentStore $commentStore */ public function __construct( $page, $conds, - LinkBatchFactory $linkBatchFactory = null + LinkBatchFactory $linkBatchFactory, + BlockRestrictionStore $blockRestrictionStore, + PermissionManager $permissionManager, + ILoadBalancer $loadBalancer, + SpecialPageFactory $specialPageFactory, + ActorMigration $actorMigration, + CommentStore $commentStore ) { + $this->mDb = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA ); parent::__construct( $page->getContext(), $page->getLinkRenderer() ); $this->conds = $conds; $this->mDefaultDirection = IndexPager::DIR_DESCENDING; - $this->linkBatchFactory = $linkBatchFactory ?? MediaWikiServices::getInstance()->getLinkBatchFactory(); + $this->linkBatchFactory = $linkBatchFactory; + $this->blockRestrictionStore = $blockRestrictionStore; + $this->permissionManager = $permissionManager; + $this->specialPageFactory = $specialPageFactory; + $this->actorMigration = $actorMigration; + $this->commentStore = $commentStore; } protected function getFieldNames() { @@ -154,25 +190,22 @@ class BlockListPager extends TablePager { $value, /* User preference timezone */true ) ); - if ( MediaWikiServices::getInstance() - ->getPermissionManager() - ->userHasRight( $this->getUser(), 'block' ) - ) { + if ( $this->permissionManager->userHasRight( $this->getUser(), 'block' ) ) { $links = []; if ( $row->ipb_auto ) { $links[] = $linkRenderer->makeKnownLink( - SpecialPage::getTitleFor( 'Unblock' ), + $this->specialPageFactory->getTitleForAlias( 'Unblock' ), $msg['unblocklink'], [], [ 'wpTarget' => "#{$row->ipb_id}" ] ); } else { $links[] = $linkRenderer->makeKnownLink( - SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ), + $this->specialPageFactory->getTitleForAlias( 'Unblock/' . $row->ipb_address ), $msg['unblocklink'] ); $links[] = $linkRenderer->makeKnownLink( - SpecialPage::getTitleFor( 'Block', $row->ipb_address ), + $this->specialPageFactory->getTitleForAlias( 'Block/' . $row->ipb_address ), $msg['change-blocklink'] ); } @@ -211,7 +244,7 @@ class BlockListPager extends TablePager { break; case 'ipb_reason': - $value = CommentStore::getStore()->getComment( 'ipb_reason', $row )->text; + $value = $this->commentStore->getComment( 'ipb_reason', $row )->text; $formatted = Linker::formatComment( $value ); break; @@ -305,7 +338,7 @@ class BlockListPager extends TablePager { 'li', [], $linkRenderer->makeLink( - SpecialPage::getTitleValueFor( 'Allpages' ), + $this->specialPageFactory->getTitleForAlias( 'Allpages' ), $text, [], [ @@ -342,8 +375,8 @@ class BlockListPager extends TablePager { } public function getQueryInfo() { - $commentQuery = CommentStore::getStore()->getJoin( 'ipb_reason' ); - $actorQuery = ActorMigration::newMigration()->getJoin( 'ipb_by' ); + $commentQuery = $this->commentStore->getJoin( 'ipb_reason' ); + $actorQuery = $this->actorMigration->getJoin( 'ipb_by' ); $info = [ 'tables' => array_merge( @@ -378,10 +411,7 @@ class BlockListPager extends TablePager { $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() ); # Is the user allowed to see hidden blocks? - if ( !MediaWikiServices::getInstance() - ->getPermissionManager() - ->userHasRight( $this->getUser(), 'hideuser' ) - ) { + if ( !$this->permissionManager->userHasRight( $this->getUser(), 'hideuser' ) ) { $info['conds']['ipb_deleted'] = 0; } @@ -430,7 +460,6 @@ class BlockListPager extends TablePager { * @param IResultWrapper $result */ public function preprocessResults( $result ) { - $services = MediaWikiServices::getInstance(); # Do a link batch query $lb = $this->linkBatchFactory->newLinkBatch(); $lb->setCaller( __METHOD__ ); @@ -453,8 +482,7 @@ class BlockListPager extends TablePager { if ( $partialBlocks ) { // Mutations to the $row object are not persisted. The restrictions will // need be stored in a separate store. - $blockRestrictionStore = $services->getBlockRestrictionStore(); - $this->restrictions = $blockRestrictionStore->loadByBlockId( $partialBlocks ); + $this->restrictions = $this->blockRestrictionStore->loadByBlockId( $partialBlocks ); foreach ( $this->restrictions as $restriction ) { if ( $restriction->getType() === PageRestriction::TYPE ) { diff --git a/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php b/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php index 56887fdaa73..b40921fe36c 100644 --- a/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php +++ b/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php @@ -1,10 +1,14 @@ linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory(); + $services = MediaWikiServices::getInstance(); + $this->linkBatchFactory = $services->getLinkBatchFactory(); + $this->blockRestrictionStore = $services->getBlockRestrictionStore(); + $this->permissionManager = $services->getPermissionManager(); + $this->loadBalancer = $services->getDBLoadBalancer(); + $this->specialPageFactory = $services->getSpecialPageFactory(); + $this->actorMigration = $services->getActorMigration(); + $this->commentStore = $services->getCommentStore(); + } + + private function getBlockListPager() { + return new BlockListPager( + new SpecialPage(), + [], + $this->linkBatchFactory, + $this->blockRestrictionStore, + $this->permissionManager, + $this->loadBalancer, + $this->specialPageFactory, + $this->actorMigration, + $this->commentStore + ); } /** @@ -39,7 +82,7 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase { $expected = $expected ?? MWTimestamp::getInstance()->format( 'H:i, j F Y' ); $row = $row ?: (object)[]; - $pager = new BlockListPager( new SpecialPage(), [], $this->linkBatchFactory ); + $pager = $this->getBlockListPager(); $wrappedPager = TestingAccessWrapper::newFromObject( $pager ); $wrappedPager->mCurrentRow = $row; @@ -128,7 +171,7 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase { 'wgScript' => '/w/index.php', ] ); - $pager = new BlockListPager( new SpecialPage(), [], $this->linkBatchFactory ); + $pager = $this->getBlockListPager(); $row = (object)[ 'ipb_id' => 0, @@ -208,7 +251,7 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase { 'ipb_sitewide' => 1, 'ipb_timestamp' => $this->db->timestamp( wfTimestamp( TS_MW ) ), ]; - $pager = new BlockListPager( new SpecialPage(), [], $this->linkBatchFactory ); + $pager = $this->getBlockListPager(); $pager->preprocessResults( [ $row ] ); foreach ( $links as $link ) { @@ -221,7 +264,7 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase { 'by_user_name' => 'Admin', 'ipb_sitewide' => 1, ]; - $pager = new BlockListPager( new SpecialPage(), [], $this->linkBatchFactory ); + $pager = $this->getBlockListPager(); $pager->preprocessResults( [ $row ] ); $this->assertObjectNotHasAttribute( 'ipb_restrictions', $row ); @@ -248,7 +291,7 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase { $result = $this->db->select( 'ipblocks', [ '*' ], [ 'ipb_id' => $block->getId() ] ); - $pager = new BlockListPager( new SpecialPage(), [], $this->linkBatchFactory ); + $pager = $this->getBlockListPager(); $pager->preprocessResults( $result ); $wrappedPager = TestingAccessWrapper::newFromObject( $pager );