MediaWiki  1.32.5
ImportableOldRevisionImporter.php
Go to the documentation of this file.
1 <?php
2 
3 use Psr\Log\LoggerInterface;
5 
10 
14  private $logger;
15 
19  private $doUpdates;
20 
24  private $loadBalancer;
25 
31  public function __construct(
32  $doUpdates,
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...?
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 }
ImportableOldRevision\getFormat
getFormat()
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
ImportableOldRevisionImporter\$doUpdates
bool $doUpdates
Definition: ImportableOldRevisionImporter.php:19
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
ImportableOldRevisionImporter
Definition: ImportableOldRevisionImporter.php:9
Title\clearCaches
static clearCaches()
Text form (spaces not underscores) of the main part.
Definition: Title.php:3709
ImportableOldRevision\getTimestamp
getTimestamp()
ImportableOldRevision\getContent
getContent()
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:592
User
User
Definition: All_system_messages.txt:425
ImportableOldRevisionImporter\$logger
LoggerInterface $logger
Definition: ImportableOldRevisionImporter.php:14
Revision\insertOn
insertOn( $dbw)
Insert a new revision into the database, returning the new revision ID number on success and dies hor...
Definition: Revision.php:1122
ImportableOldRevision\getUser
getUser()
php
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:35
ImportableOldRevision\getMinor
getMinor()
Revision
Definition: Revision.php:41
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:127
ImportableOldRevision\getSha1Base36
getSha1Base36()
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:41
ImportableOldRevision
Definition: ImportableOldRevision.php:6
ImportableOldRevision\getModel
getModel()
ImportableOldRevision\getTitle
getTitle()
ImportableOldRevisionImporter\__construct
__construct( $doUpdates, LoggerInterface $logger, LoadBalancer $loadBalancer)
Definition: ImportableOldRevisionImporter.php:31
ImportableOldRevision\getComment
getComment()
Wikimedia\Rdbms\LoadBalancer\getConnectionRef
getConnectionRef( $db, $groups=[], $domain=false, $flags=0)
Get a database connection handle reference.
Definition: LoadBalancer.php:848
OldRevisionImporter
Definition: OldRevisionImporter.php:6
true
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:2044
ImportableOldRevisionImporter\$loadBalancer
LoadBalancer $loadBalancer
Definition: ImportableOldRevisionImporter.php:24
ImportableOldRevision\getUserObj
getUserObj()