Commit | Line | Data |
---|---|---|
4bcd4c62 TO |
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 | ||
4bcd4c62 TO |
71 | \Civi\Setup::log()->info(sprintf('[%s] Load basic tables', basename(__FILE__))); |
72 | \Civi\Setup\DbUtil::sourceSQL($model->db, \Civi\Setup\SchemaGenerator::generateCreateSql($model->srcPath, $spec->database, $spec->tables)); | |
73 | ||
74 | $seedLanguage = $model->lang; | |
75 | if (!empty($model->loadGenerated)) { | |
76 | \Civi\Setup::log()->info(sprintf('[%s] Load sample data', basename(__FILE__))); | |
77 | // At time of writing, `generateSampleData()` is not yet a full replacement for `civicrm_generated.mysql`. | |
78 | \Civi\Setup\DbUtil::sourceSQL($model->db, file_get_contents($sqlPath . DIRECTORY_SEPARATOR . 'civicrm_generated.mysql')); | |
79 | // \Civi\Setup\DbUtil::sourceSQL($model->db, \Civi\Setup\SchemaGenerator::generateSampleData($model->srcPath)); | |
80 | } | |
81 | elseif ($seedLanguage) { | |
82 | global $tsLocale; | |
83 | $tsLocale = $seedLanguage; | |
84 | \Civi\Setup::log()->info(sprintf('[%s] Load basic data', basename(__FILE__))); | |
85 | \Civi\Setup\DbUtil::sourceSQL($model->db, \Civi\Setup\SchemaGenerator::generateBasicData($model->srcPath)); | |
86 | } | |
4bcd4c62 TO |
87 | } |
88 | ||
89 | /** | |
90 | * @param string $srcPath | |
91 | * @return \CRM_Core_CodeGen_Specification | |
92 | */ | |
93 | protected function loadSpecification($srcPath) { | |
94 | $schemaFile = implode(DIRECTORY_SEPARATOR, [$srcPath, 'xml', 'schema', 'Schema.xml']); | |
95 | $versionFile = implode(DIRECTORY_SEPARATOR, [$srcPath, 'xml', 'version.xml']); | |
96 | $xmlBuilt = \CRM_Core_CodeGen_Util_Xml::parse($versionFile); | |
97 | $buildVersion = preg_replace('/^(\d{1,2}\.\d{1,2})\.(\d{1,2}|\w{4,7})$/i', '$1', $xmlBuilt->version_no); | |
98 | $specification = new \CRM_Core_CodeGen_Specification(); | |
99 | $specification->parse($schemaFile, $buildVersion, FALSE); | |
100 | return $specification; | |
101 | } | |
102 | ||
103 | } | |
104 | ||
105 | \Civi\Setup::dispatcher()->addSubscriber(new InstallSchemaPlugin()); |