diff --git a/languages.php b/languages.php new file mode 100644 index 000000000..c066c48c2 --- /dev/null +++ b/languages.php @@ -0,0 +1,304 @@ + | ++------------------------------------------------------------------------------+ +| Description: Creates and maintains manual languages checkouts. | ++------------------------------------------------------------------------------+ +*/ + +// dir manual revcheck cloneUrl label + +lang( "de" , true , true , "git@github.com:php/doc-de.git" , "German" ); +lang( "en" , true , false , "git@github.com:php/doc-en.git" , "English" ); +lang( "es" , true , true , "git@github.com:php/doc-es.git" , "Spanish" ); +lang( "fr" , true , true , "git@github.com:php/doc-fr.git" , "French" ); +lang( "it" , true , true , "git@github.com:php/doc-it.git" , "Italian" ); +lang( "ja" , true , true , "git@github.com:php/doc-ja.git" , "Japanese" ); +lang( "pl" , false , true , "git@github.com:php/doc-pl.git" , "Polish" ); +lang( "pt_BR" , true , true , "git@github.com:php/doc-pt_br.git" , "Brazilian Portuguese" ); +lang( "ro" , false , true , "git@github.com:php/doc-ro.git" , "Romanian" ); +lang( "ru" , true , true , "git@github.com:php/doc-ru.git" , "Russian" ); +lang( "tr" , true , true , "git@github.com:php/doc-tr.git" , "Turkish" ); +lang( "uk" , true , true , "git@github.com:php/doc-uk.git" , "Ukrainian" ); +lang( "zh" , true , true , "git@github.com:php/doc-zh.git" , "Chinese (Simplified)" ); + +if ( count( $argv ) == 1 ) + print_usage(); +else + run(); +return; + +function print_usage() +{ + print <<path = realpath( __DIR__ . '/..' ) . "/{$code}"; + } +} + +function run() +{ + global $argv; + array_shift( $argv ); + foreach( $argv as $arg ) + { + switch( $arg ) + { + case "--clone": Conf::$clone = true; break; + case "--undo": Conf::$undo = true; break; + case "--pull": Conf::$pull = true; break; + case "--mark": Conf::$mark = true; break; + + case "--quiet": Conf::$quiet = "--quiet"; break; + + case "--list-csv": Conf::$listCsv = true; break; + case "--list-ssv": Conf::$listSsv = true; break; + + case "--rev": langAddRev(); break; + case "--all": langAddAll(); break; + default: langAdd( $arg ); break; + } + } + + // Default: languages with build manual flag + + if ( count( Conf::$langs ) == 0 ) + foreach( Conf::$knowLangs as $lang ) + if ( $lang->manual ) + Conf::$langs[ $lang->code ] = $lang; + + // Exclusive listing commands + + if ( Conf::$listCsv || Conf::$listSsv ) + { + $lst = []; + foreach( Conf::$langs as $lang ) + $lst[] = $lang->code; + if ( Conf::$listCsv ) + print implode( ',' , $lst ); + else + print implode( ' ' , $lst ); + exit; + } + + // Composite commands + + echo "Selected languages:"; + foreach( Conf::$langs as $lang ) + echo ' ' . $lang->code; + echo "\n"; + + gitAll(); + dirMark(); +} + +function langAdd( string $langCode ) +{ + foreach( Conf::$knowLangs as $lang ) + { + if ( $lang->code == $langCode ) + { + Conf::$langs[ $lang->code ] = $lang; + return; + } + } + fprintf( STDERR , "Unknown option or langcode: $langCode\n" ); + exit(-1); +} + +function langAddAll() +{ + foreach( Conf::$knowLangs as $lang ) + Conf::$langs[ $lang->code ] = $lang; +} + +function langAddRev() +{ + foreach( Conf::$knowLangs as $lang ) + if ( $lang->revcheck ) + Conf::$langs[ $lang->code ] = $lang; +} + +function gitAll() +{ + foreach( Conf::$langs as $lang ) + { + gitClone( $lang ); + gitUndo ( $lang ); + gitPull ( $lang ); + } +} + +function gitClone( Lang $lang ) +{ + if ( Conf::$clone == false ) + return; + + if ( file_exists( $lang->path ) ) + { + echo "clone {$lang->code} (already exists)\n"; + return; + } + else + echo "clone {$lang->code}\n"; + + $cmd = array( 'git' , 'clone' , Conf::$quiet , $lang->cloneUrl , $lang->path ); + cmdExecute( $cmd ); +} + +function gitUndo( Lang $lang ) +{ + if ( Conf::$undo == false ) + return; + + if ( ! file_exists( $lang->path ) ) + { + echo "undo {$lang->code}: path does not exists, skipping.\n"; + return; + } + else + echo "undo {$lang->code}\n"; + + $cmd = array( 'git' , '-C' , $lang->path , 'reset' , Conf::$quiet ); + cmdExecute( $cmd ); + + $cmd = array( 'git' , '-C' , $lang->path , 'clean' , Conf::$quiet , '-f' , '-d' ); + cmdExecute( $cmd ); + + $cmd = array( 'git' , '-C' , $lang->path , 'checkout' , Conf::$quiet , '.' ); + cmdExecute( $cmd ); +} + +function gitPull( Lang $lang ) +{ + if ( Conf::$pull == false ) + return; + + if ( ! file_exists( $lang->path ) ) + { + echo "pull {$lang->code}: path does not exists, skipping.\n"; + return; + } + else + echo "pull {$lang->code}\n"; + + $cmd = array( 'git' , '-C' , $lang->path , 'pull' , Conf::$quiet ); + cmdExecute( $cmd ); +} + +function cmdExecute( array $parts ) +{ + $escaped = []; + foreach( $parts as $part ) + if ( $part != null ) + $escaped[] = escapeshellarg( $part ); + + $cmd = implode( ' ' , $escaped ); + $rsc = null; + $ret = passthru( $cmd , $rsc ); + + if ( $ret === false || $rsc != 0 ) + { + echo "\nCommand failed, aborting: $cmd\n\n"; + exit(-1); + } +} + +function dirMark() +{ + if ( Conf::$mark == false ) + return; + + // TODO: Check if these markings are ok + + foreach( Conf::$langs as $lang ) + { + // Flag lang dir to build manual or not + + $path = "{$lang->path}/BUILDMAN"; + $text = $lang->label; + + if ( $lang->manual && ! file_exists( $path ) ) + file_put_contents( $path , $text ); + + if ( ! $lang->manual && file_exists( $path ) ) + unlink( $path ); + + // Flag lang dir to generate revcheck or not + + $path = "{$lang->path}/BUILDREV"; + + if ( $lang->revcheck && ! file_exists( $path ) ) + file_put_contents( $path , $text ); + + if ( ! $lang->revcheck && file_exists( $path ) ) + unlink( $path ); + } +}