* @license PHP License * @package WB * @subpackage MailMime */ WBClass::load('WBCli' , 'WBObserver'); /** * Command Line Interface class: NLS Knife * * Pocket knife for NLS stuff * * @version 1.1.0 * @package WB */ class WBCli_NLSKnife extends WBCli implements WBObserver { /** * Select text domain * @var string */ private $domain = null; /** * Select language * @var string */ private $lang = null; /** * @var WBDatasource_Table */ private $table; /** * markup scanner * @var WBMarkup_Scanner */ private $scanner; /** * markup scanner handler * @var WBMarkup_Handler_Xml2Xml */ private $handler; /** * XML parser resource * @var resource */ private $parser; /** * Use strict mode * @var bool */ private $strict = false; /** * List of open file handlers * @var array */ private $fh = array(); /** * XML stack * @var array */ private $xmlStack = array(); /** * Method to call for each XML element * @var string */ private $xmlHandler = ''; /** * Message timestamp * @var string */ private $xmlTimestamp = ''; /** * Command line arguments * @var array */ protected $arguments = array( 'in' => array( 'short' => 'i', 'max' => 1, 'min' => 0, 'default' => '', 'desc' => 'in file', ), 'out' => array( 'short' => 'o', 'max' => 1, 'min' => 0, 'default' => '', 'desc' => 'out file', ), 'domain' => array( 'short' => 'd', 'max' => 1, 'min' => 0, 'default' => '', 'desc' => 'text domain', ), 'lang' => array( 'short' => 'l', 'max' => 1, 'min' => 0, 'default' => '', 'desc' => 'language code, like "de_DE"', ), 'strict' => array( 'short' => 's', 'max' => 0, 'desc' => 'Validate strict use of tags"', ), CONSOLE_GETARGS_PARAMS => array( 'min' => 1, 'max' => -1, 'default' => 'export', 'desc' => 'Command', ) ); /** * 2nd constructor * * load config and setup importer */ protected function init() { $this->table = WBClass::create('WBDatasource_Table'); $this->table->switchTranslation(false); $this->scanner = WBClass::create('WBMarkup_Scanner'); $this->handler = WBClass::create('WBMarkup_Handler_Xml2Xml'); $this->scanner->setHandler($this->handler); } /** * execute programme * * */ protected function execute() { if (empty($this->commands)) { $this->commands = array('export'); } $optFmt = ' %10s: %s'; if ($this->args->isDefined('strict')) { $this->strict = true; } $this->domain = $this->args->getValue('domain'); if (empty($this->domain)) { $this->domain = null; $this->pvl(sprintf($optFmt, 'Domain', 'ANY')); } else { $this->domain = strtolower($this->domain); $this->pvl(sprintf($optFmt, 'Domain', $this->domain)); } $this->lang = $this->args->getValue('lang'); $this->pvl(sprintf($optFmt, 'Language', $this->lang)); $this->pvl(sprintf($optFmt, 'Strict', intval($this->strict))); $cmd = strtolower(array_shift($this->commands)); switch ($cmd) { case 'import': $ret = $this->import(); break; case 'export': $ret = $this->export(); break; case 'validate': $ret = $this->validate(); break; default: $this->pl('Command "' . $cmd . '" not supported!', STDERR); $ret = 127; break; } // close all open files foreach ($this->fh as $fh) { fclose($fh); } return $ret; } /** * Import XML messages to DB * * */ public function import() { if (empty($this->lang)) { $this->pl('Paramerter lang is required, but empty'); return 3; } $in = $this->openFile($this->args->getValue('in'), 'r'); if (!$in) { return 2; } $this->pvl('Import XML file ' . $this->args->getValue('in')); $this->xmlHandler = 'Import'; $length = $this->parseXml($in); if (0 > $length) { $this->pl('Import failed!'); return; } $this->pvl('OK, imported '. $length.' bytes.'); } /** * Export NLS messages to XML * * */ public function export() { if (empty($this->lang)) { $this->pl('Paramerter lang is required, but empty'); return 3; } $out = $this->openFile($this->args->getValue('out'), 'w'); if (!$out) { return 2; } fputs($out, '' . "\n"); fputs($out, '' . "\n"); fputs($out, sprintf('', gmdate('Y-m-d H:i:s')) . "\n"); $options = array( 'limit' => 100 ); $clause = array(); if ($this->domain) { $clause[] = array( 'field' => 'domain', 'value' => $this->domain ); } $clause[] = array( 'field' => 'orphan', 'value' => 0 ); $clause[] = array( 'field' => 'trans_' . $this->lang, 'relation' => 'NULL', 'value' => 1 ); if (!empty($this->commands)) { $clause[] = array( 'field' => 'mid', 'relation' => 'IN', 'value' => $this->commands ); } $pager = $this->table->getPager('imex-nlsmg', 'nlsmsg', null, $clause, $options); $pInfo = $pager->browse('__first'); $pInfo['page'] = 0; while ($pInfo['page'] < $pInfo['pages']) { $list = $pager->get(); foreach ($list as $l) { $msg = $this->exportMsg($l); fputs($out, $msg); } $pInfo = $pager->browse('__next'); } fputs($out, '' . "\n"); fputs($out, '' . "\n"); return 0; } /** * Convert DB record to XML tag * * Create wbnls:msg tag with id, changed date and message * * @param array $r record in table * @return string */ private function exportMsg($r) { $changed = $r['changed']; if ($changed < $r['created']) { $changed = $r['created']; } //$this->scanner->scan($r['trans_' . $this->lang]); $this->scanner->scan($r['msg']); $msg = $this->handler->getParsedContent(); $msg = str_replace('®', '®', $msg); return sprintf('%s', $r['id'], $changed, $msg) . "\n"; } /** * Validate XML file * * */ private function validate() { $in = $this->openFile($this->args->getValue('in'), 'r'); if (!$in) { return 2; } $this->pvl('Validate XML file ' . $this->args->getValue('in')); $this->xmlHandler = 'Validate'; $length = $this->parseXml($in); if (0 > $length) { $this->pl('Validation failed!'); return; } $this->pvl('OK, checked '. $length.' bytes.'); } /** * Handle XML Start Element: Validate * * @param string $element * @param array $attributes */ private function handleXmlStartElementValidate($element, $attributes) { if (!$this->strict) { return; } $pPos = count($this->xmlStack) - 2; if (0 > $pPos) { return; } $error = ''; $me = end($this->xmlStack); $parent = $this->xmlStack[$pPos]; switch ($element) { case 'strong': if ('ul' == $parent['element']) { $error = sprintf('Strong-tag is not allowed as direct child of