Merge pull request #18148 from civicrm/5.29
[civicrm-core.git] / CRM / Core / CodeGen / Schema.php
CommitLineData
5e434adf
ARW
1<?php
2
3/**
4 * Create SQL files to create and populate a new schema.
5 */
6class CRM_Core_CodeGen_Schema extends CRM_Core_CodeGen_BaseTask {
bf48aa29 7
2558c2b0 8 /**
bf48aa29 9 * CRM_Core_CodeGen_Schema constructor.
10 *
11 * @param \CRM_Core_CodeGen_Main $config
2558c2b0 12 */
bc02b97b
TO
13 public function __construct($config) {
14 parent::__construct($config);
5e434adf
ARW
15 $this->locales = $this->findLocales();
16 }
17
00be9182 18 public function run() {
5e434adf
ARW
19 CRM_Core_CodeGen_Util_File::createDir($this->config->sqlCodePath);
20
50b8ea3e
TO
21 $put = function ($files) {
22 foreach ($files as $file => $content) {
23 if (substr($content, -1) !== "\n") {
24 $content .= "\n";
25 }
26 file_put_contents($this->config->sqlCodePath . $file, $content);
27 }
28 };
5e434adf 29
50b8ea3e
TO
30 echo "Generating sql file\n";
31 $put($this->generateCreateSql());
32
33 echo "Generating sql drop tables file\n";
34 $put($this->generateDropSql());
35
36 foreach ($this->locales as $locale) {
37 echo "Generating data files for $locale\n";
38 $put($this->generateLocaleDataSql($locale));
39 }
5e434adf
ARW
40
41 // also create the archive tables
42 // $this->generateCreateSql('civicrm_archive.mysql' );
43 // $this->generateDropSql('civicrm_archive_drop.mysql');
44
50b8ea3e
TO
45 echo "Generating navigation file\n";
46 $put($this->generateNavigation());
47
48 echo "Generating sample file\n";
49 $put($this->generateSample());
5e434adf
ARW
50 }
51
50b8ea3e 52 public function generateCreateSql() {
5e434adf
ARW
53 $template = new CRM_Core_CodeGen_Util_Template('sql');
54
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');
60
50b8ea3e 61 return ['civicrm.mysql' => $template->fetch('schema.tpl')];
5e434adf
ARW
62 }
63
50b8ea3e 64 public function generateDropSql() {
5e434adf
ARW
65 $dropOrder = array_reverse(array_keys($this->tables));
66 $template = new CRM_Core_CodeGen_Util_Template('sql');
67 $template->assign('dropOrder', $dropOrder);
50b8ea3e 68 return ['civicrm_drop.mysql' => $template->fetch('drop.tpl')];
5e434adf
ARW
69 }
70
00be9182 71 public function generateNavigation() {
5e434adf 72 $template = new CRM_Core_CodeGen_Util_Template('sql');
50b8ea3e 73 return ['civicrm_navigation.mysql' => $template->fetch('civicrm_navigation.tpl')];
5e434adf
ARW
74 }
75
50b8ea3e
TO
76 /**
77 * @param string $locale
78 * Ex: en_US, fr_FR
79 * @return array
80 */
81 public function generateLocaleDataSql($locale) {
5e434adf
ARW
82 $template = new CRM_Core_CodeGen_Util_Template('sql');
83
84 global $tsLocale;
85 $oldTsLocale = $tsLocale;
50b8ea3e
TO
86
87 try {
88
5e434adf
ARW
89 $tsLocale = $locale;
90 $template->assign('locale', $locale);
91 $template->assign('db_version', $this->config->db_version);
92
be2fb01f 93 $sections = [
5e434adf
ARW
94 'civicrm_country.tpl',
95 'civicrm_state_province.tpl',
96 'civicrm_currency.tpl',
97 'civicrm_data.tpl',
98 'civicrm_navigation.tpl',
99 'civicrm_version_sql.tpl',
be2fb01f 100 ];
5e434adf
ARW
101
102 $ext = ($locale != 'en_US' ? ".$locale" : '');
5e434adf 103
50b8ea3e
TO
104 return [
105 "civicrm_data$ext.mysql" => $template->fetchConcat($sections),
106 "civicrm_acl$ext.mysql" => $template->fetch('civicrm_acl.tpl'),
107 ];
108 }
109 finally {
110 $tsLocale = $oldTsLocale;
5e434adf 111 }
5e434adf
ARW
112 }
113
50b8ea3e
TO
114 /**
115 * @return array
116 * Array(string $fileName => string $fileContent).
117 * List of files
118 */
00be9182 119 public function generateSample() {
5e434adf 120 $template = new CRM_Core_CodeGen_Util_Template('sql');
be2fb01f 121 $sections = [
5e434adf
ARW
122 'civicrm_sample.tpl',
123 'civicrm_acl.tpl',
be2fb01f 124 ];
50b8ea3e
TO
125 return [
126 'civicrm_sample.mysql' => $template->fetchConcat($sections),
127 'case_sample.mysql' => $template->fetch('case_sample.tpl'),
128 ];
5e434adf
ARW
129 }
130
2558c2b0
EM
131 /**
132 * @return array
133 */
00be9182 134 public function findLocales() {
5e434adf
ARW
135 require_once 'CRM/Core/Config.php';
136 $config = CRM_Core_Config::singleton(FALSE);
be2fb01f 137 $locales = [];
4d8ce7f0 138 $localeDir = CRM_Core_I18n::getResourceDir();
5e434adf 139 if (file_exists($localeDir)) {
5e434adf
ARW
140 $locales = preg_grep('/^[a-z][a-z]_[A-Z][A-Z]$/', scandir($localeDir));
141 }
142
143 $localesMask = getenv('CIVICRM_LOCALES');
144 if (!empty($localesMask)) {
145 $mask = explode(',', $localesMask);
146 $locales = array_intersect($locales, $mask);
147 }
148
149 if (!in_array('en_US', $locales)) {
150 array_unshift($locales, 'en_US');
151 }
152
153 return $locales;
154 }
96025800 155
5e434adf 156}