19 public function __construct() {
20 parent::__construct();
23 'Path to the XML file to be imported',
28 'Name of the user performing the import',
34 'Prefix to apply to unknown (and possibly also known) usernames',
40 'Comment added to the log for the import',
46 'Whether to apply the prefix to usernames that exist locally',
50 $this->requireExtension(
'Translate' );
55 $importFilePath = $this->getFilePathToImport();
56 $importUser = $this->getImportUser();
57 $comment = $this->getOption(
'comment' );
58 $interwikiPrefix = $this->getInterwikiPrefix();
59 $assignKnownUsers = $this->hasOption(
'assign-known-users' );
62 $importer = Services::getInstance()->getTranslatableBundleImporter();
63 $importer->setImportCompleteCallback( [ $this,
'logImportComplete' ] );
64 $bundleTitle = $importer->import(
72 $this->fatalError(
"An error occurred during import: {$e->getMessage()}\n" );
76 "You should now mark the page '{$bundleTitle->getPrefixedText()}' for translation.\n"
80 public function logImportComplete( Title $title ): void {
81 $this->output(
"Completed import of translatable bundle. Created page '{$title->getPrefixedText()}'\n" );
84 private function getFilePathToImport(): string {
85 $xmlPath = $this->getArg(
'xml-path' );
86 if ( !file_exists( $xmlPath ) ) {
87 $this->fatalError(
"File '$xmlPath' does not exist" );
90 if ( !is_readable( $xmlPath ) ) {
91 $this->fatalError(
"File '$xmlPath' is not readable" );
97 private function getImportUser(): UserIdentity {
98 $username = $this->getOption(
'user' );
100 $userFactory = MediaWikiServices::getInstance()->getUserFactory();
101 $user = $userFactory->newFromName( $username );
103 if ( $user ===
null || !$user->isNamed() ) {
104 $this->fatalError(
"User $username does not exist." );
110 private function getInterwikiPrefix(): string {
111 $interwikiPrefix = trim( $this->getOption(
'interwiki-prefix',
'' ) );
112 if ( $interwikiPrefix ===
'' ) {
113 $this->fatalError(
'Argument interwiki-prefix cannot be empty.' );
116 return $interwikiPrefix;