X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fimport%2FWikiImporter.php;h=8991f5ef4d4a72193a91973d6743384868cc62d5;hb=ee56f00ddf0609082f8ae9a4dc3e6e1b6f54ddfd;hp=28f3f824e51edce32a70d5c896e2c17c275d8829;hpb=08edb27f6ce31c676660a4ef89b87da79bde2cc2;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/import/WikiImporter.php b/includes/import/WikiImporter.php index 28f3f824e5..8991f5ef4d 100644 --- a/includes/import/WikiImporter.php +++ b/includes/import/WikiImporter.php @@ -47,9 +47,8 @@ class WikiImporter { private $countableCache = []; /** @var bool */ private $disableStatisticsUpdate = false; - private $usernamePrefix = 'imported'; - private $assignKnownUsers = false; - private $triedCreations = []; + /** @var ExternalUserNames */ + private $externalUserNames; /** * Creates an ImportXMLReader drawing from the source provided @@ -66,7 +65,7 @@ class WikiImporter { $this->config = $config; if ( !in_array( 'uploadsource', stream_get_wrappers() ) ) { - stream_wrapper_register( 'uploadsource', 'UploadSourceAdapter' ); + stream_wrapper_register( 'uploadsource', UploadSourceAdapter::class ); } $id = UploadSourceAdapter::registerSource( $source ); @@ -94,6 +93,7 @@ class WikiImporter { $this->setPageOutCallback( [ $this, 'finishImportPage' ] ); $this->importTitleFactory = new NaiveImportTitleFactory(); + $this->externalUserNames = new ExternalUserNames( 'imported', false ); } /** @@ -125,7 +125,9 @@ class WikiImporter { if ( is_callable( $this->mNoticeCallback ) ) { call_user_func( $this->mNoticeCallback, $msg, $params ); } else { # No ImportReporter -> CLI - echo wfMessage( $msg, $params )->text() . "\n"; + // T177997: the command line importers should call setNoticeCallback() + // for their own custom callback to echo the notice + wfDebug( wfMessage( $msg, $params )->text() . "\n" ); } } @@ -320,8 +322,7 @@ class WikiImporter { * @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; + $this->externalUserNames = new ExternalUserNames( $usernamePrefix, $assignKnownUsers ); } /** @@ -730,9 +731,11 @@ class WikiImporter { } if ( !isset( $logInfo['contributor']['username'] ) ) { - $revision->setUsername( $this->usernamePrefix . '>Unknown user' ); + $revision->setUsername( $this->externalUserNames->addPrefix( 'Unknown user' ) ); } else { - $revision->setUsername( $this->prefixUsername( $logInfo['contributor']['username'] ) ); + $revision->setUsername( + $this->externalUserNames->applyPrefix( $logInfo['contributor']['username'] ) + ); } return $this->logItemCallback( $revision ); @@ -926,9 +929,11 @@ class WikiImporter { if ( isset( $revisionInfo['contributor']['ip'] ) ) { $revision->setUserIP( $revisionInfo['contributor']['ip'] ); } elseif ( isset( $revisionInfo['contributor']['username'] ) ) { - $revision->setUsername( $this->prefixUsername( $revisionInfo['contributor']['username'] ) ); + $revision->setUsername( + $this->externalUserNames->applyPrefix( $revisionInfo['contributor']['username'] ) + ); } else { - $revision->setUsername( $this->usernamePrefix . '>Unknown user' ); + $revision->setUsername( $this->externalUserNames->addPrefix( 'Unknown user' ) ); } if ( isset( $revisionInfo['sha1'] ) ) { $revision->setSha1Base36( $revisionInfo['sha1'] ); @@ -1035,43 +1040,15 @@ class WikiImporter { $revision->setUserIP( $uploadInfo['contributor']['ip'] ); } if ( isset( $uploadInfo['contributor']['username'] ) ) { - $revision->setUsername( $this->prefixUsername( $uploadInfo['contributor']['username'] ) ); + $revision->setUsername( + $this->externalUserNames->applyPrefix( $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 */