Skip to content

Commit

Permalink
cleanup extract-db-locale-terms.php script (ChurchCRM#6960)
Browse files Browse the repository at this point in the history
# Description & Issue number it closes 
<!-- Please include a summary of the changes and the related issue.
Please also include relevant motivation and context. -->

@respencer and I chatted and found some areas for cleanup in this script

## How to test the changes?

N/A

---------

Co-authored-by: Robert Spencer <respencer@users.noreply.github.com>
  • Loading branch information
DAcodedBEAT and respencer committed May 13, 2024
1 parent 6ec1008 commit 056fa79
Showing 1 changed file with 61 additions and 37 deletions.
98 changes: 61 additions & 37 deletions locale/extract-db-locale-terms.php
Original file line number Diff line number Diff line change
@@ -1,87 +1,111 @@
<?php

echo <<<HEREDOC
===============================================
========== Building locale from DB started ===
===============================================
=====================================================
========== Building locale from DB started ==========
=====================================================
HEREDOC;

$filename = '..'.DIRECTORY_SEPARATOR.'BuildConfig.json';
$filename = '..' . DIRECTORY_SEPARATOR . 'BuildConfig.json';

if (is_file($filename)) {
$buildConfig = file_get_contents($filename);
$config = json_decode($buildConfig, true);

if (empty($config['Env']['local']['database'])) {
echo "ERROR: The file $filename does not have local db env, check ".$filename.".example for schema\n";
echo <<<HEREDOC
ERROR: The file $filename does not have local db env, check $filename.example for schema
HEREDOC;
} else {
$localDBEnv = $config['Env']['local']['database'];

$db_server = $localDBEnv['server'];
$db_port = $localDBEnv['port'];

$db_name = $localDBEnv['database'];
$db_username = $localDBEnv['user'];
$db_password = $localDBEnv['password'];

$stringsDir = 'db-strings';
$stringFiles = [];

$db = new PDO('mysql:host='.$db_server.':'.$db_port.';dbname='.$db_name.';charset=utf8mb4', $db_username, $db_password);
$query = 'select DISTINCT ucfg_tooltip as term, "" as translation, "userconfig_ucfg" as cntx from userconfig_ucfg
union all
select DISTINCT qry_Name as term, "" as translation, "query_qry" as cntx from query_qry
union all
select DISTINCT qry_Description as term, "" as translation, "query_qry" as cntx from query_qry
union all
select DISTINCT qpo_Display as term, "" as translation, "queryparameteroptions_qpo" as cntx from queryparameteroptions_qpo
union all
select DISTINCT qrp_Name as term, "" as translation, "queryparameters_qrp" as cntx from queryparameters_qrp
union all
select DISTINCT qrp_Description term, "" as translation, "queryparameters_qrp" as cntx from queryparameters_qrp';

echo "DB read complete \n";
$db = new PDO(
'mysql:host=' . $db_server . ':' . $db_port . ';dbname=' . $db_name . ';charset=utf8mb4',
$db_username,
$db_password
);

$query = <<<SQL
SELECT DISTINCT ucfg_tooltip AS term, "" AS translation, "userconfig_ucfg" AS cntx FROM userconfig_ucfg
UNION ALL
SELECT DISTINCT qry_Name AS term, "" AS translation, "query_qry" AS cntx FROM query_qry
UNION ALL
SELECT DISTINCT qry_Description AS term, "" AS translation, "query_qry" AS cntx FROM query_qry
UNION ALL
SELECT DISTINCT qpo_Display AS term, "" AS translation, "queryparameteroptions_qpo" AS cntx FROM queryparameteroptions_qpo
UNION ALL
SELECT DISTINCT qrp_Name AS term, "" AS translation, "queryparameters_qrp" AS cntx FROM queryparameters_qrp
UNION ALL
SELECT DISTINCT qrp_Description AS term, "" AS translation, "queryparameters_qrp" AS cntx FROM queryparameters_qrp
SQL;

echo "DB read complete" . PHP_EOL;

foreach ($db->query($query) as $row) {
$stringFile = $stringsDir.DIRECTORY_SEPARATOR.$row['cntx'].'.php';
$stringFile = $stringsDir . DIRECTORY_SEPARATOR . $row['cntx'] . '.php';
if (!is_file($stringFile)) {
file_put_contents($stringFile, "<?php\r\n", FILE_APPEND);
$stringFiles[] = $stringFile;
}

$rawDBTerm = $row['term'];
$dbTerm = addslashes($rawDBTerm);
file_put_contents($stringFile, "gettext('".$dbTerm."');\n", FILE_APPEND);
file_put_contents($stringFile, "gettext('" . $dbTerm . "');\r\n", FILE_APPEND);
}

foreach ($stringFiles as $stringFile) {
file_put_contents($stringFile, "\r\n?>", FILE_APPEND);
file_put_contents($stringFile, "\r\n?>" . "\r\n", FILE_APPEND);
}

$stringFile = $stringsDir.DIRECTORY_SEPARATOR.'settings-countries.php';
// Create and populate settings-countries.php
$stringFile = $stringsDir . DIRECTORY_SEPARATOR . 'settings-countries.php';
require '../src/ChurchCRM/data/Countries.php';
require '../src/ChurchCRM/data/Country.php';

file_put_contents($stringFile, "<?php\r\n", FILE_APPEND);

foreach (ChurchCRM\data\Countries::getNames() as $country) {
file_put_contents($stringFile, 'gettext("'.addslashes($country)."\");\r\n", FILE_APPEND);
$countryTerm = addslashes($country);
file_put_contents($stringFile, "gettext('" . $countryTerm . "');\r\n", FILE_APPEND);
}
file_put_contents($stringFile, "\r\n?>", FILE_APPEND);

$stringFile = $stringsDir.DIRECTORY_SEPARATOR.'settings-locales.php';
file_put_contents($stringFile, "\r\n?>" . "\r\n", FILE_APPEND);

// Create and populate settings-locales.php
$stringFile = $stringsDir . DIRECTORY_SEPARATOR . 'settings-locales.php';
file_put_contents($stringFile, "<?php\r\n", FILE_APPEND);
$localesFile = file_get_contents(
implode(DIRECTORY_SEPARATOR, ['..','src','locale','locales.json'])
);

$localesFile = file_get_contents(implode(DIRECTORY_SEPARATOR, ['..', 'src', 'locale', 'locales.json']));
$locales = json_decode($localesFile, true);

foreach ($locales as $key => $value) {
file_put_contents($stringFile, 'gettext("'.$key."\");\r\n", FILE_APPEND);
file_put_contents($stringFile, "gettext('" . $key . "');\r\n", FILE_APPEND);
}
file_put_contents($stringFile, "\r\n?>", FILE_APPEND);
echo $stringFile.' updated';

file_put_contents($stringFile, "\r\n?>" . "\r\n", FILE_APPEND);

echo $stringFile . ' updated' . PHP_EOL;
}
} else {
echo "ERROR: The file $filename does not exist \n";
echo "ERROR: The file $filename does not exist" . PHP_EOL;
}
echo "\n\n=============================================== \n";
echo "========== Building locale from DB end === \n";
echo "=============================================== \n";

echo <<<HEREDOC
=====================================================
========== Building locale from DB end ==========
=====================================================
HEREDOC;

0 comments on commit 056fa79

Please sign in to comment.