Merge "Inject services into SpecialBlockList/SpecialAutoblockList"

This commit is contained in:
jenkins-bot 2020-09-23 20:47:34 +00:00 committed by Gerrit Code Review
commit cd212ea28f
5 changed files with 220 additions and 41 deletions

View file

@ -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,

View file

@ -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
);
}

View file

@ -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 );
}
}

View file

@ -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 ) {

View file

@ -1,10 +1,14 @@
<?php
use MediaWiki\Block\BlockRestrictionStore;
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\Block\Restriction\NamespaceRestriction;
use MediaWiki\Block\Restriction\PageRestriction;
use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\SpecialPage\SpecialPageFactory;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\TestingAccessWrapper;
/**
@ -18,10 +22,49 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase {
*/
private $linkBatchFactory;
/** @var BlockRestrictionStore */
private $blockRestrictionStore;
/** @var PermissionManager */
private $permissionManager;
/** @var ILoadBalancer */
private $loadBalancer;
/** @var SpecialPageFactory */
private $specialPageFactory;
/** @var ActorMigration */
private $actorMigration;
/** @var CommentStore */
private $commentStore;
protected function setUp() : void {
parent::setUp();
$this->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 );