21 private const MARKER =
'%CODE%';
23 public function __construct() {
24 parent::__construct();
25 $this->addDescription(
'Renames language codes in repos.' );
28 'Comma separated list of group IDs (can use * as wildcard)',
47 'Target directory for exported files',
51 $this->requireExtension(
'Translate' );
54 public function execute() {
55 $target = rtrim( $this->getOption(
'target' ),
'/' );
56 $sourceLanguage = $this->getOption(
'source-language' );
57 $targetLanguage = $this->getOption(
'target-language' );
59 if ( !is_writable( $target ) ) {
60 $this->fatalError(
"Target directory is not writable ($target)." );
63 $groupIds = explode(
',', trim( $this->getOption(
'group' ) ) );
64 $groupIds = MessageGroups::expandWildcards( $groupIds );
65 $groups = MessageGroups::getGroupsById( $groupIds );
66 $groups = $this->filterGroups( $groups );
68 if ( $groups === [] ) {
69 $this->fatalError(
'EE1: No valid message groups identified.' );
72 foreach ( $groups as $group ) {
76 $sourcePath = $group->getTargetFilename( $sourceLanguage );
77 $targetPath = $group->getTargetFilename( $targetLanguage );
79 if ( !file_exists(
"$target/$sourcePath" ) ) {
83 $this->output(
"Renaming $sourcePath to $targetPath\n" );
84 $this->renameFile(
"$target/$sourcePath",
"$target/$targetPath" );
86 $pathPattern =
"$target/" . $group->getTargetFilename( self::MARKER );
88 $needsCleanup = $this->needsCleanup( $pathPattern, $sourceLanguage, $pathToRemove );
89 if ( $needsCleanup ===
'yes' ) {
90 $this->output(
"Removing empty directory $pathToRemove\n" );
91 rmdir( $pathToRemove );
92 } elseif ( $needsCleanup ===
'maybe' ) {
93 $this->output(
"Not removing (yet?) non-empty directory $pathToRemove\n" );
97 $this->output(
"Done\n" );
104 private function filterGroups( array $groups ) {
106 foreach ( $groups as $groupId => $group ) {
108 $this->output(
"Skipping non-file based message group $groupId.\n" );
111 $return[$groupId] = $group;
116 private function renameFile( $source, $target ) {
118 if ( !is_dir( dirname( $target ) ) ) {
119 mkdir( dirname( $target ), 0777,
true );
122 rename( $source, $target );
125 private function isDirectoryEmpty( $dir ) {
126 return array_diff( scandir( $dir ), [
'..',
'.' ] ) === [];
129 private function needsCleanup( $pathPattern, $sourceLanguage, &$pathToRemove ) {
131 $currentComponent = basename( $pathPattern );
132 if ( strpos( $currentComponent, self::MARKER ) ===
false ) {
133 $pathPattern = dirname( $pathPattern );
137 $pathToRemove = str_replace( self::MARKER, $sourceLanguage, $pathPattern );
138 if ( !is_dir( $pathToRemove ) ) {
143 return $this->isDirectoryEmpty( $pathToRemove ) ?
'yes' :
'maybe';
144 }
while ( $currentComponent !==
'' );