jenkins-bot has submitted this change and it was merged.

Change subject: Update export and mwcore-export to use Maintenance class
......................................................................


Update export and mwcore-export to use Maintenance class

Also move Cli::parseLanguageCodes() to TranslateUtils class, update callers
and remove some duplicate code.

Change-Id: I47e5c1063e8a695f54c06001d493f7ccfad714f6
---
M TranslateUtils.php
M scripts/cli.inc
M scripts/export.php
M scripts/magic-export.php
M scripts/mwcore-export.php
5 files changed, 421 insertions(+), 416 deletions(-)

Approvals:
  Nikerabbit: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/TranslateUtils.php b/TranslateUtils.php
index 253680c..e6b77ce 100644
--- a/TranslateUtils.php
+++ b/TranslateUtils.php
@@ -427,4 +427,20 @@
 
                return $formats;
        }
+
+       /**
+        * Parses list of language codes to an array.
+        * @param string $codes Comma separated list of language codes. "*" for 
all.
+        * @return string[] Language codes.
+        */
+       public static function parseLanguageCodes( $codes ) {
+               $langs = array_map( 'trim', explode( ',', $codes ) );
+               if ( $langs[0] === '*' ) {
+                       $languages = Language::fetchLanguageNames();
+                       ksort( $languages );
+                       $langs = array_keys( $languages );
+               }
+
+               return $langs;
+       }
 }
diff --git a/scripts/cli.inc b/scripts/cli.inc
index e1bd1b3..e44c158 100644
--- a/scripts/cli.inc
+++ b/scripts/cli.inc
@@ -82,25 +82,3 @@
 ///@{
 register_shutdown_function( 'STDOUT', false );
 ///@}
-
-/**
- * A general CLI utility class.
- * @todo Move elsewhere?
- */
-class Cli {
-       /**
-        * Parses list of language codes to an array.
-        * @param string $codes Command line input.
-        * @return string[] Language codes.
-        */
-       public static function parseLanguageCodes( $codes ) {
-               $langs = array_map( 'trim', explode( ',', $codes ) );
-               if ( $langs[0] === '*' ) {
-                       $languages = Language::getLanguageNames();
-                       ksort( $languages );
-                       $langs = array_keys( $languages );
-               }
-
-               return $langs;
-       }
-}
diff --git a/scripts/export.php b/scripts/export.php
index b6e4c45..2250ab0 100644
--- a/scripts/export.php
+++ b/scripts/export.php
@@ -9,282 +9,312 @@
  * @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',
+                       '(optional) Languages to skip, comma separated list',
+                       false, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'skipgroup',
+                       '(optional) Comma separated list of group IDs that 
should not be exported',
+                       false, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'threshold',
+                       '(optional) Do not export under this percentage 
translated',
+                       false, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'hours',
+                       '(optional) Only export languages with changes in the 
last given number of hours',
+                       false, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'ppgettext',
+                       '(optional) 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',
+                       '(optional) Only used combined with "ppgettext". This 
option will rebuild ' .
+                       'the gettext file without location information',
+                       false, /*required*/
+                       false /*has arg*/
+               );
+               $this->addOption(
+                       'no-fuzzy',
+                       '(optional) Do not include any messages marked as 
fuzzy/outdated',
+                       false, /*required*/
+                       false /*has arg*/
+               );
+               $this->addOption(
+                       'codemaponly',
+                       '(optional) Only export languages that have a codeMap 
entry',
+                       false, /*required*/
+                       false /*has arg*/
+               );
+               $this->addOption(
+                       'core-meta',
+                       '(optional) Allow export of specific MediaWiki core 
meta groups ' .
+                               '(translatewiki.net specific)',
+                       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;
-               }
-               $groupIds = $handle->getGroupIds();
-               foreach ( $groupIds as $groupId ) {
-                       $changeFilter[$groupId][$code] = true;
-               }
-       }
-}
-
-$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] );
-                       }
-               }
-       }
-
-       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;
-       }
-
-       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 );
+       public function execute() {
+               $target = $this->getOption( 'target' );
+               if ( !is_writable( $target ) ) {
+                       $this->error( "Target directory is not writable 
($target).", 1 );
                }
 
-               $whitelist = $group->getTranslatableLanguages();
+               $threshold = $this->getOption( 'threshold' );
+               $noFuzzy = $this->hasOption( 'no-fuzzy' );
 
-               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;
+               $noLocation = '';
+               if ( $this->hasOption( 'no-location' ) ) {
+                       $noLocation = '--no-location ';
+               };
+
+               $skip = array();
+               if ( $this->hasOption( 'skip' ) ) {
+                       $skip = array_map( 'trim', explode( ',', 
$this->getOption( 'skip' ) ) );
+               }
+
+               $reqLangs = TranslateUtils::parseLanguageCodes( 
$this->getOption( 'lang' ) );
+               $reqLangs = array_flip( $reqLangs );
+               foreach ( $skip as $skipLang ) {
+                       unset( $reqLangs[$skipLang] );
+               }
+               $reqLangs = array_flip( $reqLangs );
+
+               $codemapOnly = $this->hasOption( 'codemaponly' );
+
+               $groupIds = explode( ',', trim( $this->getOption( 'group' ) ) );
+               $groupIds = MessageGroups::expandWildcards( $groupIds );
+               $groups = MessageGroups::getGroupsById( $groupIds );
+
+               $coreMeta = $this->hasOption( 'core-meta' );
+               foreach ( $groups as $groupId => $group ) {
+                       if ( !$group instanceof MessageGroup ) {
+                               $this->error( "EE2: Unknown message group 
$groupId.", 1 );
                        }
 
-                       $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 ) {
-                                               STDERR( 'ERROR: ' . $ret );
-                                       }
-                               } else {
-                                       STDERR( $definitionFile . ' does not 
exist.' );
-                                       exit( 1 );
+                       if ( $group->isMeta() ) {
+                               if ( !$coreMeta ) {
+                                       $this->output( "Skipping meta message 
group $groupId.", $groupId );
+                                       unset( $groups[$groupId] );
+                                       continue;
+                               } elseif ( strstr( $group->getId(), 'core-1', 
true ) !== '' ) {
+                                       // Special case for MediaWiki core 
branches.
+                                       $this->output( "Skipping meta message 
group $groupId.", $groupId );
+                                       unset( $groups[$groupId] );
+                                       continue;
                                }
                        }
                }
-       } else {
-               if ( $noFuzzy ) {
-                       STDERR( '--no-fuzzy is not supported for this message 
group.' );
+
+               if ( !count( $groups ) ) {
+                       $this->error( "EE1: No valid message groups 
identified.", 1 );
                }
 
-               $writer = $group->getWriter();
-               $writer->fileExport( $langs, $options['target'] );
+               $changeFilter = false;
+               $hours = $this->getOption( 'hours' );
+               if ( $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( $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;
+                               }
+                       }
+               }
+
+               $skipGroups = array();
+               if ( $this->hasOption( 'skipgroup' ) ) {
+                       $skipGroups = array_map( 'trim', explode( ',', 
$this->getOption( 'skipgroup' ) ) );
+               }
+
+               foreach ( $groups as $groupId => $group ) {
+                       if ( in_array( $groupId, $skipGroups ) ) {
+                               $this->output( "Group $groupId is in 
skipgroup.", $groupId );
+                               continue;
+                       }
+
+                       // No changes to this group at all
+                       if ( is_array( $changeFilter ) && !isset( 
$changeFilter[$groupId] ) ) {
+                               $this->output( "No recent changes to 
$groupId.", $groupId );
+                               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...", $groupId );
+
+                       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->error( 
"ERROR: $ret" );
+                                                       }
+                                               } else {
+                                                       $this->error( 
"$definitionFile does not exist.", 1 );
+                                               }
+                                       }
+                               }
+                       } else {
+                               if ( $noFuzzy ) {
+                                       $this->error( "--no-fuzzy is not 
supported for this message group." );
+                               }
+
+                               $writer = $group->getWriter();
+                               $writer->fileExport( $langs, $target );
+                       }
+               }
        }
 }
+
+$maintClass = 'CommandlineExport';
+require_once RUN_MAINTENANCE_IF_MAIN;
diff --git a/scripts/magic-export.php b/scripts/magic-export.php
index 926fbfa..fc77358 100644
--- a/scripts/magic-export.php
+++ b/scripts/magic-export.php
@@ -167,7 +167,7 @@
         * file handle.
         */
        protected function writeFiles() {
-               $langs = self::parseLanguageCodes( '*' );
+               $langs = TranslateUtils::parseLanguageCodes( '*' );
                unset( $langs[array_search( 'en', $langs )] );
                $langs = array_merge( array( 'en' ), $langs );
                foreach ( $langs as $l ) {
@@ -280,22 +280,6 @@
                foreach ( $this->handles as $handle ) {
                        fclose( $handle );
                }
-       }
-
-       /**
-        * Copied from cli.inc.
-        * @param $codes
-        * @return array
-        */
-       private static function parseLanguageCodes( /* string */$codes ) {
-               $langs = array_map( 'trim', explode( ',', $codes ) );
-               if ( $langs[0] === '*' ) {
-                       $languages = Language::getLanguageNames();
-                       ksort( $languages );
-                       $langs = array_keys( $languages );
-               }
-
-               return $langs;
        }
 }
 
diff --git a/scripts/mwcore-export.php b/scripts/mwcore-export.php
index b8f5b6e..bc7b69b 100644
--- a/scripts/mwcore-export.php
+++ b/scripts/mwcore-export.php
@@ -3,132 +3,129 @@
  * Script to export special core features of %MediaWiki.
  *
  * @author Niklas Laxstrom
- * @copyright Copyright © 2009-2010, Niklas Laxström
+ * @author Siebrand Mazeland
+ * @copyright Copyright © 2009-2013, Niklas Laxström, Siebrand Mazeland
  * @license GPL-2.0+
  * @file
  */
 
-/// @cond
-
-$optionsWithArgs = array( 'lang', 'target', 'type' );
-require __DIR__ . '/cli.inc';
-
-function showUsage() {
-       STDERR( <<<EOT
-Core special features exporter.
-
-Usage: php mwcore-export.php [options...]
-
-Options:
-  --target      Target directory for exported files
-  --lang        Comma separated list of language codes or *
-  --type        namespace, special or magic
-EOT
-       );
-       exit( 1 );
+// Standard boilerplate to define $IP
+if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
+       $IP = getenv( 'MW_INSTALL_PATH' );
+} else {
+       $dir = __DIR__;
+       $IP = "$dir/../../..";
 }
+require_once "$IP/maintenance/Maintenance.php";
 
-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['type'] ) ) {
-       STDERR( "Type must be one of the following: special magic namespace." );
-       exit( 1 );
-}
-
-if ( !is_writable( $options['target'] ) ) {
-       STDERR( "Target directory is not writable." );
-       exit( 1 );
-}
-
-$langs = Cli::parseLanguageCodes( $options['lang'] );
-
-$group = MessageGroups::getGroup( 'core' );
-
-foreach ( $langs as $l ) {
-       $o = null;
-
-       switch ( $options['type'] ) {
-               case 'special':
-                       $o = new SpecialPageAliasesCM( $l );
-                       break;
-               case 'magic':
-                       $o = new MagicWordsCM( $l );
-                       break;
-               case 'namespace':
-                       $o = new NamespaceCM( $l );
-                       break;
-               default:
-                       STDERR( "Invalid type: must be one of special, magic, 
namespace." );
-                       exit( 1 );
+class MwCoreExport extends Maintenance {
+       public function __construct() {
+               parent::__construct();
+               $this->mDescription = 'Core special features exporter';
+               $this->addOption(
+                       'target',
+                       'Target directory for exported files',
+                       true, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'lang',
+                       'Comma separated list of language codes. Default: *',
+                       false, /*required*/
+                       true /*has arg*/
+               );
+               $this->addOption(
+                       'type',
+                       'Export type: "namespace", "special" or "magic"',
+                       true, /*required*/
+                       true /*has arg*/
+               );
        }
 
-       $export = $o->export( 'core' );
-       if ( $export === '' ) {
-               continue;
-       }
-
-       $matches = array();
-       preg_match( '~^(\$[a-zA-Z]+)\s*=~m', $export, $matches );
-
-       if ( !isset( $matches[1] ) ) {
-               continue;
-       }
-
-       # remove useles comment
-       $export = preg_replace( "~^# .*$\n~m", '', $export );
-
-       if ( strpos( $export, '#!!' ) !== false ) {
-               STDERR( "There are warnings with $l." );
-       }
-
-       $variable = preg_quote( $matches[1], '~' );
-
-       /**
-        * @var MessageGroupOld $group
-        */
-       $file = $group->getMessageFileWithPath( $l );
-
-       if ( !file_exists( $file ) ) {
-               STDERR( "File $file does not exist!" );
-               continue;
-       }
-
-       $data = file_get_contents( $file );
-
-       $export = trim( $export ) . "\n";
-       $escExport = addcslashes( $export, '\\$' ); # Darn backreferences
-
-       $outFile = $options['target'] . '/' . $group->getMessageFile( $l );
-
-       $count = 0;
-       $data = preg_replace( "~$variable\s*=.*?\n\);\n~s", $escExport, $data, 
1, $count );
-       if ( $count ) {
-               file_put_contents( $outFile, $data );
-       } else {
-               STDERR( "Adding new entry to $outFile, please double check 
location.", $l );
-               $pos = strpos( $data, "*/" );
-               if ( $pos === false ) {
-                       STDERR( ". FAILED! Totally new file? No header?", $l );
-               } else {
-                       $pos += 3;
+       public function execute() {
+               if ( !is_writable( $this->getOption( 'target' ) ) ) {
+                       $this->error( 'Target directory is not writable.', 1 );
                }
 
-               $data = substr( $data, 0, $pos ) . "\n" . $export . substr( 
$data, $pos );
+               $langs = TranslateUtils::parseLanguageCodes( $this->getOption( 
'lang' ) );
+               $group = MessageGroups::getGroup( 'core' );
+               $type = $this->getOption( 'type' );
 
-               file_put_contents( $outFile, $data );
+               foreach ( $langs as $l ) {
+                       $o = null;
+
+                       switch ( $type ) {
+                               case 'special':
+                                       $o = new SpecialPageAliasesCM( $l );
+                                       break;
+                               case 'magic':
+                                       $o = new MagicWordsCM( $l );
+                                       break;
+                               case 'namespace':
+                                       $o = new NamespaceCM( $l );
+                                       break;
+                               default:
+                                       $this->error( 'Invalid type: Must be 
one of special, magic, namespace.', 1 );
+                       }
+
+                       $export = $o->export( 'core' );
+                       if ( $export === '' ) {
+                               continue;
+                       }
+
+                       $matches = array();
+                       preg_match( '~^(\$[a-zA-Z]+)\s*=~m', $export, $matches 
);
+
+                       if ( !isset( $matches[1] ) ) {
+                               continue;
+                       }
+
+                       # remove useles comment
+                       $export = preg_replace( "~^# .*$\n~m", '', $export );
+
+                       if ( strpos( $export, '#!!' ) !== false ) {
+                               $this->error( "There are warnings with $l." );
+                       }
+
+                       $variable = preg_quote( $matches[1], '~' );
+
+                       /**
+                        * @var MessageGroupOld $group
+                        */
+                       $file = $group->getMessageFileWithPath( $l );
+
+                       if ( !file_exists( $file ) ) {
+                               $this->error( "File $file does not exist!" );
+                               continue;
+                       }
+
+                       $data = file_get_contents( $file );
+
+                       $export = trim( $export ) . "\n";
+                       $escExport = addcslashes( $export, '\\$' ); # Darn 
backreferences
+
+                       $outFile = $this->getOption( 'target' ) . '/' . 
$group->getMessageFile( $l );
+
+                       $count = 0;
+                       $data = preg_replace( "~$variable\s*=.*?\n\);\n~s", 
$escExport, $data, 1, $count );
+                       if ( $count ) {
+                               file_put_contents( $outFile, $data );
+                       } else {
+                               $this->error( "Adding new entry to $outFile, 
please double check location." );
+                               $pos = strpos( $data, "*/" );
+                               if ( $pos === false ) {
+                                       $this->error( ". FAILED! Totally new 
file? No header?" );
+                               } else {
+                                       $pos += 3;
+                               }
+
+                               $data = substr( $data, 0, $pos ) . "\n" . 
$export . substr( $data, $pos );
+
+                               file_put_contents( $outFile, $data );
+                       }
+               }
        }
 }
 
-/// @endcond
+$maintClass = 'MwCoreExport';
+require_once RUN_MAINTENANCE_IF_MAIN;

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I47e5c1063e8a695f54c06001d493f7ccfad714f6
Gerrit-PatchSet: 10
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Siebrand <siebr...@wikimedia.org>
Gerrit-Reviewer: Nikerabbit <niklas.laxst...@gmail.com>
Gerrit-Reviewer: Siebrand <siebr...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to