Skip to content

Instantly share code, notes, and snippets.

@jdlx
Created June 4, 2012 18:34
Show Gist options
  • Save jdlx/2870092 to your computer and use it in GitHub Desktop.
Save jdlx/2870092 to your computer and use it in GitHub Desktop.
Otrance Redaxo lang file Parser
<?php
/**
* This file is part of oTranCe released under the GNU GPL 2 license
* http://www.oTranCe.de
*
* @package oTranCe
* @subpackage Importer
* @version SVN: $
* @author $Author: $
*/
/**
* Redaxo lang file importer
*
* @package oTranCe
* @subpackage Importer
*/
class Module_Import_Redaxo implements Msd_Import_Interface
{
/**
* @var string
*/
private $_data;
/**
* @var array
*/
private $_lines;
/**
* Will hold detected and extracted data
* @var array
*/
protected $_extractedData = array();
/**
* Key -> Value separator
* @var string
*/
protected $_separator = '=';
/**
* @var string
*/
protected $_currentKey;
/**
* @var string
*/
protected $_currentValue;
/**
* Analyze data and return exracted key=>value pairs
*
* @abstract
* @param string $data String data to analyze
*
* @return array Extracted key => value-Array
*/
public function extract($data)
{
$this->_data = $data;
unset($data);
$this->_extractedData = array();
$this->_lines = explode("\n", $this->_data);
$line_count = count($this->_lines);
for ($i = 0; $i < $line_count; $i++) {
$currentLine = $this->_lines[$i];
// skip comment & empty lines
if ($currentLine[0] === '#' || $currentLine === '') {
continue;
}
$currentLine = explode($this->_separator, $this->_lines[$i], 2);
$currentKey = trim($currentLine[0]);
if ($currentKey == '') {
continue;
}
$currentValue = trim($currentLine[1]);
$dataLength = strlen($currentValue);
if (($currentValue{0} == "'" && $currentValue{$dataLength -1} == "'")
|| ($currentValue{0} == "\"" && $currentValue{$dataLength -1} == "\"")) {
$currentValue = substr($currentValue, 1, $dataLength - 2);
}
$this->_extractedData[$currentKey] = $currentValue;
}
return $this->_extractedData;
}
/**
* Get rendered info view
*
* @param Zend_View_Interface $view View instance
*
* @return string
*/
public function getInfo(Zend_View_Interface $view)
{
return $view->render('redaxo.phtml');
}
}
<?php
$code = <<<CODE
TEST_KEY1 = TEST_VALUE1
TEST_KEY2 = TEST_VALUE2
CODE
?>
<h4>CSV importer information:</h4>
<table class="bdr more-padding small" summary="">
<tr class="row-even">
<td>Author:</td>
<td>
jdlx
</td>
</tr>
<tr class="row-odd">
<td>Description:</td>
<td>
This importer is intended to import Redaxo lang files.
</td>
</tr>
<tr class="row-even">
<td>Structure that can be detected:</td>
<td>
<pre><?php echo $this->out($code);?></pre>
</td>
</tr>
</table>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
// SETTINGS
////////////////////////////////////////////////////////////////////////////////
$DB = array(
'host' => '127.0.0.1',
'db' => 'otrance_loc',
'user' => 'otrance_loc',
'pwd' => 'otrance_loc',
);
$rootdir = dirname(__FILE__).'/redaxo';
$template_qry = '
INSERT INTO `filetemplates` VALUES(
\'\',
\'###NAME###\',
\'### Redaxo 4.x Lang File\r\n### file path: ###PATH###{LOCALE}_utf8.lang\r\n### lang name: {LANG_NAME}\r\n### locale: {LOCALE}\r\n### translators: {TRANSLATOR_NAMES}\r\n\',
\'\',
\'{KEY} = {VALUE}\',
\'###PATH###{LOCALE}_utf8.lang\'
);
';
// CLI ARGS
////////////////////////////////////////////////////////////////////////////////
if(isset($argv) && count($argv) > 1) {
$rootdir = !empty($argv[1]) ? $argv[1] : $rootdir;
}
// MAIN
////////////////////////////////////////////////////////////////////////////////
$lang_pathes = scanLangFilePathes($rootdir);
$pdo = new PDO(
'mysql:host='.$DB['host'].';dbname='.$DB['db'],
$DB['user'],
$DB['pwd'],
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
foreach ($lang_pathes as $path) {
$NAME = createTemplateName($path).PHP_EOL;
$PATH = 'redaxo'.$path.'/';
$qry = str_replace(array('###NAME###','###PATH###'), array($NAME,$PATH), $template_qry);
//echo $qry.PHP_EOL.PHP_EOL;
echo $pdo->exec($qry).': '.$NAME.PHP_EOL;
}
// FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
function scanLangFilePathes($dir=false)
{
if(!$dir)
return array();
$path_arr = array();
$rdi = new RecursiveDirectoryIterator($dir);
foreach(new RecursiveIteratorIterator($rdi) as $file)
{
$path_parts = pathinfo($file);
if($path_parts['extension'] == 'lang')
{
$rel_path = str_replace($dir,'',$path_parts['dirname']);
if(!in_array($rel_path,$path_arr)) {
echo 'redaxo'.$rel_path.PHP_EOL;
$path_arr[] = $rel_path;
}
}
}
return $path_arr;
}
function createTemplateName($path)
{
preg_match('#/include/addons/(\w+)(/plugins/(\w+))*#', $path, $matches);
if(isset($matches[3])) {
// plugin
return 'Plugin: '.$matches[1].' > '.$matches[3];
}
elseif(isset($matches[1])) {
// addon
return 'Addon: '.$matches[1];
}
else {
return 'Redaxo Core';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment