Siebrand has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/88961


Change subject: Update export.php to use Maintenance class
......................................................................

Update export.php to use Maintenance class

Change-Id: I47e5c1063e8a695f54c06001d493f7ccfad714f6
---
M scripts/export.php
1 file changed, 267 insertions(+), 255 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Translate 
refs/changes/61/88961/1

diff --git a/scripts/export.php b/scripts/export.php
index b6e4c45..5496c23 100644
--- a/scripts/export.php
+++ b/scripts/export.php
@@ -9,282 +9,294 @@
  * @file
  */
 
-$optionsWithArgs = array(
-       'group',
-       'hours',
-       'lang',
-       'ppgettext',
-       'skip',
-       'skipgroup',
-       'target',
-       'threshold',
-);
-
-require __DIR__ . '/cli.inc';
-
-function showUsage() {
-       STDERR( <<<EOT
-Message exporter.
-
-Usage: php export.php [options...]
-
-Options:
-  --target      Target directory for exported files
-  --lang        Comma separated list of language codes or *
-  --skip        Languages to skip, comma separated list
-  --group       Comma separated list of group IDs (can use * as wildcard)
-  --skipgroup   Comma separated list of group IDs that should not be exported
-  --help        This help message
-  --threshold   Do not export under this percentage translated
-  --hours       Only export languages with changes in the last given number of
-                hours.
-  --ppgettext   Group root path for checkout of product. "msgmerge" will post
-                process on the export result based on the current source file
-                in that location (from sourcePattern or definitionFile)
-  --no-location Only used combined with "ppgettext". This option will rebuild
-                the gettext file without location information.
-  --no-fuzzy    Do not include any messages marked as fuzzy/outdated.
-  --codemaponly Only export languages that have a codeMap entry.
-EOT
-       );
-       exit( 1 );
-}
-
-if ( isset( $options['help'] ) || $args === 1 ) {
-       showUsage();
-}
-
-if ( !isset( $options['target'] ) ) {
-       STDERR( "You need to specify target directory" );
-       exit( 1 );
-}
-
-if ( !isset( $options['lang'] ) ) {
-       STDERR( "You need to specify languages to export" );
-       exit( 1 );
-}
-
-if ( !isset( $options['group'] ) ) {
-       STDERR( "You need to specify one or more groups" );
-       exit( 1 );
-}
-
-if ( !is_writable( $options['target'] ) ) {
-       STDERR( "Target directory is not writable (" . $options['target'] . ")" 
);
-       exit( 1 );
-}
-
-if ( isset( $options['threshold'] ) && intval( $options['threshold'] ) ) {
-       $threshold = $options['threshold'];
+// Standard boilerplate to define $IP
+if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
+       $IP = getenv( 'MW_INSTALL_PATH' );
 } else {
-       $threshold = false;
+       $dir = __DIR__;
+       $IP = "$dir/../../..";
 }
+require_once "$IP/maintenance/Maintenance.php";
 
-if ( isset( $options['no-location'] ) ) {
-       $noLocation = '--no-location ';
-} else {
-       $noLocation = '';
-}
-
-if ( isset( $options['no-fuzzy'] ) ) {
-       $noFuzzy = true;
-} else {
-       $noFuzzy = false;
-}
-
-$skip = array();
-if ( isset( $options['skip'] ) ) {
-       $skip = array_map( 'trim', explode( ',', $options['skip'] ) );
-}
-$reqLangs = Cli::parseLanguageCodes( $options['lang'] );
-$reqLangs = array_flip( $reqLangs );
-foreach ( $skip as $skipLang ) {
-       unset( $reqLangs[$skipLang] );
-}
-$reqLangs = array_flip( $reqLangs );
-
-$codemapOnly = false;
-if ( isset( $options['codemaponly'] ) ) {
-       $codemapOnly = true;
-}
-
-$groupIds = array();
-if ( isset( $options['group'] ) ) {
-       $groupIds = explode( ',', trim( $options['group'] ) );
-}
-
-$groupIds = MessageGroups::expandWildcards( $groupIds );
-$groups = MessageGroups::getGroupsById( $groupIds );
-foreach ( $groups as $groupId => $group ) {
-       if ( !$group instanceof MessageGroup ) {
-               STDERR( "EE2: Unknown message group $groupId" );
-               exit( 1 );
+class CommandlineExport extends Maintenance {
+       public function __construct() {
+               parent::__construct();
+               $this->mDescription = 'Message exporter.';
+               $this->addOption(
+                       'group',
+                       'Comma separated list of group IDs (can use * as 
wildcard)',
+                       true, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'lang',
+                       'Comma separated list of language codes or *',
+                       true, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'target',
+                       'Target directory for exported files',
+                       true, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'skip',
+                       'Languages to skip, comma separated list',
+                       false, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'skipgroup',
+                       'Comma separated list of group IDs that should not be 
exported',
+                       false, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'threshold',
+                       'Do not export under this percentage translated',
+                       false, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'hours',
+                       'Only export languages with changes in the last given 
number of hours',
+                       false, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'ppgettext',
+                       'Group root path for checkout of product. "msgmerge" 
will post ' .
+                       'process on the export result based on the current 
source file ' .
+                       'in that location (from sourcePattern or 
definitionFile)',
+                       false, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'no-location',
+                       'Only used combined with "ppgettext". This option will 
rebuild ' .
+                       'the gettext file without location information',
+                       false, /*required*/
+                       false /*has arg*/
+               );
+               $this->addOption(
+                       'no-fuzzy',
+                       'Do not include any messages marked as fuzzy/outdated',
+                       false, /*required*/
+                       false /*has arg*/
+               );
+               $this->addOption(
+                       'codemaponly',
+                       'Only export languages that have a codeMap entry',
+                       false, /*required*/
+                       false /*has arg*/
+               );
        }
 
-       if ( $group->isMeta() ) {
-               STDERR( "Skipping meta message group $groupId" );
-               unset( $groups[$groupId] );
-               continue;
-       }
-}
-
-if ( !count( $groups ) ) {
-       STDERR( "EE1: No valid message groups identified." );
-       exit( 1 );
-}
-
-$changeFilter = false;
-if ( isset( $options['hours'] ) ) {
-       $namespaces = array();
-       /**
-        * @var MessageGroup $group
-        */
-       foreach ( $groups as $group ) {
-               $namespaces[$group->getNamespace()] = true;
-       }
-       $namespaces = array_keys( $namespaces );
-       $bots = true;
-
-       $changeFilter = array();
-       $rows = TranslateUtils::translationChanges( $options['hours'], $bots, 
$namespaces );
-       foreach ( $rows as $row ) {
-               $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
-               $handle = new MessageHandle( $title );
-               $code = $handle->getCode();
-               if ( !$code ) {
-                       continue;
+       public function execute() {
+               $target = $this->getOption( 'target' );
+               if ( !is_writable( $target ) ) {
+                       $this->output( "Target directory is not writable (" . 
$target . ").\n" );
+                       exit( 1 );
                }
-               $groupIds = $handle->getGroupIds();
-               foreach ( $groupIds as $groupId ) {
-                       $changeFilter[$groupId][$code] = true;
+
+               $threshold = $this->getOption( 'threshold', false );
+               $noLocation = $this->getOption( 'no-location', '' );
+               $noFuzzy = $this->hasOption( 'no-fuzzy' ) ? true : false;
+
+               $skip = array();
+               if ( $this->hasOption( 'skip' ) ) {
+                       $skip = array_map( 'trim', explode( ',', 
$this->getOption( 'skip' ) ) );
                }
-       }
-}
 
-$skipGroups = array();
-if ( isset( $options['skipgroup'] ) ) {
-       $skipGroups = array_map( 'trim', explode( ',', $options['skipgroup'] ) 
);
-}
-
-foreach ( $groups as $groupId => $group ) {
-       if ( in_array( $groupId, $skipGroups ) ) {
-               STDERR( "Group $groupId is in skipgroup" );
-               continue;
-       }
-
-       // No changes to this group at all
-       if ( is_array( $changeFilter ) && !isset( $changeFilter[$groupId] ) ) {
-               STDERR( "No recent changes to $groupId" );
-               continue;
-       }
-
-       $langs = $reqLangs;
-
-       if ( $codemapOnly ) {
-               foreach ( $langs as $index => $code ) {
-                       if ( $group->mapCode( $code ) === $code ) {
-                               unset( $langs[$index] );
-                       }
+               $reqLangs = Cli::parseLanguageCodes( $this->getOption( 'lang' ) 
);
+               $reqLangs = array_flip( $reqLangs );
+               foreach ( $skip as $skipLang ) {
+                       unset( $reqLangs[$skipLang] );
                }
-       }
+               $reqLangs = array_flip( $reqLangs );
 
-       if ( $threshold ) {
-               $stats = MessageGroupStats::forGroup( $groupId );
-               foreach ( $langs as $index => $code ) {
-                       if ( !isset( $stats[$code] ) ) {
-                               unset( $langs[$index] );
+               $codemapOnly = $this->hasOption( 'codemaponly' ) ? true : false;
+
+               $groupIds = explode( ',', trim( $this->getOption( 'group ' ) ) 
);
+               $groupIds = MessageGroups::expandWildcards( $groupIds );
+               $groups = MessageGroups::getGroupsById( $groupIds );
+
+               foreach ( $groups as $groupId => $group ) {
+                       if ( !$group instanceof MessageGroup ) {
+                               $this->output( "EE2: Unknown message group 
$groupId.\n" );
+                               exit( 1 );
                        }
 
-                       $total = $stats[$code][MessageGroupStats::TOTAL];
-                       $translated = 
$stats[$code][MessageGroupStats::TRANSLATED];
-                       if ( $translated / $total * 100 < $threshold ) {
-                               unset( $langs[$index] );
-                       }
-               }
-       }
-
-       // Filter out unchanged languages from requested languages
-       if ( is_array( $changeFilter ) ) {
-               $langs = array_intersect( $langs, array_keys( 
$changeFilter[$groupId] ) );
-       }
-
-       if ( !count( $langs ) ) {
-               continue;
-       }
-
-       STDERR( "Exporting $groupId" );
-
-       if ( $group instanceof FileBasedMessageGroup ) {
-               $ffs = $group->getFFS();
-               $ffs->setWritePath( $options['target'] );
-               $sourceLanguage = $group->getSourceLanguage();
-               $collection = $group->initCollection( $sourceLanguage );
-
-               $definitionFile = false;
-
-               if ( isset( $options['ppgettext'] ) && $ffs instanceof 
GettextFFS ) {
-                       global $wgMaxShellMemory, $wgTranslateGroupRoot;
-
-                       // Need more shell memory for msgmerge.
-                       $wgMaxShellMemory = 402400;
-
-                       $conf = $group->getConfiguration();
-                       $path = $group->getSourceFilePath( $sourceLanguage );
-                       $definitionFile = str_replace( $wgTranslateGroupRoot, 
$options['ppgettext'], $path );
-               }
-
-               $whitelist = $group->getTranslatableLanguages();
-
-               foreach ( $langs as $lang ) {
-                       // Do not export languges that are blacklisted (or not 
whitelisted).
-                       // Also check that whitelist is not null, which means 
that all
-                       // languages are allowed for translation and export.
-                       if ( is_array( $whitelist ) && !isset( 
$whitelist[$lang] ) ) {
+                       if ( $group->isMeta() ) {
+                               $this->output( "Skipping meta message group 
$groupId.\n" );
+                               unset( $groups[$groupId] );
                                continue;
                        }
+               }
 
-                       $collection->resetForNewLanguage( $lang );
-                       $collection->loadTranslations();
-                       // Don't export ignored, unless it is the source 
language
-                       // or message documentation
-                       global $wgTranslateDocumentationLanguageCode;
-                       if ( $lang !== $wgTranslateDocumentationLanguageCode
-                               && $lang !== $sourceLanguage
-                       ) {
-                               $collection->filter( 'ignored' );
+               if ( !count( $groups ) ) {
+                       $this->output( "EE1: No valid message groups 
identified.\n" );
+                       exit( 1 );
+               }
+
+               $changeFilter = false;
+               $hours = $this->getOption( 'hours', null );
+               if ( $hours ) {
+                       $namespaces = array();
+
+                       /** @var MessageGroup $group */
+                       foreach ( $groups as $group ) {
+                               $namespaces[$group->getNamespace()] = true;
                        }
+                       $namespaces = array_keys( $namespaces );
+                       $bots = true;
 
-                       if ( $noFuzzy ) {
-                               $collection->filter( 'fuzzy' );
-                       }
-
-                       $ffs->write( $collection );
-
-                       // Do post processing if requested.
-                       if ( $definitionFile ) {
-                               if ( is_file( $definitionFile ) ) {
-                                       $targetFileName = $ffs->getWritePath() .
-                                               "/" . 
$group->getTargetFilename( $collection->code );
-                                       $cmd = "msgmerge --quiet " . 
$noLocation . "--output-file=" .
-                                               $targetFileName . ' ' . 
$targetFileName . ' ' . $definitionFile;
-                                       wfShellExec( $cmd, $ret );
-
-                                       // Report on errors.
-                                       if ( $ret ) {
-                                               STDERR( 'ERROR: ' . $ret );
-                                       }
-                               } else {
-                                       STDERR( $definitionFile . ' does not 
exist.' );
-                                       exit( 1 );
+                       $changeFilter = array();
+                       $rows = TranslateUtils::translationChanges( $hours, 
$bots, $namespaces );
+                       foreach ( $rows as $row ) {
+                               $title = Title::makeTitle( $row->rc_namespace, 
$row->rc_title );
+                               $handle = new MessageHandle( $title );
+                               $code = $handle->getCode();
+                               if ( !$code ) {
+                                       continue;
+                               }
+                               $groupIds = $handle->getGroupIds();
+                               foreach ( $groupIds as $groupId ) {
+                                       $changeFilter[$groupId][$code] = true;
                                }
                        }
                }
-       } else {
-               if ( $noFuzzy ) {
-                       STDERR( '--no-fuzzy is not supported for this message 
group.' );
+
+               $skipGroups = array();
+               if ( $this->hasOption( 'skipgroup' ) ) {
+                       $skipGroups = array_map( 'trim', explode( ',', 
$this->getOption( 'skipgroup' ) ) );
                }
 
-               $writer = $group->getWriter();
-               $writer->fileExport( $langs, $options['target'] );
+               foreach ( $groups as $groupId => $group ) {
+                       if ( in_array( $groupId, $skipGroups ) ) {
+                               $this->output( "Group $groupId is in 
skipgroup.\n" );
+                               continue;
+                       }
+
+                       // No changes to this group at all
+                       if ( is_array( $changeFilter ) && !isset( 
$changeFilter[$groupId] ) ) {
+                               $this->output( "No recent changes to 
$groupId.\n" );
+                               continue;
+                       }
+
+                       $langs = $reqLangs;
+
+                       if ( $codemapOnly ) {
+                               foreach ( $langs as $index => $code ) {
+                                       if ( $group->mapCode( $code ) === $code 
) {
+                                               unset( $langs[$index] );
+                                       }
+                               }
+                       }
+
+                       if ( $threshold ) {
+                               $stats = MessageGroupStats::forGroup( $groupId 
);
+                               foreach ( $langs as $index => $code ) {
+                                       if ( !isset( $stats[$code] ) ) {
+                                               unset( $langs[$index] );
+                                       }
+
+                                       $total = 
$stats[$code][MessageGroupStats::TOTAL];
+                                       $translated = 
$stats[$code][MessageGroupStats::TRANSLATED];
+                                       if ( $translated / $total * 100 < 
$threshold ) {
+                                               unset( $langs[$index] );
+                                       }
+                               }
+                       }
+
+                       // Filter out unchanged languages from requested 
languages
+                       if ( is_array( $changeFilter ) ) {
+                               $langs = array_intersect( $langs, array_keys( 
$changeFilter[$groupId] ) );
+                       }
+
+                       if ( !count( $langs ) ) {
+                               continue;
+                       }
+
+                       $this->output( "Exporting $groupId...\n" );
+
+                       if ( $group instanceof FileBasedMessageGroup ) {
+                               $ffs = $group->getFFS();
+                               $ffs->setWritePath( $target );
+                               $sourceLanguage = $group->getSourceLanguage();
+                               $collection = $group->initCollection( 
$sourceLanguage );
+
+                               $definitionFile = false;
+
+                               if ( $this->hasOption( 'ppgettext' ) && $ffs 
instanceof GettextFFS ) {
+                                       global $wgMaxShellMemory, 
$wgTranslateGroupRoot;
+
+                                       // Need more shell memory for msgmerge.
+                                       $wgMaxShellMemory = 402400;
+
+                                       $path = $group->getSourceFilePath( 
$sourceLanguage );
+                                       $definitionFile = str_replace(
+                                               $wgTranslateGroupRoot,
+                                               $this->getOption( 'ppgettext' ),
+                                               $path
+                                       );
+                               }
+
+                               $whitelist = $group->getTranslatableLanguages();
+
+                               foreach ( $langs as $lang ) {
+                                       // Do not export languges that are 
blacklisted (or not whitelisted).
+                                       // Also check that whitelist is not 
null, which means that all
+                                       // languages are allowed for 
translation and export.
+                                       if ( is_array( $whitelist ) && !isset( 
$whitelist[$lang] ) ) {
+                                               continue;
+                                       }
+
+                                       $collection->resetForNewLanguage( $lang 
);
+                                       $collection->loadTranslations();
+                                       // Don't export ignored, unless it is 
the source language
+                                       // or message documentation
+                                       global 
$wgTranslateDocumentationLanguageCode;
+                                       if ( $lang !== 
$wgTranslateDocumentationLanguageCode
+                                               && $lang !== $sourceLanguage
+                                       ) {
+                                               $collection->filter( 'ignored' 
);
+                                       }
+
+                                       if ( $noFuzzy ) {
+                                               $collection->filter( 'fuzzy' );
+                                       }
+
+                                       $ffs->write( $collection );
+
+                                       // Do post processing if requested.
+                                       if ( $definitionFile ) {
+                                               if ( is_file( $definitionFile ) 
) {
+                                                       $targetFileName = 
$ffs->getWritePath() .
+                                                               "/" . 
$group->getTargetFilename( $collection->code );
+                                                       $cmd = "msgmerge 
--quiet " . $noLocation . "--output-file=" .
+                                                               $targetFileName 
. ' ' . $targetFileName . ' ' . $definitionFile;
+                                                       wfShellExec( $cmd, $ret 
);
+
+                                                       // Report on errors.
+                                                       if ( $ret ) {
+                                                               $this->output( 
'ERROR: ' . $ret . '.\n' );
+                                                       }
+                                               } else {
+                                                       $this->output( 
$definitionFile . " does not exist.\n" );
+                                                       exit( 1 );
+                                               }
+                                       }
+                               }
+                       } else {
+                               if ( $noFuzzy ) {
+                                       $this->output( "--no-fuzzy is not 
supported for this message group.\n" );
+                               }
+
+                               $writer = $group->getWriter();
+                               $writer->fileExport( $langs, $target );
+                       }
+               }
        }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/88961
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I47e5c1063e8a695f54c06001d493f7ccfad714f6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Siebrand <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to