* @copyright 2005 by http://wombat.exit0.net * @package wombatSite * @subpackage install */ // Don't use the factory in this stage! require_once 'Console/Getargs.php'; /** * Console interface for translate tools * * @todo add colored output * * @version 1.0.0 * @package wombatSite * @subpackage install */ class wbTranslateTools_Console extends wbTranslateTools { /** * console args * @access protected * @var array $_consoleArgs */ var $_consoleArgs = array( 'basedir' => array( 'short' => 'b', 'min' => 1, 'max' => 1, 'desc' => 'Set basic site directory. Uses current directory if not specified.', 'default' => '.' ), /* 'list' => array( 'short' => 'l', 'min' => 0, 'max' => 0, 'desc' => 'List available langauges and exit. Try with "verbose".', 'default' => 1 ), */ 'system' => array( 'short' => 's', 'min' => 0, 'max' => 0, 'desc' => 'Run for wombat system as well', 'default' => 0 ), 'all' => array( 'short' => 'a', 'min' => 0, 'max' => 0, 'desc' => 'Run all commands', 'default' => 1 ), 'verbose' => array( 'short' => 'v', 'min' => 0, 'max' => 0, 'desc' => 'Switch on verbosity', 'default' => 1 ), 'debug' => array( 'short' => 'd', 'min' => 0, 'max' => 0, 'desc' => 'Switch to debug mode', 'default' => 1 ), 'help' => array( 'short' => 'h', 'min' => 0, 'max' => 0, 'desc' => 'Display command line help and exit' ), CONSOLE_GETARGS_PARAMS => array( 'min' => 1, 'max' => -1, 'desc' => 'commands', 'default' => 1 ) ); /** * console arg object * @access private * @var object $_args */ var $_args; /** * Display user interface * * @access public * @return int $return 0 on success */ function display( ) { if( PEAR::isError( $this->_args ) ) { return $this->displayHelp( 1 ); } // display help? if( $this->_args === CONSOLE_GETARGS_HELP ) { return $this->displayHelp( 0 ); } // be verbose $this->_verbose = $this->_args->getValue( 'verbose' ); if( is_array( $this->_verbose ) && !empty( $this->_verbose ) ) { $this->_verbose = 1; } // run in debug mode? $this->_debug = $this->_args->getValue( 'debug' ); if( is_array( $this->_debug ) && !empty( $this->_debug ) ) { $this->_debug = 1; } // run over system files as well $this->_system = $this->_args->getValue( 'system' ); if( is_array( $this->_system ) && !empty( $this->_system ) ) { $this->_system = 1; } if( $this->_debug ) { wbDebugger::setActive( true ); $this->_pvl( "Debug mode is ON" ); } // check parameter basedir $baseDir = realpath( $this->_args->getValue( 'basedir' ) ); if( !$baseDir ) { $this->_pl( 'Basedir not found!', STDERR ); return 1; } // get factory parameter from environment variables foreach( $this->_parameter as $confKey ) { $confEnv = getenv( 'WOMBAT_' . strtoupper( $confKey ) ); if( !empty( $confEnv ) ) { $this->_pvl( "Set parameter: $confKey=$confEnv" ); wbFactory::setParam( $confKey, $confEnv ); } } // check whether config file exists $confFile = $baseDir . '/' . wbFactory::getParam( 'configDir' ) . '/' . wbFactory::getParam( 'configFile' ); if( !is_file( $confFile ) ) { $this->_pl( 'Configuration file '. $confFile .' could not be found!', STDERR ); return 1; } // check command(s) $all = $this->_args->getValue( 'all' ); if( is_array( $all ) && !empty( $all ) ) { $all = true; } if( $all ) { $commands = array_keys( $this->_commands ); $this->_pvl( 'Run all commands' ); } else { $cmd = $this->_args->getValue( CONSOLE_GETARGS_PARAMS ); if( empty( $cmd ) ) { $this->_pl( 'No command(s) specified', STDERR ); return 1; } if( !is_array( $cmd ) ) { $cmd = array( $cmd ); } // collect targets, but remain order as defined in $_targets $availables = array_keys( $this->_commands ); $commands = array(); foreach( $cmd as $c ) { $c = strtolower( $c ); $pos = array_search( $c, $availables ); // invalid target if( $pos === false ) { $this->_pl( 'Unknown command: ' . $c, STDERR ); $this->_pl( 'Use option "--help" to recieve a list of available commands' ); return 1; } $commands[$pos] = $c; } ksort( $commands ); $commands = array_values( array_unique( $commands ) ); } // actually load config and set up factory $this->_pvl( 'Load Configuration ' . $confFile ); wbFactory::setParam( 'baseDir', $baseDir ); wbFactory::setParam( 'useCache', 0 ); wbFactory::loadSiteConfig(); $config = wbFactory::getParam( 'config' ); $this->_domains['site'] = $config['locale_domain']; $res = $this->_processCommands( $commands ); if( patErrorManager::isError( $res ) ) { $this->_pl( 'Stoped after fatal error!', STDERR ); return 127; } if( $res ) { $this->_pvl( 'Done and done!' ); $result = 0; } else { $this->_pl( 'Stoped processing after error', STDERR ); $result = 1; } if( $this->_debug ) { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Total processing time: ' . wbDebugger::getUtime( 'genesis', 1000, 6 ) . ' ms', 'Stopwatch' ); wbDebugger::printMsg(); } return $result; } /** * process all selected targets * * @access privat * @param array $commands * @return boolean $result true on success, false otherwise */ function _processCommands( $commands ) { $this->_pl( 'Processing commands' ); foreach( $commands as $c ) { $this->_pl( ' ' . $c ); $function = 'run' . ucfirst( $c ); $res = $this->$function(); if( patErrorManager::isError( $res ) ) { return $res; } if( !$res ) { $this->_pl( ' FAILED'); return false; } } return true; } /** * event hanlder * * @access private * @param string $e named event * @param mixed $info event inforation * @return boolean true on success, false if any target has failed (or patError object on error!) */ function _onEvent( $e, $info ) { switch( $e ) { case 'source': if( $this->_verbose ) { $this->_pv( ' Source "' . $info . '", ' ); } else { $this->_pl( ' Source "' . $info . '"' ); } break; case 'adminmenu': $this->_pvl( ' found ' . $info . ' translabeable strings in admin menu' ); break; case 'phpfiles': $this->_pvl( 'extract text from ' . count( $info ) . ' php files' ); break; case 'pagesfiles': $this->_pvl( 'extract text from ' . count( $info ) . ' page defintions' ); break; case 'templatefiles': $this->_pvl( 'extract text from ' . count( $info ) . ' template files' ); break; case 'templatestart': $this->_pv( sprintf( ' %60s : ', $info) ); break; case 'templateend': $this->_pvl( $info ); break; case 'msgmergestart': $this->_pvl( 'merge to ' . count( $info ) . ' language(s)' ); break; case 'msgmergepo': $this->_pv( ' ' . $info . ' ' ); break; case 'translatestart': $this->_pvl( 'translate to ' . count( $info ) . ' language(s)' ); break; case 'translatepo': $this->_pv( ' ' . $info . ' ' ); break; default: $this->_pvl( $e . ': ' . $info ); break; } return true; } /** * print text * * @access privat * @param string $txt * @param mixed $to target either STDOUT or STDERR * @return boolean $result true if text was written to output */ function _p( $txt, $to = STDOUT ) { fwrite( $to, $txt ); return true; } /** * print text and "\n" * * @access privat * @param string $txt * @param mixed $to target either STDOUT or STDERR * @return boolean $result true if text was written to output */ function _pl( $txt, $to = STDOUT ) { return $this->_p( $txt . "\n", $to ); } /** * print text when verbose * * @access privat * @param string $txt * @return boolean $result true if text was written to output */ function _pv( $txt ) { if( $this->_verbose ) { return $this->_p( $txt, STDOUT ); } return false; } /** * print text and "\n" when verbose * * @access privat * @param string $txt * @return boolean $result true if text was written to output */ function _pvl( $txt ) { if( $this->_verbose ) { return $this->_p( $txt . "\n", STDOUT ); } return false; } /** * Display help * * @access public * @param int $return return code * @return int $return code from callee */ function displayHelp( $return = 0 ) { $head = array(); array_push( $head, 'Wombat Translate Tools' ); array_push( $head, 'Language tools for developer and translators' ); array_push( $head, '' ); array_push( $head, 'Usage: ' . basename( $_SERVER['_'] ) . ' [OPTION]... COMMAND... ' ); array_push( $head, 'Options:' ); array_push( $head, '' ); $foot = array( '' ); array_push( $foot, 'Commands:' ); foreach( $this->_commands as $cmd => $desc ) { array_push( $foot, sprintf( '%-15s %s', $cmd, $desc ) ); } array_push( $foot, '' ); array_push( $foot, 'Environment variables:' ); array_push( $foot, ' Also you may want to overwrite factory parameter with environment variables.' ); array_push( $foot, ' Example: WOMBAT_' . strtoupper( $this->_parameter[0] ) . '=your_new_value' ); array_push( $foot, ' Overwriteable parameters are:' ); array_push( $foot, ' "'. implode( '", "', $this->_parameter ) . '"' ); array_push( $foot, '' ); echo Console_Getargs::getHelp( $this->_consoleArgs, implode( "\n", $head ), implode( "\n", $foot ) ); return $return; } /** * pass arguments from console * * This is usually just a copy from $_SERVER['argv'] * * @access public * @param array $argv */ function setArgv( $argv ) { $this->_args =& Console_GetArgs::factory( $this->_consoleArgs, $argv ); return true; } } ?>