Skip to content

Instantly share code, notes, and snippets.

@thinkerytim
Created February 24, 2011 00:08
Show Gist options
  • Save thinkerytim/841488 to your computer and use it in GitHub Desktop.
Save thinkerytim/841488 to your computer and use it in GitHub Desktop.
revised and improved
<?php
// JOOMLA LANGUAGE FILE UPDATER
// CONVERTS JOOMLA 1.5 FILES TO JOOMLA 1.6 FORMAT
// AND THEN MODIFIES ALL JTEXT USAGES TO FIT NEW FORMAT
//
// THIS IS INTENDED TO BE USED BY JOOMLA DEVELOPERS ON THEIR OWN CODE REPOSITORY
// NOT AGAINST A FULL JOOMLA SITE!
//
// COPYRIGHT 2011 THE THINKERY LLC
// WWW.THETHINKERY.NET
// INFO@THETHINKERY.NET
//
// USE AT YOUR OWN RISK!
//
// EXAMPLE USAGE:
//
// require "JLangFileConvert.php";
// pass in $component, $directory, $loglevel-- loglevel is optional,
// and only works if you have KLogger in same dir as script-- https://github.com/katzgrau/KLogger
// $new = new JLangFileUpdater('COM_HELLOWORLD', '/home/code/com_helloworld', 'DEBUG');
ini_set('auto_detect_line_endings',TRUE);
class JLangFileUpdater
{
private $comp;
private $directory;
private $loglevel;
private $phpfiles;
private $langfiles;
public function __construct($component, $directory, $loglevel = '') {
$this->comp = $component;
$this->directory = $directory;
if ($loglevel != '') {
require "KLogger.php";
$this->loglevel = $loglevel;
// set log level
switch ($loglevel){
case 'OFF':
$this->log = new KLogger($component . '-' . date('Y-m-d_H:i:s') . ".log", KLogger::OFF);
break;
case 'FATAL':
$this->log = new KLogger($component . '-' . date('Y-m-d_H:i:s') . ".log", KLogger::FATAL);
break;
case 'ERROR':
$this->log = new KLogger($component . '-' . date('Y-m-d_H:i:s') . ".log", KLogger::ERROR);
break;
case 'WARN':
$this->log = new KLogger($component . '-' . date('Y-m-d_H:i:s') . ".log", KLogger::WARN);
break;
case 'INFO':
$this->log = new KLogger($component . '-' . date('Y-m-d_H:i:s') . ".log", KLogger::INFO);
break;
case 'DEBUG':
$this->log = new KLogger($component . '-' . date('Y-m-d_H:i:s') . ".log", KLogger::DEBUG);
break;
default:
$this->log = new KLogger($component . '-' . date('Y-m-d_H:i:s') . ".log", KLogger::OFF);
break;
}
} else {
$this->loglevel = false;
}
////////////////////////////////////////////////////////////////
// now we're going to actually do the work!
////////////////////////////////////////////////////////////////
$this->scanDirectories(); // this will read all files we need to work with into arrays
if (is_array($this->langfiles)){
foreach ($this->langfiles as $lang){
$this->convertLangFile($lang);
}
} else {
if ($this->loglevel) $this->log->logError($this->langfiles. " failed-- not an array");
}
if (is_array($this->phpfiles)){
foreach ($this->phpfiles as $phpfile){
$this->convertPHPFile($phpfile);
}
} else {
if ($this->loglevel) $this->log->logError($this->phpfiles. " failed-- not an array");
}
// should be done!
}
####################################################################
# BELOW HERE ARE THE LANGFILE FUNCTIONS #
####################################################################
private function convertLangFile($inputfile)
{
$handle = file($inputfile);
if ($this->loglevel) $this->log->logDebug("Converting langfile: " . $inputfile);
if (is_array($handle)) {
$newfile = array();
foreach ($handle as $row){
// convert the line
$newfile[] = $this->convertLine($row);
}
// write the array back to the page
file_put_contents($inputfile, implode('', $newfile));
} else {
if ($this->loglevel) $this->log->logError("Converting langfile: " . $inputfile . " failed-- not an array");
}
}
private function convertLine($line)
{
if ($this->loglevel) $this->log->logDebug("Raw line: " . $line);
// trim off the line endings/returns
$line = rtrim($line);
// first check if it's a comment and change hash to semicolon
if (substr($line, 0, 1) == '#') {
$line = substr_replace($line , ';' , 0, 1);
if ($this->loglevel) $this->log->logDebug("Comment line: " . $line);
return $line . "\r\n";
} else {
// clean line with regex check
$line = $this->regexReplace($line);
if ($this->loglevel) $this->log->logDebug("Cleaned line: " . $line);
// make sure we're not dealing with a blank line
if (!strlen($line) >= 2) return $line . "\r\n";
// now split line up at =
$line = explode("=", $line);
// replace spaces with underscores, prepend component, uppercase string
$first = strtoupper($this->comp . '_' . str_replace(" ","_",$line[0]));
// replace " with ', add quotes at beginning and end of line
$last = '"' . str_replace("\"","'",$line[1]) . '"';
$last = rtrim($last);
$newline = $first . "=" . $last . "\r\n";
if ($this->loglevel) $this->log->logDebug("Final line: " . $newline);
return $newline;
}
}
private function regexReplace($line)
{
$count = 0;
$line = preg_replace(array('^(null|yes|no|true|false|on|off|none)=(.+)^'), '', $line, -1 , $count);
return $line;
}
####################################################################
# BELOW HERE ARE THE PHP FILE FUNCTIONS #
####################################################################
private function convertPHPFile($inputfile)
{
$handle = file($inputfile);
if ($this->loglevel) $this->log->LogDebug("Scanning file: " . $inputfile);
if (is_array($handle)) {
$newfile = array();
foreach ($handle as $row){
// convert the line
$newfile[] = $this->doRegex($row);
}
// write the array back to the page
file_put_contents($inputfile, implode('', $newfile));
} else {
if ($this->loglevel) $this->log->logError("Converting PHP file: " . $inputfile . " failed-- not an array");
}
}
private function doRegex($line)
{
$pattern = "&(JText::_\(\s?)('|\")([A-Za-z\s]+)('|\")\s?\)&";
if (preg_match($pattern, $line, $matches)) {
if ($this->loglevel) $this->log->LogDebug("Found a match: " . $matches[0]);
$line = preg_replace($pattern, "JText::_( '" . $this->transformString($matches[3]) . "' )", $line);
if ($this->loglevel) $this->log->LogDebug("Writing new line: " . $line);
return $line;
} else {
return $line;
}
}
private function transformString($string)
{
$line = strtoupper($string);
$line = str_replace(" ","_",$line);
$line = $this->comp . "_" . $line;
return $line;
}
####################################################################
# BELOW HERE ARE UTILTY FUNCTIONS #
####################################################################
private function scanDirectories()
{
$path = $this->directory;
// create arrays to hold files
$this->langfiles = array(); // this will get passed to the convertLangFile function
$this->phpfiles = array(); // this will get passed to the convertPHPFile function
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach($files as $fname => $object){
$ext = substr(strrchr($fname, '.'), 1);
if ($ext == 'ini') {
$this->langfiles[] = $fname;
if ($this->loglevel) $this->log->LogDebug("Found INI file: " . $fname);
} else if ($ext == 'php') {
$this->phpfiles[] = $fname;
if ($this->loglevel) $this->log->LogDebug("Found PHP file: " . $fname);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment