59 $dbw = $this->dbProvider->getPrimaryDatabase();
61 # Sneak a single revision into place
62 $user = $importableRevision->
getUserObj() ?: $this->userFactory->newFromName( $importableRevision->
getUser() );
64 $userId = $user->getId();
65 $userText = $user->getName();
68 $userText = $importableRevision->
getUser();
69 $user = $this->userFactory->newAnonymous();
75 $page = $this->wikiPageFactory->newFromTitle( $importableRevision->
getTitle() );
76 $page->loadPageData( IDBAccessObject::READ_LATEST );
77 $mustCreatePage = !$page->exists();
78 if ( $mustCreatePage ) {
79 $pageId = $page->insertOn( $dbw );
81 $pageId = $page->getId();
87 if ( $importContentHash ) {
89 $revIds = $dbw->newSelectQueryBuilder()
93 'rev_page' => $pageId,
94 'rev_timestamp' => $dbw->timestamp( $importableRevision->
getTimestamp() ),
96 ->caller( __METHOD__ )
99 foreach ( $revIds as $revId ) {
100 $revision = $this->revisionStore->getRevisionById( $revId );
102 throw new RuntimeException(
"Revision $revId not found" );
105 if ( $revision->getSha1() === $importContentHash ) {
106 $this->logger->debug( __METHOD__ .
": skipping existing revision for [[" .
107 $importableRevision->
getTitle()->getPrefixedText() .
"]], timestamp " .
118 $this->logger->debug( __METHOD__ .
': got invalid $pageId when importing revision of [[' .
119 $importableRevision->
getTitle()->getPrefixedText() .
']], timestamp ' .
127 $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $dbw )
129 ->where( [
'rev_page' => $pageId ] )
130 ->andWhere( $dbw->expr(
131 'rev_timestamp',
'<=', $dbw->timestamp( $importableRevision->
getTimestamp() )
133 ->orderBy( [
'rev_timestamp',
'rev_id' ], SelectQueryBuilder::SORT_DESC );
134 $prevRevRow = $queryBuilder->caller( __METHOD__ )->fetchRow();
136 # @todo FIXME: Use original rev_id optionally (better for backups)
139 $revisionRecord->setParentId( $prevRevRow ? (
int)$prevRevRow->rev_id : 0 );
140 $revisionRecord->setComment(
141 CommentStoreComment::newUnsavedComment( $importableRevision->
getComment() )
145 $revUser = $this->userFactory->newFromAnyId( $userId, $userText );
146 }
catch ( InvalidArgumentException ) {
147 $revUser = RequestContext::getMain()->getUser();
149 $revisionRecord->setUser( $revUser );
151 $originalRevision = $prevRevRow
152 ? $this->revisionStore->newRevisionFromRow(
154 IDBAccessObject::READ_LATEST,
159 foreach ( $importableRevision->
getSlotRoles() as $role ) {
160 if ( !$this->slotRoleRegistry->isDefinedRole( $role ) ) {
161 throw new RuntimeException(
"Undefined slot role $role" );
164 $newContent = $importableRevision->
getContent( $role );
165 if ( !$originalRevision || !$originalRevision->hasSlot( $role ) ) {
166 $revisionRecord->setContent( $role, $newContent );
168 $originalSlot = $originalRevision->getSlot( $role );
169 if ( !$originalSlot->hasSameContent( $importableRevision->
getSlot( $role ) ) ) {
170 $revisionRecord->setContent( $role, $newContent );
172 $revisionRecord->inheritSlot( $originalRevision->getSlot( $role ) );
177 $revisionRecord->setTimestamp( $importableRevision->
getTimestamp() );
178 $revisionRecord->setMinorEdit( $importableRevision->
getMinor() );
179 $revisionRecord->setPageId( $pageId );
181 $updater = $this->pageUpdaterFactory->newDerivedPageDataUpdater( $page );
182 $latestRev = $updater->grabCurrentRevision();
183 $latestRevId = $latestRev ? $latestRev->getId() :
null;
185 $inserted = $this->revisionStore->insertRevisionOn( $revisionRecord, $dbw );
189 $latestRevTimestamp = $latestRev->getTimestamp();
191 $latestRevTimestamp = 0;
193 if ( $importableRevision->
getTimestamp() >= $latestRevTimestamp ) {
194 $changed = $page->updateRevisionOn( $dbw, $inserted, $latestRevId );
199 $tags = $importableRevision->
getTags();
200 if ( $tags !== [] ) {
201 MediaWikiServices::getInstance()->getChangeTagsStore()->addTags( $tags,
null, $inserted->getId() );
204 if ( $changed !==
false && $this->doUpdates ) {
205 $this->logger->debug( __METHOD__ .
": running updates" );
209 PageLatestRevisionChangedEvent::FLAG_SILENT =>
true,
210 PageLatestRevisionChangedEvent::FLAG_IMPLICIT =>
true,
211 'created' => $mustCreatePage,
212 'oldcountable' =>
'no-change',
215 $updater->setCause( PageUpdater::CAUSE_IMPORT );
216 $updater->setPerformer( $user );
217 $updater->prepareUpdate( $inserted, $options );
218 $updater->doUpdates();