Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
ImportTranslatableBundleMaintenanceScript.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
8use MediaWiki\MediaWikiServices;
9use MediaWiki\User\UserIdentity;
10use Title;
11
19 public function __construct() {
20 parent::__construct();
21 $this->addArg(
22 'xml-path',
23 'Path to the XML file to be imported',
24 self::REQUIRED
25 );
26 $this->addOption(
27 'user',
28 'Name of the user performing the import',
29 self::REQUIRED,
30 self::HAS_ARG
31 );
32 $this->addOption(
33 'interwiki-prefix',
34 'Prefix to apply to unknown (and possibly also known) usernames',
35 self::REQUIRED,
36 self::HAS_ARG
37 );
38 $this->addOption(
39 'comment',
40 'Comment added to the log for the import',
41 self::OPTIONAL,
42 self::HAS_ARG
43 );
44 $this->addOption(
45 'assign-known-users',
46 'Whether to apply the prefix to usernames that exist locally',
47 self::OPTIONAL
48 );
49
50 $this->requireExtension( 'Translate' );
51 }
52
54 public function execute() {
55 $importFilePath = $this->getFilePathToImport();
56 $importUser = $this->getImportUser();
57 $comment = $this->getOption( 'comment' );
58 $interwikiPrefix = $this->getInterwikiPrefix();
59 $assignKnownUsers = $this->hasOption( 'assign-known-users' );
60
61 try {
62 $importer = Services::getInstance()->getTranslatableBundleImporter();
63 $importer->setImportCompleteCallback( [ $this, 'logImportComplete' ] );
64 $bundleTitle = $importer->import(
65 $importFilePath,
66 $interwikiPrefix,
67 $assignKnownUsers,
68 $importUser,
69 $comment
70 );
72 $this->fatalError( "An error occurred during import: {$e->getMessage()}\n" );
73 }
74
75 $this->output(
76 "You should now mark the page '{$bundleTitle->getPrefixedText()}' for translation.\n"
77 );
78 }
79
80 public function logImportComplete( Title $title ): void {
81 $this->output( "Completed import of translatable bundle. Created page '{$title->getPrefixedText()}'\n" );
82 }
83
84 private function getFilePathToImport(): string {
85 $xmlPath = $this->getArg( 'xml-path' );
86 if ( !file_exists( $xmlPath ) ) {
87 $this->fatalError( "File '$xmlPath' does not exist" );
88 }
89
90 if ( !is_readable( $xmlPath ) ) {
91 $this->fatalError( "File '$xmlPath' is not readable" );
92 }
93
94 return $xmlPath;
95 }
96
97 private function getImportUser(): UserIdentity {
98 $username = $this->getOption( 'user' );
99
100 $userFactory = MediaWikiServices::getInstance()->getUserFactory();
101 $user = $userFactory->newFromName( $username );
102
103 if ( $user === null || !$user->isNamed() ) {
104 $this->fatalError( "User $username does not exist." );
105 }
106
107 return $user;
108 }
109
110 private function getInterwikiPrefix(): string {
111 $interwikiPrefix = trim( $this->getOption( 'interwiki-prefix', '' ) );
112 if ( $interwikiPrefix === '' ) {
113 $this->fatalError( 'Argument interwiki-prefix cannot be empty.' );
114 }
115
116 return $interwikiPrefix;
117 }
118}
Exception thrown when an error occurs when importing a translatable bundle via TranslatableBundleImpo...
Minimal service container.
Definition Services.php:44
Constants for making code for maintenance scripts more readable.