4 * Create SQL files to create and populate a new schema.
6 class CRM_Core_CodeGen_Schema
extends CRM_Core_CodeGen_BaseTask
{
9 * CRM_Core_CodeGen_Schema constructor.
11 * @param \CRM_Core_CodeGen_Main $config
13 public function __construct($config) {
14 parent
::__construct($config);
15 $this->locales
= $this->findLocales();
18 public function run() {
19 CRM_Core_CodeGen_Util_File
::createDir($this->config
->sqlCodePath
);
21 $put = function ($files) {
22 foreach ($files as $file => $content) {
23 if (substr($content, -1) !== "\n") {
26 file_put_contents($this->config
->sqlCodePath
. $file, $content);
30 echo "Generating sql file\n";
31 $put($this->generateCreateSql());
33 echo "Generating sql drop tables file\n";
34 $put($this->generateDropSql());
36 foreach ($this->locales
as $locale) {
37 echo "Generating data files for $locale\n";
38 $put($this->generateLocaleDataSql($locale));
41 // also create the archive tables
42 // $this->generateCreateSql('civicrm_archive.mysql' );
43 // $this->generateDropSql('civicrm_archive_drop.mysql');
45 echo "Generating navigation file\n";
46 $put($this->generateNavigation());
48 echo "Generating sample file\n";
49 $put($this->generateSample());
52 public function generateCreateSql() {
53 $template = new CRM_Core_CodeGen_Util_Template('sql');
55 $template->assign('database', $this->config
->database
);
56 $template->assign('tables', $this->tables
);
57 $dropOrder = array_reverse(array_keys($this->tables
));
58 $template->assign('dropOrder', $dropOrder);
59 $template->assign('mysql', 'modern');
61 return ['civicrm.mysql' => $template->fetch('schema.tpl')];
64 public function generateDropSql() {
65 $dropOrder = array_reverse(array_keys($this->tables
));
66 $template = new CRM_Core_CodeGen_Util_Template('sql');
67 $template->assign('dropOrder', $dropOrder);
68 $template->assign('isOutputLicense', TRUE);
69 return ['civicrm_drop.mysql' => $template->fetch('drop.tpl')];
72 public function generateNavigation() {
73 $template = new CRM_Core_CodeGen_Util_Template('sql');
74 return ['civicrm_navigation.mysql' => $template->fetch('civicrm_navigation.tpl')];
78 * @param string $locale
82 public function generateLocaleDataSql($locale) {
83 $template = new CRM_Core_CodeGen_Util_Template('sql');
86 $oldTsLocale = $tsLocale;
91 $template->assign('locale', $locale);
92 $template->assign('db_version', $this->config
->db_version
);
95 'civicrm_country.tpl',
96 'civicrm_state_province.tpl',
97 'civicrm_currency.tpl',
99 'civicrm_navigation.tpl',
100 'civicrm_version_sql.tpl',
103 $ext = ($locale != 'en_US' ?
".$locale" : '');
106 "civicrm_data$ext.mysql" => $template->fetchConcat($sections),
107 "civicrm_acl$ext.mysql" => $template->fetch('civicrm_acl.tpl'),
111 $tsLocale = $oldTsLocale;
117 * Array(string $fileName => string $fileContent).
120 public function generateSample() {
121 $template = new CRM_Core_CodeGen_Util_Template('sql');
123 'civicrm_sample.tpl',
127 'civicrm_sample.mysql' => $template->fetchConcat($sections),
128 'case_sample.mysql' => $template->fetch('case_sample.tpl'),
135 public function findLocales() {
136 require_once 'CRM/Core/Config.php';
137 $config = CRM_Core_Config
::singleton(FALSE);
139 $localeDir = CRM_Core_I18n
::getResourceDir();
140 if (file_exists($localeDir)) {
141 $locales = preg_grep('/^[a-z][a-z]_[A-Z][A-Z]$/', scandir($localeDir));
144 $localesMask = getenv('CIVICRM_LOCALES');
145 if (!empty($localesMask)) {
146 $mask = explode(',', $localesMask);
147 $locales = array_intersect($locales, $mask);
150 if (!in_array('en_US', $locales)) {
151 array_unshift($locales, 'en_US');