wiki.techinc.nl/includes/Storage/PageUpdaterFactory.php
DannyS712 9d0d489412 Cleanup to PageUpdaterFactory and tests
Use one line instead of three to document variable types.

Make ContentHandlerFactory final again, no need to make
non-final.

Use MockServiceDependenciesTrait to simplify creation
of PageUpdaterFactory in unit tests.

Use Wikimedia\Rdbms\LoadBalancer instead of deprecated
alias \LoadBalancer.

Follow-up: I2a060bfa8ac098edf24fc4d51212eeb7ddf3942d
Change-Id: I63b18f71cb6e40c971511e71e8475d49ef530d71
2021-07-20 09:11:13 +00:00

286 lines
8 KiB
PHP

<?php
/**
* A factory for DerivedPageDataUpdater instances.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Storage;
use JobQueueGroup;
use Language;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Revision\RevisionRenderer;
use MediaWiki\Revision\RevisionStore;
use MediaWiki\Revision\SlotRoleRegistry;
use MediaWiki\User\UserEditTracker;
use MediaWiki\User\UserGroupManager;
use MediaWiki\User\UserIdentity;
use MediaWiki\User\UserNameUtils;
use MessageCache;
use ParserCache;
use Psr\Log\LoggerInterface;
use Wikimedia\Assert\Assert;
use Wikimedia\Rdbms\ILBFactory;
use WikiPage;
/**
* A factory for PageUpdater instances.
*
* @since 1.37
* @ingroup Page
*/
class PageUpdaterFactory {
/**
* Options that have to be present in the ServiceOptions object passed to the constructor.
* @note must include PageUpdater::CONSTRUCTOR_OPTIONS
* @internal
*/
public const CONSTRUCTOR_OPTIONS = [
'ArticleCountMethod',
'RCWatchCategoryMembership',
'PageCreationLog',
'AjaxEditStash',
'UseAutomaticEditSummaries',
'ManualRevertSearchRadius',
'UseRCPatrol',
];
/** @var RevisionStore */
private $revisionStore;
/** @var RevisionRenderer */
private $revisionRenderer;
/** @var SlotRoleRegistry */
private $slotRoleRegistry;
/** @var ParserCache */
private $parserCache;
/** @var JobQueueGroup */
private $jobQueueGroup;
/** @var MessageCache */
private $messageCache;
/** @var Language */
private $contLang;
/** @var ILBFactory */
private $loadbalancerFactory;
/** @var IContentHandlerFactory */
private $contentHandlerFactory;
/** @var HookContainer */
private $hookContainer;
/** @var EditResultCache */
private $editResultCache;
/** @var UserNameUtils */
private $userNameUtils;
/** @var LoggerInterface */
private $logger;
/** @var ServiceOptions */
private $options;
/** @var UserEditTracker */
private $userEditTracker;
/** @var UserGroupManager */
private $userGroupManager;
/** @var string[] */
private $softwareTags;
/**
* @param RevisionStore $revisionStore
* @param RevisionRenderer $revisionRenderer
* @param SlotRoleRegistry $slotRoleRegistry
* @param ParserCache $parserCache
* @param JobQueueGroup $jobQueueGroup
* @param MessageCache $messageCache
* @param Language $contLang
* @param ILBFactory $loadbalancerFactory
* @param IContentHandlerFactory $contentHandlerFactory
* @param HookContainer $hookContainer
* @param EditResultCache $editResultCache
* @param UserNameUtils $userNameUtils
* @param LoggerInterface $logger
* @param ServiceOptions $options
* @param UserEditTracker $userEditTracker
* @param UserGroupManager $userGroupManager
* @param string[] $softwareTags
*/
public function __construct(
RevisionStore $revisionStore,
RevisionRenderer $revisionRenderer,
SlotRoleRegistry $slotRoleRegistry,
ParserCache $parserCache,
JobQueueGroup $jobQueueGroup,
MessageCache $messageCache,
Language $contLang,
ILBFactory $loadbalancerFactory,
IContentHandlerFactory $contentHandlerFactory,
HookContainer $hookContainer,
EditResultCache $editResultCache,
UserNameUtils $userNameUtils,
LoggerInterface $logger,
ServiceOptions $options,
UserEditTracker $userEditTracker,
UserGroupManager $userGroupManager,
array $softwareTags
) {
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->revisionStore = $revisionStore;
$this->revisionRenderer = $revisionRenderer;
$this->slotRoleRegistry = $slotRoleRegistry;
$this->parserCache = $parserCache;
$this->jobQueueGroup = $jobQueueGroup;
$this->messageCache = $messageCache;
$this->contLang = $contLang;
$this->loadbalancerFactory = $loadbalancerFactory;
$this->contentHandlerFactory = $contentHandlerFactory;
$this->hookContainer = $hookContainer;
$this->editResultCache = $editResultCache;
$this->userNameUtils = $userNameUtils;
$this->logger = $logger;
$this->options = $options;
$this->userEditTracker = $userEditTracker;
$this->userGroupManager = $userGroupManager;
$this->softwareTags = $softwareTags;
}
/**
* Return a PageUpdater for building an update to a page.
*
* @internal For now, most code should keep using WikiPage::newPageUpdater() instead.
* @note We can only start using this method everywhere when WikiPage::prepareContentForEdit()
* and WikiPage::getCurrentUpdate() have been removed. For now, the WikiPage instance is
* used to make the state of an ongoing edit available to hook handlers.
*
* @param WikiPage $page
* @param UserIdentity $user
*
* @return PageUpdater
* @since 1.37
*/
public function newPageUpdater(
WikiPage $page,
UserIdentity $user
): PageUpdater {
return $this->newPageUpdaterForDerivedPageDataUpdater(
$page,
$user,
$this->newDerivedPageDataUpdater( $page )
);
}
/**
* Return a PageUpdater for building an update to a page, reusing the state of
* an existing DerivedPageDataUpdater.
*
* @param WikiPage $page
* @param UserIdentity $user
* @param DerivedPageDataUpdater $derivedPageDataUpdater
*
* @return PageUpdater
* @internal needed by WikiPage to back the WikiPage::newPageUpdater method.
*
* @since 1.37
*/
public function newPageUpdaterForDerivedPageDataUpdater(
WikiPage $page,
UserIdentity $user,
DerivedPageDataUpdater $derivedPageDataUpdater
): PageUpdater {
Assert::precondition(
$page->canExist(),
'The WikiPage instance does not represent a proper page!'
);
$pageUpdater = new PageUpdater(
$user,
$page, // NOTE: eventually, PageUpdater should not know about WikiPage
$derivedPageDataUpdater,
$this->loadbalancerFactory->getMainLB(),
$this->revisionStore,
$this->slotRoleRegistry,
$this->contentHandlerFactory,
$this->hookContainer,
$this->userEditTracker,
$this->userGroupManager,
new ServiceOptions(
PageUpdater::CONSTRUCTOR_OPTIONS,
$this->options
),
$this->softwareTags
);
$pageUpdater->setUsePageCreationLog( $this->options->get( 'PageCreationLog' ) );
$pageUpdater->setAjaxEditStash( $this->options->get( 'AjaxEditStash' ) );
$pageUpdater->setUseAutomaticEditSummaries(
$this->options->get( 'UseAutomaticEditSummaries' )
);
return $pageUpdater;
}
/**
* @param WikiPage $page
*
* @return DerivedPageDataUpdater
* @internal Needed by WikiPage to back the deprecated prepareContentForEdit() method.
* @note Avoid direct usage of DerivedPageDataUpdater.
* @see docs/pageupdater.md for more information.
*/
public function newDerivedPageDataUpdater( WikiPage $page ): DerivedPageDataUpdater {
$derivedDataUpdater = new DerivedPageDataUpdater(
$page, // NOTE: eventually, PageUpdater should not know about WikiPage
$this->revisionStore,
$this->revisionRenderer,
$this->slotRoleRegistry,
$this->parserCache,
$this->jobQueueGroup,
$this->messageCache,
$this->contLang,
$this->loadbalancerFactory,
$this->contentHandlerFactory,
$this->hookContainer,
$this->editResultCache,
$this->userNameUtils
);
$derivedDataUpdater->setLogger( $this->logger );
$derivedDataUpdater->setArticleCountMethod( $this->options->get( 'ArticleCountMethod' ) );
$derivedDataUpdater->setRcWatchCategoryMembership(
$this->options->get( 'RCWatchCategoryMembership' )
);
return $derivedDataUpdater;
}
}