MediaWiki REL1_31
ImportableOldRevisionImporter.php
Go to the documentation of this file.
1<?php
2
3use Psr\Log\LoggerInterface;
5
10
14 private $logger;
15
19 private $doUpdates;
20
25
31 public function __construct(
33 LoggerInterface $logger,
35 ) {
36 $this->doUpdates = $doUpdates;
37 $this->logger = $logger;
38 $this->loadBalancer = $loadBalancer;
39 }
40
41 public function import( ImportableOldRevision $importableRevision, $doUpdates = true ) {
42 $dbw = $this->loadBalancer->getConnectionRef( DB_MASTER );
43
44 # Sneak a single revision into place
45 $user = $importableRevision->getUserObj() ?: User::newFromName( $importableRevision->getUser() );
46 if ( $user ) {
47 $userId = intval( $user->getId() );
48 $userText = $user->getName();
49 } else {
50 $userId = 0;
51 $userText = $importableRevision->getUser();
52 $user = new User;
53 }
54
55 // avoid memory leak...?
56 Title::clearCaches();
57
58 $page = WikiPage::factory( $importableRevision->getTitle() );
59 $page->loadPageData( 'fromdbmaster' );
60 if ( !$page->exists() ) {
61 // must create the page...
62 $pageId = $page->insertOn( $dbw );
63 $created = true;
64 $oldcountable = null;
65 } else {
66 $pageId = $page->getId();
67 $created = false;
68
69 // Note: sha1 has been in XML dumps since 2012. If you have an
70 // older dump, the duplicate detection here won't work.
71 if ( $importableRevision->getSha1Base36() !== false ) {
72 $prior = $dbw->selectField( 'revision', '1',
73 [ 'rev_page' => $pageId,
74 'rev_timestamp' => $dbw->timestamp( $importableRevision->getTimestamp() ),
75 'rev_sha1' => $importableRevision->getSha1Base36() ],
76 __METHOD__
77 );
78 if ( $prior ) {
79 // @todo FIXME: This could fail slightly for multiple matches :P
80 $this->logger->debug( __METHOD__ . ": skipping existing revision for [[" .
81 $importableRevision->getTitle()->getPrefixedText() . "]], timestamp " .
82 $importableRevision->getTimestamp() . "\n" );
83 return false;
84 }
85 }
86 }
87
88 if ( !$pageId ) {
89 // This seems to happen if two clients simultaneously try to import the
90 // same page
91 $this->logger->debug( __METHOD__ . ': got invalid $pageId when importing revision of [[' .
92 $importableRevision->getTitle()->getPrefixedText() . ']], timestamp ' .
93 $importableRevision->getTimestamp() . "\n" );
94 return false;
95 }
96
97 // Select previous version to make size diffs correct
98 // @todo This assumes that multiple revisions of the same page are imported
99 // in order from oldest to newest.
100 $prevId = $dbw->selectField( 'revision', 'rev_id',
101 [
102 'rev_page' => $pageId,
103 'rev_timestamp <= ' . $dbw->addQuotes( $dbw->timestamp( $importableRevision->getTimestamp() ) ),
104 ],
105 __METHOD__,
106 [ 'ORDER BY' => [
107 'rev_timestamp DESC',
108 'rev_id DESC', // timestamp is not unique per page
109 ]
110 ]
111 );
112
113 # @todo FIXME: Use original rev_id optionally (better for backups)
114 # Insert the row
115 $revision = new Revision( [
116 'title' => $importableRevision->getTitle(),
117 'page' => $pageId,
118 'content_model' => $importableRevision->getModel(),
119 'content_format' => $importableRevision->getFormat(),
120 // XXX: just set 'content' => $wikiRevision->getContent()?
121 'text' => $importableRevision->getContent()->serialize( $importableRevision->getFormat() ),
122 'comment' => $importableRevision->getComment(),
123 'user' => $userId,
124 'user_text' => $userText,
125 'timestamp' => $importableRevision->getTimestamp(),
126 'minor_edit' => $importableRevision->getMinor(),
127 'parent_id' => $prevId,
128 ] );
129 $revision->insertOn( $dbw );
130 $changed = $page->updateIfNewerOn( $dbw, $revision );
131
132 if ( $changed !== false && $this->doUpdates ) {
133 $this->logger->debug( __METHOD__ . ": running updates\n" );
134 // countable/oldcountable stuff is handled in WikiImporter::finishImportPage
135 $page->doEditUpdates(
136 $revision,
137 $user,
138 [ 'created' => $created, 'oldcountable' => 'no-change' ]
139 );
140 }
141
142 return true;
143 }
144
145}
__construct( $doUpdates, LoggerInterface $logger, LoadBalancer $loadBalancer)
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:591
Database connection, tracking, load balancing, and transaction manager for a cluster.
getConnectionRef( $db, $groups=[], $domain=false, $flags=0)
Get a database connection handle reference.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
const DB_MASTER
Definition defines.php:29