| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Create SQL files to create and populate a new schema. |
| 5 | */ |
| 6 | class CRM_Core_CodeGen_Schema extends CRM_Core_CodeGen_BaseTask { |
| 7 | |
| 8 | /** |
| 9 | * CRM_Core_CodeGen_Schema constructor. |
| 10 | * |
| 11 | * @param \CRM_Core_CodeGen_Main $config |
| 12 | */ |
| 13 | public function __construct($config) { |
| 14 | parent::__construct($config); |
| 15 | $this->locales = $this->findLocales(); |
| 16 | } |
| 17 | |
| 18 | public function run() { |
| 19 | CRM_Core_CodeGen_Util_File::createDir($this->config->sqlCodePath); |
| 20 | |
| 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 | }; |
| 29 | |
| 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 | } |
| 40 | |
| 41 | // also create the archive tables |
| 42 | // $this->generateCreateSql('civicrm_archive.mysql' ); |
| 43 | // $this->generateDropSql('civicrm_archive_drop.mysql'); |
| 44 | |
| 45 | echo "Generating navigation file\n"; |
| 46 | $put($this->generateNavigation()); |
| 47 | |
| 48 | echo "Generating sample file\n"; |
| 49 | $put($this->generateSample()); |
| 50 | } |
| 51 | |
| 52 | public function generateCreateSql() { |
| 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 | |
| 61 | return ['civicrm.mysql' => $template->fetch('schema.tpl')]; |
| 62 | } |
| 63 | |
| 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')]; |
| 70 | } |
| 71 | |
| 72 | public function generateNavigation() { |
| 73 | $template = new CRM_Core_CodeGen_Util_Template('sql'); |
| 74 | return ['civicrm_navigation.mysql' => $template->fetch('civicrm_navigation.tpl')]; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param string $locale |
| 79 | * Ex: en_US, fr_FR |
| 80 | * @return array |
| 81 | */ |
| 82 | public function generateLocaleDataSql($locale) { |
| 83 | $template = new CRM_Core_CodeGen_Util_Template('sql'); |
| 84 | |
| 85 | global $tsLocale; |
| 86 | $oldTsLocale = $tsLocale; |
| 87 | |
| 88 | try { |
| 89 | |
| 90 | $tsLocale = $locale; |
| 91 | $template->assign('locale', $locale); |
| 92 | $template->assign('db_version', $this->config->db_version); |
| 93 | |
| 94 | $sections = [ |
| 95 | 'civicrm_country.tpl', |
| 96 | 'civicrm_state_province.tpl', |
| 97 | 'civicrm_currency.tpl', |
| 98 | 'civicrm_data.tpl', |
| 99 | 'civicrm_navigation.tpl', |
| 100 | 'civicrm_version_sql.tpl', |
| 101 | ]; |
| 102 | |
| 103 | $ext = ($locale != 'en_US' ? ".$locale" : ''); |
| 104 | |
| 105 | return [ |
| 106 | "civicrm_data$ext.mysql" => $template->fetchConcat($sections), |
| 107 | "civicrm_acl$ext.mysql" => $template->fetch('civicrm_acl.tpl'), |
| 108 | ]; |
| 109 | } |
| 110 | finally { |
| 111 | $tsLocale = $oldTsLocale; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @return array |
| 117 | * Array(string $fileName => string $fileContent). |
| 118 | * List of files |
| 119 | */ |
| 120 | public function generateSample() { |
| 121 | $template = new CRM_Core_CodeGen_Util_Template('sql'); |
| 122 | $sections = [ |
| 123 | 'civicrm_sample.tpl', |
| 124 | 'civicrm_acl.tpl', |
| 125 | ]; |
| 126 | return [ |
| 127 | 'civicrm_sample.mysql' => $template->fetchConcat($sections), |
| 128 | 'case_sample.mysql' => $template->fetch('case_sample.tpl'), |
| 129 | ]; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @return array |
| 134 | */ |
| 135 | public function findLocales() { |
| 136 | require_once 'CRM/Core/Config.php'; |
| 137 | $config = CRM_Core_Config::singleton(FALSE); |
| 138 | $locales = []; |
| 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)); |
| 142 | } |
| 143 | |
| 144 | $localesMask = getenv('CIVICRM_LOCALES'); |
| 145 | if (!empty($localesMask)) { |
| 146 | $mask = explode(',', $localesMask); |
| 147 | $locales = array_intersect($locales, $mask); |
| 148 | } |
| 149 | |
| 150 | if (!in_array('en_US', $locales)) { |
| 151 | array_unshift($locales, 'en_US'); |
| 152 | } |
| 153 | |
| 154 | return $locales; |
| 155 | } |
| 156 | |
| 157 | } |