X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22messagerie%22%29%20.%20%22?a=blobdiff_plain;f=includes%2Fimport%2FWikiImporter.php;h=1424f3363755b656960e7773af0e8d531aa5eb36;hb=d0142faf253e069473888dc378979b804fb2b07e;hp=7c89b23a2097a47103ffeb560dea0a281bff64bb;hpb=c05926bc7ce57cbea42e90053ef799fa58b9da1e;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/import/WikiImporter.php b/includes/import/WikiImporter.php index 7c89b23a20..1424f33637 100644 --- a/includes/import/WikiImporter.php +++ b/includes/import/WikiImporter.php @@ -23,7 +23,6 @@ * @file * @ingroup SpecialPage */ -use MediaWiki\MediaWikiServices; /** * XML file reader for the page data importer. @@ -48,6 +47,9 @@ class WikiImporter { private $countableCache = []; /** @var bool */ private $disableStatisticsUpdate = false; + private $usernamePrefix = 'imported'; + private $assignKnownUsers = false; + private $triedCreations = []; /** * Creates an ImportXMLReader drawing from the source provided @@ -55,16 +57,12 @@ class WikiImporter { * @param Config $config * @throws Exception */ - function __construct( ImportSource $source, Config $config = null ) { + function __construct( ImportSource $source, Config $config ) { if ( !class_exists( 'XMLReader' ) ) { throw new Exception( 'Import requires PHP to have been compiled with libxml support' ); } $this->reader = new XMLReader(); - if ( !$config ) { - wfDeprecated( __METHOD__ . ' without a Config instance', '1.25' ); - $config = MediaWikiServices::getInstance()->getMainConfig(); - } $this->config = $config; if ( !in_array( 'uploadsource', stream_get_wrappers() ) ) { @@ -316,6 +314,16 @@ class WikiImporter { $this->mImportUploads = $import; } + /** + * @since 1.31 + * @param string $usernamePrefix Prefix to apply to unknown (and possibly also known) usernames + * @param bool $assignKnownUsers Whether to apply the prefix to usernames that exist locally + */ + public function setUsernamePrefix( $usernamePrefix, $assignKnownUsers ) { + $this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' ); + $this->assignKnownUsers = (bool)$assignKnownUsers; + } + /** * Statistics update can cause a lot of time * @since 1.29 @@ -551,6 +559,7 @@ class WikiImporter { /** * Primary entry point + * @throws Exception * @throws MWException * @return bool */ @@ -721,9 +730,9 @@ class WikiImporter { } if ( !isset( $logInfo['contributor']['username'] ) ) { - $revision->setUsername( 'Unknown user' ); + $revision->setUsername( $this->usernamePrefix . '>Unknown user' ); } else { - $revision->setUsername( $logInfo['contributor']['username'] ); + $revision->setUsername( $this->prefixUsername( $logInfo['contributor']['username'] ) ); } return $this->logItemCallback( $revision ); @@ -817,7 +826,7 @@ class WikiImporter { $this->debug( "Enter revision handler" ); $revisionInfo = []; - $normalFields = [ 'id', 'timestamp', 'comment', 'minor', 'model', 'format', 'text' ]; + $normalFields = [ 'id', 'timestamp', 'comment', 'minor', 'model', 'format', 'text', 'sha1' ]; $skip = false; @@ -852,6 +861,7 @@ class WikiImporter { /** * @param array $pageInfo * @param array $revisionInfo + * @throws MWException * @return bool|mixed */ private function processRevision( $pageInfo, $revisionInfo ) { @@ -916,9 +926,12 @@ class WikiImporter { if ( isset( $revisionInfo['contributor']['ip'] ) ) { $revision->setUserIP( $revisionInfo['contributor']['ip'] ); } elseif ( isset( $revisionInfo['contributor']['username'] ) ) { - $revision->setUsername( $revisionInfo['contributor']['username'] ); + $revision->setUsername( $this->prefixUsername( $revisionInfo['contributor']['username'] ) ); } else { - $revision->setUsername( 'Unknown user' ); + $revision->setUsername( $this->usernamePrefix . '>Unknown user' ); + } + if ( isset( $revisionInfo['sha1'] ) ) { + $revision->setSha1Base36( $revisionInfo['sha1'] ); } $revision->setNoUpdates( $this->mNoUpdates ); @@ -1022,13 +1035,43 @@ class WikiImporter { $revision->setUserIP( $uploadInfo['contributor']['ip'] ); } if ( isset( $uploadInfo['contributor']['username'] ) ) { - $revision->setUsername( $uploadInfo['contributor']['username'] ); + $revision->setUsername( $this->prefixUsername( $uploadInfo['contributor']['username'] ) ); } $revision->setNoUpdates( $this->mNoUpdates ); return call_user_func( $this->mUploadCallback, $revision ); } + /** + * Add an interwiki prefix to the username, if appropriate + * @since 1.31 + * @param string $name Name being imported + * @return string Name, possibly with the prefix prepended. + */ + protected function prefixUsername( $name ) { + if ( !User::isUsableName( $name ) ) { + return $name; + } + + if ( $this->assignKnownUsers ) { + if ( User::idFromName( $name ) ) { + return $name; + } + + // See if any extension wants to create it. + if ( !isset( $this->triedCreations[$name] ) ) { + $this->triedCreations[$name] = true; + if ( !Hooks::run( 'ImportHandleUnknownUser', [ $name ] ) && + User::idFromName( $name, User::READ_LATEST ) + ) { + return $name; + } + } + } + + return substr( $this->usernamePrefix . '>' . $name, 0, 255 ); + } + /** * @return array */