Commit 2b67f012 authored by totten's avatar totten

CRM-15832, INFRA-132 - Cleanup code-style in *extractor.php

parent 8dfbebe6
......@@ -4,29 +4,29 @@
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License along with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License along with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
......@@ -46,36 +46,36 @@ error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
* Called from bin/create-pot-files.sh
*/
$phpModifier = "-iname '*.php' ";
$jsModifier = "-iname '*.js' ";
$phpModifier = "-iname '*.php' ";
$jsModifier = "-iname '*.js' ";
$smartyModifier = "\( -iname '*.tpl' -or -iname '*.hlp' \) ";
if ($argv[1] == 'base') {
// 'base' is a special case for the common-base.pot file
// it fetches a list of directories from bin/basedirs
$phpDir = array();
$jsDir = array();
$tplDir = array();
$phpDir[] = "-iwholename '*/packages/HTML/QuickForm/*'";
foreach (explode("\n", file_get_contents('bin/basedirs')) as $dir) {
$phpDir[] = "-iwholename '*/CRM/$dir/*'";
$jsDir[] = "-iwholename '*/templates/CRM/$dir/*'";
$tplDir[] = "-iwholename '*/templates/CRM/$dir/*'";
}
$phpModifier .= "\( " . implode(' -or ', $phpDir) . " \)";
$jsModifier .= "\( " . implode(' -or ', $jsDir) . " \)";
$smartyModifier .= "\( " . implode(' -or ', $tplDir) . " \)";
// 'base' is a special case for the common-base.pot file
// it fetches a list of directories from bin/basedirs
$phpDir = array();
$jsDir = array();
$tplDir = array();
$phpDir[] = "-iwholename '*/packages/HTML/QuickForm/*'";
foreach (explode("\n", file_get_contents('bin/basedirs')) as $dir) {
$phpDir[] = "-iwholename '*/CRM/$dir/*'";
$jsDir[] = "-iwholename '*/templates/CRM/$dir/*'";
$tplDir[] = "-iwholename '*/templates/CRM/$dir/*'";
}
$phpModifier .= "\( " . implode(' -or ', $phpDir) . " \)";
$jsModifier .= "\( " . implode(' -or ', $jsDir) . " \)";
$smartyModifier .= "\( " . implode(' -or ', $tplDir) . " \)";
}
elseif ($argv[1] == 'extension' || $argv[1] == 'install') {
// nothing to do
// see special handling of $command below.
// nothing to do
// see special handling of $command below.
}
else {
$phpModifier .= "-iwholename '*/CRM/{$argv[1]}/*'";
$jsModifier .= "-iwholename '*/templates/CRM/{$argv[1]}/*'";
$smartyModifier .= "-iwholename '*/templates/CRM/{$argv[1]}/*'";
$phpModifier .= "-iwholename '*/CRM/{$argv[1]}/*'";
$jsModifier .= "-iwholename '*/templates/CRM/{$argv[1]}/*'";
$smartyModifier .= "-iwholename '*/templates/CRM/{$argv[1]}/*'";
}
$component = $argv[1];
......@@ -148,68 +148,72 @@ $resultArray = array();
$originalArray = explode("\n", $phpPot . $jsPot . $smartyPot);
while ($originalArray[0] != '') {
$resultArray[] = array_shift($originalArray);
$resultArray[] = array_shift($originalArray);
}
$resultArray[] = array_shift($originalArray);
// break the POT contents into separate comments/msgid blocks
foreach ($originalArray as $line) {
// if it's the end of a block, put the $block in $blocks and start a new one
if ($line == '' and $block != array()) {
$blocks[] = $block;
$block = array();
} else {
// else add the line to the proper $block part
// the lines in the POT file are either comments, single- and multiline
// msgids or empty msgstrs; we ignore the msgstrs
if (substr($line, 0, 1) == '#') {
$block['comments'][] = $line;
} elseif (substr($line, 0, 6) != 'msgstr') {
$block['msgid'][] = $line;
}
// if it's the end of a block, put the $block in $blocks and start a new one
if ($line == '' and $block != array()) {
$blocks[] = $block;
$block = array();
}
else {
// else add the line to the proper $block part
// the lines in the POT file are either comments, single- and multiline
// msgids or empty msgstrs; we ignore the msgstrs
if (substr($line, 0, 1) == '#') {
$block['comments'][] = $line;
}
elseif (substr($line, 0, 6) != 'msgstr') {
$block['msgid'][] = $line;
}
}
}
// combine the msgid parts into single strings and build a new array with msgid
// as key and arrays with comments as value; drop the empty msgids
foreach ($blocks as $block) {
$msgid = implode("\n", $block['msgid']);
if ($msgid != 'msgid ""') {
foreach ($block['comments'] as $comment) {
$msgidArray[$msgid][] = $comment;
}
$msgid = implode("\n", $block['msgid']);
if ($msgid != 'msgid ""') {
foreach ($block['comments'] as $comment) {
$msgidArray[$msgid][] = $comment;
}
}
}
// combine the comments indicating the source files into single comment lines
foreach ($msgidArray as $msgid => $commentsArray) {
$newCommentsArray = array();
$sourceComments = array();
foreach ($commentsArray as $comment) {
if (substr($comment, 0, 3) == '#: ') {
$sourceComments[] = substr($comment, 3);
} else {
$newCommentsArray[] = $comment;
}
$newCommentsArray = array();
$sourceComments = array();
foreach ($commentsArray as $comment) {
if (substr($comment, 0, 3) == '#: ') {
$sourceComments[] = substr($comment, 3);
}
if (count($sourceComments)) {
$newCommentsArray[] = '#: ' . implode(' ', $sourceComments);
else {
$newCommentsArray[] = $comment;
}
$msgidArray[$msgid] = $newCommentsArray;
}
if (count($sourceComments)) {
$newCommentsArray[] = '#: ' . implode(' ', $sourceComments);
}
$msgidArray[$msgid] = $newCommentsArray;
}
// build the rest of the $resultArray from the $msgidArray
foreach ($msgidArray as $msgid => $commentsArray) {
foreach ($commentsArray as $comment) {
$resultArray[] = $comment;
}
$resultArray[] = $msgid;
// if it's a plural, add plural msgstr, else add singular
if (strpos($msgid, "\nmsgid_plural ")) {
$resultArray[] = "msgstr[0] \"\"\nmsgstr[1] \"\"\n";
} else {
$resultArray[] = "msgstr \"\"\n";
}
foreach ($commentsArray as $comment) {
$resultArray[] = $comment;
}
$resultArray[] = $msgid;
// if it's a plural, add plural msgstr, else add singular
if (strpos($msgid, "\nmsgid_plural ")) {
$resultArray[] = "msgstr[0] \"\"\nmsgstr[1] \"\"\n";
}
else {
$resultArray[] = "msgstr \"\"\n";
}
}
// output the $resultArray to STDOUT
......
......@@ -41,8 +41,10 @@ array_splice($_SERVER['argv'], 1, 1);
// extensions of js files, used when going through a directory
$extensions = array('js');
// "fix" string - strip slashes, escape and convert new lines to \n
// see: http://issues.civicrm.org/jira/browse/CRM-10833
/**
* "fix" string - strip slashes, escape and convert new lines to \n
* @link http://issues.civicrm.org/jira/browse/CRM-10833
*/
function fs($text) {
$quote = $text[0];
// Remove newlines
......@@ -56,66 +58,71 @@ function fs($text) {
return $text;
}
// rips gettext strings from $file and prints them in C format
/**
* Rips gettext strings from $file and prints them in C format.
*/
function do_file($file) {
$content = @file_get_contents($file);
$strings = array();
$content = @file_get_contents($file);
$strings = array();
if (empty($content)) {
return;
}
if (empty($content)) {
return;
}
global $root;
global $root;
// Match all calls to ts()
// Note: \s also matches newlines with the 's' modifier.
preg_match_all('~
// Match all calls to ts()
// Note: \s also matches newlines with the 's' modifier.
preg_match_all('~
[^\w]ts\s* # match "ts" with whitespace
\(\s* # match "(" argument list start
((?:(?:\'(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+)\s*
[,\)] # match ")" or "," to finish
~sx', $content, $matches);
foreach($matches[1] as $text) {
if ($text = fs($text)) {
print '#: ' . substr($file, strlen($root) + 1) . "\n";
print 'msgid "' . $text . "\"\n";
print "msgstr \"\"\n\n";
}
foreach ($matches[1] as $text) {
if ($text = fs($text)) {
print '#: ' . substr($file, strlen($root) + 1) . "\n";
print 'msgid "' . $text . "\"\n";
print "msgstr \"\"\n\n";
}
}
}
// go through a directory
function do_dir($dir)
{
$d = dir($dir);
while (false !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry = $dir.'/'.$entry;
if (is_dir($entry)) {
// if a directory, go through it
do_dir($entry);
} else {
// if file, parse only if extension is matched
$pi = pathinfo($entry);
if (isset($pi['extension']) && in_array($pi['extension'], $GLOBALS['extensions'])) {
do_file($entry);
}
}
/**
* Go through a directory.
*/
function do_dir($dir) {
$d = dir($dir);
while (FALSE !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
$d->close();
}
$entry = $dir . '/' . $entry;
if (is_dir($entry)) {
// if a directory, go through it
do_dir($entry);
}
else {
// if file, parse only if extension is matched
$pi = pathinfo($entry);
for ($ac=1; $ac < $_SERVER['argc']; $ac++) {
if (is_dir($_SERVER['argv'][$ac])) {
do_dir($_SERVER['argv'][$ac]);
} else {
do_file($_SERVER['argv'][$ac]);
if (isset($pi['extension']) && in_array($pi['extension'], $GLOBALS['extensions'])) {
do_file($entry);
}
}
}
$d->close();
}
for ($ac = 1; $ac < $_SERVER['argc']; $ac++) {
if (is_dir($_SERVER['argv'][$ac])) {
do_dir($_SERVER['argv'][$ac]);
}
else {
do_file($_SERVER['argv'][$ac]);
}
}
This diff is collapsed.
......@@ -53,106 +53,112 @@ $cmd = preg_quote('ts');
// extensions of smarty files, used when going through a directory
$extensions = array('tpl', 'hlp');
// "fix" string - strip slashes, escape and convert new lines to \n
function fs($str)
{
$str = stripslashes($str);
$str = str_replace('"', '\"', $str);
$str = str_replace("\n", '\n', $str);
return $str;
/**
* "fix" string - strip slashes, escape and convert new lines to \n
*/
function fs($str) {
$str = stripslashes($str);
$str = str_replace('"', '\"', $str);
$str = str_replace("\n", '\n', $str);
return $str;
}
// rips gettext strings from $file and prints them in C format
function do_file($file)
{
$content = @file_get_contents($file);
if (empty($content)) {
return;
/**
* Rips gettext strings from $file and prints them in C format.
*/
function do_file($file) {
$content = @file_get_contents($file);
if (empty($content)) {
return;
}
global $ldq, $rdq, $cmd, $root;
// if there’s a {php} tag, fetch its contents into a file and parse it with php-extractor.php
$phpTagMatches = array();
preg_match_all("/{$ldq}\s*(php)\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/", $content, $phpTagMatches);
$phpCode = $phpTagMatches[3][0];
if ($phpCode) {
// we want to create a file with the same path and name, but under the
// tempdir; this allows the php-extractor.php to add the right comment
$tempdir = sys_get_temp_dir();
$filedir = substr(dirname($file), strlen($root) + 1);
$filename = basename($file);
@mkdir("$tempdir/$filedir", 0777, TRUE);
file_put_contents("$tempdir/$filedir/$filename", "<?php $phpCode ?>");
passthru("bin/php-extractor.php $tempdir $tempdir/$filedir/$filename");
}
preg_match_all("/{$ldq}\s*({$cmd})\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/", $content, $matches);
for ($i = 0; $i < count($matches[0]); $i++) {
$output = array();
$output[] = '#: ' . substr($file, strlen($root) + 1);
if (preg_match('/plural\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) {
$output[] = 'msgid "' . fs($matches[3][$i]) . '"';
$output[] = 'msgid_plural "' . fs($match[1]) . '"';
$output[] = 'msgstr[0] ""';
$output[] = 'msgstr[1] ""';
}
else {
$output[] = 'msgid "' . fs($matches[3][$i]) . '"';
$output[] = 'msgstr ""';
}
print implode("\n", $output) . "\n\n";
}
global $ldq, $rdq, $cmd, $root;
// if there’s a {php} tag, fetch its contents into a file and parse it with php-extractor.php
$phpTagMatches = array();
preg_match_all("/{$ldq}\s*(php)\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/", $content, $phpTagMatches);
$phpCode = $phpTagMatches[3][0];
if ($phpCode) {
// we want to create a file with the same path and name, but under the
// tempdir; this allows the php-extractor.php to add the right comment
$tempdir = sys_get_temp_dir();
$filedir = substr(dirname($file), strlen($root) + 1);
$filename = basename($file);
@mkdir("$tempdir/$filedir", 0777, true);
file_put_contents("$tempdir/$filedir/$filename", "<?php $phpCode ?>");
passthru("bin/php-extractor.php $tempdir $tempdir/$filedir/$filename");
preg_match_all("/{$ldq}\s*(docURL)\s*([^{$rdq}]*){$rdq}/", $content, $matches);
for ($i = 0; $i < count($matches[0]); $i++) {
if (preg_match('/text\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) {
print '#: ' . substr($file, strlen($root) + 1) . "\n";
print 'msgid "' . fs($match[1]) . "\"\n";
print "msgstr \"\"\n\n";
}
if (preg_match('/title\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) {
print '#: ' . substr($file, strlen($root) + 1) . "\n";
print 'msgid "' . fs($match[1]) . "\"\n";
print "msgstr \"\"\n\n";
}
}
}
/**
* Go through a directory.
*/
function do_dir($dir) {
$d = dir($dir);
preg_match_all("/{$ldq}\s*({$cmd})\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/", $content, $matches);
for ($i=0; $i < count($matches[0]); $i++) {
$output = array();
$output[] = '#: ' . substr($file, strlen($root) + 1);
if (preg_match('/plural\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) {
$output[] = 'msgid "' . fs($matches[3][$i]) . '"';
$output[] = 'msgid_plural "' . fs($match[1]) . '"';
$output[] = 'msgstr[0] ""';
$output[] = 'msgstr[1] ""';
} else {
$output[] = 'msgid "' . fs($matches[3][$i]) . '"';
$output[] = 'msgstr ""';
}
print implode("\n", $output) . "\n\n";
while (FALSE !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
preg_match_all("/{$ldq}\s*(docURL)\s*([^{$rdq}]*){$rdq}/", $content, $matches);
for ($i=0; $i < count($matches[0]); $i++) {
if (preg_match('/text\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) {
print '#: ' . substr($file, strlen($root) + 1) . "\n";
print 'msgid "' . fs($match[1]) . "\"\n";
print "msgstr \"\"\n\n";
}
if (preg_match('/title\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) {
print '#: ' . substr($file, strlen($root) + 1) . "\n";
print 'msgid "' . fs($match[1]) . "\"\n";
print "msgstr \"\"\n\n";
}
$entry = $dir . '/' . $entry;
if (is_dir($entry)) {
// if a directory, go through it
do_dir($entry);
}
}
else {
// if file, parse only if extension is matched
$pi = pathinfo($entry);
// go through a directory
function do_dir($dir)
{
$d = dir($dir);
while (false !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry = $dir.'/'.$entry;
if (is_dir($entry)) {
// if a directory, go through it
do_dir($entry);
} else {
// if file, parse only if extension is matched
$pi = pathinfo($entry);
if (isset($pi['extension']) && in_array($pi['extension'], $GLOBALS['extensions'])) {
do_file($entry);
}
}
if (isset($pi['extension']) && in_array($pi['extension'], $GLOBALS['extensions'])) {
do_file($entry);
}
}
}
$d->close();
$d->close();
}
for ($ac=1; $ac < $_SERVER['argc']; $ac++) {
if (is_dir($_SERVER['argv'][$ac])) {
do_dir($_SERVER['argv'][$ac]);
} else {
do_file($_SERVER['argv'][$ac]);
}
for ($ac = 1; $ac < $_SERVER['argc']; $ac++) {
if (is_dir($_SERVER['argv'][$ac])) {
do_dir($_SERVER['argv'][$ac]);
}
else {
do_file($_SERVER['argv'][$ac]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment