Title: Title::getSubpage should not lose the interwiki prefix
[lhc/web/wiklou.git] / includes / Storage / DerivedPageDataUpdater.php
1 <?php
2 /**
3 * A handle for managing updates for derived page data on edit, import, purge, etc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Storage;
24
25 use CategoryMembershipChangeJob;
26 use Content;
27 use ContentHandler;
28 use DataUpdate;
29 use DeferrableUpdate;
30 use DeferredUpdates;
31 use Hooks;
32 use IDBAccessObject;
33 use InvalidArgumentException;
34 use JobQueueGroup;
35 use Language;
36 use LinksDeletionUpdate;
37 use LinksUpdate;
38 use LogicException;
39 use MediaWiki\Edit\PreparedEdit;
40 use MediaWiki\MediaWikiServices;
41 use MediaWiki\Revision\MutableRevisionRecord;
42 use MediaWiki\Revision\RenderedRevision;
43 use MediaWiki\Revision\RevisionRecord;
44 use MediaWiki\Revision\RevisionRenderer;
45 use MediaWiki\Revision\RevisionSlots;
46 use MediaWiki\Revision\RevisionStore;
47 use MediaWiki\Revision\SlotRoleRegistry;
48 use MediaWiki\Revision\SlotRecord;
49 use MediaWiki\User\UserIdentity;
50 use MessageCache;
51 use MWCallableUpdate;
52 use ParserCache;
53 use ParserOptions;
54 use ParserOutput;
55 use Psr\Log\LoggerAwareInterface;
56 use Psr\Log\LoggerInterface;
57 use Psr\Log\NullLogger;
58 use RecentChangesUpdateJob;
59 use ResourceLoaderWikiModule;
60 use Revision;
61 use SearchUpdate;
62 use SiteStatsUpdate;
63 use Title;
64 use User;
65 use Wikimedia\Assert\Assert;
66 use Wikimedia\Rdbms\ILBFactory;
67 use WikiPage;
68
69 /**
70 * A handle for managing updates for derived page data on edit, import, purge, etc.
71 *
72 * @note Avoid direct usage of DerivedPageDataUpdater.
73 *
74 * @todo Define interfaces for the different use cases of DerivedPageDataUpdater, particularly
75 * providing access to post-PST content and ParserOutput to callbacks during revision creation,
76 * which currently use WikiPage::prepareContentForEdit, and allowing updates to be triggered on
77 * purge, import, and undeletion, which currently use WikiPage::doEditUpdates() and
78 * Content::getSecondaryDataUpdates().
79 *
80 * DerivedPageDataUpdater instances are designed to be cached inside a WikiPage instance,
81 * and re-used by callback code over the course of an update operation. It's a stepping stone
82 * one the way to a more complete refactoring of WikiPage.
83 *
84 * When using a DerivedPageDataUpdater, the following life cycle must be observed:
85 * grabCurrentRevision (optional), prepareContent (optional), prepareUpdate (required
86 * for doUpdates). getCanonicalParserOutput, getSlots, and getSecondaryDataUpdates
87 * require prepareContent or prepareUpdate to have been called first, to initialize the
88 * DerivedPageDataUpdater.
89 *
90 * @see docs/pageupdater.txt for more information.
91 *
92 * MCR migration note: this replaces the relevant methods in WikiPage, and covers the use cases
93 * of PreparedEdit.
94 *
95 * @internal
96 *
97 * @since 1.32
98 * @ingroup Page
99 */
100 class DerivedPageDataUpdater implements IDBAccessObject, LoggerAwareInterface {
101
102 /**
103 * @var UserIdentity|null
104 */
105 private $user = null;
106
107 /**
108 * @var WikiPage
109 */
110 private $wikiPage;
111
112 /**
113 * @var ParserCache
114 */
115 private $parserCache;
116
117 /**
118 * @var RevisionStore
119 */
120 private $revisionStore;
121
122 /**
123 * @var Language
124 */
125 private $contLang;
126
127 /**
128 * @var JobQueueGroup
129 */
130 private $jobQueueGroup;
131
132 /**
133 * @var MessageCache
134 */
135 private $messageCache;
136
137 /**
138 * @var ILBFactory
139 */
140 private $loadbalancerFactory;
141
142 /**
143 * @var LoggerInterface
144 */
145 private $logger;
146
147 /**
148 * @var string see $wgArticleCountMethod
149 */
150 private $articleCountMethod;
151
152 /**
153 * @var boolean see $wgRCWatchCategoryMembership
154 */
155 private $rcWatchCategoryMembership = false;
156
157 /**
158 * Stores (most of) the $options parameter of prepareUpdate().
159 * @see prepareUpdate()
160 */
161 private $options = [
162 'changed' => true,
163 // newrev is true if prepareUpdate is handling the creation of a new revision,
164 // as opposed to a null edit or a forced update.
165 'newrev' => false,
166 'created' => false,
167 'moved' => false,
168 'restored' => false,
169 'oldrevision' => null,
170 'oldcountable' => null,
171 'oldredirect' => null,
172 'triggeringUser' => null,
173 // causeAction/causeAgent default to 'unknown' but that's handled where it's read,
174 // to make the life of prepareUpdate() callers easier.
175 'causeAction' => null,
176 'causeAgent' => null,
177 ];
178
179 /**
180 * The state of the relevant row in page table before the edit.
181 * This is determined by the first call to grabCurrentRevision, prepareContent,
182 * or prepareUpdate (so it is only accessible in 'knows-current' or a later stage).
183 * If pageState was not initialized when prepareUpdate() is called, prepareUpdate() will
184 * attempt to emulate the state of the page table before the edit.
185 *
186 * Contains the following fields:
187 * - oldRevision (RevisionRecord|null): the revision that was current before the change
188 * associated with this update. Might not be set, use getParentRevision().
189 * - oldId (int|null): the id of the above revision. 0 if there is no such revision (the change
190 * was about creating a new page); null if not known (that should not happen).
191 * - oldIsRedirect (bool|null): whether the page was a redirect before the change. Lazy-loaded,
192 * can be null; use wasRedirect() instead of direct access.
193 * - oldCountable (bool|null): whether the page was countable before the change (or null
194 * if we don't have that information)
195 *
196 * @var array
197 */
198 private $pageState = null;
199
200 /**
201 * @var RevisionSlotsUpdate|null
202 */
203 private $slotsUpdate = null;
204
205 /**
206 * @var RevisionRecord|null
207 */
208 private $parentRevision = null;
209
210 /**
211 * @var RevisionRecord|null
212 */
213 private $revision = null;
214
215 /**
216 * @var RenderedRevision
217 */
218 private $renderedRevision = null;
219
220 /**
221 * @var RevisionRenderer
222 */
223 private $revisionRenderer;
224
225 /** @var SlotRoleRegistry */
226 private $slotRoleRegistry;
227
228 /**
229 * A stage identifier for managing the life cycle of this instance.
230 * Possible stages are 'new', 'knows-current', 'has-content', 'has-revision', and 'done'.
231 *
232 * @see docs/pageupdater.txt for documentation of the life cycle.
233 *
234 * @var string
235 */
236 private $stage = 'new';
237
238 /**
239 * Transition table for managing the life cycle of DerivedPageDateUpdater instances.
240 *
241 * XXX: Overkill. This is a linear order, we could just count. Names are nice though,
242 * and constants are also overkill...
243 *
244 * @see docs/pageupdater.txt for documentation of the life cycle.
245 *
246 * @var array[]
247 */
248 private static $transitions = [
249 'new' => [
250 'new' => true,
251 'knows-current' => true,
252 'has-content' => true,
253 'has-revision' => true,
254 ],
255 'knows-current' => [
256 'knows-current' => true,
257 'has-content' => true,
258 'has-revision' => true,
259 ],
260 'has-content' => [
261 'has-content' => true,
262 'has-revision' => true,
263 ],
264 'has-revision' => [
265 'has-revision' => true,
266 'done' => true,
267 ],
268 ];
269
270 /**
271 * @param WikiPage $wikiPage ,
272 * @param RevisionStore $revisionStore
273 * @param RevisionRenderer $revisionRenderer
274 * @param SlotRoleRegistry $slotRoleRegistry
275 * @param ParserCache $parserCache
276 * @param JobQueueGroup $jobQueueGroup
277 * @param MessageCache $messageCache
278 * @param Language $contLang
279 * @param ILBFactory $loadbalancerFactory
280 */
281 public function __construct(
282 WikiPage $wikiPage,
283 RevisionStore $revisionStore,
284 RevisionRenderer $revisionRenderer,
285 SlotRoleRegistry $slotRoleRegistry,
286 ParserCache $parserCache,
287 JobQueueGroup $jobQueueGroup,
288 MessageCache $messageCache,
289 Language $contLang,
290 ILBFactory $loadbalancerFactory
291 ) {
292 $this->wikiPage = $wikiPage;
293
294 $this->parserCache = $parserCache;
295 $this->revisionStore = $revisionStore;
296 $this->revisionRenderer = $revisionRenderer;
297 $this->slotRoleRegistry = $slotRoleRegistry;
298 $this->jobQueueGroup = $jobQueueGroup;
299 $this->messageCache = $messageCache;
300 $this->contLang = $contLang;
301 // XXX only needed for waiting for replicas to catch up; there should be a narrower
302 // interface for that.
303 $this->loadbalancerFactory = $loadbalancerFactory;
304 $this->logger = new NullLogger();
305 }
306
307 public function setLogger( LoggerInterface $logger ) {
308 $this->logger = $logger;
309 }
310
311 /**
312 * Transition function for managing the life cycle of this instances.
313 *
314 * @see docs/pageupdater.txt for documentation of the life cycle.
315 *
316 * @param string $newStage the new stage
317 * @return string the previous stage
318 *
319 * @throws LogicException If a transition to the given stage is not possible in the current
320 * stage.
321 */
322 private function doTransition( $newStage ) {
323 $this->assertTransition( $newStage );
324
325 $oldStage = $this->stage;
326 $this->stage = $newStage;
327
328 return $oldStage;
329 }
330
331 /**
332 * Asserts that a transition to the given stage is possible, without performing it.
333 *
334 * @see docs/pageupdater.txt for documentation of the life cycle.
335 *
336 * @param string $newStage the new stage
337 *
338 * @throws LogicException If this instance is not in the expected stage
339 */
340 private function assertTransition( $newStage ) {
341 if ( empty( self::$transitions[$this->stage][$newStage] ) ) {
342 throw new LogicException( "Cannot transition from {$this->stage} to $newStage" );
343 }
344 }
345
346 /**
347 * @return bool|string
348 */
349 private function getWikiId() {
350 // TODO: get from RevisionStore
351 return false;
352 }
353
354 /**
355 * Checks whether this DerivedPageDataUpdater can be re-used for running updates targeting
356 * the given revision.
357 *
358 * @param UserIdentity|null $user The user creating the revision in question
359 * @param RevisionRecord|null $revision New revision (after save, if already saved)
360 * @param RevisionSlotsUpdate|null $slotsUpdate New content (before PST)
361 * @param null|int $parentId Parent revision of the edit (use 0 for page creation)
362 *
363 * @return bool
364 */
365 public function isReusableFor(
366 UserIdentity $user = null,
367 RevisionRecord $revision = null,
368 RevisionSlotsUpdate $slotsUpdate = null,
369 $parentId = null
370 ) {
371 if ( $revision
372 && $parentId
373 && $revision->getParentId() !== $parentId
374 ) {
375 throw new InvalidArgumentException( '$parentId should match the parent of $revision' );
376 }
377
378 // NOTE: For null revisions, $user may be different from $this->revision->getUser
379 // and also from $revision->getUser.
380 // But $user should always match $this->user.
381 if ( $user && $this->user && $user->getName() !== $this->user->getName() ) {
382 return false;
383 }
384
385 if ( $revision && $this->revision && $this->revision->getId()
386 && $this->revision->getId() !== $revision->getId()
387 ) {
388 return false;
389 }
390
391 if ( $this->pageState
392 && $revision
393 && $revision->getParentId() !== null
394 && $this->pageState['oldId'] !== $revision->getParentId()
395 ) {
396 return false;
397 }
398
399 if ( $this->pageState
400 && $parentId !== null
401 && $this->pageState['oldId'] !== $parentId
402 ) {
403 return false;
404 }
405
406 // NOTE: this check is the primary reason for having the $this->slotsUpdate field!
407 if ( $this->slotsUpdate
408 && $slotsUpdate
409 && !$this->slotsUpdate->hasSameUpdates( $slotsUpdate )
410 ) {
411 return false;
412 }
413
414 if ( $revision
415 && $this->revision
416 && !$this->revision->getSlots()->hasSameContent( $revision->getSlots() )
417 ) {
418 return false;
419 }
420
421 return true;
422 }
423
424 /**
425 * @param string $articleCountMethod "any" or "link".
426 * @see $wgArticleCountMethod
427 */
428 public function setArticleCountMethod( $articleCountMethod ) {
429 $this->articleCountMethod = $articleCountMethod;
430 }
431
432 /**
433 * @param bool $rcWatchCategoryMembership
434 * @see $wgRCWatchCategoryMembership
435 */
436 public function setRcWatchCategoryMembership( $rcWatchCategoryMembership ) {
437 $this->rcWatchCategoryMembership = $rcWatchCategoryMembership;
438 }
439
440 /**
441 * @return Title
442 */
443 private function getTitle() {
444 // NOTE: eventually, we won't get a WikiPage passed into the constructor any more
445 return $this->wikiPage->getTitle();
446 }
447
448 /**
449 * @return WikiPage
450 */
451 private function getWikiPage() {
452 // NOTE: eventually, we won't get a WikiPage passed into the constructor any more
453 return $this->wikiPage;
454 }
455
456 /**
457 * Determines whether the page being edited already existed.
458 * Only defined after calling grabCurrentRevision() or prepareContent() or prepareUpdate()!
459 *
460 * @return bool
461 * @throws LogicException if called before grabCurrentRevision
462 */
463 public function pageExisted() {
464 $this->assertHasPageState( __METHOD__ );
465
466 return $this->pageState['oldId'] > 0;
467 }
468
469 /**
470 * Returns the parent revision of the new revision wrapped by this update.
471 * If the update is a null-edit, this will return the parent of the current (and new) revision.
472 * This will return null if the revision wrapped by this update created the page.
473 * Only defined after calling prepareContent() or prepareUpdate()!
474 *
475 * @return RevisionRecord|null the parent revision of the new revision, or null if
476 * the update created the page.
477 */
478 private function getParentRevision() {
479 $this->assertPrepared( __METHOD__ );
480
481 if ( $this->parentRevision ) {
482 return $this->parentRevision;
483 }
484
485 if ( !$this->pageState['oldId'] ) {
486 // If there was no current revision, there is no parent revision,
487 // since the page didn't exist.
488 return null;
489 }
490
491 $oldId = $this->revision->getParentId();
492 $flags = $this->useMaster() ? RevisionStore::READ_LATEST : 0;
493 $this->parentRevision = $oldId
494 ? $this->revisionStore->getRevisionById( $oldId, $flags )
495 : null;
496
497 return $this->parentRevision;
498 }
499
500 /**
501 * Returns the revision that was the page's current revision when grabCurrentRevision()
502 * was first called.
503 *
504 * During an edit, that revision will act as the logical parent of the new revision.
505 *
506 * Some updates are performed based on the difference between the database state at the
507 * moment this method is first called, and the state after the edit.
508 *
509 * @see docs/pageupdater.txt for more information on when thie method can and should be called.
510 *
511 * @note After prepareUpdate() was called, grabCurrentRevision() will throw an exception
512 * to avoid confusion, since the page's current revision is then the new revision after
513 * the edit, which was presumably passed to prepareUpdate() as the $revision parameter.
514 * Use getParentRevision() instead to access the revision that is the parent of the
515 * new revision.
516 *
517 * @return RevisionRecord|null the page's current revision, or null if the page does not
518 * yet exist.
519 */
520 public function grabCurrentRevision() {
521 if ( $this->pageState ) {
522 return $this->pageState['oldRevision'];
523 }
524
525 $this->assertTransition( 'knows-current' );
526
527 // NOTE: eventually, we won't get a WikiPage passed into the constructor any more
528 $wikiPage = $this->getWikiPage();
529
530 // Do not call WikiPage::clear(), since the caller may already have caused page data
531 // to be loaded with SELECT FOR UPDATE. Just assert it's loaded now.
532 $wikiPage->loadPageData( self::READ_LATEST );
533 $rev = $wikiPage->getRevision();
534 $current = $rev ? $rev->getRevisionRecord() : null;
535
536 $this->pageState = [
537 'oldRevision' => $current,
538 'oldId' => $rev ? $rev->getId() : 0,
539 'oldIsRedirect' => $wikiPage->isRedirect(), // NOTE: uses page table
540 'oldCountable' => $wikiPage->isCountable(), // NOTE: uses pagelinks table
541 ];
542
543 $this->doTransition( 'knows-current' );
544
545 return $this->pageState['oldRevision'];
546 }
547
548 /**
549 * Whether prepareUpdate() or prepareContent() have been called on this instance.
550 *
551 * @return bool
552 */
553 public function isContentPrepared() {
554 return $this->revision !== null;
555 }
556
557 /**
558 * Whether prepareUpdate() has been called on this instance.
559 *
560 * @note will also return null in case of a null-edit!
561 *
562 * @return bool
563 */
564 public function isUpdatePrepared() {
565 return $this->revision !== null && $this->revision->getId() !== null;
566 }
567
568 /**
569 * @return int
570 */
571 private function getPageId() {
572 // NOTE: eventually, we won't get a WikiPage passed into the constructor any more
573 return $this->wikiPage->getId();
574 }
575
576 /**
577 * Whether the content is deleted and thus not visible to the public.
578 *
579 * @return bool
580 */
581 public function isContentDeleted() {
582 if ( $this->revision ) {
583 // XXX: if that revision is the current revision, this should be skipped
584 return $this->revision->isDeleted( RevisionRecord::DELETED_TEXT );
585 } else {
586 // If the content has not been saved yet, it cannot have been deleted yet.
587 return false;
588 }
589 }
590
591 /**
592 * Returns the slot, modified or inherited, after PST, with no audience checks applied.
593 *
594 * @param string $role slot role name
595 *
596 * @throws PageUpdateException If the slot is neither set for update nor inherited from the
597 * parent revision.
598 * @return SlotRecord
599 */
600 public function getRawSlot( $role ) {
601 return $this->getSlots()->getSlot( $role );
602 }
603
604 /**
605 * Returns the content of the given slot, with no audience checks.
606 *
607 * @throws PageUpdateException If the slot is neither set for update nor inherited from the
608 * parent revision.
609 * @param string $role slot role name
610 * @return Content
611 */
612 public function getRawContent( $role ) {
613 return $this->getRawSlot( $role )->getContent();
614 }
615
616 /**
617 * Returns the content model of the given slot
618 *
619 * @param string $role slot role name
620 * @return string
621 */
622 private function getContentModel( $role ) {
623 return $this->getRawSlot( $role )->getModel();
624 }
625
626 /**
627 * @param string $role slot role name
628 * @return ContentHandler
629 */
630 private function getContentHandler( $role ) {
631 // TODO: inject something like a ContentHandlerRegistry
632 return ContentHandler::getForModelID( $this->getContentModel( $role ) );
633 }
634
635 private function useMaster() {
636 // TODO: can we just set a flag to true in prepareContent()?
637 return $this->wikiPage->wasLoadedFrom( self::READ_LATEST );
638 }
639
640 /**
641 * @return bool
642 */
643 public function isCountable() {
644 // NOTE: Keep in sync with WikiPage::isCountable.
645
646 if ( !$this->getTitle()->isContentPage() ) {
647 return false;
648 }
649
650 if ( $this->isContentDeleted() ) {
651 // This should be irrelevant: countability only applies to the current revision,
652 // and the current revision is never suppressed.
653 return false;
654 }
655
656 if ( $this->isRedirect() ) {
657 return false;
658 }
659
660 $hasLinks = null;
661
662 if ( $this->articleCountMethod === 'link' ) {
663 // NOTE: it would be more appropriate to determine for each slot separately
664 // whether it has links, and use that information with that slot's
665 // isCountable() method. However, that would break parity with
666 // WikiPage::isCountable, which uses the pagelinks table to determine
667 // whether the current revision has links.
668 $hasLinks = (bool)count( $this->getCanonicalParserOutput()->getLinks() );
669 }
670
671 foreach ( $this->getModifiedSlotRoles() as $role ) {
672 $roleHandler = $this->slotRoleRegistry->getRoleHandler( $role );
673 if ( $roleHandler->supportsArticleCount() ) {
674 $content = $this->getRawContent( $role );
675
676 if ( $content->isCountable( $hasLinks ) ) {
677 return true;
678 }
679 }
680 }
681
682 return false;
683 }
684
685 /**
686 * @return bool
687 */
688 public function isRedirect() {
689 // NOTE: main slot determines redirect status
690 // TODO: MCR: this should be controlled by a PageTypeHandler
691 $mainContent = $this->getRawContent( SlotRecord::MAIN );
692
693 return $mainContent->isRedirect();
694 }
695
696 /**
697 * @param RevisionRecord $rev
698 *
699 * @return bool
700 */
701 private function revisionIsRedirect( RevisionRecord $rev ) {
702 // NOTE: main slot determines redirect status
703 $mainContent = $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
704
705 return $mainContent->isRedirect();
706 }
707
708 /**
709 * Prepare updates based on an update which has not yet been saved.
710 *
711 * This may be used to create derived data that is needed when creating a new revision;
712 * particularly, this makes available the slots of the new revision via the getSlots()
713 * method, after applying PST and slot inheritance.
714 *
715 * The derived data prepared for revision creation may then later be re-used by doUpdates(),
716 * without the need to re-calculate.
717 *
718 * @see docs/pageupdater.txt for more information on when thie method can and should be called.
719 *
720 * @note Calling this method more than once with the same $slotsUpdate
721 * has no effect. Calling this method multiple times with different content will cause
722 * an exception.
723 *
724 * @note Calling this method after prepareUpdate() has been called will cause an exception.
725 *
726 * @param User $user The user to act as context for pre-save transformation (PST).
727 * Type hint should be reduced to UserIdentity at some point.
728 * @param RevisionSlotsUpdate $slotsUpdate The new content of the slots to be updated
729 * by this edit, before PST.
730 * @param bool $useStash Whether to use stashed ParserOutput
731 */
732 public function prepareContent(
733 User $user,
734 RevisionSlotsUpdate $slotsUpdate,
735 $useStash = true
736 ) {
737 if ( $this->slotsUpdate ) {
738 if ( !$this->user ) {
739 throw new LogicException(
740 'Unexpected state: $this->slotsUpdate was initialized, '
741 . 'but $this->user was not.'
742 );
743 }
744
745 if ( $this->user->getName() !== $user->getName() ) {
746 throw new LogicException( 'Can\'t call prepareContent() again for different user! '
747 . 'Expected ' . $this->user->getName() . ', got ' . $user->getName()
748 );
749 }
750
751 if ( !$this->slotsUpdate->hasSameUpdates( $slotsUpdate ) ) {
752 throw new LogicException(
753 'Can\'t call prepareContent() again with different slot content!'
754 );
755 }
756
757 return; // prepareContent() already done, nothing to do
758 }
759
760 $this->assertTransition( 'has-content' );
761
762 $wikiPage = $this->getWikiPage(); // TODO: use only for legacy hooks!
763 $title = $this->getTitle();
764
765 $parentRevision = $this->grabCurrentRevision();
766
767 // The edit may have already been prepared via api.php?action=stashedit
768 $stashedEdit = false;
769
770 // TODO: MCR: allow output for all slots to be stashed.
771 if ( $useStash && $slotsUpdate->isModifiedSlot( SlotRecord::MAIN ) ) {
772 $editStash = MediaWikiServices::getInstance()->getPageEditStash();
773 $stashedEdit = $editStash->checkCache(
774 $title,
775 $slotsUpdate->getModifiedSlot( SlotRecord::MAIN )->getContent(),
776 User::newFromIdentity( $user )
777 );
778 }
779
780 $userPopts = ParserOptions::newFromUserAndLang( $user, $this->contLang );
781 Hooks::run( 'ArticlePrepareTextForEdit', [ $wikiPage, $userPopts ] );
782
783 $this->user = $user;
784 $this->slotsUpdate = $slotsUpdate;
785
786 if ( $parentRevision ) {
787 $this->revision = MutableRevisionRecord::newFromParentRevision( $parentRevision );
788 } else {
789 $this->revision = new MutableRevisionRecord( $title );
790 }
791
792 // NOTE: user and timestamp must be set, so they can be used for
793 // {{subst:REVISIONUSER}} and {{subst:REVISIONTIMESTAMP}} in PST!
794 $this->revision->setTimestamp( wfTimestampNow() );
795 $this->revision->setUser( $user );
796
797 // Set up ParserOptions to operate on the new revision
798 $oldCallback = $userPopts->getCurrentRevisionCallback();
799 $userPopts->setCurrentRevisionCallback(
800 function ( Title $parserTitle, $parser = false ) use ( $title, $oldCallback ) {
801 if ( $parserTitle->equals( $title ) ) {
802 $legacyRevision = new Revision( $this->revision );
803 return $legacyRevision;
804 } else {
805 return call_user_func( $oldCallback, $parserTitle, $parser );
806 }
807 }
808 );
809
810 $pstContentSlots = $this->revision->getSlots();
811
812 foreach ( $slotsUpdate->getModifiedRoles() as $role ) {
813 $slot = $slotsUpdate->getModifiedSlot( $role );
814
815 if ( $slot->isInherited() ) {
816 // No PST for inherited slots! Note that "modified" slots may still be inherited
817 // from an earlier version, e.g. for rollbacks.
818 $pstSlot = $slot;
819 } elseif ( $role === SlotRecord::MAIN && $stashedEdit ) {
820 // TODO: MCR: allow PST content for all slots to be stashed.
821 $pstSlot = SlotRecord::newUnsaved( $role, $stashedEdit->pstContent );
822 } else {
823 $content = $slot->getContent();
824 $pstContent = $content->preSaveTransform( $title, $this->user, $userPopts );
825 $pstSlot = SlotRecord::newUnsaved( $role, $pstContent );
826 }
827
828 $pstContentSlots->setSlot( $pstSlot );
829 }
830
831 foreach ( $slotsUpdate->getRemovedRoles() as $role ) {
832 $pstContentSlots->removeSlot( $role );
833 }
834
835 $this->options['created'] = ( $parentRevision === null );
836 $this->options['changed'] = ( $parentRevision === null
837 || !$pstContentSlots->hasSameContent( $parentRevision->getSlots() ) );
838
839 $this->doTransition( 'has-content' );
840
841 if ( !$this->options['changed'] ) {
842 // null-edit!
843
844 // TODO: move this into MutableRevisionRecord
845 // TODO: This needs to behave differently for a forced dummy edit!
846 $this->revision->setId( $parentRevision->getId() );
847 $this->revision->setTimestamp( $parentRevision->getTimestamp() );
848 $this->revision->setPageId( $parentRevision->getPageId() );
849 $this->revision->setParentId( $parentRevision->getParentId() );
850 $this->revision->setUser( $parentRevision->getUser( RevisionRecord::RAW ) );
851 $this->revision->setComment( $parentRevision->getComment( RevisionRecord::RAW ) );
852 $this->revision->setMinorEdit( $parentRevision->isMinor() );
853 $this->revision->setVisibility( $parentRevision->getVisibility() );
854
855 // prepareUpdate() is redundant for null-edits
856 $this->doTransition( 'has-revision' );
857 } else {
858 $this->parentRevision = $parentRevision;
859 }
860
861 $renderHints = [ 'use-master' => $this->useMaster(), 'audience' => RevisionRecord::RAW ];
862
863 if ( $stashedEdit ) {
864 /** @var ParserOutput $output */
865 $output = $stashedEdit->output;
866 // TODO: this should happen when stashing the ParserOutput, not now!
867 $output->setCacheTime( $stashedEdit->timestamp );
868
869 $renderHints['known-revision-output'] = $output;
870
871 $this->logger->debug( __METHOD__ . ': using stashed edit output...' );
872 }
873
874 // NOTE: we want a canonical rendering, so don't pass $this->user or ParserOptions
875 // NOTE: the revision is either new or current, so we can bypass audience checks.
876 $this->renderedRevision = $this->revisionRenderer->getRenderedRevision(
877 $this->revision,
878 null,
879 null,
880 $renderHints
881 );
882 }
883
884 /**
885 * Returns the update's target revision - that is, the revision that will be the current
886 * revision after the update.
887 *
888 * @note Callers must treat the returned RevisionRecord's content as immutable, even
889 * if it is a MutableRevisionRecord instance. Other aspects of a MutableRevisionRecord
890 * returned from here, such as the user or the comment, may be changed, but may not
891 * be reflected in ParserOutput until after prepareUpdate() has been called.
892 *
893 * @todo This is currently used by PageUpdater::makeNewRevision() to construct an unsaved
894 * MutableRevisionRecord instance. Introduce something like an UnsavedRevisionFactory service
895 * for that purpose instead!
896 *
897 * @return RevisionRecord
898 */
899 public function getRevision() {
900 $this->assertPrepared( __METHOD__ );
901 return $this->revision;
902 }
903
904 /**
905 * @return RenderedRevision
906 */
907 public function getRenderedRevision() {
908 $this->assertPrepared( __METHOD__ );
909
910 return $this->renderedRevision;
911 }
912
913 private function assertHasPageState( $method ) {
914 if ( !$this->pageState ) {
915 throw new LogicException(
916 'Must call grabCurrentRevision() or prepareContent() '
917 . 'or prepareUpdate() before calling ' . $method
918 );
919 }
920 }
921
922 private function assertPrepared( $method ) {
923 if ( !$this->revision ) {
924 throw new LogicException(
925 'Must call prepareContent() or prepareUpdate() before calling ' . $method
926 );
927 }
928 }
929
930 private function assertHasRevision( $method ) {
931 if ( !$this->revision->getId() ) {
932 throw new LogicException(
933 'Must call prepareUpdate() before calling ' . $method
934 );
935 }
936 }
937
938 /**
939 * Whether the edit creates the page.
940 *
941 * @return bool
942 */
943 public function isCreation() {
944 $this->assertPrepared( __METHOD__ );
945 return $this->options['created'];
946 }
947
948 /**
949 * Whether the edit created, or should create, a new revision (that is, it's not a null-edit).
950 *
951 * @warning at present, "null-revisions" that do not change content but do have a revision
952 * record would return false after prepareContent(), but true after prepareUpdate()!
953 * This should probably be fixed.
954 *
955 * @return bool
956 */
957 public function isChange() {
958 $this->assertPrepared( __METHOD__ );
959 return $this->options['changed'];
960 }
961
962 /**
963 * Whether the page was a redirect before the edit.
964 *
965 * @return bool
966 */
967 public function wasRedirect() {
968 $this->assertHasPageState( __METHOD__ );
969
970 if ( $this->pageState['oldIsRedirect'] === null ) {
971 /** @var RevisionRecord $rev */
972 $rev = $this->pageState['oldRevision'];
973 if ( $rev ) {
974 $this->pageState['oldIsRedirect'] = $this->revisionIsRedirect( $rev );
975 } else {
976 $this->pageState['oldIsRedirect'] = false;
977 }
978 }
979
980 return $this->pageState['oldIsRedirect'];
981 }
982
983 /**
984 * Returns the slots of the target revision, after PST.
985 *
986 * @note Callers must treat the returned RevisionSlots instance as immutable, even
987 * if it is a MutableRevisionSlots instance.
988 *
989 * @return RevisionSlots
990 */
991 public function getSlots() {
992 $this->assertPrepared( __METHOD__ );
993 return $this->revision->getSlots();
994 }
995
996 /**
997 * Returns the RevisionSlotsUpdate for this updater.
998 *
999 * @return RevisionSlotsUpdate
1000 */
1001 private function getRevisionSlotsUpdate() {
1002 $this->assertPrepared( __METHOD__ );
1003
1004 if ( !$this->slotsUpdate ) {
1005 $old = $this->getParentRevision();
1006 $this->slotsUpdate = RevisionSlotsUpdate::newFromRevisionSlots(
1007 $this->revision->getSlots(),
1008 $old ? $old->getSlots() : null
1009 );
1010 }
1011 return $this->slotsUpdate;
1012 }
1013
1014 /**
1015 * Returns the role names of the slots touched by the new revision,
1016 * including removed roles.
1017 *
1018 * @return string[]
1019 */
1020 public function getTouchedSlotRoles() {
1021 return $this->getRevisionSlotsUpdate()->getTouchedRoles();
1022 }
1023
1024 /**
1025 * Returns the role names of the slots modified by the new revision,
1026 * not including removed roles.
1027 *
1028 * @return string[]
1029 */
1030 public function getModifiedSlotRoles() {
1031 return $this->getRevisionSlotsUpdate()->getModifiedRoles();
1032 }
1033
1034 /**
1035 * Returns the role names of the slots removed by the new revision.
1036 *
1037 * @return string[]
1038 */
1039 public function getRemovedSlotRoles() {
1040 return $this->getRevisionSlotsUpdate()->getRemovedRoles();
1041 }
1042
1043 /**
1044 * Prepare derived data updates targeting the given Revision.
1045 *
1046 * Calling this method requires the given revision to be present in the database.
1047 * This may be right after a new revision has been created, or when re-generating
1048 * derived data e.g. in ApiPurge, RefreshLinksJob, and the refreshLinks
1049 * script.
1050 *
1051 * @see docs/pageupdater.txt for more information on when thie method can and should be called.
1052 *
1053 * @note Calling this method more than once with the same revision has no effect.
1054 * $options are only used for the first call. Calling this method multiple times with
1055 * different revisions will cause an exception.
1056 *
1057 * @note If grabCurrentRevision() (or prepareContent()) has been called before
1058 * calling this method, $revision->getParentRevision() has to refer to the revision that
1059 * was the current revision at the time grabCurrentRevision() was called.
1060 *
1061 * @param RevisionRecord $revision
1062 * @param array $options Array of options, following indexes are used:
1063 * - changed: bool, whether the revision changed the content (default true)
1064 * - created: bool, whether the revision created the page (default false)
1065 * - moved: bool, whether the page was moved (default false)
1066 * - restored: bool, whether the page was undeleted (default false)
1067 * - oldrevision: Revision object for the pre-update revision (default null)
1068 * - triggeringUser: The user triggering the update (UserIdentity, defaults to the
1069 * user who created the revision)
1070 * - oldredirect: bool, null, or string 'no-change' (default null):
1071 * - bool: whether the page was counted as a redirect before that
1072 * revision, only used in changed is true and created is false
1073 * - null or 'no-change': don't update the redirect status.
1074 * - oldcountable: bool, null, or string 'no-change' (default null):
1075 * - bool: whether the page was counted as an article before that
1076 * revision, only used in changed is true and created is false
1077 * - null: if created is false, don't update the article count; if created
1078 * is true, do update the article count
1079 * - 'no-change': don't update the article count, ever
1080 * When set to null, pageState['oldCountable'] will be used instead if available.
1081 * - causeAction: an arbitrary string identifying the reason for the update.
1082 * See DataUpdate::getCauseAction(). (default 'unknown')
1083 * - causeAgent: name of the user who caused the update. See DataUpdate::getCauseAgent().
1084 * (string, default 'unknown')
1085 */
1086 public function prepareUpdate( RevisionRecord $revision, array $options = [] ) {
1087 Assert::parameter(
1088 !isset( $options['oldrevision'] )
1089 || $options['oldrevision'] instanceof Revision
1090 || $options['oldrevision'] instanceof RevisionRecord,
1091 '$options["oldrevision"]',
1092 'must be a RevisionRecord (or Revision)'
1093 );
1094 Assert::parameter(
1095 !isset( $options['triggeringUser'] )
1096 || $options['triggeringUser'] instanceof UserIdentity,
1097 '$options["triggeringUser"]',
1098 'must be a UserIdentity'
1099 );
1100
1101 if ( !$revision->getId() ) {
1102 throw new InvalidArgumentException(
1103 'Revision must have an ID set for it to be used with prepareUpdate()!'
1104 );
1105 }
1106
1107 if ( $this->revision && $this->revision->getId() ) {
1108 if ( $this->revision->getId() === $revision->getId() ) {
1109 return; // nothing to do!
1110 } else {
1111 throw new LogicException(
1112 'Trying to re-use DerivedPageDataUpdater with revision '
1113 . $revision->getId()
1114 . ', but it\'s already bound to revision '
1115 . $this->revision->getId()
1116 );
1117 }
1118 }
1119
1120 if ( $this->revision
1121 && !$this->revision->getSlots()->hasSameContent( $revision->getSlots() )
1122 ) {
1123 throw new LogicException(
1124 'The Revision provided has mismatching content!'
1125 );
1126 }
1127
1128 // Override fields defined in $this->options with values from $options.
1129 $this->options = array_intersect_key( $options, $this->options ) + $this->options;
1130
1131 if ( $this->revision ) {
1132 $oldId = $this->pageState['oldId'] ?? 0;
1133 $this->options['newrev'] = ( $revision->getId() !== $oldId );
1134 } elseif ( isset( $this->options['oldrevision'] ) ) {
1135 /** @var Revision|RevisionRecord $oldRev */
1136 $oldRev = $this->options['oldrevision'];
1137 $oldId = $oldRev->getId();
1138 $this->options['newrev'] = ( $revision->getId() !== $oldId );
1139 } else {
1140 $oldId = $revision->getParentId();
1141 }
1142
1143 if ( $oldId !== null ) {
1144 // XXX: what if $options['changed'] disagrees?
1145 // MovePage creates a dummy revision with changed = false!
1146 // We may want to explicitly distinguish between "no new revision" (null-edit)
1147 // and "new revision without new content" (dummy revision).
1148
1149 if ( $oldId === $revision->getParentId() ) {
1150 // NOTE: this may still be a NullRevision!
1151 // New revision!
1152 $this->options['changed'] = true;
1153 } elseif ( $oldId === $revision->getId() ) {
1154 // Null-edit!
1155 $this->options['changed'] = false;
1156 } else {
1157 // This indicates that calling code has given us the wrong Revision object
1158 throw new LogicException(
1159 'The Revision mismatches old revision ID: '
1160 . 'Old ID is ' . $oldId
1161 . ', parent ID is ' . $revision->getParentId()
1162 . ', revision ID is ' . $revision->getId()
1163 );
1164 }
1165 }
1166
1167 // If prepareContent() was used to generate the PST content (which is indicated by
1168 // $this->slotsUpdate being set), and this is not a null-edit, then the given
1169 // revision must have the acting user as the revision author. Otherwise, user
1170 // signatures generated by PST would mismatch the user in the revision record.
1171 if ( $this->user !== null && $this->options['changed'] && $this->slotsUpdate ) {
1172 $user = $revision->getUser();
1173 if ( !$this->user->equals( $user ) ) {
1174 throw new LogicException(
1175 'The Revision provided has a mismatching actor: expected '
1176 . $this->user->getName()
1177 . ', got '
1178 . $user->getName()
1179 );
1180 }
1181 }
1182
1183 // If $this->pageState was not yet initialized by grabCurrentRevision or prepareContent,
1184 // emulate the state of the page table before the edit, as good as we can.
1185 if ( !$this->pageState ) {
1186 $this->pageState = [
1187 'oldIsRedirect' => isset( $this->options['oldredirect'] )
1188 && is_bool( $this->options['oldredirect'] )
1189 ? $this->options['oldredirect']
1190 : null,
1191 'oldCountable' => isset( $this->options['oldcountable'] )
1192 && is_bool( $this->options['oldcountable'] )
1193 ? $this->options['oldcountable']
1194 : null,
1195 ];
1196
1197 if ( $this->options['changed'] ) {
1198 // The edit created a new revision
1199 $this->pageState['oldId'] = $revision->getParentId();
1200
1201 if ( isset( $this->options['oldrevision'] ) ) {
1202 $rev = $this->options['oldrevision'];
1203 $this->pageState['oldRevision'] = $rev instanceof Revision
1204 ? $rev->getRevisionRecord()
1205 : $rev;
1206 }
1207 } else {
1208 // This is a null-edit, so the old revision IS the new revision!
1209 $this->pageState['oldId'] = $revision->getId();
1210 $this->pageState['oldRevision'] = $revision;
1211 }
1212 }
1213
1214 // "created" is forced here
1215 $this->options['created'] = ( $this->pageState['oldId'] === 0 );
1216
1217 $this->revision = $revision;
1218
1219 $this->doTransition( 'has-revision' );
1220
1221 // NOTE: in case we have a User object, don't override with a UserIdentity.
1222 // We already checked that $revision->getUser() mathces $this->user;
1223 if ( !$this->user ) {
1224 $this->user = $revision->getUser( RevisionRecord::RAW );
1225 }
1226
1227 // Prune any output that depends on the revision ID.
1228 if ( $this->renderedRevision ) {
1229 $this->renderedRevision->updateRevision( $revision );
1230 } else {
1231
1232 // NOTE: we want a canonical rendering, so don't pass $this->user or ParserOptions
1233 // NOTE: the revision is either new or current, so we can bypass audience checks.
1234 $this->renderedRevision = $this->revisionRenderer->getRenderedRevision(
1235 $this->revision,
1236 null,
1237 null,
1238 [ 'use-master' => $this->useMaster(), 'audience' => RevisionRecord::RAW ]
1239 );
1240
1241 // XXX: Since we presumably are dealing with the current revision,
1242 // we could try to get the ParserOutput from the parser cache.
1243 }
1244
1245 // TODO: optionally get ParserOutput from the ParserCache here.
1246 // Move the logic used by RefreshLinksJob here!
1247 }
1248
1249 /**
1250 * @deprecated This only exists for B/C, use the getters on DerivedPageDataUpdater directly!
1251 * @return PreparedEdit
1252 */
1253 public function getPreparedEdit() {
1254 $this->assertPrepared( __METHOD__ );
1255
1256 $slotsUpdate = $this->getRevisionSlotsUpdate();
1257 $preparedEdit = new PreparedEdit();
1258
1259 $preparedEdit->popts = $this->getCanonicalParserOptions();
1260 $preparedEdit->parserOutputCallback = [ $this, 'getCanonicalParserOutput' ];
1261 $preparedEdit->pstContent = $this->revision->getContent( SlotRecord::MAIN );
1262 $preparedEdit->newContent =
1263 $slotsUpdate->isModifiedSlot( SlotRecord::MAIN )
1264 ? $slotsUpdate->getModifiedSlot( SlotRecord::MAIN )->getContent()
1265 : $this->revision->getContent( SlotRecord::MAIN ); // XXX: can we just remove this?
1266 $preparedEdit->oldContent = null; // unused. // XXX: could get this from the parent revision
1267 $preparedEdit->revid = $this->revision ? $this->revision->getId() : null;
1268 $preparedEdit->timestamp = $preparedEdit->output->getCacheTime();
1269 $preparedEdit->format = $preparedEdit->pstContent->getDefaultFormat();
1270
1271 return $preparedEdit;
1272 }
1273
1274 /**
1275 * @param string $role
1276 * @param bool $generateHtml
1277 * @return ParserOutput
1278 */
1279 public function getSlotParserOutput( $role, $generateHtml = true ) {
1280 return $this->getRenderedRevision()->getSlotParserOutput(
1281 $role,
1282 [ 'generate-html' => $generateHtml ]
1283 );
1284 }
1285
1286 /**
1287 * @return ParserOutput
1288 */
1289 public function getCanonicalParserOutput() {
1290 return $this->getRenderedRevision()->getRevisionParserOutput();
1291 }
1292
1293 /**
1294 * @return ParserOptions
1295 */
1296 public function getCanonicalParserOptions() {
1297 return $this->getRenderedRevision()->getOptions();
1298 }
1299
1300 /**
1301 * @param bool $recursive
1302 *
1303 * @return DeferrableUpdate[]
1304 */
1305 public function getSecondaryDataUpdates( $recursive = false ) {
1306 if ( $this->isContentDeleted() ) {
1307 // This shouldn't happen, since the current content is always public,
1308 // and DataUpates are only needed for current content.
1309 return [];
1310 }
1311
1312 $output = $this->getCanonicalParserOutput();
1313
1314 // Construct a LinksUpdate for the combined canonical output.
1315 $linksUpdate = new LinksUpdate(
1316 $this->getTitle(),
1317 $output,
1318 $recursive
1319 );
1320
1321 $allUpdates = [ $linksUpdate ];
1322
1323 // NOTE: Run updates for all slots, not just the modified slots! Otherwise,
1324 // info for an inherited slot may end up being removed. This is also needed
1325 // to ensure that purges are effective.
1326 $renderedRevision = $this->getRenderedRevision();
1327 foreach ( $this->getSlots()->getSlotRoles() as $role ) {
1328 $slot = $this->getRawSlot( $role );
1329 $content = $slot->getContent();
1330 $handler = $content->getContentHandler();
1331
1332 $updates = $handler->getSecondaryDataUpdates(
1333 $this->getTitle(),
1334 $content,
1335 $role,
1336 $renderedRevision
1337 );
1338 $allUpdates = array_merge( $allUpdates, $updates );
1339
1340 // TODO: remove B/C hack in 1.32!
1341 // NOTE: we assume that the combined output contains all relevant meta-data for
1342 // all slots!
1343 $legacyUpdates = $content->getSecondaryDataUpdates(
1344 $this->getTitle(),
1345 null,
1346 $recursive,
1347 $output
1348 );
1349
1350 // HACK: filter out redundant and incomplete LinksUpdates
1351 $legacyUpdates = array_filter( $legacyUpdates, function ( $update ) {
1352 return !( $update instanceof LinksUpdate );
1353 } );
1354
1355 $allUpdates = array_merge( $allUpdates, $legacyUpdates );
1356 }
1357
1358 // XXX: if a slot was removed by an earlier edit, but deletion updates failed to run at
1359 // that time, we don't know for which slots to run deletion updates when purging a page.
1360 // We'd have to examine the entire history of the page to determine that. Perhaps there
1361 // could be a "try extra hard" mode for that case that would run a DB query to find all
1362 // roles/models ever used on the page. On the other hand, removing slots should be quite
1363 // rare, so perhaps this isn't worth the trouble.
1364
1365 // TODO: consolidate with similar logic in WikiPage::getDeletionUpdates()
1366 $wikiPage = $this->getWikiPage();
1367 $parentRevision = $this->getParentRevision();
1368 foreach ( $this->getRemovedSlotRoles() as $role ) {
1369 // HACK: we should get the content model of the removed slot from a SlotRoleHandler!
1370 // For now, find the slot in the parent revision - if the slot was removed, it should
1371 // always exist in the parent revision.
1372 $parentSlot = $parentRevision->getSlot( $role, RevisionRecord::RAW );
1373 $content = $parentSlot->getContent();
1374 $handler = $content->getContentHandler();
1375
1376 $updates = $handler->getDeletionUpdates(
1377 $this->getTitle(),
1378 $role
1379 );
1380 $allUpdates = array_merge( $allUpdates, $updates );
1381
1382 // TODO: remove B/C hack in 1.32!
1383 $legacyUpdates = $content->getDeletionUpdates( $wikiPage );
1384
1385 // HACK: filter out redundant and incomplete LinksDeletionUpdate
1386 $legacyUpdates = array_filter( $legacyUpdates, function ( $update ) {
1387 return !( $update instanceof LinksDeletionUpdate );
1388 } );
1389
1390 $allUpdates = array_merge( $allUpdates, $legacyUpdates );
1391 }
1392
1393 // TODO: hard deprecate SecondaryDataUpdates in favor of RevisionDataUpdates in 1.33!
1394 Hooks::run(
1395 'RevisionDataUpdates',
1396 [ $this->getTitle(), $renderedRevision, &$allUpdates ]
1397 );
1398
1399 return $allUpdates;
1400 }
1401
1402 /**
1403 * Do standard updates after page edit, purge, or import.
1404 * Update links tables, site stats, search index, title cache, message cache, etc.
1405 * Purges pages that depend on this page when appropriate.
1406 * With a 10% chance, triggers pruning the recent changes table.
1407 *
1408 * @note prepareUpdate() must be called before calling this method!
1409 *
1410 * MCR migration note: this replaces WikiPage::doEditUpdates.
1411 */
1412 public function doUpdates() {
1413 $this->assertTransition( 'done' );
1414
1415 // TODO: move logic into a PageEventEmitter service
1416
1417 $wikiPage = $this->getWikiPage(); // TODO: use only for legacy hooks!
1418
1419 $legacyUser = User::newFromIdentity( $this->user );
1420 $legacyRevision = new Revision( $this->revision );
1421
1422 $userParserOptions = ParserOptions::newFromUser( $legacyUser );
1423 // Decide whether to save the final canonical parser ouput based on the fact that
1424 // users are typically redirected to viewing pages right after they edit those pages.
1425 // Due to vary-revision-id, getting/saving that output here might require a reparse.
1426 if ( $userParserOptions->matchesForCacheKey( $this->getCanonicalParserOptions() ) ) {
1427 // Whether getting the final output requires a reparse or not, the user will
1428 // need canonical output anyway, since that is what their parser options use.
1429 // A reparse now at least has the benefit of various warm process caches.
1430 $this->doParserCacheUpdate();
1431 } else {
1432 // If the user does not have canonical parse options, then don't risk another parse
1433 // to make output they cannot use on the page refresh that typically occurs after
1434 // editing. Doing the parser output save post-send will still benefit *other* users.
1435 DeferredUpdates::addCallableUpdate( function () {
1436 $this->doParserCacheUpdate();
1437 } );
1438 }
1439
1440 // Defer the getCannonicalParserOutput() call triggered by getSecondaryDataUpdates()
1441 // by wrapping the code that schedules the secondary updates in a callback itself
1442 $wrapperUpdate = new MWCallableUpdate(
1443 function () {
1444 $this->doSecondaryDataUpdates( [
1445 // T52785 do not update any other pages on a null edit
1446 'recursive' => $this->options['changed']
1447 ] );
1448 },
1449 __METHOD__
1450 );
1451 $wrapperUpdate->setTransactionRoundRequirement( $wrapperUpdate::TRX_ROUND_ABSENT );
1452 DeferredUpdates::addUpdate( $wrapperUpdate );
1453
1454 // TODO: MCR: check if *any* changed slot supports categories!
1455 if ( $this->rcWatchCategoryMembership
1456 && $this->getContentHandler( SlotRecord::MAIN )->supportsCategories() === true
1457 && ( $this->options['changed'] || $this->options['created'] )
1458 && !$this->options['restored']
1459 ) {
1460 // Note: jobs are pushed after deferred updates, so the job should be able to see
1461 // the recent change entry (also done via deferred updates) and carry over any
1462 // bot/deletion/IP flags, ect.
1463 $this->jobQueueGroup->lazyPush(
1464 CategoryMembershipChangeJob::newSpec(
1465 $this->getTitle(),
1466 $this->revision->getTimestamp()
1467 )
1468 );
1469 }
1470
1471 // TODO: replace legacy hook! Use a listener on PageEventEmitter instead!
1472 // @note: Extensions should *avoid* calling getCannonicalParserOutput() when using
1473 // this hook whenever possible in order to avoid unnecessary additional parses.
1474 $editInfo = $this->getPreparedEdit();
1475 Hooks::run( 'ArticleEditUpdates',
1476 [ &$wikiPage, &$editInfo, $this->options['changed'] ] );
1477
1478 // TODO: replace legacy hook! Use a listener on PageEventEmitter instead!
1479 if ( Hooks::run( 'ArticleEditUpdatesDeleteFromRecentchanges', [ &$wikiPage ] ) ) {
1480 // Flush old entries from the `recentchanges` table
1481 if ( mt_rand( 0, 9 ) == 0 ) {
1482 $this->jobQueueGroup->lazyPush( RecentChangesUpdateJob::newPurgeJob() );
1483 }
1484 }
1485
1486 $id = $this->getPageId();
1487 $title = $this->getTitle();
1488 $dbKey = $title->getPrefixedDBkey();
1489 $shortTitle = $title->getDBkey();
1490
1491 if ( !$title->exists() ) {
1492 wfDebug( __METHOD__ . ": Page doesn't exist any more, bailing out\n" );
1493
1494 $this->doTransition( 'done' );
1495 return;
1496 }
1497
1498 DeferredUpdates::addCallableUpdate( function () {
1499 if (
1500 $this->options['oldcountable'] === 'no-change' ||
1501 ( !$this->options['changed'] && !$this->options['moved'] )
1502 ) {
1503 $good = 0;
1504 } elseif ( $this->options['created'] ) {
1505 $good = (int)$this->isCountable();
1506 } elseif ( $this->options['oldcountable'] !== null ) {
1507 $good = (int)$this->isCountable()
1508 - (int)$this->options['oldcountable'];
1509 } elseif ( isset( $this->pageState['oldCountable'] ) ) {
1510 $good = (int)$this->isCountable()
1511 - (int)$this->pageState['oldCountable'];
1512 } else {
1513 $good = 0;
1514 }
1515 $edits = $this->options['changed'] ? 1 : 0;
1516 $pages = $this->options['created'] ? 1 : 0;
1517
1518 DeferredUpdates::addUpdate( SiteStatsUpdate::factory(
1519 [ 'edits' => $edits, 'articles' => $good, 'pages' => $pages ]
1520 ) );
1521 } );
1522
1523 // TODO: make search infrastructure aware of slots!
1524 $mainSlot = $this->revision->getSlot( SlotRecord::MAIN );
1525 if ( !$mainSlot->isInherited() && !$this->isContentDeleted() ) {
1526 DeferredUpdates::addUpdate( new SearchUpdate( $id, $dbKey, $mainSlot->getContent() ) );
1527 }
1528
1529 // If this is another user's talk page, update newtalk.
1530 // Don't do this if $options['changed'] = false (null-edits) nor if
1531 // it's a minor edit and the user making the edit doesn't generate notifications for those.
1532 if ( $this->options['changed']
1533 && $title->getNamespace() == NS_USER_TALK
1534 && $shortTitle != $legacyUser->getTitleKey()
1535 && !( $this->revision->isMinor() && $legacyUser->isAllowed( 'nominornewtalk' ) )
1536 ) {
1537 $recipient = User::newFromName( $shortTitle, false );
1538 if ( !$recipient ) {
1539 wfDebug( __METHOD__ . ": invalid username\n" );
1540 } else {
1541 // Allow extensions to prevent user notification
1542 // when a new message is added to their talk page
1543 // TODO: replace legacy hook! Use a listener on PageEventEmitter instead!
1544 if ( Hooks::run( 'ArticleEditUpdateNewTalk', [ &$wikiPage, $recipient ] ) ) {
1545 if ( User::isIP( $shortTitle ) ) {
1546 // An anonymous user
1547 $recipient->setNewtalk( true, $legacyRevision );
1548 } elseif ( $recipient->isLoggedIn() ) {
1549 $recipient->setNewtalk( true, $legacyRevision );
1550 } else {
1551 wfDebug( __METHOD__ . ": don't need to notify a nonexistent user\n" );
1552 }
1553 }
1554 }
1555 }
1556
1557 if ( $title->getNamespace() == NS_MEDIAWIKI
1558 && $this->getRevisionSlotsUpdate()->isModifiedSlot( SlotRecord::MAIN )
1559 ) {
1560 $mainContent = $this->isContentDeleted() ? null : $this->getRawContent( SlotRecord::MAIN );
1561
1562 $this->messageCache->updateMessageOverride( $title, $mainContent );
1563 }
1564
1565 // TODO: move onArticleCreate and onArticle into a PageEventEmitter service
1566 if ( $this->options['created'] ) {
1567 WikiPage::onArticleCreate( $title );
1568 } elseif ( $this->options['changed'] ) { // T52785
1569 WikiPage::onArticleEdit( $title, $legacyRevision, $this->getTouchedSlotRoles() );
1570 }
1571
1572 $oldRevision = $this->getParentRevision();
1573 $oldLegacyRevision = $oldRevision ? new Revision( $oldRevision ) : null;
1574
1575 // TODO: In the wiring, register a listener for this on the new PageEventEmitter
1576 ResourceLoaderWikiModule::invalidateModuleCache(
1577 $title, $oldLegacyRevision, $legacyRevision, $this->getWikiId() ?: wfWikiID()
1578 );
1579
1580 $this->doTransition( 'done' );
1581 }
1582
1583 /**
1584 * Do secondary data updates (such as updating link tables).
1585 *
1586 * MCR note: this method is temporarily exposed via WikiPage::doSecondaryDataUpdates.
1587 *
1588 * @param array $options
1589 * - recursive: make the update recursive, i.e. also update pages which transclude the
1590 * current page or otherwise depend on it (default: false)
1591 * - defer: one of the DeferredUpdates constants, or false to run immediately after waiting
1592 * for replication of the changes from the SecondaryDataUpdates hooks (default: false)
1593 * @since 1.32
1594 */
1595 public function doSecondaryDataUpdates( array $options = [] ) {
1596 $this->assertHasRevision( __METHOD__ );
1597 $options += [ 'recursive' => false, 'defer' => false ];
1598 $deferValues = [ false, DeferredUpdates::PRESEND, DeferredUpdates::POSTSEND ];
1599 if ( !in_array( $options['defer'], $deferValues, true ) ) {
1600 throw new InvalidArgumentException( 'Invalid value for defer: ' . $options['defer'] );
1601 }
1602 $updates = $this->getSecondaryDataUpdates( $options['recursive'] );
1603
1604 $triggeringUser = $this->options['triggeringUser'] ?? $this->user;
1605 if ( !$triggeringUser instanceof User ) {
1606 $triggeringUser = User::newFromIdentity( $triggeringUser );
1607 }
1608 $causeAction = $this->options['causeAction'] ?? 'unknown';
1609 $causeAgent = $this->options['causeAgent'] ?? 'unknown';
1610 $legacyRevision = new Revision( $this->revision );
1611
1612 foreach ( $updates as $update ) {
1613 if ( $update instanceof DataUpdate ) {
1614 $update->setCause( $causeAction, $causeAgent );
1615 }
1616 if ( $update instanceof LinksUpdate ) {
1617 $update->setRevision( $legacyRevision );
1618 $update->setTriggeringUser( $triggeringUser );
1619 }
1620 }
1621
1622 if ( $options['defer'] === false ) {
1623 // T221577: flush any transaction; each update needs outer transaction scope
1624 $this->loadbalancerFactory->commitMasterChanges( __METHOD__ );
1625 foreach ( $updates as $update ) {
1626 DeferredUpdates::attemptUpdate( $update, $this->loadbalancerFactory );
1627 }
1628 } else {
1629 foreach ( $updates as $update ) {
1630 DeferredUpdates::addUpdate( $update, $options['defer'] );
1631 }
1632 }
1633 }
1634
1635 public function doParserCacheUpdate() {
1636 $this->assertHasRevision( __METHOD__ );
1637
1638 $wikiPage = $this->getWikiPage(); // TODO: ParserCache should accept a RevisionRecord instead
1639
1640 // NOTE: this may trigger the first parsing of the new content after an edit (when not
1641 // using pre-generated stashed output).
1642 // XXX: we may want to use the PoolCounter here. This would perhaps allow the initial parse
1643 // to be performed post-send. The client could already follow a HTTP redirect to the
1644 // page view, but would then have to wait for a response until rendering is complete.
1645 $output = $this->getCanonicalParserOutput();
1646
1647 // Save it to the parser cache. Use the revision timestamp in the case of a
1648 // freshly saved edit, as that matches page_touched and a mismatch would trigger an
1649 // unnecessary reparse.
1650 $timestamp = $this->options['newrev'] ? $this->revision->getTimestamp()
1651 : $output->getCacheTime();
1652 $this->parserCache->save(
1653 $output, $wikiPage, $this->getCanonicalParserOptions(),
1654 $timestamp, $this->revision->getId()
1655 );
1656 }
1657
1658 }