Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
84 / 84
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
PageUpdaterFactory
100.00% covered (success)
100.00%
84 / 84
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
1
 newPageUpdater
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 newPageUpdaterForDerivedPageDataUpdater
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
1
 newDerivedPageDataUpdater
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Storage;
22
23use JobQueueGroup;
24use Language;
25use MediaWiki\Config\ServiceOptions;
26use MediaWiki\Content\IContentHandlerFactory;
27use MediaWiki\Content\Transform\ContentTransformer;
28use MediaWiki\HookContainer\HookContainer;
29use MediaWiki\MainConfigNames;
30use MediaWiki\Page\PageIdentity;
31use MediaWiki\Page\WikiPageFactory;
32use MediaWiki\Permissions\PermissionManager;
33use MediaWiki\Revision\RevisionRenderer;
34use MediaWiki\Revision\RevisionStore;
35use MediaWiki\Revision\SlotRoleRegistry;
36use MediaWiki\Title\TitleFormatter;
37use MediaWiki\User\TalkPageNotificationManager;
38use MediaWiki\User\UserEditTracker;
39use MediaWiki\User\UserGroupManager;
40use MediaWiki\User\UserIdentity;
41use MediaWiki\User\UserNameUtils;
42use MessageCache;
43use ParserCache;
44use Psr\Log\LoggerInterface;
45use WANObjectCache;
46use Wikimedia\Rdbms\ILBFactory;
47use WikiPage;
48
49/**
50 * A factory for PageUpdater and DerivedPageDataUpdater instances.
51 *
52 * @since 1.37
53 * @ingroup Page
54 */
55class PageUpdaterFactory {
56
57    /**
58     * Options that have to be present in the ServiceOptions object passed to the constructor.
59     * @note must include PageUpdater::CONSTRUCTOR_OPTIONS
60     * @internal
61     */
62    public const CONSTRUCTOR_OPTIONS = [
63        MainConfigNames::ArticleCountMethod,
64        MainConfigNames::RCWatchCategoryMembership,
65        MainConfigNames::PageCreationLog,
66        MainConfigNames::UseAutomaticEditSummaries,
67        MainConfigNames::ManualRevertSearchRadius,
68        MainConfigNames::UseRCPatrol,
69        MainConfigNames::ParsoidCacheConfig,
70    ];
71
72    /** @var RevisionStore */
73    private $revisionStore;
74
75    /** @var RevisionRenderer */
76    private $revisionRenderer;
77
78    /** @var SlotRoleRegistry */
79    private $slotRoleRegistry;
80
81    /** @var ParserCache */
82    private $parserCache;
83
84    /** @var JobQueueGroup */
85    private $jobQueueGroup;
86
87    /** @var MessageCache */
88    private $messageCache;
89
90    /** @var Language */
91    private $contLang;
92
93    /** @var ILBFactory */
94    private $loadbalancerFactory;
95
96    /** @var IContentHandlerFactory */
97    private $contentHandlerFactory;
98
99    /** @var HookContainer */
100    private $hookContainer;
101
102    /** @var EditResultCache */
103    private $editResultCache;
104
105    /** @var UserNameUtils */
106    private $userNameUtils;
107
108    /** @var LoggerInterface */
109    private $logger;
110
111    /** @var ServiceOptions */
112    private $options;
113
114    /** @var UserEditTracker */
115    private $userEditTracker;
116
117    /** @var UserGroupManager */
118    private $userGroupManager;
119
120    /** @var TitleFormatter */
121    private $titleFormatter;
122
123    /** @var ContentTransformer */
124    private $contentTransformer;
125
126    /** @var PageEditStash */
127    private $pageEditStash;
128
129    /** @var TalkPageNotificationManager */
130    private $talkPageNotificationManager;
131
132    /** @var WANObjectCache */
133    private $mainWANObjectCache;
134
135    /** @var PermissionManager */
136    private $permissionManager;
137
138    /** @var WikiPageFactory */
139    private $wikiPageFactory;
140
141    /** @var string[] */
142    private $softwareTags;
143
144    /**
145     * @param RevisionStore $revisionStore
146     * @param RevisionRenderer $revisionRenderer
147     * @param SlotRoleRegistry $slotRoleRegistry
148     * @param ParserCache $parserCache
149     * @param JobQueueGroup $jobQueueGroup
150     * @param MessageCache $messageCache
151     * @param Language $contLang
152     * @param ILBFactory $loadbalancerFactory
153     * @param IContentHandlerFactory $contentHandlerFactory
154     * @param HookContainer $hookContainer
155     * @param EditResultCache $editResultCache
156     * @param UserNameUtils $userNameUtils
157     * @param LoggerInterface $logger
158     * @param ServiceOptions $options
159     * @param UserEditTracker $userEditTracker
160     * @param UserGroupManager $userGroupManager
161     * @param TitleFormatter $titleFormatter
162     * @param ContentTransformer $contentTransformer
163     * @param PageEditStash $pageEditStash
164     * @param TalkPageNotificationManager $talkPageNotificationManager
165     * @param WANObjectCache $mainWANObjectCache
166     * @param PermissionManager $permissionManager
167     * @param WikiPageFactory $wikiPageFactory
168     * @param string[] $softwareTags
169     */
170    public function __construct(
171        RevisionStore $revisionStore,
172        RevisionRenderer $revisionRenderer,
173        SlotRoleRegistry $slotRoleRegistry,
174        ParserCache $parserCache,
175        JobQueueGroup $jobQueueGroup,
176        MessageCache $messageCache,
177        Language $contLang,
178        ILBFactory $loadbalancerFactory,
179        IContentHandlerFactory $contentHandlerFactory,
180        HookContainer $hookContainer,
181        EditResultCache $editResultCache,
182        UserNameUtils $userNameUtils,
183        LoggerInterface $logger,
184        ServiceOptions $options,
185        UserEditTracker $userEditTracker,
186        UserGroupManager $userGroupManager,
187        TitleFormatter $titleFormatter,
188        ContentTransformer $contentTransformer,
189        PageEditStash $pageEditStash,
190        TalkPageNotificationManager $talkPageNotificationManager,
191        WANObjectCache $mainWANObjectCache,
192        PermissionManager $permissionManager,
193        WikiPageFactory $wikiPageFactory,
194        array $softwareTags
195    ) {
196        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
197
198        $this->revisionStore = $revisionStore;
199        $this->revisionRenderer = $revisionRenderer;
200        $this->slotRoleRegistry = $slotRoleRegistry;
201        $this->parserCache = $parserCache;
202        $this->jobQueueGroup = $jobQueueGroup;
203        $this->messageCache = $messageCache;
204        $this->contLang = $contLang;
205        $this->loadbalancerFactory = $loadbalancerFactory;
206        $this->contentHandlerFactory = $contentHandlerFactory;
207        $this->hookContainer = $hookContainer;
208        $this->editResultCache = $editResultCache;
209        $this->userNameUtils = $userNameUtils;
210        $this->logger = $logger;
211        $this->options = $options;
212        $this->userEditTracker = $userEditTracker;
213        $this->userGroupManager = $userGroupManager;
214        $this->titleFormatter = $titleFormatter;
215        $this->contentTransformer = $contentTransformer;
216        $this->pageEditStash = $pageEditStash;
217        $this->talkPageNotificationManager = $talkPageNotificationManager;
218        $this->mainWANObjectCache = $mainWANObjectCache;
219        $this->permissionManager = $permissionManager;
220        $this->softwareTags = $softwareTags;
221        $this->wikiPageFactory = $wikiPageFactory;
222    }
223
224    /**
225     * Return a PageUpdater for building an update to a page.
226     *
227     * @internal For now, most code should keep using WikiPage::newPageUpdater() instead.
228     * @note We can only start using this method everywhere when WikiPage::prepareContentForEdit()
229     * and WikiPage::getCurrentUpdate() have been removed. For now, the WikiPage instance is
230     * used to make the state of an ongoing edit available to hook handlers.
231     *
232     * @param PageIdentity $page
233     * @param UserIdentity $user
234     *
235     * @return PageUpdater
236     * @since 1.37
237     */
238    public function newPageUpdater(
239        PageIdentity $page,
240        UserIdentity $user
241    ): PageUpdater {
242        $page = $this->wikiPageFactory->newFromTitle( $page );
243
244        return $this->newPageUpdaterForDerivedPageDataUpdater(
245            $page,
246            $user,
247            $this->newDerivedPageDataUpdater( $page )
248        );
249    }
250
251    /**
252     * Return a PageUpdater for building an update to a page, reusing the state of
253     * an existing DerivedPageDataUpdater.
254     *
255     * @param WikiPage $page
256     * @param UserIdentity $user
257     * @param DerivedPageDataUpdater $derivedPageDataUpdater
258     *
259     * @return PageUpdater
260     * @internal needed by WikiPage to back the WikiPage::newPageUpdater method.
261     *
262     * @since 1.37
263     */
264    public function newPageUpdaterForDerivedPageDataUpdater(
265        WikiPage $page,
266        UserIdentity $user,
267        DerivedPageDataUpdater $derivedPageDataUpdater
268    ): PageUpdater {
269        $pageUpdater = new PageUpdater(
270            $user,
271            $page, // NOTE: eventually, PageUpdater should not know about WikiPage
272            $derivedPageDataUpdater,
273            $this->loadbalancerFactory,
274            $this->revisionStore,
275            $this->slotRoleRegistry,
276            $this->contentHandlerFactory,
277            $this->hookContainer,
278            $this->userEditTracker,
279            $this->userGroupManager,
280            $this->titleFormatter,
281            new ServiceOptions(
282                PageUpdater::CONSTRUCTOR_OPTIONS,
283                $this->options
284            ),
285            $this->softwareTags,
286            $this->logger
287        );
288
289        $pageUpdater->setUsePageCreationLog(
290            $this->options->get( MainConfigNames::PageCreationLog ) );
291        $pageUpdater->setUseAutomaticEditSummaries(
292            $this->options->get( MainConfigNames::UseAutomaticEditSummaries )
293        );
294
295        return $pageUpdater;
296    }
297
298    /**
299     * @param WikiPage $page
300     *
301     * @return DerivedPageDataUpdater
302     * @internal Needed by WikiPage to back the deprecated prepareContentForEdit() method.
303     * @note Avoid direct usage of DerivedPageDataUpdater.
304     * @see docs/pageupdater.md for more information.
305     */
306    public function newDerivedPageDataUpdater( WikiPage $page ): DerivedPageDataUpdater {
307        $derivedDataUpdater = new DerivedPageDataUpdater(
308            $this->options,
309            $page, // NOTE: eventually, PageUpdater should not know about WikiPage
310            $this->revisionStore,
311            $this->revisionRenderer,
312            $this->slotRoleRegistry,
313            $this->parserCache,
314            $this->jobQueueGroup,
315            $this->messageCache,
316            $this->contLang,
317            $this->loadbalancerFactory,
318            $this->contentHandlerFactory,
319            $this->hookContainer,
320            $this->editResultCache,
321            $this->userNameUtils,
322            $this->contentTransformer,
323            $this->pageEditStash,
324            $this->talkPageNotificationManager,
325            $this->mainWANObjectCache,
326            $this->permissionManager
327        );
328
329        $derivedDataUpdater->setLogger( $this->logger );
330        $derivedDataUpdater->setArticleCountMethod(
331            $this->options->get( MainConfigNames::ArticleCountMethod ) );
332        $derivedDataUpdater->setRcWatchCategoryMembership(
333            $this->options->get( MainConfigNames::RCWatchCategoryMembership )
334        );
335
336        return $derivedDataUpdater;
337    }
338
339}