Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[lhc/web/wiklou.git] / includes / Storage / PageUpdater.php
1 <?php
2 /**
3 * Controller-like object for creating and updating pages by creating new revisions.
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 * @author Daniel Kinzler
23 */
24
25 namespace MediaWiki\Storage;
26
27 use AtomicSectionUpdate;
28 use ChangeTags;
29 use CommentStoreComment;
30 use Content;
31 use ContentHandler;
32 use DeferredUpdates;
33 use Hooks;
34 use InvalidArgumentException;
35 use LogicException;
36 use ManualLogEntry;
37 use MediaWiki\Linker\LinkTarget;
38 use MWException;
39 use RecentChange;
40 use Revision;
41 use RuntimeException;
42 use Status;
43 use Title;
44 use User;
45 use Wikimedia\Assert\Assert;
46 use Wikimedia\Rdbms\DBConnRef;
47 use Wikimedia\Rdbms\DBUnexpectedError;
48 use Wikimedia\Rdbms\IDatabase;
49 use Wikimedia\Rdbms\LoadBalancer;
50 use WikiPage;
51
52 /**
53 * Controller-like object for creating and updating pages by creating new revisions.
54 *
55 * PageUpdater instances provide compare-and-swap (CAS) protection against concurrent updates
56 * between the time grabParentRevision() is called and saveRevision() inserts a new revision.
57 * This allows application logic to safely perform edit conflict resolution using the parent
58 * revision's content.
59 *
60 * @see docs/pageupdater.txt for more information.
61 *
62 * MCR migration note: this replaces the relevant methods in WikiPage.
63 *
64 * @since 1.32
65 * @ingroup Page
66 */
67 class PageUpdater {
68
69 /**
70 * @var User
71 */
72 private $user;
73
74 /**
75 * @var WikiPage
76 */
77 private $wikiPage;
78
79 /**
80 * @var DerivedPageDataUpdater
81 */
82 private $derivedDataUpdater;
83
84 /**
85 * @var LoadBalancer
86 */
87 private $loadBalancer;
88
89 /**
90 * @var RevisionStore
91 */
92 private $revisionStore;
93
94 /**
95 * @var boolean see $wgUseAutomaticEditSummaries
96 * @see $wgUseAutomaticEditSummaries
97 */
98 private $useAutomaticEditSummaries = true;
99
100 /**
101 * @var int the RC patrol status the new revision should be marked with.
102 */
103 private $rcPatrolStatus = RecentChange::PRC_UNPATROLLED;
104
105 /**
106 * @var bool whether to create a log entry for new page creations.
107 */
108 private $usePageCreationLog = true;
109
110 /**
111 * @var boolean see $wgAjaxEditStash
112 */
113 private $ajaxEditStash = true;
114
115 /**
116 * @var bool|int
117 */
118 private $originalRevId = false;
119
120 /**
121 * @var array
122 */
123 private $tags = [];
124
125 /**
126 * @var int
127 */
128 private $undidRevId = 0;
129
130 /**
131 * @var RevisionSlotsUpdate
132 */
133 private $slotsUpdate;
134
135 /**
136 * @var Status|null
137 */
138 private $status = null;
139
140 /**
141 * @param User $user
142 * @param WikiPage $wikiPage
143 * @param DerivedPageDataUpdater $derivedDataUpdater
144 * @param LoadBalancer $loadBalancer
145 * @param RevisionStore $revisionStore
146 */
147 public function __construct(
148 User $user,
149 WikiPage $wikiPage,
150 DerivedPageDataUpdater $derivedDataUpdater,
151 LoadBalancer $loadBalancer,
152 RevisionStore $revisionStore
153 ) {
154 $this->user = $user;
155 $this->wikiPage = $wikiPage;
156 $this->derivedDataUpdater = $derivedDataUpdater;
157
158 $this->loadBalancer = $loadBalancer;
159 $this->revisionStore = $revisionStore;
160
161 $this->slotsUpdate = new RevisionSlotsUpdate();
162 }
163
164 /**
165 * Can be used to enable or disable automatic summaries that are applied to certain kinds of
166 * changes, like completely blanking a page.
167 *
168 * @param bool $useAutomaticEditSummaries
169 * @see $wgUseAutomaticEditSummaries
170 */
171 public function setUseAutomaticEditSummaries( $useAutomaticEditSummaries ) {
172 $this->useAutomaticEditSummaries = $useAutomaticEditSummaries;
173 }
174
175 /**
176 * Sets the "patrolled" status of the edit.
177 * Callers should check the "patrol" and "autopatrol" permissions as appropriate.
178 *
179 * @see $wgUseRCPatrol
180 * @see $wgUseNPPatrol
181 *
182 * @param int $status RC patrol status, e.g. RecentChange::PRC_AUTOPATROLLED.
183 */
184 public function setRcPatrolStatus( $status ) {
185 $this->rcPatrolStatus = $status;
186 }
187
188 /**
189 * Whether to create a log entry for new page creations.
190 *
191 * @see $wgPageCreationLog
192 *
193 * @param bool $use
194 */
195 public function setUsePageCreationLog( $use ) {
196 $this->usePageCreationLog = $use;
197 }
198
199 /**
200 * @param bool $ajaxEditStash
201 * @see $wgAjaxEditStash
202 */
203 public function setAjaxEditStash( $ajaxEditStash ) {
204 $this->ajaxEditStash = $ajaxEditStash;
205 }
206
207 private function getWikiId() {
208 return false; // TODO: get from RevisionStore!
209 }
210
211 /**
212 * @param int $mode DB_MASTER or DB_REPLICA
213 *
214 * @return DBConnRef
215 */
216 private function getDBConnectionRef( $mode ) {
217 return $this->loadBalancer->getConnectionRef( $mode, [], $this->getWikiId() );
218 }
219
220 /**
221 * @return LinkTarget
222 */
223 private function getLinkTarget() {
224 // NOTE: eventually, we won't get a WikiPage passed into the constructor any more
225 return $this->wikiPage->getTitle();
226 }
227
228 /**
229 * @return Title
230 */
231 private function getTitle() {
232 // NOTE: eventually, we won't get a WikiPage passed into the constructor any more
233 return $this->wikiPage->getTitle();
234 }
235
236 /**
237 * @return WikiPage
238 */
239 private function getWikiPage() {
240 // NOTE: eventually, we won't get a WikiPage passed into the constructor any more
241 return $this->wikiPage;
242 }
243
244 /**
245 * Checks whether this update conflicts with another update performed between the client
246 * loading data to prepare an edit, and the client committing the edit. This is intended to
247 * detect user level "edit conflict" when the latest revision known to the client
248 * is no longer the current revision when processing the update.
249 *
250 * An update expected to create a new page can be checked by setting $expectedParentRevision = 0.
251 * Such an update is considered to have a conflict if a current revision exists (that is,
252 * the page was created since the edit was initiated on the client).
253 *
254 * This method returning true indicates to calling code that edit conflict resolution should
255 * be applied before saving any data. It does not prevent the update from being performed, and
256 * it should not be confused with a "late" conflict indicated by the "edit-conflict" status.
257 * A "late" conflict is a CAS failure caused by an update being performed concurrently between
258 * the time grabParentRevision() was called and the time saveRevision() trying to insert the
259 * new revision.
260 *
261 * @note A user level edit conflict is not the same as the "edit-conflict" status triggered by
262 * a CAS failure. Calling this method establishes the CAS token, it does not check against it:
263 * This method calls grabParentRevision(), and thus causes the expected parent revision
264 * for the update to be fixed to the page's current revision at this point in time.
265 * It acts as a compare-and-swap (CAS) token in that it is guaranteed that saveRevision()
266 * will fail with the "edit-conflict" status if the current revision of the page changes after
267 * hasEditConflict() (or grabParentRevision()) was called and before saveRevision() could insert
268 * a new revision.
269 *
270 * @see grabParentRevision()
271 *
272 * @param int $expectedParentRevision The ID of the revision the client expects to be the
273 * current one. Use 0 to indicate that the page is expected to not yet exist.
274 *
275 * @return bool
276 */
277 public function hasEditConflict( $expectedParentRevision ) {
278 $parent = $this->grabParentRevision();
279 $parentId = $parent ? $parent->getId() : 0;
280
281 return $parentId !== $expectedParentRevision;
282 }
283
284 /**
285 * Returns the revision that was the page's current revision when grabParentRevision()
286 * was first called. This revision is the expected parent revision of the update, and will be
287 * recorded as the new revision's parent revision (unless no new revision is created because
288 * the content was not changed).
289 *
290 * This method MUST not be called after saveRevision() was called!
291 *
292 * The current revision determined by the first call to this methods effectively acts a
293 * compare-and-swap (CAS) token which is checked by saveRevision(), which fails if any
294 * concurrent updates created a new revision.
295 *
296 * Application code should call this method before applying transformations to the new
297 * content that depend on the parent revision, e.g. adding/replacing sections, or resolving
298 * conflicts via a 3-way merge. This protects against race conditions triggered by concurrent
299 * updates.
300 *
301 * @see DerivedPageDataUpdater::grabCurrentRevision()
302 *
303 * @note The expected parent revision is not to be confused with the logical base revision.
304 * The base revision is specified by the client, the parent revision is determined from the
305 * database. If base revision and parent revision are not the same, the updates is considered
306 * to require edit conflict resolution.
307 *
308 * @throws LogicException if called after saveRevision().
309 * @return RevisionRecord|null the parent revision, or null of the page does not yet exist.
310 */
311 public function grabParentRevision() {
312 return $this->derivedDataUpdater->grabCurrentRevision();
313 }
314
315 /**
316 * @return string
317 */
318 private function getTimestampNow() {
319 // TODO: allow an override to be injected for testing
320 return wfTimestampNow();
321 }
322
323 /**
324 * Check flags and add EDIT_NEW or EDIT_UPDATE to them as needed.
325 *
326 * @param int $flags
327 * @return int Updated $flags
328 */
329 private function checkFlags( $flags ) {
330 if ( !( $flags & EDIT_NEW ) && !( $flags & EDIT_UPDATE ) ) {
331 $flags |= ( $this->derivedDataUpdater->pageExisted() ) ? EDIT_UPDATE : EDIT_NEW;
332 }
333
334 return $flags;
335 }
336
337 /**
338 * Set the new content for the given slot role
339 *
340 * @param string $role A slot role name (such as "main")
341 * @param Content $content
342 */
343 public function setContent( $role, Content $content ) {
344 // TODO: MCR: check the role and the content's model against the list of supported
345 // roles, see T194046.
346
347 $this->slotsUpdate->modifyContent( $role, $content );
348 }
349
350 /**
351 * Set the new slot for the given slot role
352 *
353 * @param SlotRecord $slot
354 */
355 public function setSlot( SlotRecord $slot ) {
356 $this->slotsUpdate->modifySlot( $slot );
357 }
358
359 /**
360 * Explicitly inherit a slot from some earlier revision.
361 *
362 * The primary use case for this is rollbacks, when slots are to be inherited from
363 * the rollback target, overriding the content from the parent revision (which is the
364 * revision being rolled back).
365 *
366 * This should typically not be used to inherit slots from the parent revision, which
367 * happens implicitly. Using this method causes the given slot to be treated as "modified"
368 * during revision creation, even if it has the same content as in the parent revision.
369 *
370 * @param SlotRecord $originalSlot A slot already existing in the database, to be inherited
371 * by the new revision.
372 */
373 public function inheritSlot( SlotRecord $originalSlot ) {
374 // NOTE: this slot is inherited from some other revision, but it's
375 // a "modified" slot for the RevisionSlotsUpdate and DerivedPageDataUpdater,
376 // since it's not implicitly inherited from the parent revision.
377 $inheritedSlot = SlotRecord::newInherited( $originalSlot );
378 $this->slotsUpdate->modifySlot( $inheritedSlot );
379 }
380
381 /**
382 * Removes the slot with the given role.
383 *
384 * This discontinues the "stream" of slots with this role on the page,
385 * preventing the new revision, and any subsequent revisions, from
386 * inheriting the slot with this role.
387 *
388 * @param string $role A slot role name (but not "main")
389 */
390 public function removeSlot( $role ) {
391 if ( $role === 'main' ) {
392 throw new InvalidArgumentException( 'Cannot remove the main slot!' );
393 }
394
395 $this->slotsUpdate->removeSlot( $role );
396 }
397
398 /**
399 * Returns the ID of an earlier revision that is being repeated or restored by this update.
400 *
401 * @return bool|int The original revision id, or false if no earlier revision is known to be
402 * repeated or restored by this update.
403 */
404 public function getOriginalRevisionId() {
405 return $this->originalRevId;
406 }
407
408 /**
409 * Sets the ID of an earlier revision that is being repeated or restored by this update.
410 * The new revision is expected to have the exact same content as the given original revision.
411 * This is used with rollbacks and with dummy "null" revisions which are created to record
412 * things like page moves.
413 *
414 * This value is passed to the PageContentSaveComplete and NewRevisionFromEditComplete hooks.
415 *
416 * @param int|bool $originalRevId The original revision id, or false if no earlier revision
417 * is known to be repeated or restored by this update.
418 */
419 public function setOriginalRevisionId( $originalRevId ) {
420 Assert::parameterType( 'integer|boolean', $originalRevId, '$originalRevId' );
421 $this->originalRevId = $originalRevId;
422 }
423
424 /**
425 * Returns the revision ID set by setUndidRevisionId(), indicating what revision is being
426 * undone by this edit.
427 *
428 * @return int
429 */
430 public function getUndidRevisionId() {
431 return $this->undidRevId;
432 }
433
434 /**
435 * Sets the ID of revision that was undone by the present update.
436 * This is used with the "undo" action, and is expected to hold the oldest revision ID
437 * in case more then one revision is being undone.
438 *
439 * @param int $undidRevId
440 */
441 public function setUndidRevisionId( $undidRevId ) {
442 Assert::parameterType( 'integer', $undidRevId, '$undidRevId' );
443 $this->undidRevId = $undidRevId;
444 }
445
446 /**
447 * Sets a tag to apply to this update.
448 * Callers are responsible for permission checks,
449 * using ChangeTags::canAddTagsAccompanyingChange.
450 * @param string $tag
451 */
452 public function addTag( $tag ) {
453 Assert::parameterType( 'string', $tag, '$tag' );
454 $this->tags[] = trim( $tag );
455 }
456
457 /**
458 * Sets tags to apply to this update.
459 * Callers are responsible for permission checks,
460 * using ChangeTags::canAddTagsAccompanyingChange.
461 * @param string[] $tags
462 */
463 public function addTags( array $tags ) {
464 Assert::parameterElementType( 'string', $tags, '$tags' );
465 foreach ( $tags as $tag ) {
466 $this->addTag( $tag );
467 }
468 }
469
470 /**
471 * Returns the list of tags set using the addTag() method.
472 *
473 * @return string[]
474 */
475 public function getExplicitTags() {
476 return $this->tags;
477 }
478
479 /**
480 * @param int $flags Bit mask: a bit mask of EDIT_XXX flags.
481 * @return string[]
482 */
483 private function computeEffectiveTags( $flags ) {
484 $tags = $this->tags;
485
486 foreach ( $this->slotsUpdate->getModifiedRoles() as $role ) {
487 $old_content = $this->getParentContent( $role );
488
489 $handler = $this->getContentHandler( $role );
490 $content = $this->slotsUpdate->getModifiedSlot( $role )->getContent();
491
492 // TODO: MCR: Do this for all slots. Also add tags for removing roles!
493 $tag = $handler->getChangeTag( $old_content, $content, $flags );
494 // If there is no applicable tag, null is returned, so we need to check
495 if ( $tag ) {
496 $tags[] = $tag;
497 }
498 }
499
500 // Check for undo tag
501 if ( $this->undidRevId !== 0 && in_array( 'mw-undo', ChangeTags::getSoftwareTags() ) ) {
502 $tags[] = 'mw-undo';
503 }
504
505 return array_unique( $tags );
506 }
507
508 /**
509 * Returns the content of the given slot of the parent revision, with no audience checks applied.
510 * If there is no parent revision or the slot is not defined, this returns null.
511 *
512 * @param string $role slot role name
513 * @return Content|null
514 */
515 private function getParentContent( $role ) {
516 $parent = $this->grabParentRevision();
517
518 if ( $parent && $parent->hasSlot( $role ) ) {
519 return $parent->getContent( $role, RevisionRecord::RAW );
520 }
521
522 return null;
523 }
524
525 /**
526 * @param string $role slot role name
527 * @return ContentHandler
528 */
529 private function getContentHandler( $role ) {
530 // TODO: inject something like a ContentHandlerRegistry
531 if ( $this->slotsUpdate->isModifiedSlot( $role ) ) {
532 $slot = $this->slotsUpdate->getModifiedSlot( $role );
533 } else {
534 $parent = $this->grabParentRevision();
535
536 if ( $parent ) {
537 $slot = $parent->getSlot( $role, RevisionRecord::RAW );
538 } else {
539 throw new RevisionAccessException( 'No such slot: ' . $role );
540 }
541 }
542
543 return ContentHandler::getForModelID( $slot->getModel() );
544 }
545
546 /**
547 * @param int $flags Bit mask: a bit mask of EDIT_XXX flags.
548 *
549 * @return CommentStoreComment
550 */
551 private function makeAutoSummary( $flags ) {
552 if ( !$this->useAutomaticEditSummaries || ( $flags & EDIT_AUTOSUMMARY ) === 0 ) {
553 return CommentStoreComment::newUnsavedComment( '' );
554 }
555
556 // NOTE: this generates an auto-summary for SOME RANDOM changed slot!
557 // TODO: combine auto-summaries for multiple slots!
558 // XXX: this logic should not be in the storage layer!
559 $roles = $this->slotsUpdate->getModifiedRoles();
560 $role = reset( $roles );
561
562 if ( $role === false ) {
563 return CommentStoreComment::newUnsavedComment( '' );
564 }
565
566 $handler = $this->getContentHandler( $role );
567 $content = $this->slotsUpdate->getModifiedSlot( $role )->getContent();
568 $old_content = $this->getParentContent( $role );
569 $summary = $handler->getAutosummary( $old_content, $content, $flags );
570
571 return CommentStoreComment::newUnsavedComment( $summary );
572 }
573
574 /**
575 * Change an existing article or create a new article. Updates RC and all necessary caches,
576 * optionally via the deferred update array. This does not check user permissions.
577 *
578 * It is guaranteed that saveRevision() will fail if the current revision of the page
579 * changes after grabParentRevision() was called and before saveRevision() can insert
580 * a new revision, as per the CAS mechanism described above.
581 *
582 * The caller is however responsible for calling hasEditConflict() to detect a
583 * user-level edit conflict, and to adjust the content of the new revision accordingly,
584 * e.g. by using a 3-way-merge.
585 *
586 * MCR migration note: this replaces WikiPage::doEditContent. Callers that change to using
587 * saveRevision() now need to check the "minoredit" themselves before using EDIT_MINOR.
588 *
589 * @param CommentStoreComment $summary Edit summary
590 * @param int $flags Bitfield:
591 * EDIT_NEW
592 * Create a new page, or fail with "edit-already-exists" if the page exists.
593 * EDIT_UPDATE
594 * Create a new revision, or fail with "edit-gone-missing" if the page does not exist.
595 * EDIT_MINOR
596 * Mark this revision as minor
597 * EDIT_SUPPRESS_RC
598 * Do not log the change in recentchanges
599 * EDIT_FORCE_BOT
600 * Mark the revision as automated ("bot edit")
601 * EDIT_AUTOSUMMARY
602 * Fill in blank summaries with generated text where possible
603 * EDIT_INTERNAL
604 * Signal that the page retrieve/save cycle happened entirely in this request.
605 *
606 * If neither EDIT_NEW nor EDIT_UPDATE is specified, the expected state is detected
607 * automatically via grabParentRevision(). In this case, the "edit-already-exists" or
608 * "edit-gone-missing" errors may still be triggered due to race conditions, if the page
609 * was unexpectedly created or deleted while revision creation is in progress. This can be
610 * viewed as part of the CAS mechanism described above.
611 *
612 * @return RevisionRecord|null The new revision, or null if no new revision was created due
613 * to a failure or a null-edit. Use isUnchanged(), wasSuccessful() and getStatus()
614 * to determine the outcome of the revision creation.
615 *
616 * @throws MWException
617 * @throws RuntimeException
618 */
619 public function saveRevision( CommentStoreComment $summary, $flags = 0 ) {
620 // Defend against mistakes caused by differences with the
621 // signature of WikiPage::doEditContent.
622 Assert::parameterType( 'integer', $flags, '$flags' );
623
624 if ( $this->wasCommitted() ) {
625 throw new RuntimeException( 'saveRevision() has already been called on this PageUpdater!' );
626 }
627
628 // Low-level sanity check
629 if ( $this->getLinkTarget()->getText() === '' ) {
630 throw new RuntimeException( 'Something is trying to edit an article with an empty title' );
631 }
632
633 // TODO: MCR: check the role and the content's model against the list of supported
634 // and required roles, see T194046.
635
636 // Make sure the given content type is allowed for this page
637 // TODO: decide: Extend check to other slots? Consider the role in check? [PageType]
638 $mainContentHandler = $this->getContentHandler( 'main' );
639 if ( !$mainContentHandler->canBeUsedOn( $this->getTitle() ) ) {
640 $this->status = Status::newFatal( 'content-not-allowed-here',
641 ContentHandler::getLocalizedName( $mainContentHandler->getModelID() ),
642 $this->getTitle()->getPrefixedText()
643 );
644 return null;
645 }
646
647 // Load the data from the master database if needed. Needed to check flags.
648 // NOTE: This grabs the parent revision as the CAS token, if grabParentRevision
649 // wasn't called yet. If the page is modified by another process before we are done with
650 // it, this method must fail (with status 'edit-conflict')!
651 // NOTE: The parent revision may be different from $this->originalRevisionId.
652 $this->grabParentRevision();
653 $flags = $this->checkFlags( $flags );
654
655 // Avoid statsd noise and wasted cycles check the edit stash (T136678)
656 if ( ( $flags & EDIT_INTERNAL ) || ( $flags & EDIT_FORCE_BOT ) ) {
657 $useStashed = false;
658 } else {
659 $useStashed = $this->ajaxEditStash;
660 }
661
662 // TODO: use this only for the legacy hook, and only if something uses the legacy hook
663 $wikiPage = $this->getWikiPage();
664
665 $user = $this->user;
666
667 // Prepare the update. This performs PST and generates the canonical ParserOutput.
668 $this->derivedDataUpdater->prepareContent(
669 $this->user,
670 $this->slotsUpdate,
671 $useStashed
672 );
673
674 // TODO: don't force initialization here!
675 // This is a hack to work around the fact that late initialization of the ParserOutput
676 // causes ApiFlowEditHeaderTest::testCache to fail. Whether that failure indicates an
677 // actual problem, or is just an issue with the test setup, remains to be determined
678 // [dk, 2018-03].
679 // Anomie said in 2018-03:
680 /*
681 I suspect that what's breaking is this:
682
683 The old version of WikiPage::doEditContent() called prepareContentForEdit() which
684 generated the ParserOutput right then, so when doEditUpdates() gets called from the
685 DeferredUpdate scheduled by WikiPage::doCreate() there's no need to parse. I note
686 there's a comment there that says "Get the pre-save transform content and final
687 parser output".
688 The new version of WikiPage::doEditContent() makes a PageUpdater and calls its
689 saveRevision(), which calls DerivedPageDataUpdater::prepareContent() and
690 PageUpdater::doCreate() without ever having to actually generate a ParserOutput.
691 Thus, when DerivedPageDataUpdater::doUpdates() is called from the DeferredUpdate
692 scheduled by PageUpdater::doCreate(), it does find that it needs to parse at that point.
693
694 And the order of operations in that Flow test is presumably:
695
696 - Create a page with a call to WikiPage::doEditContent(), in a way that somehow avoids
697 processing the DeferredUpdate.
698 - Set up the "no set!" mock cache in Flow\Tests\Api\ApiTestCase::expectCacheInvalidate()
699 - Then, during the course of doing that test, a $db->commit() results in the
700 DeferredUpdates being run.
701 */
702 $this->derivedDataUpdater->getCanonicalParserOutput();
703
704 $mainContent = $this->derivedDataUpdater->getSlots()->getContent( 'main' );
705
706 // Trigger pre-save hook (using provided edit summary)
707 $hookStatus = Status::newGood( [] );
708 // TODO: replace legacy hook!
709 // TODO: avoid pass-by-reference, see T193950
710 $hook_args = [ &$wikiPage, &$user, &$mainContent, &$summary,
711 $flags & EDIT_MINOR, null, null, &$flags, &$hookStatus ];
712 // Check if the hook rejected the attempted save
713 if ( !Hooks::run( 'PageContentSave', $hook_args ) ) {
714 if ( $hookStatus->isOK() ) {
715 // Hook returned false but didn't call fatal(); use generic message
716 $hookStatus->fatal( 'edit-hook-aborted' );
717 }
718
719 $this->status = $hookStatus;
720 return null;
721 }
722
723 // Provide autosummaries if one is not provided and autosummaries are enabled
724 // XXX: $summary == null seems logical, but the empty string may actually come from the user
725 // XXX: Move this logic out of the storage layer! It does not belong here! Use a callback?
726 if ( $summary->text === '' && $summary->data === null ) {
727 $summary = $this->makeAutoSummary( $flags );
728 }
729
730 // Actually create the revision and create/update the page.
731 // Do NOT yet set $this->status!
732 if ( $flags & EDIT_UPDATE ) {
733 $status = $this->doModify( $summary, $this->user, $flags );
734 } else {
735 $status = $this->doCreate( $summary, $this->user, $flags );
736 }
737
738 // Promote user to any groups they meet the criteria for
739 DeferredUpdates::addCallableUpdate( function () use ( $user ) {
740 $user->addAutopromoteOnceGroups( 'onEdit' );
741 $user->addAutopromoteOnceGroups( 'onView' ); // b/c
742 } );
743
744 // NOTE: set $this->status only after all hooks have been called,
745 // so wasCommitted doesn't return true wehn called indirectly from a hook handler!
746 $this->status = $status;
747
748 // TODO: replace bad status with Exceptions!
749 return ( $this->status && $this->status->isOK() )
750 ? $this->status->value['revision-record']
751 : null;
752 }
753
754 /**
755 * Whether saveRevision() has been called on this instance
756 *
757 * @return bool
758 */
759 public function wasCommitted() {
760 return $this->status !== null;
761 }
762
763 /**
764 * The Status object indicating whether saveRevision() was successful, or null if
765 * saveRevision() was not yet called on this instance.
766 *
767 * @note This is here for compatibility with WikiPage::doEditContent. It may be deprecated
768 * soon.
769 *
770 * Possible status errors:
771 * edit-hook-aborted: The ArticleSave hook aborted the update but didn't
772 * set the fatal flag of $status.
773 * edit-gone-missing: In update mode, but the article didn't exist.
774 * edit-conflict: In update mode, the article changed unexpectedly.
775 * edit-no-change: Warning that the text was the same as before.
776 * edit-already-exists: In creation mode, but the article already exists.
777 *
778 * Extensions may define additional errors.
779 *
780 * $return->value will contain an associative array with members as follows:
781 * new: Boolean indicating if the function attempted to create a new article.
782 * revision: The revision object for the inserted revision, or null.
783 *
784 * @return null|Status
785 */
786 public function getStatus() {
787 return $this->status;
788 }
789
790 /**
791 * Whether saveRevision() completed successfully
792 *
793 * @return bool
794 */
795 public function wasSuccessful() {
796 return $this->status && $this->status->isOK();
797 }
798
799 /**
800 * Whether saveRevision() was called and created a new page.
801 *
802 * @return bool
803 */
804 public function isNew() {
805 return $this->status && $this->status->isOK() && $this->status->value['new'];
806 }
807
808 /**
809 * Whether saveRevision() did not create a revision because the content didn't change
810 * (null-edit). Whether the content changed or not is determined by
811 * DerivedPageDataUpdater::isChange().
812 *
813 * @return bool
814 */
815 public function isUnchanged() {
816 return $this->status
817 && $this->status->isOK()
818 && $this->status->value['revision-record'] === null;
819 }
820
821 /**
822 * The new revision created by saveRevision(), or null if saveRevision() has not yet been
823 * called, failed, or did not create a new revision because the content did not change.
824 *
825 * @return RevisionRecord|null
826 */
827 public function getNewRevision() {
828 return ( $this->status && $this->status->isOK() )
829 ? $this->status->value['revision-record']
830 : null;
831 }
832
833 /**
834 * Constructs a MutableRevisionRecord based on the Content prepared by the
835 * DerivedPageDataUpdater. This takes care of inheriting slots, updating slots
836 * with PST applied, and removing discontinued slots.
837 *
838 * This calls Content::prepareSave() to verify that the slot content can be saved.
839 * The $status parameter is updated with any errors or warnings found by Content::prepareSave().
840 *
841 * @param CommentStoreComment $comment
842 * @param User $user
843 * @param int $flags
844 * @param Status $status
845 *
846 * @return MutableRevisionRecord
847 */
848 private function makeNewRevision(
849 CommentStoreComment $comment,
850 User $user,
851 $flags,
852 Status $status
853 ) {
854 $wikiPage = $this->getWikiPage();
855 $title = $this->getTitle();
856 $parent = $this->grabParentRevision();
857
858 // XXX: we expect to get a MutableRevisionRecord here, but that's a bit brittle!
859 // TODO: introduce something like an UnsavedRevisionFactory service instead!
860 /** @var MutableRevisionRecord $rev */
861 $rev = $this->derivedDataUpdater->getRevision();
862
863 $rev->setPageId( $title->getArticleID() );
864
865 if ( $parent ) {
866 $oldid = $parent->getId();
867 $rev->setParentId( $oldid );
868 } else {
869 $oldid = 0;
870 }
871
872 $rev->setComment( $comment );
873 $rev->setUser( $user );
874 $rev->setMinorEdit( ( $flags & EDIT_MINOR ) > 0 );
875
876 foreach ( $rev->getSlots()->getSlots() as $slot ) {
877 $content = $slot->getContent();
878
879 // XXX: We may push this up to the "edit controller" level, see T192777.
880 // TODO: change the signature of PrepareSave to not take a WikiPage!
881 $prepStatus = $content->prepareSave( $wikiPage, $flags, $oldid, $user );
882
883 // TODO: MCR: record which problem arose in which slot.
884 $status->merge( $prepStatus );
885 }
886
887 return $rev;
888 }
889
890 /**
891 * @param CommentStoreComment $summary The edit summary
892 * @param User $user The revision's author
893 * @param int $flags EXIT_XXX constants
894 *
895 * @throws MWException
896 * @return Status
897 */
898 private function doModify( CommentStoreComment $summary, User $user, $flags ) {
899 $wikiPage = $this->getWikiPage(); // TODO: use for legacy hooks only!
900
901 // Update article, but only if changed.
902 $status = Status::newGood( [ 'new' => false, 'revision' => null, 'revision-record' => null ] );
903
904 $oldRev = $this->grabParentRevision();
905 $oldid = $oldRev ? $oldRev->getId() : 0;
906
907 if ( !$oldRev ) {
908 // Article gone missing
909 $status->fatal( 'edit-gone-missing' );
910
911 return $status;
912 }
913
914 $newRevisionRecord = $this->makeNewRevision(
915 $summary,
916 $user,
917 $flags,
918 $status
919 );
920
921 if ( !$status->isOK() ) {
922 return $status;
923 }
924
925 $now = $newRevisionRecord->getTimestamp();
926
927 // XXX: we may want a flag that allows a null revision to be forced!
928 $changed = $this->derivedDataUpdater->isChange();
929
930 $dbw = $this->getDBConnectionRef( DB_MASTER );
931
932 if ( $changed ) {
933 $dbw->startAtomic( __METHOD__ );
934
935 // Get the latest page_latest value while locking it.
936 // Do a CAS style check to see if it's the same as when this method
937 // started. If it changed then bail out before touching the DB.
938 $latestNow = $wikiPage->lockAndGetLatest(); // TODO: move to storage service, pass DB
939 if ( $latestNow != $oldid ) {
940 // We don't need to roll back, since we did not modify the database yet.
941 // XXX: Or do we want to rollback, any transaction started by calling
942 // code will fail? If we want that, we should probably throw an exception.
943 $dbw->endAtomic( __METHOD__ );
944 // Page updated or deleted in the mean time
945 $status->fatal( 'edit-conflict' );
946
947 return $status;
948 }
949
950 // At this point we are now comitted to returning an OK
951 // status unless some DB query error or other exception comes up.
952 // This way callers don't have to call rollback() if $status is bad
953 // unless they actually try to catch exceptions (which is rare).
954
955 // Save revision content and meta-data
956 $newRevisionRecord = $this->revisionStore->insertRevisionOn( $newRevisionRecord, $dbw );
957 $newLegacyRevision = new Revision( $newRevisionRecord );
958
959 // Update page_latest and friends to reflect the new revision
960 // TODO: move to storage service
961 $wasRedirect = $this->derivedDataUpdater->wasRedirect();
962 if ( !$wikiPage->updateRevisionOn( $dbw, $newLegacyRevision, null, $wasRedirect ) ) {
963 throw new PageUpdateException( "Failed to update page row to use new revision." );
964 }
965
966 // TODO: replace legacy hook!
967 $tags = $this->computeEffectiveTags( $flags );
968 Hooks::run(
969 'NewRevisionFromEditComplete',
970 [ $wikiPage, $newLegacyRevision, $this->getOriginalRevisionId(), $user, &$tags ]
971 );
972
973 // Update recentchanges
974 if ( !( $flags & EDIT_SUPPRESS_RC ) ) {
975 // Add RC row to the DB
976 RecentChange::notifyEdit(
977 $now,
978 $this->getTitle(),
979 $newRevisionRecord->isMinor(),
980 $user,
981 $summary->text, // TODO: pass object when that becomes possible
982 $oldid,
983 $newRevisionRecord->getTimestamp(),
984 ( $flags & EDIT_FORCE_BOT ) > 0,
985 '',
986 $oldRev->getSize(),
987 $newRevisionRecord->getSize(),
988 $newRevisionRecord->getId(),
989 $this->rcPatrolStatus,
990 $tags
991 );
992 }
993
994 $user->incEditCount();
995
996 $dbw->endAtomic( __METHOD__ );
997
998 // Return the new revision to the caller
999 $status->value['revision-record'] = $newRevisionRecord;
1000
1001 // TODO: globally replace usages of 'revision' with getNewRevision()
1002 $status->value['revision'] = $newLegacyRevision;
1003 } else {
1004 // T34948: revision ID must be set to page {{REVISIONID}} and
1005 // related variables correctly. Likewise for {{REVISIONUSER}} (T135261).
1006 // Since we don't insert a new revision into the database, the least
1007 // error-prone way is to reuse given old revision.
1008 $newRevisionRecord = $oldRev;
1009
1010 $status->warning( 'edit-no-change' );
1011 // Update page_touched as updateRevisionOn() was not called.
1012 // Other cache updates are managed in WikiPage::onArticleEdit()
1013 // via WikiPage::doEditUpdates().
1014 $this->getTitle()->invalidateCache( $now );
1015 }
1016
1017 // Do secondary updates once the main changes have been committed...
1018 // NOTE: the updates have to be processed before sending the response to the client
1019 // (DeferredUpdates::PRESEND), otherwise the client may already be following the
1020 // HTTP redirect to the standard view before dervide data has been created - most
1021 // importantly, before the parser cache has been updated. This would cause the
1022 // content to be parsed a second time, or may cause stale content to be shown.
1023 DeferredUpdates::addUpdate(
1024 $this->getAtomicSectionUpdate(
1025 $dbw,
1026 $wikiPage,
1027 $newRevisionRecord,
1028 $user,
1029 $summary,
1030 $flags,
1031 $status,
1032 [ 'changed' => $changed, ]
1033 ),
1034 DeferredUpdates::PRESEND
1035 );
1036
1037 return $status;
1038 }
1039
1040 /**
1041 * @param CommentStoreComment $summary The edit summary
1042 * @param User $user The revision's author
1043 * @param int $flags EXIT_XXX constants
1044 *
1045 * @throws DBUnexpectedError
1046 * @throws MWException
1047 * @return Status
1048 */
1049 private function doCreate( CommentStoreComment $summary, User $user, $flags ) {
1050 $wikiPage = $this->getWikiPage(); // TODO: use for legacy hooks only!
1051
1052 if ( !$this->derivedDataUpdater->getSlots()->hasSlot( 'main' ) ) {
1053 throw new PageUpdateException( 'Must provide a main slot when creating a page!' );
1054 }
1055
1056 $status = Status::newGood( [ 'new' => true, 'revision' => null, 'revision-record' => null ] );
1057
1058 $newRevisionRecord = $this->makeNewRevision(
1059 $summary,
1060 $user,
1061 $flags,
1062 $status
1063 );
1064
1065 if ( !$status->isOK() ) {
1066 return $status;
1067 }
1068
1069 $now = $newRevisionRecord->getTimestamp();
1070
1071 $dbw = $this->getDBConnectionRef( DB_MASTER );
1072 $dbw->startAtomic( __METHOD__ );
1073
1074 // Add the page record unless one already exists for the title
1075 // TODO: move to storage service
1076 $newid = $wikiPage->insertOn( $dbw );
1077 if ( $newid === false ) {
1078 $dbw->endAtomic( __METHOD__ ); // nothing inserted
1079 $status->fatal( 'edit-already-exists' );
1080
1081 return $status; // nothing done
1082 }
1083
1084 // At this point we are now comitted to returning an OK
1085 // status unless some DB query error or other exception comes up.
1086 // This way callers don't have to call rollback() if $status is bad
1087 // unless they actually try to catch exceptions (which is rare).
1088 $newRevisionRecord->setPageId( $newid );
1089
1090 // Save the revision text...
1091 $newRevisionRecord = $this->revisionStore->insertRevisionOn( $newRevisionRecord, $dbw );
1092 $newLegacyRevision = new Revision( $newRevisionRecord );
1093
1094 // Update the page record with revision data
1095 // TODO: move to storage service
1096 if ( !$wikiPage->updateRevisionOn( $dbw, $newLegacyRevision, 0 ) ) {
1097 throw new PageUpdateException( "Failed to update page row to use new revision." );
1098 }
1099
1100 // TODO: replace legacy hook!
1101 $tags = $this->computeEffectiveTags( $flags );
1102 Hooks::run(
1103 'NewRevisionFromEditComplete',
1104 [ $wikiPage, $newLegacyRevision, false, $user, &$tags ]
1105 );
1106
1107 // Update recentchanges
1108 if ( !( $flags & EDIT_SUPPRESS_RC ) ) {
1109 // Add RC row to the DB
1110 RecentChange::notifyNew(
1111 $now,
1112 $this->getTitle(),
1113 $newRevisionRecord->isMinor(),
1114 $user,
1115 $summary->text, // TODO: pass object when that becomes possible
1116 ( $flags & EDIT_FORCE_BOT ) > 0,
1117 '',
1118 $newRevisionRecord->getSize(),
1119 $newRevisionRecord->getId(),
1120 $this->rcPatrolStatus,
1121 $tags
1122 );
1123 }
1124
1125 $user->incEditCount();
1126
1127 if ( $this->usePageCreationLog ) {
1128 // Log the page creation
1129 // @TODO: Do we want a 'recreate' action?
1130 $logEntry = new ManualLogEntry( 'create', 'create' );
1131 $logEntry->setPerformer( $user );
1132 $logEntry->setTarget( $this->getTitle() );
1133 $logEntry->setComment( $summary->text );
1134 $logEntry->setTimestamp( $now );
1135 $logEntry->setAssociatedRevId( $newRevisionRecord->getId() );
1136 $logEntry->insert();
1137 // Note that we don't publish page creation events to recentchanges
1138 // (i.e. $logEntry->publish()) since this would create duplicate entries,
1139 // one for the edit and one for the page creation.
1140 }
1141
1142 $dbw->endAtomic( __METHOD__ );
1143
1144 // Return the new revision to the caller
1145 // TODO: globally replace usages of 'revision' with getNewRevision()
1146 $status->value['revision'] = $newLegacyRevision;
1147 $status->value['revision-record'] = $newRevisionRecord;
1148
1149 // Do secondary updates once the main changes have been committed...
1150 DeferredUpdates::addUpdate(
1151 $this->getAtomicSectionUpdate(
1152 $dbw,
1153 $wikiPage,
1154 $newRevisionRecord,
1155 $user,
1156 $summary,
1157 $flags,
1158 $status,
1159 [ 'created' => true ]
1160 ),
1161 DeferredUpdates::PRESEND
1162 );
1163
1164 return $status;
1165 }
1166
1167 private function getAtomicSectionUpdate(
1168 IDatabase $dbw,
1169 WikiPage $wikiPage,
1170 RevisionRecord $newRevisionRecord,
1171 User $user,
1172 CommentStoreComment $summary,
1173 $flags,
1174 Status $status,
1175 $hints = []
1176 ) {
1177 return new AtomicSectionUpdate(
1178 $dbw,
1179 __METHOD__,
1180 function () use (
1181 $wikiPage, $newRevisionRecord, $user,
1182 $summary, $flags, $status, $hints
1183 ) {
1184 // set debug data
1185 $hints['causeAction'] = 'edit-page';
1186 $hints['causeAgent'] = $user->getName();
1187
1188 $newLegacyRevision = new Revision( $newRevisionRecord );
1189 $mainContent = $newRevisionRecord->getContent( 'main', RevisionRecord::RAW );
1190
1191 // Update links tables, site stats, etc.
1192 $this->derivedDataUpdater->prepareUpdate( $newRevisionRecord, $hints );
1193 $this->derivedDataUpdater->doUpdates();
1194
1195 // TODO: replace legacy hook!
1196 // TODO: avoid pass-by-reference, see T193950
1197
1198 if ( $hints['created'] ?? false ) {
1199 // Trigger post-create hook
1200 $params = [ &$wikiPage, &$user, $mainContent, $summary->text,
1201 $flags & EDIT_MINOR, null, null, &$flags, $newLegacyRevision ];
1202 Hooks::run( 'PageContentInsertComplete', $params );
1203 }
1204
1205 // Trigger post-save hook
1206 $params = [ &$wikiPage, &$user, $mainContent, $summary->text,
1207 $flags & EDIT_MINOR, null, null, &$flags, $newLegacyRevision,
1208 &$status, $this->getOriginalRevisionId(), $this->undidRevId ];
1209 Hooks::run( 'PageContentSaveComplete', $params );
1210 }
1211 );
1212 }
1213
1214 }