19 public function __construct() {
20 parent::__construct();
21 $this->addDescription(
'Script for processing message changes in file based message groups' );
24 '(optional) Comma separated list of group IDs to process (can use * as wildcard). ' .
31 '(optional) Comma separated list of group IDs to not process (can use * ' .
32 'as wildcard). Overrides --group parameter.',
38 '(optional) Unique name to avoid conflicts with multiple invocations of this script.',
44 '(optional) Import "safe" changes: message additions when no other kind of changes.'
47 'skip-group-sync-check',
48 '(optional) Skip importing group if synchronization is still in progress or if there ' .
49 'was an error during synchronization. See: ' .
50 'https://www.mediawiki.org/wiki/Help:Extension:Translate/Group_management#Strong_synchronization'
54 '(optional) Import non renames: if a language in a group has only additions and changes to existing ' .
55 ' strings, then the additions are imported'
57 $this->requireExtension(
'Translate' );
60 public function execute() {
61 $name = $this->getOption(
'name', MessageChangeStorage::DEFAULT_NAME );
63 $this->fatalError(
'Invalid name' );
66 $groups = $this->getGroups();
68 $comparator = Services::getInstance()->getExternalMessageSourceStateComparator();
70 $importStrategy = $this->getImportStrategy();
71 $skipGroupSyncCache = $this->hasOption(
'skip-group-sync-check' );
73 $services = Services::getInstance();
74 $importer = $services->getExternalMessageSourceStateImporter();
76 $statusFormatter = MediaWikiServices::getInstance()
77 ->getFormatterFactory()
78 ->getStatusFormatter( RequestContext::getMain() );
80 foreach ( $groups as $id => $group ) {
81 $status = $importer->canImportGroup( $group, $skipGroupSyncCache );
82 if ( !$status->isOK() ) {
83 $this->error( $statusFormatter->getMessage( $status )->plain() );
88 $changes[$id] = $comparator->processGroup( $group );
89 }
catch ( Exception $e ) {
90 $errorMsg =
"Exception occurred while processing group: $id.\nException: $e";
91 $this->error( $errorMsg );
92 error_log( $errorMsg );
101 if ( $changes === [] ) {
102 if ( $importStrategy === ExternalMessageSourceStateImporter::IMPORT_NONE ) {
103 $this->output(
"No changes found\n" );
109 $info = $importer->import( $changes, $name, $importStrategy );
110 $this->printChangeInfo( $info );
117 private function getGroups(): array {
118 $groups = MessageGroups::getGroupsByType( FileBasedMessageGroup::class );
121 $include = $this->getOption(
'group',
'*' );
122 $include = explode(
',', $include );
123 $include = array_map(
'trim', $include );
124 $include = MessageGroups::expandWildcards( $include );
127 $exclude = $this->getOption(
'skipgroup',
'' );
128 $exclude = explode(
',', $exclude );
129 $exclude = array_map(
'trim', $exclude );
130 $exclude = MessageGroups::expandWildcards( $exclude );
133 $include = array_flip( $include );
134 $exclude = array_flip( $exclude );
136 return array_filter( $groups,
137 static function (
MessageGroup $group ) use ( $include, $exclude ) {
138 $id = $group->
getId();
140 return isset( $include[$id] ) && !isset( $exclude[$id] );
145 private function printChangeInfo( array $info ):
void {
146 foreach ( $info[
'processed'] as $group => $languages ) {
147 $newMessageCount = array_sum( $languages );
148 if ( $newMessageCount ) {
149 $this->output(
"Imported $newMessageCount new messages or translations for $group.\n" );
153 if ( $info[
'skipped'] !== [] ) {
154 $skipped = implode(
', ', array_keys( $info[
'skipped'] ) );
155 $this->output(
"There are changes to check for groups $skipped.\n" );
156 $url = SpecialPage::getTitleFor(
'ManageMessageGroups', $info[
'name'] )->getFullURL();
157 $this->output(
"You can process them at $url\n" );
161 private function getImportStrategy():
int {
162 $importStrategy = ExternalMessageSourceStateImporter::IMPORT_NONE;
163 if ( $this->hasOption(
'safe-import' ) ) {
164 $importStrategy = ExternalMessageSourceStateImporter::IMPORT_SAFE;
167 if ( $this->hasOption(
'import-non-renames' ) ) {
168 $importStrategy = ExternalMessageSourceStateImporter::IMPORT_NON_RENAMES;
171 return $importStrategy;