Support fluent interface for PageUpdater

Also refactored some calls to use it.

Bug: T287484
Change-Id: Ibb745e0131761600789dcb1a2065a981a65ec515
This commit is contained in:
TChin 2021-07-29 10:09:48 -04:00
parent bf3d4dfa25
commit 48496b8526
7 changed files with 72 additions and 48 deletions

View file

@ -275,10 +275,12 @@ class PageUpdater {
* changes, like completely blanking a page.
*
* @param bool $useAutomaticEditSummaries
* @return $this
* @see $wgUseAutomaticEditSummaries
*/
public function setUseAutomaticEditSummaries( $useAutomaticEditSummaries ) {
$this->useAutomaticEditSummaries = $useAutomaticEditSummaries;
return $this;
}
/**
@ -289,9 +291,11 @@ class PageUpdater {
* @see $wgUseNPPatrol
*
* @param int $status RC patrol status, e.g. RecentChange::PRC_AUTOPATROLLED.
* @return $this
*/
public function setRcPatrolStatus( $status ) {
$this->rcPatrolStatus = $status;
return $this;
}
/**
@ -300,17 +304,21 @@ class PageUpdater {
* @see $wgPageCreationLog
*
* @param bool $use
* @return $this
*/
public function setUsePageCreationLog( $use ) {
$this->usePageCreationLog = $use;
return $this;
}
/**
* @param bool $ajaxEditStash
* @return $this
* @see $wgAjaxEditStash
*/
public function setAjaxEditStash( $ajaxEditStash ) {
$this->ajaxEditStash = $ajaxEditStash;
return $this;
}
private function getWikiId() {
@ -440,22 +448,26 @@ class PageUpdater {
*
* @param string $role A slot role name (such as "main")
* @param Content $content
* @return $this
*/
public function setContent( $role, Content $content ) {
$this->ensureRoleAllowed( $role );
$this->slotsUpdate->modifyContent( $role, $content );
return $this;
}
/**
* Set the new slot for the given slot role
*
* @param SlotRecord $slot
* @return $this
*/
public function setSlot( SlotRecord $slot ) {
$this->ensureRoleAllowed( $slot->getRole() );
$this->slotsUpdate->modifySlot( $slot );
return $this;
}
/**
@ -471,6 +483,7 @@ class PageUpdater {
*
* @param SlotRecord $originalSlot A slot already existing in the database, to be inherited
* by the new revision.
* @return $this
*/
public function inheritSlot( SlotRecord $originalSlot ) {
// NOTE: slots can be inherited even if the role is not "allowed" on the title.
@ -479,6 +492,7 @@ class PageUpdater {
// since it's not implicitly inherited from the parent revision.
$inheritedSlot = SlotRecord::newInherited( $originalSlot );
$this->slotsUpdate->modifySlot( $inheritedSlot );
return $this;
}
/**
@ -507,9 +521,11 @@ class PageUpdater {
*
* @param int|bool $originalRevId The original revision id, or false if no earlier revision
* is known to be repeated or restored by this update.
* @return $this
*/
public function setOriginalRevisionId( $originalRevId ) {
$this->editResultBuilder->setOriginalRevisionId( $originalRevId );
return $this;
}
/**
@ -522,6 +538,7 @@ class PageUpdater {
* @param int $oldestRevertedRevId The ID of the oldest revision that was reverted.
* @param int $newestRevertedRevId The ID of the newest revision that was reverted. This
* parameter is optional, default value is $oldestRevertedRevId
* @return $this
*
* @see EditResultBuilder::markAsRevert()
*/
@ -533,6 +550,7 @@ class PageUpdater {
$this->editResultBuilder->markAsRevert(
$revertMethod, $oldestRevertedRevId, $newestRevertedRevId
);
return $this;
}
/**
@ -551,10 +569,12 @@ class PageUpdater {
* Callers are responsible for permission checks,
* using ChangeTags::canAddTagsAccompanyingChange.
* @param string $tag
* @return $this
*/
public function addTag( $tag ) {
Assert::parameterType( 'string', $tag, '$tag' );
$this->tags[] = trim( $tag );
return $this;
}
/**
@ -562,12 +582,14 @@ class PageUpdater {
* Callers are responsible for permission checks,
* using ChangeTags::canAddTagsAccompanyingChange.
* @param string[] $tags
* @return $this
*/
public function addTags( array $tags ) {
Assert::parameterElementType( 'string', $tags, '$tags' );
foreach ( $tags as $tag ) {
$this->addTag( $tag );
}
return $this;
}
/**

View file

@ -1986,8 +1986,8 @@ class WikiPage implements Page, IDBAccessObject, PageRecord {
// NOTE: while doUserEditContent() executes, callbacks to getDerivedDataUpdater and
// prepareContentForEdit will generally use the DerivedPageDataUpdater that is also
// used by this PageUpdater. However, there is no guarantee for this.
$updater = $this->newPageUpdater( $performer, $slotsUpdate );
$updater->setContent( SlotRecord::MAIN, $content );
$updater = $this->newPageUpdater( $performer, $slotsUpdate )
->setContent( SlotRecord::MAIN, $content );
$revisionStore = $this->getRevisionStore();
$originalRevision = $originalRevId ? $revisionStore->getRevisionById( $originalRevId ) : null;

View file

@ -22,6 +22,7 @@
*/
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;
use Wikimedia\Rdbms\DBQueryError;
use Wikimedia\Rdbms\IDatabase;
@ -242,17 +243,17 @@ class PopulateArchiveRevId extends LoggedUpdateMaintenance {
// Make a title and revision and insert them
$title = Title::newFromText( "PopulateArchiveRevId_4b05b46a81e29" );
$page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
$updater = $page->newPageUpdater(
$page->newPageUpdater(
User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] )
);
$updater->setContent(
'main',
ContentHandler::makeContent( "Content for dummy rev", $title )
);
$updater->saveRevision(
CommentStoreComment::newUnsavedComment( 'dummy rev summary' ),
EDIT_NEW | EDIT_SUPPRESS_RC
);
)
->setContent(
SlotRecord::MAIN,
ContentHandler::makeContent( "Content for dummy rev", $title )
)
->saveRevision(
CommentStoreComment::newUnsavedComment( 'dummy rev summary' ),
EDIT_NEW | EDIT_SUPPRESS_RC
);
// get the revision row just inserted
$rev = $dbw->selectRow(

View file

@ -2,6 +2,7 @@
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
/**
* @group Database
@ -517,12 +518,13 @@ class LinkerTest extends MediaWikiLangTestCase {
$pageData = $this->insertPage( $title );
$page = WikiPage::factory( $pageData['title'] );
$updater = $page->newPageUpdater( $user );
$updater->setContent( \MediaWiki\Revision\SlotRecord::MAIN,
new TextContent( 'Technical Wishes 123!' )
);
$summary = CommentStoreComment::newUnsavedComment( 'Some comment!' );
$updater->saveRevision( $summary );
$page->newPageUpdater( $user )
->setContent(
SlotRecord::MAIN,
new TextContent( 'Technical Wishes 123!' )
)
->saveRevision( $summary );
$rollbackOutput = Linker::generateRollback( $page->getRevisionRecord(), $context );
$modules = $context->getOutput()->getModules();

View file

@ -35,8 +35,8 @@ class TitleDefTest extends TypeDefTestCase {
if ( $this->dataName() === 'must exist (success)' ) {
$updater = MediaWikiServices::getInstance()->getWikiPageFactory()
->newFromTitle( Title::newFromText( 'exists' ) )
->newPageUpdater( new User );
$updater->setContent( SlotRecord::MAIN, new WikitextContent( 'exists' ) );
->newPageUpdater( new User )
->setContent( SlotRecord::MAIN, new WikitextContent( 'exists' ) );
$updater->saveRevision( CommentStoreComment::newUnsavedComment( 'test' ) );
$this->assertTrue( $updater->getStatus()->isOK(), 'sanity' );
}

View file

@ -397,10 +397,9 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
$content = new TextContent( $content ?? $summary );
}
$updater = $page->newPageUpdater( $authority );
$updater->setContent( SlotRecord::MAIN, $content );
$rev = $updater->saveRevision( $comment );
return $rev;
return $page->newPageUpdater( $authority )
->setContent( SlotRecord::MAIN, $content )
->saveRevision( $comment );
}
/**
@ -643,12 +642,12 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
$title = $this->getDummyTitle( __METHOD__ );
$page = WikiPage::factory( $title );
$updater = $page->newPageUpdater( $authority );
$summary = CommentStoreComment::newUnsavedComment( 'Lorem ipsum ' . $patrolled );
$updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum ' . $patrolled ) );
$updater->setRcPatrolStatus( $patrolled );
$rev = $updater->saveRevision( $summary );
$rev = $page->newPageUpdater( $authority )
->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum ' . $patrolled ) )
->setRcPatrolStatus( $patrolled )
->saveRevision( $summary );
$rc = $revisionStore->getRecentChange( $rev );
$this->assertEquals( $patrolled, $rc->getAttribute( 'rc_patrolled' ) );
@ -821,9 +820,9 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
$title = $this->getDummyTitle( __METHOD__ );
$page = WikiPage::factory( $title );
$updater = $page->newPageUpdater( $authority );
$updater->setUseAutomaticEditSummaries( true );
$updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
$updater = $page->newPageUpdater( $authority )
->setUseAutomaticEditSummaries( true )
->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
// empty comment triggers auto-summary
$summary = CommentStoreComment::newUnsavedComment( '' );
@ -834,9 +833,9 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
$this->assertSame( '(autosumm-new: Lorem Ipsum)', $comment->text, 'comment text' );
// check that this also works when blanking the page
$updater = $page->newPageUpdater( $authority );
$updater->setUseAutomaticEditSummaries( true );
$updater->setContent( SlotRecord::MAIN, new TextContent( '' ) );
$updater = $page->newPageUpdater( $authority )
->setUseAutomaticEditSummaries( true )
->setContent( SlotRecord::MAIN, new TextContent( '' ) );
$summary = CommentStoreComment::newUnsavedComment( '' );
$updater->saveRevision( $summary, EDIT_AUTOSUMMARY );
@ -849,9 +848,9 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
$title2 = $this->getDummyTitle( __METHOD__ . '/2' );
$page2 = WikiPage::factory( $title2 );
$updater = $page2->newPageUpdater( $authority );
$updater->setUseAutomaticEditSummaries( false );
$updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
$updater = $page2->newPageUpdater( $authority )
->setUseAutomaticEditSummaries( false )
->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
$summary = CommentStoreComment::newUnsavedComment( '' );
$updater->saveRevision( $summary, EDIT_AUTOSUMMARY );
@ -861,9 +860,9 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
$this->assertSame( '', $comment->text, 'comment text should still be blank' );
// check that we don't do auto.summaries without the EDIT_AUTOSUMMARY flag
$updater = $page2->newPageUpdater( $authority );
$updater->setUseAutomaticEditSummaries( true );
$updater->setContent( SlotRecord::MAIN, new TextContent( '' ) );
$updater = $page2->newPageUpdater( $authority )
->setUseAutomaticEditSummaries( true )
->setContent( SlotRecord::MAIN, new TextContent( '' ) );
$summary = CommentStoreComment::newUnsavedComment( '' );
$updater->saveRevision( $summary, 0 );
@ -888,10 +887,10 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
$title = $this->getDummyTitle( __METHOD__ . ( $use ? '_logged' : '_unlogged' ) );
$page = WikiPage::factory( $title );
$updater = $page->newPageUpdater( $authority );
$updater->setUsePageCreationLog( $use );
$summary = CommentStoreComment::newUnsavedComment( 'cmt' );
$updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
$updater = $page->newPageUpdater( $authority )
->setUsePageCreationLog( $use )
->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
$updater->saveRevision( $summary, EDIT_NEW );
$rev = $updater->getNewRevision();
@ -969,9 +968,8 @@ class PageUpdaterTest extends MediaWikiIntegrationTestCase {
$this->insertPage( $title );
$page = WikiPage::factory( $title );
$updater = $page->newPageUpdater( $authority );
$updater->setContent( SlotRecord::MAIN, new \WikitextContent( $wikitext ) );
$updater = $page->newPageUpdater( $authority )
->setContent( SlotRecord::MAIN, new \WikitextContent( $wikitext ) );
$summary = CommentStoreComment::newUnsavedComment( 'Just a test' );
$rev = $updater->saveRevision( $summary, EDIT_UPDATE );

View file

@ -6,6 +6,7 @@ use MediaWiki\Block\Restriction\NamespaceRestriction;
use MediaWiki\Block\Restriction\PageRestriction;
use MediaWiki\Block\SystemBlock;
use MediaWiki\MediaWikiServices;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Tests\Unit\DummyServicesTrait;
use MediaWiki\User\CentralId\CentralIdLookupFactory;
use MediaWiki\User\UserIdentityValue;
@ -1650,9 +1651,9 @@ class UserTest extends MediaWikiIntegrationTestCase {
private static function makeEdit( User $user, $title, $content, $comment ) {
$page = WikiPage::factory( Title::newFromText( $title ) );
$content = ContentHandler::makeContent( $content, $page->getTitle() );
$updater = $page->newPageUpdater( $user );
$updater->setContent( 'main', $content );
return $updater->saveRevision( CommentStoreComment::newUnsavedComment( $comment ) );
return $page->newPageUpdater( $user )
->setContent( SlotRecord::MAIN, $content )
->saveRevision( CommentStoreComment::newUnsavedComment( $comment ) );
}
/**