Merge pull request #16020 from mattwire/setentityid
[civicrm-core.git] / setup / plugins / installDatabase / InstallSchema.civi-setup.php
1 <?php
2 /**
3 * @file
4 *
5 * Populate the database schema.
6 */
7
8 if (!defined('CIVI_SETUP')) {
9 exit("Installation plugins must only be loaded by the installer.\n");
10 }
11
12 class InstallSchemaPlugin implements \Symfony\Component\EventDispatcher\EventSubscriberInterface {
13
14 public static function getSubscribedEvents() {
15 return [
16 'civi.setup.checkRequirements' => [
17 ['checkXmlFiles', 0],
18 ['checkSqlFiles', 0],
19 ],
20 'civi.setup.installDatabase' => [
21 ['installDatabase', 0],
22 ],
23 ];
24 }
25
26 public function checkXmlFiles(\Civi\Setup\Event\CheckRequirementsEvent $e) {
27 $m = $e->getModel();
28 $files = array(
29 'xmlMissing' => implode(DIRECTORY_SEPARATOR, [$m->srcPath, 'xml']),
30 'xmlSchemaMissing' => implode(DIRECTORY_SEPARATOR, [$m->srcPath, 'xml', 'schema', 'Schema.xml']),
31 'xmlVersionMissing' => implode(DIRECTORY_SEPARATOR, [$m->srcPath, 'xml', 'version.xml']),
32 );
33
34 foreach ($files as $key => $file) {
35 if (!file_exists($file)) {
36 $e->addError('system', $key, "Schema file is missing: \"$file\"");
37 }
38 }
39 }
40
41 public function checkSqlFiles(\Civi\Setup\Event\CheckRequirementsEvent $e) {
42 \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'checkRequirements'));
43 $seedLanguage = $e->getModel()->lang;
44 $sqlPath = $e->getModel()->srcPath . DIRECTORY_SEPARATOR . 'sql';
45
46 if (!$seedLanguage || $seedLanguage === 'en_US') {
47 $e->addInfo('system', 'lang', "Default language is allowed");
48 return;
49 }
50
51 if (!preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $seedLanguage)) {
52 $e->addError('system', 'langMalformed', 'Language name is malformed.');
53 return;
54 }
55
56 if (!file_exists($e->getModel()->settingsPath)) {
57 $e->addError('system', 'settingsPath', sprintf('The CiviCRM setting file is missing.'));
58 }
59
60 $e->addInfo('system', 'lang', "Language $seedLanguage is allowed.");
61 }
62
63 public function installDatabase(\Civi\Setup\Event\InstallDatabaseEvent $e) {
64 \Civi\Setup::log()->info(sprintf('[%s] Install database schema', basename(__FILE__)));
65
66 $model = $e->getModel();
67
68 $sqlPath = $model->srcPath . DIRECTORY_SEPARATOR . 'sql';
69 $spec = $this->loadSpecification($model->srcPath);
70
71 $conn = \Civi\Setup\DbUtil::connect($model->db);
72 \CRM_Core_I18n::$SQL_ESCAPER = function($text) use ($conn) {
73 return $conn->escape_string($text);
74 };
75
76 \Civi\Setup::log()->info(sprintf('[%s] Load basic tables', basename(__FILE__)));
77 \Civi\Setup\DbUtil::sourceSQL($model->db, \Civi\Setup\SchemaGenerator::generateCreateSql($model->srcPath, $spec->database, $spec->tables));
78
79 $seedLanguage = $model->lang;
80 if (!empty($model->loadGenerated)) {
81 \Civi\Setup::log()->info(sprintf('[%s] Load sample data', basename(__FILE__)));
82 // At time of writing, `generateSampleData()` is not yet a full replacement for `civicrm_generated.mysql`.
83 \Civi\Setup\DbUtil::sourceSQL($model->db, file_get_contents($sqlPath . DIRECTORY_SEPARATOR . 'civicrm_generated.mysql'));
84 // \Civi\Setup\DbUtil::sourceSQL($model->db, \Civi\Setup\SchemaGenerator::generateSampleData($model->srcPath));
85 }
86 elseif ($seedLanguage) {
87 global $tsLocale;
88 $tsLocale = $seedLanguage;
89 \Civi\Setup::log()->info(sprintf('[%s] Load basic data', basename(__FILE__)));
90 \Civi\Setup\DbUtil::sourceSQL($model->db, \Civi\Setup\SchemaGenerator::generateBasicData($model->srcPath));
91 }
92
93 require_once $model->settingsPath;
94 \Civi\Core\Container::boot(TRUE);
95
96 \CRM_Core_I18n::$SQL_ESCAPER = NULL;
97 }
98
99 /**
100 * @param string $srcPath
101 * @return \CRM_Core_CodeGen_Specification
102 */
103 protected function loadSpecification($srcPath) {
104 $schemaFile = implode(DIRECTORY_SEPARATOR, [$srcPath, 'xml', 'schema', 'Schema.xml']);
105 $versionFile = implode(DIRECTORY_SEPARATOR, [$srcPath, 'xml', 'version.xml']);
106 $xmlBuilt = \CRM_Core_CodeGen_Util_Xml::parse($versionFile);
107 $buildVersion = preg_replace('/^(\d{1,2}\.\d{1,2})\.(\d{1,2}|\w{4,7})$/i', '$1', $xmlBuilt->version_no);
108 $specification = new \CRM_Core_CodeGen_Specification();
109 $specification->parse($schemaFile, $buildVersion, FALSE);
110 return $specification;
111 }
112
113 }
114
115 \Civi\Setup::dispatcher()->addSubscriber(new InstallSchemaPlugin());