57 $dbw = $this->dbProvider->getPrimaryDatabase();
59 # Sneak a single revision into place
60 $user = $importableRevision->
getUserObj() ?: $this->userFactory->newFromName( $importableRevision->
getUser() );
62 $userId = $user->getId();
63 $userText = $user->getName();
66 $userText = $importableRevision->
getUser();
67 $user = $this->userFactory->newAnonymous();
73 $page = $this->wikiPageFactory->newFromTitle( $importableRevision->
getTitle() );
74 $page->loadPageData( IDBAccessObject::READ_LATEST );
75 $mustCreatePage = !$page->exists();
76 if ( $mustCreatePage ) {
77 $pageId = $page->insertOn( $dbw );
79 $pageId = $page->getId();
84 $prior = (bool)$dbw->newSelectQueryBuilder()
88 'rev_page' => $pageId,
89 'rev_timestamp' => $dbw->timestamp( $importableRevision->
getTimestamp() ),
92 ->caller( __METHOD__ )->fetchField();
95 $this->logger->debug( __METHOD__ .
": skipping existing revision for [[" .
96 $importableRevision->
getTitle()->getPrefixedText() .
"]], timestamp " .
106 $this->logger->debug( __METHOD__ .
': got invalid $pageId when importing revision of [[' .
107 $importableRevision->
getTitle()->getPrefixedText() .
']], timestamp ' .
115 $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $dbw )
117 ->where( [
'rev_page' => $pageId ] )
118 ->andWhere( $dbw->expr(
119 'rev_timestamp',
'<=', $dbw->timestamp( $importableRevision->
getTimestamp() )
121 ->orderBy( [
'rev_timestamp',
'rev_id' ], SelectQueryBuilder::SORT_DESC );
122 $prevRevRow = $queryBuilder->caller( __METHOD__ )->fetchRow();
124 # @todo FIXME: Use original rev_id optionally (better for backups)
127 $revisionRecord->setParentId( $prevRevRow ? (
int)$prevRevRow->rev_id : 0 );
128 $revisionRecord->setComment(
129 CommentStoreComment::newUnsavedComment( $importableRevision->
getComment() )
133 $revUser = $this->userFactory->newFromAnyId( $userId, $userText );
134 }
catch ( InvalidArgumentException $ex ) {
135 $revUser = RequestContext::getMain()->getUser();
137 $revisionRecord->setUser( $revUser );
139 $originalRevision = $prevRevRow
140 ? $this->revisionStore->newRevisionFromRow(
142 IDBAccessObject::READ_LATEST,
147 foreach ( $importableRevision->
getSlotRoles() as $role ) {
148 if ( !$this->slotRoleRegistry->isDefinedRole( $role ) ) {
149 throw new RuntimeException(
"Undefined slot role $role" );
152 $newContent = $importableRevision->
getContent( $role );
153 if ( !$originalRevision || !$originalRevision->hasSlot( $role ) ) {
154 $revisionRecord->setContent( $role, $newContent );
156 $originalSlot = $originalRevision->getSlot( $role );
157 if ( !$originalSlot->hasSameContent( $importableRevision->
getSlot( $role ) ) ) {
158 $revisionRecord->setContent( $role, $newContent );
160 $revisionRecord->inheritSlot( $originalRevision->getSlot( $role ) );
165 $revisionRecord->setTimestamp( $importableRevision->
getTimestamp() );
166 $revisionRecord->setMinorEdit( $importableRevision->
getMinor() );
167 $revisionRecord->setPageId( $pageId );
169 $latestRevId = $page->getLatest();
171 $inserted = $this->revisionStore->insertRevisionOn( $revisionRecord, $dbw );
172 if ( $latestRevId ) {
175 $latestRevTimestamp = (int)$this->revisionStore->getTimestampFromId(
177 IDBAccessObject::READ_LATEST
180 $latestRevTimestamp = 0;
182 if ( $importableRevision->
getTimestamp() >= $latestRevTimestamp ) {
183 $changed = $page->updateRevisionOn( $dbw, $inserted, $latestRevId );
188 $tags = $importableRevision->
getTags();
189 if ( $tags !== [] ) {
193 if ( $changed !==
false && $this->doUpdates ) {
194 $this->logger->debug( __METHOD__ .
": running updates" );
198 'created' => $mustCreatePage,
199 'oldcountable' =>
'no-change',
200 'causeAction' =>
'import-page',
201 'causeAgent' => $user->getName(),
204 $updater = $this->pageUpdaterFactory->newDerivedPageDataUpdater( $page );
205 $updater->prepareUpdate( $inserted, $options );
206 $updater->doUpdates();