Inject services in Job

Change-Id: Ib60ad4128eb93eb7ac02e4e8f54af0c57ce4e296
This commit is contained in:
Fomafix 2024-02-11 18:44:30 +00:00
parent 030fb699f7
commit 7edb3c9670
4 changed files with 83 additions and 18 deletions

View file

@ -37,12 +37,23 @@
"SearchEngineConfig",
"SlotRoleStore",
"UserFactory",
"UserOptionsLookup"
"UserOptionsLookup",
"WatchlistManager",
"WikiPageFactory"
]
}
},
"JobClasses": {
"replaceText": "MediaWiki\\Extension\\ReplaceText\\Job"
"replaceText": {
"class": "MediaWiki\\Extension\\ReplaceText\\Job",
"services": [
"MovePageFactory",
"PermissionManager",
"UserFactory",
"WatchlistManager",
"WikiPageFactory"
]
}
},
"MessagesDirs": {
"ReplaceText": [

View file

@ -322,7 +322,14 @@ EOF;
}
$this->output( "Replacing on $title... " );
$job = new Job( $title, $params );
$services = $this->getServiceContainer();
$job = new Job( $title, $params,
$services->getMovePageFactory(),
$services->getPermissionManager(),
$services->getUserFactory(),
$services->getWatchlistManager(),
$services->getWikiPageFactory()
);
if ( $job->run() !== true ) {
$this->error( "Trouble on the page '$title'." );
}

View file

@ -24,8 +24,12 @@ namespace MediaWiki\Extension\ReplaceText;
use ContentHandler;
use Job as JobParent;
use MediaWiki\CommentStore\CommentStoreComment;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\MovePageFactory;
use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\Title\Title;
use MediaWiki\User\UserFactory;
use MediaWiki\Watchlist\WatchlistManager;
use RecentChange;
use RequestContext;
use TextContent;
@ -36,13 +40,35 @@ use Wikimedia\ScopedCallback;
* - based on /includes/RefreshLinksJob.php
*/
class Job extends JobParent {
private MovePageFactory $movePageFactory;
private PermissionManager $permissionManager;
private UserFactory $userFactory;
private WatchlistManager $watchlistManager;
private WikiPageFactory $wikiPageFactory;
/**
* Constructor.
* @param Title $title
* @param array|bool $params Cannot be === true
* @param MovePageFactory $movePageFactory
* @param PermissionManager $permissionManager
* @param UserFactory $userFactory
* @param WatchlistManager $watchlistManager
* @param WikiPageFactory $wikiPageFactory
*/
function __construct( $title, $params = [] ) {
function __construct( $title, $params,
MovePageFactory $movePageFactory,
PermissionManager $permissionManager,
UserFactory $userFactory,
WatchlistManager $watchlistManager,
WikiPageFactory $wikiPageFactory
) {
parent::__construct( 'replaceText', $title, $params );
$this->movePageFactory = $movePageFactory;
$this->permissionManager = $permissionManager;
$this->userFactory = $userFactory;
$this->watchlistManager = $watchlistManager;
$this->wikiPageFactory = $wikiPageFactory;
}
/**
@ -50,16 +76,14 @@ class Job extends JobParent {
* @return bool success
*/
function run() {
$services = MediaWikiServices::getInstance();
// T279090
$current_user = $services->getUserFactory()->newFromId( $this->params['user_id'] );
$current_user = $this->userFactory->newFromId( $this->params['user_id'] );
if ( !$current_user->isRegistered() ) {
$this->error = 'replacetext: the user ID ' . $this->params['user_id'] .
' does not belong to a registered user.';
return false;
}
$permissionManager = $services->getPermissionManager();
if ( !$permissionManager->userCan(
if ( !$this->permissionManager->userCan(
'replacetext', $current_user, $this->title
) ) {
$this->error = 'replacetext: permission no longer valid';
@ -94,7 +118,7 @@ class Job extends JobParent {
$reason = $this->params['edit_summary'];
$create_redirect = $this->params['create_redirect'];
$mvPage = $services->getMovePageFactory()->newMovePage( $this->title, $new_title );
$mvPage = $this->movePageFactory->newMovePage( $this->title, $new_title );
$mvStatus = $mvPage->move( $current_user, $reason, $create_redirect );
if ( !$mvStatus->isOK() ) {
$this->error = 'replaceText: error while moving: ' . $this->title->getPrefixedDBkey() .
@ -103,10 +127,10 @@ class Job extends JobParent {
}
if ( $this->params['watch_page'] ) {
$services->getWatchlistManager()->addWatch( $current_user, $new_title );
$this->watchlistManager->addWatch( $current_user, $new_title );
}
} else {
$wikiPage = $services->getWikiPageFactory()->newFromTitle( $this->title );
$wikiPage = $this->wikiPageFactory->newFromTitle( $this->title );
$latestRevision = $wikiPage->getRevisionRecord();
if ( $latestRevision === null ) {
@ -171,14 +195,14 @@ class Job extends JobParent {
if ( $hasMatches ) {
$edit_summary = CommentStoreComment::newUnsavedComment( $this->params['edit_summary'] );
$flags = EDIT_MINOR;
if ( $permissionManager->userHasRight( $current_user, 'bot' ) ) {
if ( $this->permissionManager->userHasRight( $current_user, 'bot' ) ) {
$flags |= EDIT_FORCE_BOT;
}
if ( isset( $this->params['botEdit'] ) && $this->params['botEdit'] ) {
$flags |= EDIT_FORCE_BOT;
}
if ( $permissionManager->userHasRight( $current_user, 'patrol' ) ||
$permissionManager->userHasRight( $current_user, 'autopatrol' ) ) {
if ( $this->permissionManager->userHasRight( $current_user, 'patrol' ) ||
$this->permissionManager->userHasRight( $current_user, 'autopatrol' ) ) {
$updater->setRcPatrolStatus( RecentChange::PRC_PATROLLED );
}
$updater->saveRevision( $edit_summary, $flags );

View file

@ -26,6 +26,7 @@ use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Html\Html;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Page\MovePageFactory;
use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\SpecialPage\SpecialPage;
@ -34,6 +35,7 @@ use MediaWiki\Title\NamespaceInfo;
use MediaWiki\Title\Title;
use MediaWiki\User\Options\UserOptionsLookup;
use MediaWiki\User\UserFactory;
use MediaWiki\Watchlist\WatchlistManager;
use OOUI;
use PermissionsError;
use SearchEngineConfig;
@ -93,6 +95,9 @@ class SpecialReplaceText extends SpecialPage {
/** @var UserOptionsLookup */
private $userOptionsLookup;
private WatchlistManager $watchlistManager;
private WikiPageFactory $wikiPageFactory;
/** @var Search */
private $search;
@ -110,6 +115,8 @@ class SpecialReplaceText extends SpecialPage {
* @param NameTableStore $slotRoleStore
* @param UserFactory $userFactory
* @param UserOptionsLookup $userOptionsLookup
* @param WatchlistManager $watchlistManager
* @param WikiPageFactory $wikiPageFactory
*/
public function __construct(
HookContainer $hookContainer,
@ -124,7 +131,9 @@ class SpecialReplaceText extends SpecialPage {
SearchEngineConfig $searchEngineConfig,
NameTableStore $slotRoleStore,
UserFactory $userFactory,
UserOptionsLookup $userOptionsLookup
UserOptionsLookup $userOptionsLookup,
WatchlistManager $watchlistManager,
WikiPageFactory $wikiPageFactory
) {
parent::__construct( 'ReplaceText', 'replacetext' );
$this->hookHelper = new HookHelper( $hookContainer );
@ -140,6 +149,8 @@ class SpecialReplaceText extends SpecialPage {
$this->slotRoleStore = $slotRoleStore;
$this->userFactory = $userFactory;
$this->userOptionsLookup = $userOptionsLookup;
$this->watchlistManager = $watchlistManager;
$this->wikiPageFactory = $wikiPageFactory;
$this->search = new Search(
$this->getConfig(),
$dbProvider
@ -386,7 +397,13 @@ class SpecialReplaceText extends SpecialPage {
$title = Title::newFromID( (int)substr( $key, 5 ) );
$replacement_params['move_page'] = true;
if ( $title !== null ) {
$jobs[] = new Job( $title, $replacement_params );
$jobs[] = new Job( $title, $replacement_params,
$this->movePageFactory,
$this->permissionManager,
$this->userFactory,
$this->watchlistManager,
$this->wikiPageFactory
);
}
unset( $replacement_params['move_page'] );
} elseif ( strpos( $key, '|' ) !== false ) {
@ -400,7 +417,13 @@ class SpecialReplaceText extends SpecialPage {
$title = Title::newFromID( (int)$page_id );
$replacement_params['roles'] = $roles;
if ( $title !== null ) {
$jobs[] = new Job( $title, $replacement_params );
$jobs[] = new Job( $title, $replacement_params,
$this->movePageFactory,
$this->permissionManager,
$this->userFactory,
$this->watchlistManager,
$this->wikiPageFactory
);
}
unset( $replacement_params['roles'] );
}