diff options
Diffstat (limited to 'MLEB/Translate/scripts/export.php')
-rw-r--r-- | MLEB/Translate/scripts/export.php | 104 |
1 files changed, 96 insertions, 8 deletions
diff --git a/MLEB/Translate/scripts/export.php b/MLEB/Translate/scripts/export.php index 469bd2ac..b0bc314d 100644 --- a/MLEB/Translate/scripts/export.php +++ b/MLEB/Translate/scripts/export.php @@ -19,9 +19,11 @@ if ( getenv( 'MW_INSTALL_PATH' ) !== false ) { require_once "$IP/maintenance/Maintenance.php"; class CommandlineExport extends Maintenance { + private const EXPORT_LOG_FILE = 'translation-exports'; + public function __construct() { parent::__construct(); - $this->mDescription = 'Message exporter.'; + $this->addDescription( 'Message exporter.' ); $this->addOption( 'group', 'Comma separated list of group IDs (can use * as wildcard)', @@ -91,12 +93,25 @@ class CommandlineExport extends Maintenance { false, /*required*/ false /*has arg*/ ); + + $this->addOption( + 'offline-gettext-format', + '(optional) Export languages in offline Gettext format. Give a file pattern with ' + . '%GROUPID% and %CODE%. Empty pattern defaults to %GROUPID%/%CODE%.po.', + false, /*required*/ + true /*has arg*/ + ); + $this->requireExtension( 'Translate' ); } public function execute() { + wfDebugLog( self::EXPORT_LOG_FILE, 'Starting exports for groups - ' + . $this->getOption( 'group' ) . '... ' ); + $exportStartTime = microtime( true ); + $target = $this->getOption( 'target' ); if ( !is_writable( $target ) ) { - $this->error( "Target directory is not writable ($target).", 1 ); + $this->fatalError( "Target directory is not writable ($target)." ); } $threshold = $this->getOption( 'threshold' ); @@ -105,7 +120,7 @@ class CommandlineExport extends Maintenance { $noLocation = ''; if ( $this->hasOption( 'no-location' ) ) { $noLocation = '--no-location '; - }; + } $skip = []; if ( $this->hasOption( 'skip' ) ) { @@ -120,10 +135,13 @@ class CommandlineExport extends Maintenance { $reqLangs = array_flip( $reqLangs ); $codemapOnly = $this->hasOption( 'codemaponly' ); + $forOffline = $this->hasOption( 'offline-gettext-format' ); + $offlineTargetPattern = $this->getOption( 'offline-gettext-format' ) ?: "%GROUPID%/%CODE%.po"; $groupIds = explode( ',', trim( $this->getOption( 'group' ) ) ); $groupIds = MessageGroups::expandWildcards( $groupIds ); $groups = MessageGroups::getGroupsById( $groupIds ); + '@phan-var FileBasedMessageGroup[] $groups'; /** @var FileBasedMessageGroup $group */ foreach ( $groups as $groupId => $group ) { @@ -133,7 +151,7 @@ class CommandlineExport extends Maintenance { continue; } - if ( !$group instanceof FileBasedMessageGroup ) { + if ( !$forOffline && !$group instanceof FileBasedMessageGroup ) { $this->output( "EE2: Unexportable message group $groupId.\n" ); unset( $groups[$groupId] ); continue; @@ -141,7 +159,7 @@ class CommandlineExport extends Maintenance { } if ( !count( $groups ) ) { - $this->error( 'EE1: No valid message groups identified.', 1 ); + $this->fatalError( 'EE1: No valid message groups identified.' ); } $changeFilter = false; @@ -201,7 +219,10 @@ class CommandlineExport extends Maintenance { } if ( $threshold ) { + wfDebugLog( self::EXPORT_LOG_FILE, "Calculating stats for group $groupId" ); + $tStartTime = microtime( true ); $stats = MessageGroupStats::forGroup( $groupId ); + $emptyLangs = []; foreach ( $langs as $index => $code ) { if ( !isset( $stats[$code] ) ) { unset( $langs[$index] ); @@ -210,10 +231,29 @@ class CommandlineExport extends Maintenance { $total = $stats[$code][MessageGroupStats::TOTAL]; $translated = $stats[$code][MessageGroupStats::TRANSLATED]; + + if ( $total === 0 ) { + $emptyLangs[] = $code; + unset( $langs[$index] ); + continue; + } + if ( $translated / $total * 100 < $threshold ) { unset( $langs[$index] ); } } + + if ( $emptyLangs !== [] ) { + $this->output( + "Message group $groupId doesn't contain messages in language(s): " . + implode( ', ', $emptyLangs ) . "." + ); + } + + $tEndTime = microtime( true ); + wfDebugLog( self::EXPORT_LOG_FILE, + "Finished calculating stats for group $groupId. Time: " + . ( $tEndTime - $tStartTime ) . ' secs.' ); } // Filter out unchanged languages from requested languages @@ -225,9 +265,16 @@ class CommandlineExport extends Maintenance { continue; } - $this->output( "Exporting $groupId...\n" ); + $this->output( 'Exporting ' . count( $langs ) . " languages for group $groupId" ); + + if ( $forOffline ) { + $fileBasedGroup = FileBasedMessageGroup::newFromMessageGroup( $group, $offlineTargetPattern ); + $ffs = new GettextFFS( $fileBasedGroup ); + $ffs->setOfflineMode( true ); + } else { + $ffs = $group->getFFS(); + } - $ffs = $group->getFFS(); $ffs->setWritePath( $target ); $sourceLanguage = $group->getSourceLanguage(); $collection = $group->initCollection( $sourceLanguage ); @@ -250,6 +297,17 @@ class CommandlineExport extends Maintenance { $whitelist = $group->getTranslatableLanguages(); + wfDebugLog( + self::EXPORT_LOG_FILE, 'Exporting languages (' + . count( $langs ) . ") for group - $groupId." + ); + + $langExportTimes = [ + 'collection' => 0, + 'ffs' => 0, + 'definitionFile' => 0 + ]; + $langStartTime = microtime( true ); foreach ( $langs as $lang ) { // Do not export languages that are blacklisted (or not whitelisted). // Also check that whitelist is not null, which means that all @@ -258,6 +316,7 @@ class CommandlineExport extends Maintenance { continue; } + $startTime = microtime( true ); $collection->resetForNewLanguage( $lang ); $collection->loadTranslations(); // Don't export ignored, unless it is the source language @@ -272,11 +331,17 @@ class CommandlineExport extends Maintenance { if ( $noFuzzy ) { $collection->filter( 'fuzzy' ); } + $endTime = microtime( true ); + $langExportTimes['collection'] += ( $endTime - $startTime ); + $startTime = microtime( true ); $ffs->write( $collection ); + $endTime = microtime( true ); + $langExportTimes['ffs'] += ( $endTime - $startTime ); // Do post processing if requested. if ( $definitionFile ) { + $startTime = microtime( true ); if ( is_file( $definitionFile ) ) { $targetFileName = $ffs->getWritePath() . '/' . $group->getTargetFilename( $collection->code ); @@ -289,11 +354,34 @@ class CommandlineExport extends Maintenance { $this->error( "ERROR: $ret" ); } } else { - $this->error( "$definitionFile does not exist.", 1 ); + $this->fatalError( "$definitionFile does not exist for group $groupId." ); } + $endTime = microtime( true ); + $langExportTimes['definitionFile'] += ( $endTime - $startTime ); } } + $langEndTime = microtime( true ); + + wfDebugLog( + self::EXPORT_LOG_FILE, + "Done exporting languages for group - $groupId. " . + 'Time taken - ' . ( $langEndTime - $langStartTime ) . ' secs.' + ); + + foreach ( $langExportTimes as $type => $time ) { + wfDebugLog( + self::EXPORT_LOG_FILE, + "Time taken by '$type' for group $groupId - $time secs." + ); + } } + + $exportEndTime = microtime( true ); + wfDebugLog( + self::EXPORT_LOG_FILE, 'Finished export process for groups - ' . + $this->getOption( 'group' ) . + '. Time: ' . ( $exportEndTime - $exportStartTime ) . ' secs.' + ); } } |