52 $dbw = $this->dbProvider->getPrimaryDatabase();
54 # Sneak a single revision into place
55 $user = $importableRevision->
getUserObj() ?: $this->userFactory->newFromName( $importableRevision->
getUser() );
57 $userId = $user->getId();
58 $userText = $user->getName();
61 $userText = $importableRevision->
getUser();
67 $page = $this->wikiPageFactory->newFromTitle( $importableRevision->
getTitle() );
68 $page->loadPageData( IDBAccessObject::READ_LATEST );
69 $mustCreatePage = !$page->exists();
70 if ( $mustCreatePage ) {
71 $pageId = $page->insertOn( $dbw );
73 $pageId = $page->getId();
79 if ( $importContentHash ) {
81 $revIds = $dbw->newSelectQueryBuilder()
85 'rev_page' => $pageId,
86 'rev_timestamp' => $dbw->timestamp( $importableRevision->
getTimestamp() ),
88 ->caller( __METHOD__ )
91 foreach ( $revIds as $revId ) {
92 $revision = $this->revisionStore->getRevisionById( $revId );
94 throw new RuntimeException(
"Revision $revId not found" );
97 if ( $revision->getSha1() === $importContentHash ) {
98 $this->logger->debug( __METHOD__ .
": skipping existing revision for [[" .
99 $importableRevision->
getTitle()->getPrefixedText() .
"]], timestamp " .
110 $this->logger->debug( __METHOD__ .
': got invalid $pageId when importing revision of [[' .
111 $importableRevision->
getTitle()->getPrefixedText() .
']], timestamp ' .
119 $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $dbw )
121 ->where( [
'rev_page' => $pageId ] )
122 ->andWhere( $dbw->expr(
123 'rev_timestamp',
'<=', $dbw->timestamp( $importableRevision->
getTimestamp() )
125 ->orderBy( [
'rev_timestamp',
'rev_id' ], SelectQueryBuilder::SORT_DESC );
126 $prevRevRow = $queryBuilder->caller( __METHOD__ )->fetchRow();
128 # @todo FIXME: Use original rev_id optionally (better for backups)
131 $revisionRecord->setParentId( $prevRevRow ? (
int)$prevRevRow->rev_id : 0 );
132 $revisionRecord->setComment(
133 CommentStoreComment::newUnsavedComment( $importableRevision->
getComment() )
137 $revUser = $this->userFactory->newFromAnyId( $userId, $userText );
138 }
catch ( InvalidArgumentException ) {
139 $revUser = RequestContext::getMain()->getUser();
141 $revisionRecord->setUser( $revUser );
143 $originalRevision = $prevRevRow
144 ? $this->revisionStore->newRevisionFromRow(
146 IDBAccessObject::READ_LATEST,
151 foreach ( $importableRevision->
getSlotRoles() as $role ) {
152 if ( !$this->slotRoleRegistry->isDefinedRole( $role ) ) {
153 throw new RuntimeException(
"Undefined slot role $role" );
156 $newContent = $importableRevision->
getContent( $role );
157 if ( !$originalRevision || !$originalRevision->hasSlot( $role ) ) {
158 $revisionRecord->setContent( $role, $newContent );
160 $originalSlot = $originalRevision->getSlot( $role );
161 if ( !$originalSlot->hasSameContent( $importableRevision->
getSlot( $role ) ) ) {
162 $revisionRecord->setContent( $role, $newContent );
164 $revisionRecord->inheritSlot( $originalRevision->getSlot( $role ) );
169 $revisionRecord->setTimestamp( $importableRevision->
getTimestamp() );
170 $revisionRecord->setMinorEdit( $importableRevision->
getMinor() );
171 $revisionRecord->setPageId( $pageId );
173 $updater = $this->pageUpdaterFactory->newDerivedPageDataUpdater( $page );
174 $latestRev = $updater->grabLatestRevision();
175 $latestRevId = $latestRev ? $latestRev->getId() :
null;
177 $inserted = $this->revisionStore->insertRevisionOn( $revisionRecord, $dbw );
181 $latestRevTimestamp = $latestRev->getTimestamp();
183 $latestRevTimestamp = 0;
185 if ( $importableRevision->
getTimestamp() >= $latestRevTimestamp ) {
186 $changed = $page->updateRevisionOn( $dbw, $inserted, $latestRevId );
191 $tags = $importableRevision->
getTags();
192 if ( $tags !== [] ) {
196 if ( $changed !==
false && $this->doUpdates ) {
197 $this->logger->debug( __METHOD__ .
": running updates" );
201 PageLatestRevisionChangedEvent::FLAG_SILENT =>
true,
202 PageLatestRevisionChangedEvent::FLAG_IMPLICIT =>
true,
203 'created' => $mustCreatePage,
204 'oldcountable' =>
'no-change',
207 $updater->setCause( PageUpdater::CAUSE_IMPORT );
208 $updater->setPerformer( RequestContext::getMain()->getUser() );
209 $updater->prepareUpdate( $inserted, $options );
210 $updater->doUpdates();