MediaWiki master
ImportableOldRevisionImporter.php
Go to the documentation of this file.
1<?php
2
13use Psr\Log\LoggerInterface;
17
22
23 private bool $doUpdates;
24 private LoggerInterface $logger;
25 private IConnectionProvider $dbProvider;
26 private RevisionStore $revisionStore;
27 private SlotRoleRegistry $slotRoleRegistry;
28 private WikiPageFactory $wikiPageFactory;
29 private PageUpdaterFactory $pageUpdaterFactory;
30 private UserFactory $userFactory;
31
32 public function __construct(
33 $doUpdates,
34 LoggerInterface $logger,
35 IConnectionProvider $dbProvider,
36 RevisionStore $revisionStore,
37 SlotRoleRegistry $slotRoleRegistry,
38 WikiPageFactory $wikiPageFactory = null,
39 PageUpdaterFactory $pageUpdaterFactory = null,
40 UserFactory $userFactory = null
41 ) {
42 $this->doUpdates = $doUpdates;
43 $this->logger = $logger;
44 $this->dbProvider = $dbProvider;
45 $this->revisionStore = $revisionStore;
46 $this->slotRoleRegistry = $slotRoleRegistry;
47
48 $services = MediaWikiServices::getInstance();
49 // @todo: temporary - remove when FileImporter extension is updated
50 $this->wikiPageFactory = $wikiPageFactory ?? $services->getWikiPageFactory();
51 $this->pageUpdaterFactory = $pageUpdaterFactory ?? $services->getPageUpdaterFactory();
52 $this->userFactory = $userFactory ?? $services->getUserFactory();
53 }
54
56 public function import( ImportableOldRevision $importableRevision, $doUpdates = true ) {
57 $dbw = $this->dbProvider->getPrimaryDatabase();
58
59 # Sneak a single revision into place
60 $user = $importableRevision->getUserObj() ?: $this->userFactory->newFromName( $importableRevision->getUser() );
61 if ( $user ) {
62 $userId = $user->getId();
63 $userText = $user->getName();
64 } else {
65 $userId = 0;
66 $userText = $importableRevision->getUser();
67 $user = $this->userFactory->newAnonymous();
68 }
69
70 // avoid memory leak...?
71 Title::clearCaches();
72
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 );
78 } else {
79 $pageId = $page->getId();
80
81 // Note: sha1 has been in XML dumps since 2012. If you have an
82 // older dump, the duplicate detection here won't work.
83 if ( $importableRevision->getSha1Base36() !== false ) {
84 $prior = (bool)$dbw->newSelectQueryBuilder()
85 ->select( '1' )
86 ->from( 'revision' )
87 ->where( [
88 'rev_page' => $pageId,
89 'rev_timestamp' => $dbw->timestamp( $importableRevision->getTimestamp() ),
90 'rev_sha1' => $importableRevision->getSha1Base36()
91 ] )
92 ->caller( __METHOD__ )->fetchField();
93 if ( $prior ) {
94 // @todo FIXME: This could fail slightly for multiple matches :P
95 $this->logger->debug( __METHOD__ . ": skipping existing revision for [[" .
96 $importableRevision->getTitle()->getPrefixedText() . "]], timestamp " .
97 $importableRevision->getTimestamp() . "\n" );
98 return false;
99 }
100 }
101 }
102
103 if ( !$pageId ) {
104 // This seems to happen if two clients simultaneously try to import the
105 // same page
106 $this->logger->debug( __METHOD__ . ': got invalid $pageId when importing revision of [[' .
107 $importableRevision->getTitle()->getPrefixedText() . ']], timestamp ' .
108 $importableRevision->getTimestamp() . "\n" );
109 return false;
110 }
111
112 // Select previous version to make size diffs correct
113 // @todo This assumes that multiple revisions of the same page are imported
114 // in order from oldest to newest.
115 $queryBuilder = $this->revisionStore->newSelectQueryBuilder( $dbw )
116 ->joinComment()
117 ->where( [ 'rev_page' => $pageId ] )
118 ->andWhere( $dbw->expr(
119 'rev_timestamp', '<=', $dbw->timestamp( $importableRevision->getTimestamp() )
120 ) )
121 ->orderBy( [ 'rev_timestamp', 'rev_id' ], SelectQueryBuilder::SORT_DESC );
122 $prevRevRow = $queryBuilder->caller( __METHOD__ )->fetchRow();
123
124 # @todo FIXME: Use original rev_id optionally (better for backups)
125 # Insert the row
126 $revisionRecord = new MutableRevisionRecord( $importableRevision->getTitle() );
127 $revisionRecord->setParentId( $prevRevRow ? (int)$prevRevRow->rev_id : 0 );
128 $revisionRecord->setComment(
129 CommentStoreComment::newUnsavedComment( $importableRevision->getComment() )
130 );
131
132 try {
133 $revUser = $this->userFactory->newFromAnyId( $userId, $userText );
134 } catch ( InvalidArgumentException $ex ) {
135 $revUser = RequestContext::getMain()->getUser();
136 }
137 $revisionRecord->setUser( $revUser );
138
139 $originalRevision = $prevRevRow
140 ? $this->revisionStore->newRevisionFromRow(
141 $prevRevRow,
142 IDBAccessObject::READ_LATEST,
143 $importableRevision->getTitle()
144 )
145 : null;
146
147 foreach ( $importableRevision->getSlotRoles() as $role ) {
148 if ( !$this->slotRoleRegistry->isDefinedRole( $role ) ) {
149 throw new RuntimeException( "Undefined slot role $role" );
150 }
151
152 $newContent = $importableRevision->getContent( $role );
153 if ( !$originalRevision || !$originalRevision->hasSlot( $role ) ) {
154 $revisionRecord->setContent( $role, $newContent );
155 } else {
156 $originalSlot = $originalRevision->getSlot( $role );
157 if ( !$originalSlot->hasSameContent( $importableRevision->getSlot( $role ) ) ) {
158 $revisionRecord->setContent( $role, $newContent );
159 } else {
160 $revisionRecord->inheritSlot( $originalRevision->getSlot( $role ) );
161 }
162 }
163 }
164
165 $revisionRecord->setTimestamp( $importableRevision->getTimestamp() );
166 $revisionRecord->setMinorEdit( $importableRevision->getMinor() );
167 $revisionRecord->setPageId( $pageId );
168
169 $latestRevId = $page->getLatest();
170
171 $inserted = $this->revisionStore->insertRevisionOn( $revisionRecord, $dbw );
172 if ( $latestRevId ) {
173 // If not found (false), cast to 0 so that the page is updated
174 // Just to be on the safe side, even though it should always be found
175 $latestRevTimestamp = (int)$this->revisionStore->getTimestampFromId(
176 $latestRevId,
177 IDBAccessObject::READ_LATEST
178 );
179 } else {
180 $latestRevTimestamp = 0;
181 }
182 if ( $importableRevision->getTimestamp() >= $latestRevTimestamp ) {
183 $changed = $page->updateRevisionOn( $dbw, $inserted, $latestRevId );
184 } else {
185 $changed = false;
186 }
187
188 $tags = $importableRevision->getTags();
189 if ( $tags !== [] ) {
190 ChangeTags::addTags( $tags, null, $inserted->getId() );
191 }
192
193 if ( $changed !== false && $this->doUpdates ) {
194 $this->logger->debug( __METHOD__ . ": running updates" );
195 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
196
197 $options = [
198 'created' => $mustCreatePage,
199 'oldcountable' => 'no-change',
200 'causeAction' => 'import-page',
201 'causeAgent' => $user->getName(),
202 ];
203
204 $updater = $this->pageUpdaterFactory->newDerivedPageDataUpdater( $page );
205 $updater->prepareUpdate( $inserted, $options );
206 $updater->doUpdates();
207 }
208
209 return true;
210 }
211
212}
static addTags( $tags, $rc_id=null, $rev_id=null, $log_id=null, $params=null, RecentChange $rc=null)
Add tags to a change given its rc_id, rev_id and/or log_id.
__construct( $doUpdates, LoggerInterface $logger, IConnectionProvider $dbProvider, RevisionStore $revisionStore, SlotRoleRegistry $slotRoleRegistry, WikiPageFactory $wikiPageFactory=null, PageUpdaterFactory $pageUpdaterFactory=null, UserFactory $userFactory=null)
Value object for a comment stored by CommentStore.
Group all the pieces relevant to the context of a request into one instance.
Service locator for MediaWiki core services.
Service for creating WikiPage objects.
Service for looking up page revisions.
A registry service for SlotRoleHandlers, used to define which slot roles are available on which page.
A factory for PageUpdater and DerivedPageDataUpdater instances.
Represents a title within MediaWiki.
Definition Title.php:78
Creates User objects.
Build SELECT queries with a fluent interface.
getContent( $role=SlotRecord::MAIN)
Provide primary and replica IDatabase connections.
Interface for database access objects.