From d4226f88a3bf2138634a888f179b965d45ac5ace Mon Sep 17 00:00:00 2001 From: Julia Longtin Date: Tue, 26 Jan 2016 22:35:53 +0000 Subject: [PATCH 1/2] add command line argument for dbDsn. --- CRM/Core/CodeGen/Main.php | 9 ++++++++- CRM/Core/CodeGen/Schema.php | 10 +++++----- CRM/Core/CodeGen/Util/Template.php | 9 ++++++++- CRM/Core/Smarty/plugins/block.ts.php | 6 ++++++ xml/GenCode.php | 3 ++- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/CRM/Core/CodeGen/Main.php b/CRM/Core/CodeGen/Main.php index 6a5ff4387243..067684257858 100644 --- a/CRM/Core/CodeGen/Main.php +++ b/CRM/Core/CodeGen/Main.php @@ -25,6 +25,11 @@ class CRM_Core_CodeGen_Main { */ var $digest; + /** + * @var string|NULL authentication credentials, for connecting to the database during character type conversions, and escaping. + */ + var $dbDsn; + /** * @param $CoreDAOCodePath * @param $sqlCodePath @@ -35,13 +40,15 @@ class CRM_Core_CodeGen_Main { * @param $argVersion * @param $schemaPath * @param $digestPath + * @param $dbDsn */ - public function __construct($CoreDAOCodePath, $sqlCodePath, $phpCodePath, $tplCodePath, $smartyPluginDirs, $argCms, $argVersion, $schemaPath, $digestPath) { + public function __construct($CoreDAOCodePath, $sqlCodePath, $phpCodePath, $tplCodePath, $smartyPluginDirs, $argCms, $argVersion, $schemaPath, $digestPath, $dbDsn) { $this->CoreDAOCodePath = $CoreDAOCodePath; $this->sqlCodePath = $sqlCodePath; $this->phpCodePath = $phpCodePath; $this->tplCodePath = $tplCodePath; $this->digestPath = $digestPath; + $this->dbDsn = $dbDsn; $this->digest = NULL; // default cms is 'drupal', if not specified diff --git a/CRM/Core/CodeGen/Schema.php b/CRM/Core/CodeGen/Schema.php index c8b0a04f9d77..29307f22c2cc 100644 --- a/CRM/Core/CodeGen/Schema.php +++ b/CRM/Core/CodeGen/Schema.php @@ -32,7 +32,7 @@ public function run() { */ public function generateCreateSql($fileName = 'civicrm.mysql') { echo "Generating sql file\n"; - $template = new CRM_Core_CodeGen_Util_Template('sql'); + $template = new CRM_Core_CodeGen_Util_Template('sql', $this->config->dbDsn); $template->assign('database', $this->config->database); $template->assign('tables', $this->tables); @@ -49,19 +49,19 @@ public function generateCreateSql($fileName = 'civicrm.mysql') { public function generateDropSql($fileName = 'civicrm_drop.mysql') { echo "Generating sql drop tables file\n"; $dropOrder = array_reverse(array_keys($this->tables)); - $template = new CRM_Core_CodeGen_Util_Template('sql'); + $template = new CRM_Core_CodeGen_Util_Template('sql', $this->config->dbDsn); $template->assign('dropOrder', $dropOrder); $template->run('drop.tpl', $this->config->sqlCodePath . $fileName); } public function generateNavigation() { echo "Generating navigation file\n"; - $template = new CRM_Core_CodeGen_Util_Template('sql'); + $template = new CRM_Core_CodeGen_Util_Template('sql', $this->config->dbDsn); $template->run('civicrm_navigation.tpl', $this->config->sqlCodePath . "civicrm_navigation.mysql"); } public function generateLocaleDataSql() { - $template = new CRM_Core_CodeGen_Util_Template('sql'); + $template = new CRM_Core_CodeGen_Util_Template('sql', $this->config->dbDsn); global $tsLocale; $oldTsLocale = $tsLocale; @@ -91,7 +91,7 @@ public function generateLocaleDataSql() { } public function generateSample() { - $template = new CRM_Core_CodeGen_Util_Template('sql'); + $template = new CRM_Core_CodeGen_Util_Template('sql', $this->config->dbDsn); $sections = array( 'civicrm_sample.tpl', 'civicrm_acl.tpl', diff --git a/CRM/Core/CodeGen/Util/Template.php b/CRM/Core/CodeGen/Util/Template.php index 2590301c347a..a0151393e0e8 100644 --- a/CRM/Core/CodeGen/Util/Template.php +++ b/CRM/Core/CodeGen/Util/Template.php @@ -6,14 +6,17 @@ class CRM_Core_CodeGen_Util_Template { protected $filetype; + protected $dbDsn; + protected $smarty; protected $beautifier; /** * @param string $filetype */ - public function __construct($filetype) { + public function __construct($filetype, $dbDsn = "mysql://username:password@localhost/database") { $this->filetype = $filetype; + $this->dbDsn = $dbDsn; $this->smarty = CRM_Core_CodeGen_Util_Smarty::singleton()->getSmarty(); @@ -36,6 +39,10 @@ public function __construct($filetype) { $this->beautifier->setIndentNumber(2); $this->beautifier->setNewLine("\n"); } + if ($this->filetype === 'sql') { + // localization needs a database connection. + $this->assign('dbDsn', $this->dbDsn); + } } /** diff --git a/CRM/Core/Smarty/plugins/block.ts.php b/CRM/Core/Smarty/plugins/block.ts.php index d5460eee405c..eff8c23d095a 100644 --- a/CRM/Core/Smarty/plugins/block.ts.php +++ b/CRM/Core/Smarty/plugins/block.ts.php @@ -51,8 +51,14 @@ * the string, translated by gettext */ function smarty_block_ts($params, $text, &$smarty) { + static $i18n = NULL; if (!isset($params['domain'])) { $params['domain'] = $smarty->get_template_vars('extensionKey'); } + + if(!$i18n) { + $i18n = CRM_Core_I18n::singleton(); + $i18n->dbDsn=$smarty->get_template_vars('dbDsn'); + } return ts($text, $params); } diff --git a/xml/GenCode.php b/xml/GenCode.php index 76e6fa6c9d15..b3e4b07a7db5 100644 --- a/xml/GenCode.php +++ b/xml/GenCode.php @@ -35,6 +35,7 @@ @$argv[3], // cms empty($argv[2]) ? NULL : $argv[2], // db version empty($argv[1]) ? 'schema/Schema.xml' : $argv[1], // schema file - getenv('CIVICRM_GENCODE_DIGEST') ? getenv('CIVICRM_GENCODE_DIGEST') : NULL // path to digest file + getenv('CIVICRM_GENCODE_DIGEST') ? getenv('CIVICRM_GENCODE_DIGEST') : NULL, // path to digest file + empty($argv[4]) ? "mysql://username:password@localhost/database" : $argv[4] // db connect string ); $genCode->main(); From 8f2b9aed8b60d6c5bf192609d767811f28594900 Mon Sep 17 00:00:00 2001 From: Julia Longtin Date: Tue, 26 Jan 2016 22:59:19 +0000 Subject: [PATCH 2/2] add command line argument for dbDsn. --- CRM/Core/I18n.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index 59a5f7b2c7a6..05f48afe341d 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -63,6 +63,15 @@ class CRM_Core_I18n { */ private $locale; + /** + * A dbDsn, for when we are connecting to the database server, for localization. + * + * @var string + * the dbDsn string used to connect to the database. + */ + + var $dbDsn = ""; + /** * A locale-based constructor that shouldn't be called from outside of this class (use singleton() instead). * @@ -225,8 +234,9 @@ public static function getResourceDir() { * where n is 1 for the first parameter. The following parameters are reserved: * - escape - sets escape mode: * - 'html' for HTML escaping, this is the default. + * - 'sql' for SQL escaping. Requires connecting to the database engine to perform escaping. * - 'js' for javascript escaping. - * - 'no'/'off'/0 - turns off escaping + * - 'no'/'off'/0 - turns off escaping. * - plural - The plural version of the text (2nd parameter of ngettext()) * - count - The item count for plural mode (3rd parameter of ngettext()) * - context - gettext context of that string (for homonym handling) @@ -315,7 +325,11 @@ public function crm_translate($text, $params = array()) { // escape SQL if we were asked for it if (isset($escape) and ($escape == 'sql')) { - $text = CRM_Core_DAO::escapeString($text); + if ($DAO === NULL) { + $DAO = new CRM_Core_DAO; + $DAO->setDsn($this->dbDsn); + } + $text = $DAO->escapeString($text); } // escape for JavaScript (if requested)