MediaWiki REL1_33
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
__construct( $doUpdates, LoggerInterface $logger, LoadBalancer $loadBalancer)
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:585
Database connection, tracking, load balancing, and transaction manager for a cluster.
getConnectionRef( $i, $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:2004
return true to allow those checks to and false if checking is done & $user
Definition hooks.txt:1510
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
const DB_MASTER
Definition defines.php:26