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