5 * Populate the database schema.
8 if (!defined('CIVI_SETUP')) {
9 exit("Installation plugins must only be loaded by the installer.\n");
12 class InstallSchemaPlugin
implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
14 public static function getSubscribedEvents() {
16 'civi.setup.checkRequirements' => [
20 'civi.setup.installDatabase' => [
21 ['installDatabase', 0],
26 public function checkXmlFiles(\Civi\Setup\Event\CheckRequirementsEvent
$e) {
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']),
34 foreach ($files as $key => $file) {
35 if (!file_exists($file)) {
36 $e->addError('system', $key, "Schema file is missing: \"$file\"");
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';
46 if (!$seedLanguage ||
$seedLanguage === 'en_US') {
47 $e->addInfo('system', 'lang', "Default language is allowed");
51 if (!preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $seedLanguage)) {
52 $e->addError('system', 'langMalformed', 'Language name is malformed.');
56 if (!file_exists($e->getModel()->settingsPath
)) {
57 $e->addError('system', 'settingsPath', sprintf('The CiviCRM setting file is missing.'));
60 $e->addInfo('system', 'lang', "Language $seedLanguage is allowed.");
63 public function installDatabase(\Civi\Setup\Event\InstallDatabaseEvent
$e) {
64 \Civi\Setup
::log()->info(sprintf('[%s] Install database schema', basename(__FILE__
)));
66 $model = $e->getModel();
68 $sqlPath = $model->srcPath
. DIRECTORY_SEPARATOR
. 'sql';
69 $spec = $this->loadSpecification($model->srcPath
);
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
));
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));
81 elseif ($seedLanguage) {
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
));
90 * @param string $srcPath
91 * @return \CRM_Core_CodeGen_Specification
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;
105 \Civi\Setup
::dispatcher()->addSubscriber(new InstallSchemaPlugin());